MediaPlayer.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. using System;
  2. namespace Agora.Rtc
  3. {
  4. public sealed class MediaPlayer : IMediaPlayer
  5. {
  6. private IRtcEngine _rtcEngineInstance = null;
  7. private MediaPlayerImpl _mediaPlayerImpl = null;
  8. private const int ErrorCode = -7;
  9. private int playerId;
  10. internal MediaPlayer(IRtcEngine rtcEngine, MediaPlayerImpl impl)
  11. {
  12. _rtcEngineInstance = rtcEngine;
  13. _mediaPlayerImpl = impl;
  14. playerId = _mediaPlayerImpl.CreateMediaPlayer();
  15. }
  16. ~MediaPlayer()
  17. {
  18. _mediaPlayerImpl = null;
  19. _rtcEngineInstance = null;
  20. }
  21. public override void Dispose()
  22. {
  23. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  24. {
  25. return;
  26. }
  27. _mediaPlayerImpl.DestroyMediaPlayer(playerId);
  28. playerId = 0;
  29. }
  30. public override int GetId()
  31. {
  32. return playerId;
  33. }
  34. public override void InitEventHandler(IMediaPlayerSourceObserver engineEventHandler)
  35. {
  36. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  37. {
  38. return;
  39. }
  40. _mediaPlayerImpl.InitEventHandler(playerId, engineEventHandler);
  41. }
  42. public override void RegisterAudioFrameObserver(IMediaPlayerAudioFrameObserver observer)
  43. {
  44. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  45. {
  46. return;
  47. }
  48. _mediaPlayerImpl.RegisterAudioFrameObserver(playerId, observer);
  49. }
  50. public override void RegisterAudioFrameObserver(IMediaPlayerAudioFrameObserver observer, RAW_AUDIO_FRAME_OP_MODE_TYPE mode)
  51. {
  52. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  53. {
  54. return;
  55. }
  56. _mediaPlayerImpl.RegisterAudioFrameObserver(playerId, observer, mode);
  57. }
  58. public override void UnregisterAudioFrameObserver()
  59. {
  60. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  61. {
  62. return;
  63. }
  64. _mediaPlayerImpl.UnregisterAudioFrameObserver(playerId);
  65. }
  66. public override void RegisterMediaPlayerAudioSpectrumObserver(IAudioSpectrumObserver observer, int intervalInMS)
  67. {
  68. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  69. {
  70. return;
  71. }
  72. _mediaPlayerImpl.RegisterMediaPlayerAudioSpectrumObserver(playerId, observer, intervalInMS);
  73. }
  74. public override void UnregisterMediaPlayerAudioSpectrumObserver()
  75. {
  76. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  77. {
  78. return;
  79. }
  80. _mediaPlayerImpl.UnregisterMediaPlayerAudioSpectrumObserver(playerId);
  81. }
  82. public override int Open(string url, Int64 startPos)
  83. {
  84. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  85. {
  86. return ErrorCode;
  87. }
  88. return _mediaPlayerImpl.Open(playerId, url, startPos);
  89. }
  90. public override int OpenWithCustomSource(Int64 startPos, IMediaPlayerCustomDataProvider provider)
  91. {
  92. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  93. {
  94. return ErrorCode;
  95. }
  96. return _mediaPlayerImpl.OpenWithCustomSource(playerId, startPos, provider);
  97. }
  98. public override int OpenWithMediaSource(MediaSource source)
  99. {
  100. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  101. {
  102. return ErrorCode;
  103. }
  104. return _mediaPlayerImpl.OpenWithMediaSource(playerId, source);
  105. }
  106. public override int SetSoundPositionParams(float pan, float gain)
  107. {
  108. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  109. {
  110. return ErrorCode;
  111. }
  112. return _mediaPlayerImpl.SetSoundPositionParams(pan, gain);
  113. }
  114. public override int Play()
  115. {
  116. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  117. {
  118. return ErrorCode;
  119. }
  120. return _mediaPlayerImpl.Play(playerId);
  121. }
  122. public override int Pause()
  123. {
  124. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  125. {
  126. return ErrorCode;
  127. }
  128. return _mediaPlayerImpl.Pause(playerId);
  129. }
  130. public override int Stop()
  131. {
  132. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  133. {
  134. return ErrorCode;
  135. }
  136. return _mediaPlayerImpl.Stop(playerId);
  137. }
  138. public override int Resume()
  139. {
  140. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  141. {
  142. return ErrorCode;
  143. }
  144. return _mediaPlayerImpl.Resume(playerId);
  145. }
  146. public override int Seek(Int64 newPos)
  147. {
  148. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  149. {
  150. return ErrorCode;
  151. }
  152. return _mediaPlayerImpl.Seek(playerId, newPos);
  153. }
  154. public override int GetDuration(ref Int64 duration)
  155. {
  156. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  157. {
  158. return ErrorCode;
  159. }
  160. return _mediaPlayerImpl.GetDuration(playerId, ref duration);
  161. }
  162. public override int GetPlayPosition(ref Int64 pos)
  163. {
  164. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  165. {
  166. return ErrorCode;
  167. }
  168. return _mediaPlayerImpl.GetPlayPosition(playerId, ref pos);
  169. }
  170. public override int GetStreamCount(ref Int64 count)
  171. {
  172. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  173. {
  174. return ErrorCode;
  175. }
  176. return _mediaPlayerImpl.GetStreamCount(playerId, ref count);
  177. }
  178. public override int GetStreamInfo(Int64 index, ref PlayerStreamInfo info)
  179. {
  180. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  181. {
  182. return ErrorCode;
  183. }
  184. return _mediaPlayerImpl.GetStreamInfo(playerId, index, ref info);
  185. }
  186. public override int SetLoopCount(int loopCount)
  187. {
  188. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  189. {
  190. return ErrorCode;
  191. }
  192. return _mediaPlayerImpl.SetLoopCount(playerId, loopCount);
  193. }
  194. public override int SetPlaybackSpeed(int speed)
  195. {
  196. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  197. {
  198. return ErrorCode;
  199. }
  200. return _mediaPlayerImpl.SetPlaybackSpeed(playerId, speed);
  201. }
  202. public override int SelectAudioTrack(int index)
  203. {
  204. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  205. {
  206. return ErrorCode;
  207. }
  208. return _mediaPlayerImpl.SelectAudioTrack(playerId, index);
  209. }
  210. public override int SetPlayerOption(string key, int value)
  211. {
  212. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  213. {
  214. return ErrorCode;
  215. }
  216. return _mediaPlayerImpl.SetPlayerOption(playerId, key, value);
  217. }
  218. public override int SetPlayerOption(string key, string value)
  219. {
  220. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  221. {
  222. return ErrorCode;
  223. }
  224. return _mediaPlayerImpl.SetPlayerOption(playerId, key, value);
  225. }
  226. public override int TakeScreenshot(string filename)
  227. {
  228. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  229. {
  230. return ErrorCode;
  231. }
  232. return _mediaPlayerImpl.TakeScreenshot(playerId, filename);
  233. }
  234. public override int SelectInternalSubtitle(int index)
  235. {
  236. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  237. {
  238. return ErrorCode;
  239. }
  240. return _mediaPlayerImpl.SelectInternalSubtitle(playerId, index);
  241. }
  242. public override int SetExternalSubtitle(string url)
  243. {
  244. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  245. {
  246. return ErrorCode;
  247. }
  248. return _mediaPlayerImpl.SetExternalSubtitle(playerId, url);
  249. }
  250. public override MEDIA_PLAYER_STATE GetState()
  251. {
  252. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  253. {
  254. return MEDIA_PLAYER_STATE.PLAYER_STATE_DO_NOTHING_INTERNAL;
  255. }
  256. return _mediaPlayerImpl.GetState(playerId);
  257. }
  258. public override int Mute(bool muted)
  259. {
  260. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  261. {
  262. return ErrorCode;
  263. }
  264. return _mediaPlayerImpl.Mute(playerId, muted);
  265. }
  266. public override int GetMute(ref bool muted)
  267. {
  268. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  269. {
  270. return ErrorCode;
  271. }
  272. return _mediaPlayerImpl.GetMute(playerId, ref muted);
  273. }
  274. public override int AdjustPlayoutVolume(int volume)
  275. {
  276. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  277. {
  278. return ErrorCode;
  279. }
  280. return _mediaPlayerImpl.AdjustPlayoutVolume(playerId, volume);
  281. }
  282. public override int GetPlayoutVolume(ref int volume)
  283. {
  284. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  285. {
  286. return ErrorCode;
  287. }
  288. return _mediaPlayerImpl.GetPlayoutVolume(playerId, ref volume);
  289. }
  290. public override int AdjustPublishSignalVolume(int volume)
  291. {
  292. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  293. {
  294. return ErrorCode;
  295. }
  296. return _mediaPlayerImpl.AdjustPublishSignalVolume(playerId, volume);
  297. }
  298. public override int GetPublishSignalVolume(ref int volume)
  299. {
  300. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  301. {
  302. return ErrorCode;
  303. }
  304. return _mediaPlayerImpl.GetPublishSignalVolume(playerId, ref volume);
  305. }
  306. public override int SetView()
  307. {
  308. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  309. {
  310. return ErrorCode;
  311. }
  312. return _mediaPlayerImpl.SetView(playerId);
  313. }
  314. public override int SetRenderMode(RENDER_MODE_TYPE renderMode)
  315. {
  316. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  317. {
  318. return ErrorCode;
  319. }
  320. return _mediaPlayerImpl.SetRenderMode(playerId, renderMode);
  321. }
  322. public override int SetAudioDualMonoMode(AUDIO_DUAL_MONO_MODE mode)
  323. {
  324. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  325. {
  326. return ErrorCode;
  327. }
  328. return _mediaPlayerImpl.SetAudioDualMonoMode(playerId, mode);
  329. }
  330. public override string GetPlayerSdkVersion()
  331. {
  332. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  333. {
  334. return null;
  335. }
  336. return _mediaPlayerImpl.GetPlayerSdkVersion(playerId);
  337. }
  338. public override string GetPlaySrc()
  339. {
  340. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  341. {
  342. return null;
  343. }
  344. return _mediaPlayerImpl.GetPlaySrc(playerId);
  345. }
  346. public override int SetAudioPitch(int pitch)
  347. {
  348. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  349. {
  350. return ErrorCode;
  351. }
  352. return _mediaPlayerImpl.SetAudioPitch(playerId, pitch);
  353. }
  354. public override int SetSpatialAudioParams(SpatialAudioParams spatial_audio_params)
  355. {
  356. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  357. {
  358. return ErrorCode;
  359. }
  360. return _mediaPlayerImpl.SetSpatialAudioParams(playerId, spatial_audio_params);
  361. }
  362. public override int OpenWithAgoraCDNSrc(string src, Int64 startPos)
  363. {
  364. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  365. {
  366. return ErrorCode;
  367. }
  368. return _mediaPlayerImpl.OpenWithAgoraCDNSrc(playerId, src, startPos);
  369. }
  370. public override int GetAgoraCDNLineCount()
  371. {
  372. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  373. {
  374. return ErrorCode;
  375. }
  376. return _mediaPlayerImpl.GetAgoraCDNLineCount(playerId);
  377. }
  378. public override int SwitchAgoraCDNLineByIndex(int index)
  379. {
  380. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  381. {
  382. return ErrorCode;
  383. }
  384. return _mediaPlayerImpl.SwitchAgoraCDNLineByIndex(playerId, index);
  385. }
  386. public override int GetCurrentAgoraCDNIndex()
  387. {
  388. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  389. {
  390. return ErrorCode;
  391. }
  392. return _mediaPlayerImpl.GetCurrentAgoraCDNIndex(playerId);
  393. }
  394. public override int EnableAutoSwitchAgoraCDN(bool enable)
  395. {
  396. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  397. {
  398. return ErrorCode;
  399. }
  400. return _mediaPlayerImpl.EnableAutoSwitchAgoraCDN(playerId, enable);
  401. }
  402. public override int RenewAgoraCDNSrcToken(string token, Int64 ts)
  403. {
  404. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  405. {
  406. return ErrorCode;
  407. }
  408. return _mediaPlayerImpl.RenewAgoraCDNSrcToken(playerId, token, ts);
  409. }
  410. public override int SwitchAgoraCDNSrc(string src, bool syncPts = false)
  411. {
  412. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  413. {
  414. return ErrorCode;
  415. }
  416. return _mediaPlayerImpl.SwitchAgoraCDNSrc(playerId, src, syncPts);
  417. }
  418. public override int SwitchSrc(string src, bool syncPts = true)
  419. {
  420. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  421. {
  422. return ErrorCode;
  423. }
  424. return _mediaPlayerImpl.SwitchSrc(playerId, src, syncPts);
  425. }
  426. public override int PreloadSrc(string src, Int64 startPos)
  427. {
  428. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  429. {
  430. return ErrorCode;
  431. }
  432. return _mediaPlayerImpl.PreloadSrc(playerId, src, startPos);
  433. }
  434. public override int PlayPreloadedSrc(string src)
  435. {
  436. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  437. {
  438. return ErrorCode;
  439. }
  440. return _mediaPlayerImpl.PlayPreloadedSrc(playerId, src);
  441. }
  442. public override int UnloadSrc(string src)
  443. {
  444. if (_rtcEngineInstance == null || _mediaPlayerImpl == null)
  445. {
  446. return ErrorCode;
  447. }
  448. return _mediaPlayerImpl.UnloadSrc(playerId, src);
  449. }
  450. }
  451. }