MediaPlayerWithCustomDataProviderExample.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Serialization;
  7. using Agora.Rtc;
  8. using Agora.Util;
  9. using Logger = Agora.Util.Logger;
  10. using Random = UnityEngine.Random;
  11. using System.IO;
  12. using System.Runtime.InteropServices;
  13. namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.MediaPlayerWithCustomDataProviderExample
  14. {
  15. public class MediaPlayerWithCustomDataProviderExample : MonoBehaviour
  16. {
  17. [FormerlySerializedAs("appIdInput")]
  18. [SerializeField]
  19. private AppIdInput _appIdInput;
  20. [Header("_____________Basic Configuration_____________")]
  21. [FormerlySerializedAs("APP_ID")]
  22. [SerializeField]
  23. private string _appID = "";
  24. [FormerlySerializedAs("TOKEN")]
  25. [SerializeField]
  26. private string _token = "";
  27. [FormerlySerializedAs("CHANNEL_NAME")]
  28. [SerializeField]
  29. private string _channelName = "";
  30. public Text LogText;
  31. internal Logger Log;
  32. internal IRtcEngine RtcEngine = null;
  33. internal IMediaPlayer MediaPlayer = null;
  34. internal UserPlayerCustomDataProvider customDataProvider = null;
  35. private Button _button1;
  36. private Button _button2;
  37. private Button _button3;
  38. private Button _button4;
  39. private Button _button5;
  40. // Use this for initialization
  41. private void Start()
  42. {
  43. LoadAssetData();
  44. if (CheckAppId())
  45. {
  46. SetUpUI();
  47. //EnableUI(false);
  48. InitEngine();
  49. InitMediaPlayer();
  50. JoinChannelWithMPK();
  51. }
  52. }
  53. // Update is called once per frame
  54. private void Update()
  55. {
  56. PermissionHelper.RequestMicrophontPermission();
  57. PermissionHelper.RequestCameraPermission();
  58. }
  59. private void SetUpUI()
  60. {
  61. _button1 = GameObject.Find("Button1").GetComponent<Button>();
  62. _button1.onClick.AddListener(OnPlayButtonPress);
  63. _button2 = GameObject.Find("Button2").GetComponent<Button>();
  64. _button2.onClick.AddListener(OnStopButtonPress);
  65. _button3 = GameObject.Find("Button3").GetComponent<Button>();
  66. _button3.onClick.AddListener(OnPauseButtonPress);
  67. _button4 = GameObject.Find("Button4").GetComponent<Button>();
  68. _button4.onClick.AddListener(OnResumeButtonPress);
  69. _button5 = GameObject.Find("Button5").GetComponent<Button>();
  70. _button5.onClick.AddListener(OnOpenButtonPress);
  71. }
  72. public void EnableUI(bool val)
  73. {
  74. var obj = this.transform.Find("Button1").gameObject;
  75. obj.SetActive(val);
  76. obj = this.transform.Find("Button2").gameObject;
  77. obj.SetActive(val);
  78. obj = this.transform.Find("Button3").gameObject;
  79. obj.SetActive(val);
  80. obj = this.transform.Find("Button4").gameObject;
  81. obj.SetActive(val);
  82. }
  83. private bool CheckAppId()
  84. {
  85. Log = new Logger(LogText);
  86. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  87. }
  88. //Show data in AgoraBasicProfile
  89. [ContextMenu("ShowAgoraBasicProfileData")]
  90. private void LoadAssetData()
  91. {
  92. if (_appIdInput == null) return;
  93. _appID = _appIdInput.appID;
  94. _token = _appIdInput.token;
  95. _channelName = _appIdInput.channelName;
  96. }
  97. private void InitEngine()
  98. {
  99. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  100. UserEventHandler handler = new UserEventHandler(this);
  101. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  102. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  103. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  104. RtcEngine.Initialize(context);
  105. RtcEngine.InitEventHandler(handler);
  106. var logFile = Application.persistentDataPath + "/rtc.log";
  107. RtcEngine.SetLogFile(logFile);
  108. this.Log.UpdateLog("logFile:" + logFile);
  109. }
  110. private void InitMediaPlayer()
  111. {
  112. MediaPlayer = RtcEngine.CreateMediaPlayer();
  113. if (MediaPlayer == null)
  114. {
  115. this.Log.UpdateLog("CreateMediaPlayer failed!");
  116. return;
  117. }
  118. MpkEventHandler handler = new MpkEventHandler(this);
  119. MediaPlayer.InitEventHandler(handler);
  120. this.Log.UpdateLog("playerId id: " + MediaPlayer.GetId());
  121. }
  122. private void JoinChannelWithMPK()
  123. {
  124. RtcEngine.EnableAudio();
  125. RtcEngine.EnableVideo();
  126. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  127. ChannelMediaOptions options = new ChannelMediaOptions();
  128. options.autoSubscribeAudio.SetValue(true);
  129. options.autoSubscribeVideo.SetValue(true);
  130. options.publishCustomAudioTrack.SetValue(false);
  131. options.publishCameraTrack.SetValue(false);
  132. options.publishMediaPlayerAudioTrack.SetValue(true);
  133. options.publishMediaPlayerVideoTrack.SetValue(true);
  134. options.publishMediaPlayerId.SetValue(MediaPlayer.GetId());
  135. options.enableAudioRecordingOrPlayout.SetValue(true);
  136. options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  137. var ret = RtcEngine.JoinChannel(_token, _channelName, 0, options);
  138. this.Log.UpdateLog("RtcEngineController JoinChannel_MPK returns: " + ret);
  139. }
  140. private void OnPlayButtonPress()
  141. {
  142. var ret = MediaPlayer.Play();
  143. this.Log.UpdateLog("Play return" + ret);
  144. //this.TestMediaPlayer();
  145. }
  146. private void OnStopButtonPress()
  147. {
  148. var ret = MediaPlayer.Stop();
  149. this.Log.UpdateLog("Stop return" + ret);
  150. this.customDataProvider.Close();
  151. }
  152. private void OnPauseButtonPress()
  153. {
  154. var ret = MediaPlayer.Pause();
  155. this.Log.UpdateLog("Pause return" + ret);
  156. }
  157. private void OnResumeButtonPress()
  158. {
  159. var ret = MediaPlayer.Resume();
  160. this.Log.UpdateLog("Resume returns: " + ret);
  161. }
  162. private void OnOpenButtonPress()
  163. {
  164. this.customDataProvider = new UserPlayerCustomDataProvider(this);
  165. string file;
  166. #if UNITY_ANDROID && !UNITY_EDITOR
  167. // On Android, the StreamingAssetPath is just accessed by /assets instead of Application.streamingAssetPath
  168. file = "/assets/img/MPK.mp4";
  169. #else
  170. file = Application.streamingAssetsPath + "/img/" + "MPK.mp4";
  171. #endif
  172. this.customDataProvider.Open(file);
  173. //var ret = MediaPlayer.OpenWithCustomSource(0, this.customDataProvider);
  174. //this.Log.UpdateLog("OpenWithCustomSource: " + ret);
  175. var source = new MediaSource();
  176. source.url = null;
  177. source.uri = null;
  178. source.provider = this.customDataProvider;
  179. source.autoPlay = false;
  180. var ret = MediaPlayer.OpenWithMediaSource(source);
  181. this.Log.UpdateLog("OpenWithMediaSource: " + ret);
  182. }
  183. private void TestMediaPlayer()
  184. {
  185. long duration = 0;
  186. var ret = MediaPlayer.GetDuration(ref duration);
  187. Debug.Log("_mediaPlayer.GetDuration returns: " + ret + "duration: " + duration);
  188. long pos = 0;
  189. ret = MediaPlayer.GetPlayPosition(ref pos);
  190. Debug.Log("_mediaPlayer.GetPlayPosition returns: " + ret + "position: " + pos);
  191. Debug.Log("_mediaPlayer.GetState:" + MediaPlayer.GetState());
  192. bool mute = true;
  193. ret = MediaPlayer.GetMute(ref mute);
  194. Debug.Log("_mediaPlayer.GetMute returns: " + ret + "mute: " + mute);
  195. int volume = 0;
  196. ret = MediaPlayer.GetPlayoutVolume(ref volume);
  197. Debug.Log("_mediaPlayer.GetPlayoutVolume returns: " + ret + "volume: " + volume);
  198. Debug.Log("SDK Version:" + MediaPlayer.GetPlayerSdkVersion());
  199. Debug.Log("GetPlaySrc:" + MediaPlayer.GetPlaySrc());
  200. }
  201. private void OnDestroy()
  202. {
  203. Debug.Log("OnDestroy");
  204. if (RtcEngine == null) return;
  205. if (MediaPlayer != null)
  206. {
  207. MediaPlayer.Stop();
  208. RtcEngine.DestroyMediaPlayer(MediaPlayer);
  209. }
  210. if (customDataProvider != null)
  211. customDataProvider.Close();
  212. RtcEngine.InitEventHandler(null);
  213. RtcEngine.LeaveChannel();
  214. RtcEngine.Dispose();
  215. RtcEngine = null;
  216. }
  217. internal string GetChannelName()
  218. {
  219. return _channelName;
  220. }
  221. internal static void MakeVideoView(uint uid, string channelId = "", VIDEO_SOURCE_TYPE videoSourceType = VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA)
  222. {
  223. var go = GameObject.Find(uid.ToString());
  224. if (!ReferenceEquals(go, null))
  225. {
  226. return; // reuse
  227. }
  228. // create a GameObject and assign to this new user
  229. var videoSurface = MakeImageSurface(uid.ToString());
  230. if (ReferenceEquals(videoSurface, null)) return;
  231. // configure videoSurface
  232. videoSurface.SetForUser(uid, channelId, videoSourceType);
  233. videoSurface.SetEnable(true);
  234. videoSurface.OnTextureSizeModify += (int width, int height) =>
  235. {
  236. float scale = (float)height / (float)width;
  237. videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  238. Debug.Log("OnTextureSizeModify: " + width + " " + height);
  239. };
  240. }
  241. // VIDEO TYPE 1: 3D Object
  242. private VideoSurface MakePlaneSurface(string goName)
  243. {
  244. var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  245. if (go == null)
  246. {
  247. return null;
  248. }
  249. go.name = goName;
  250. // set up transform
  251. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  252. go.transform.position = Vector3.zero;
  253. go.transform.localScale = new Vector3(1.0f, 1.333f, 0.5f);
  254. // configure videoSurface
  255. var videoSurface = go.AddComponent<VideoSurface>();
  256. return videoSurface;
  257. }
  258. // Video TYPE 2: RawImage
  259. private static VideoSurface MakeImageSurface(string goName)
  260. {
  261. GameObject go = new GameObject();
  262. if (go == null)
  263. {
  264. return null;
  265. }
  266. go.name = goName;
  267. // to be renderered onto
  268. go.AddComponent<RawImage>();
  269. // make the object draggable
  270. go.AddComponent<UIElementDrag>();
  271. var canvas = GameObject.Find("VideoCanvas");
  272. if (canvas != null)
  273. {
  274. go.transform.parent = canvas.transform;
  275. Debug.Log("add video view");
  276. }
  277. else
  278. {
  279. Debug.Log("Canvas is null video view");
  280. }
  281. // set up transform
  282. go.transform.Rotate(0f, 0.0f, 180.0f);
  283. go.transform.localPosition = Vector3.zero;
  284. go.transform.localScale = new Vector3(4.5f, 3f, 1f);
  285. // configure videoSurface
  286. var videoSurface = go.AddComponent<VideoSurface>();
  287. return videoSurface;
  288. }
  289. internal static void DestroyVideoView(uint uid)
  290. {
  291. var go = GameObject.Find(uid.ToString());
  292. if (!ReferenceEquals(go, null))
  293. {
  294. Destroy(go);
  295. }
  296. }
  297. }
  298. internal class MpkEventHandler : IMediaPlayerSourceObserver
  299. {
  300. private readonly MediaPlayerWithCustomDataProviderExample _sample;
  301. internal MpkEventHandler(MediaPlayerWithCustomDataProviderExample sample)
  302. {
  303. _sample = sample;
  304. }
  305. public override void OnPlayerSourceStateChanged(MEDIA_PLAYER_STATE state, MEDIA_PLAYER_ERROR ec)
  306. {
  307. _sample.Log.UpdateLog(string.Format(
  308. "OnPlayerSourceStateChanged state: {0}, ec: {1}, playId: {2}", state, ec, _sample.MediaPlayer.GetId()));
  309. Debug.Log("OnPlayerSourceStateChanged");
  310. if (state == MEDIA_PLAYER_STATE.PLAYER_STATE_OPEN_COMPLETED)
  311. {
  312. MediaPlayerWithCustomDataProviderExample.MakeVideoView((uint)_sample.MediaPlayer.GetId(), "", VIDEO_SOURCE_TYPE.VIDEO_SOURCE_MEDIA_PLAYER);
  313. _sample.EnableUI(true);
  314. _sample.Log.UpdateLog("Open Complete. Click start to play media");
  315. }
  316. else if (state == MEDIA_PLAYER_STATE.PLAYER_STATE_STOPPED)
  317. {
  318. MediaPlayerWithCustomDataProviderExample.DestroyVideoView((uint)_sample.MediaPlayer.GetId());
  319. _sample.EnableUI(false);
  320. }
  321. }
  322. public override void OnPlayerEvent(MEDIA_PLAYER_EVENT @event, Int64 elapsedTime, string message)
  323. {
  324. _sample.Log.UpdateLog(string.Format("OnPlayerEvent state: {0}", @event));
  325. }
  326. }
  327. internal class UserEventHandler : IRtcEngineEventHandler
  328. {
  329. private readonly MediaPlayerWithCustomDataProviderExample _sample;
  330. internal UserEventHandler(MediaPlayerWithCustomDataProviderExample sample)
  331. {
  332. _sample = sample;
  333. }
  334. public override void OnError(int err, string msg)
  335. {
  336. _sample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  337. }
  338. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  339. {
  340. int build = 0;
  341. _sample.Log.UpdateLog(string.Format("sdk version: ${0}",
  342. _sample.RtcEngine.GetVersion(ref build)));
  343. _sample.Log.UpdateLog(
  344. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  345. connection.channelId, connection.localUid, elapsed));
  346. }
  347. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  348. {
  349. _sample.Log.UpdateLog("OnRejoinChannelSuccess");
  350. }
  351. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  352. {
  353. _sample.Log.UpdateLog("OnLeaveChannel");
  354. MediaPlayerWithCustomDataProviderExample.DestroyVideoView(0);
  355. }
  356. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole,
  357. CLIENT_ROLE_TYPE newRole)
  358. {
  359. _sample.Log.UpdateLog("OnClientRoleChanged");
  360. }
  361. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  362. {
  363. _sample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  364. }
  365. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  366. {
  367. _sample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  368. (int)reason));
  369. MediaPlayerWithCustomDataProviderExample.DestroyVideoView(uid);
  370. }
  371. }
  372. internal class UserPlayerCustomDataProvider : IMediaPlayerCustomDataProvider
  373. {
  374. MediaPlayerWithCustomDataProviderExample _sample;
  375. private FileStream fis = null;
  376. private Int64 fileSize = 0;
  377. internal UserPlayerCustomDataProvider(MediaPlayerWithCustomDataProviderExample sample)
  378. {
  379. _sample = sample;
  380. }
  381. public bool Open(string file)
  382. {
  383. try
  384. {
  385. if (File.Exists(file))
  386. {
  387. fis = new FileStream(file, FileMode.Open, FileAccess.Read);
  388. fileSize = fis.Length;
  389. this._sample.Log.UpdateLog("open file sucess size: " + fileSize);
  390. }
  391. }
  392. catch (Exception e)
  393. {
  394. this._sample.Log.UpdateLog("open catch exception " + e);
  395. return false;
  396. }
  397. return true;
  398. }
  399. public void Close()
  400. {
  401. if (fis == null)
  402. {
  403. return;
  404. }
  405. try
  406. {
  407. fis.Close();
  408. }
  409. catch (Exception e)
  410. {
  411. this._sample.Log.UpdateLog("close catch exception " + e);
  412. }
  413. fis = null;
  414. }
  415. public override Int64 OnSeek(Int64 offset, int whence)
  416. {
  417. string str = String.Format("OnSeek offset:{0} whence:{1}", offset, whence);
  418. Debug.Log(str);
  419. if (whence == 0)
  420. {
  421. try
  422. {
  423. if (fis == null)
  424. {
  425. return -1;
  426. }
  427. fis.Seek(offset, SeekOrigin.Begin);
  428. }
  429. catch (Exception e)
  430. {
  431. Debug.Log("onseek catch exception " + e);
  432. return -1;
  433. }
  434. return offset;
  435. }
  436. else if (whence == 65536)
  437. {
  438. return fileSize;
  439. }
  440. return 0;
  441. }
  442. public override int OnReadData(IntPtr bufferPtr, int bufferSize)
  443. {
  444. string str = String.Format("OnReadData bufferPtr:{0} bufferSize:{1}", (System.Int64)bufferPtr, bufferSize);
  445. Debug.Log(str);
  446. if (fis == null)
  447. {
  448. return -1;
  449. }
  450. byte[] byte_buffer = new byte[bufferSize];
  451. int read_count = -1;
  452. try
  453. {
  454. read_count = fis.Read(byte_buffer, 0, bufferSize);
  455. if (read_count == -1)
  456. {
  457. return -1;
  458. }
  459. UnityEngine.Debug.Log("onReadData: " + read_count);
  460. Marshal.Copy(byte_buffer, 0, bufferPtr, read_count);
  461. }
  462. catch (Exception e)
  463. {
  464. Debug.Log("onseek catch exception " + e);
  465. return -1;
  466. }
  467. return read_count;
  468. }
  469. }
  470. }