TRTCLivePusher.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using ManageLiteAV;
  2. using System;
  3. using System.IO;
  4. using System.Threading;
  5. using Vinno.FIS.TRTCClient.Common.Enum;
  6. using Vinno.FIS.TRTCClient.Common.Log;
  7. using Vinno.FIS.TRTCClient.Common.Models;
  8. namespace Vinno.FIS.TRTCClient
  9. {
  10. internal class TRTCLivePusher : ITRTCCloudCallback, IDisposable
  11. {
  12. private readonly ManualResetEvent _exitResetEvent = new ManualResetEvent(false);
  13. private ITRTCCloud _cloud;
  14. private ITXDeviceManager _deviceManager;
  15. private TRTCVideoFrame _videoFrame;
  16. public EventHandler<bool> SendFirstLocalVideoFrame;
  17. public TRTCLivePusher(string path, EnumLiveChannelCategory category)
  18. {
  19. _cloud = ITRTCCloud.getTRTCShareInstance();
  20. _deviceManager = _cloud.getDeviceManager();
  21. var logPath = Path.Combine(path, category.ToString());
  22. if (!Directory.Exists(logPath))
  23. {
  24. Directory.CreateDirectory(logPath);
  25. }
  26. _cloud.setLogCompressEnabled(false);
  27. _cloud.setLogLevel(TRTCLogLevel.TRTCLogLevelError);
  28. _cloud.setLogDirPath(logPath);
  29. _cloud.addCallback(this);
  30. _cloud.setLocalVideoRenderCallback(TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32, TRTCVideoBufferType.TRTCVideoBufferType_Buffer, null);
  31. }
  32. public void EnterRoom(TRTCRoomInfo roomInfo)
  33. {
  34. TRTCParams trtcParams = new TRTCParams
  35. {
  36. sdkAppId = roomInfo.AppId,
  37. roomId = roomInfo.RoomId,
  38. userId = roomInfo.UserId,
  39. userSig = roomInfo.UserSig,
  40. // 如果您有进房权限保护的需求,则参考文档{https://cloud.tencent.com/document/product/647/32240}完成该功能。
  41. // 在有权限进房的用户中的下面字段中添加在服务器获取到的privateMapKey。
  42. privateMapKey = "",
  43. businessInfo = "",
  44. role = TRTCRoleType.TRTCRoleAnchor
  45. };
  46. var resolution = GetResolution(roomInfo.OutputWidth, roomInfo.OutputHeight);
  47. InitVideoParam(resolution, roomInfo.VideoFps, roomInfo.VideoBitrate, roomInfo.MinVideoBitrate);
  48. _cloud.setDefaultStreamRecvMode(false, false);
  49. _cloud.setVideoEncoderMirror(false);
  50. var correctMicId = GetCorrectId(HardwareType.Mic, roomInfo.MicDeviceId);
  51. if (!string.IsNullOrEmpty(correctMicId))
  52. {
  53. _deviceManager.setCurrentDevice(TXMediaDeviceType.TXMediaDeviceTypeMic, correctMicId);
  54. _cloud.startLocalAudio(TRTCAudioQuality.TRTCAudioQualityDefault);//开启本地音频的采集和上行
  55. }
  56. _cloud.muteLocalAudio(roomInfo.IsMute);
  57. _cloud.muteAllRemoteAudio(true);
  58. _cloud.muteAllRemoteVideoStreams(true);
  59. _cloud.enableCustomVideoCapture(TRTCVideoStreamType.TRTCVideoStreamTypeBig, true);
  60. _cloud.enterRoom(ref trtcParams, TRTCAppScene.TRTCAppSceneLIVE);
  61. }
  62. internal void AdjustMicVolume(int value)
  63. {
  64. _cloud?.setAudioCaptureVolume(value);
  65. }
  66. /// <summary>
  67. /// 发送要推流的数据
  68. /// </summary>
  69. /// <param name="width"></param>
  70. /// <param name="height"></param>
  71. /// <param name="frameBuffer"></param>
  72. public void SendData(uint width, uint height, byte[] frameBuffer)
  73. {
  74. try
  75. {
  76. var len = width * height * 3 / 2;
  77. if (_videoFrame == null)
  78. {
  79. _videoFrame = new TRTCVideoFrame
  80. {
  81. videoFormat = TRTCVideoPixelFormat.TRTCVideoPixelFormat_I420,
  82. bufferType = TRTCVideoBufferType.TRTCVideoBufferType_Buffer,
  83. data = new byte[len],
  84. width = width,
  85. height = height,
  86. length = len,
  87. timestamp = 0
  88. };
  89. }
  90. _videoFrame.data = frameBuffer;
  91. _cloud?.sendCustomVideoData(TRTCVideoStreamType.TRTCVideoStreamTypeBig, _videoFrame);
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.WriteLineError($"TRTCLivePusher Send Data Error:{ex}");
  96. }
  97. }
  98. public void ExitRoom()
  99. {
  100. Logger.WriteLineInfo("Exit Room");
  101. _cloud.stopAllRemoteView();
  102. _cloud.stopLocalPreview();
  103. _cloud.stopLocalAudio();
  104. _cloud.muteLocalAudio(true);
  105. _cloud.muteLocalVideo(TRTCVideoStreamType.TRTCVideoStreamTypeBig, true);
  106. _exitResetEvent.Reset();
  107. _cloud.exitRoom();
  108. _exitResetEvent.WaitOne(5000);
  109. }
  110. private void InitVideoParam(TRTCVideoResolution resolution, uint fps, uint videoBitrate, uint minVideoBitrate)
  111. {
  112. TRTCVideoEncParam videoEncParams = new TRTCVideoEncParam
  113. {
  114. videoBitrate = videoBitrate,
  115. minVideoBitrate = minVideoBitrate,
  116. videoFps = fps,
  117. videoResolution = resolution,
  118. resMode = TRTCVideoResolutionMode.TRTCVideoResolutionModeLandscape,
  119. enableAdjustRes = false
  120. };
  121. _cloud.setVideoEncoderParam(ref videoEncParams);
  122. var qosParams = new TRTCNetworkQosParam
  123. {
  124. preference = TRTCVideoQosPreference.TRTCVideoQosPreferenceClear,
  125. controlMode = TRTCQosControlMode.TRTCQosControlModeServer
  126. };
  127. _cloud.setNetworkQosParam(ref qosParams);
  128. }
  129. private TRTCVideoResolution GetResolution(int outputWidth, int outputHeight)
  130. {
  131. if (outputWidth <= 320 && outputHeight <= 240)//TRTC分辨率不支持,则通过ImageSender传图
  132. {
  133. return TRTCVideoResolution.TRTCVideoResolution_320_240;
  134. }
  135. else if (outputWidth <= 480 && outputHeight <= 360)
  136. {
  137. return TRTCVideoResolution.TRTCVideoResolution_480_360;
  138. }
  139. else if (outputWidth <= 640 && outputHeight <= 360)
  140. {
  141. return TRTCVideoResolution.TRTCVideoResolution_640_360;
  142. }
  143. else if (outputWidth <= 640 && outputHeight <= 480)
  144. {
  145. return TRTCVideoResolution.TRTCVideoResolution_640_480;
  146. }
  147. else if (outputWidth <= 960 && outputHeight <= 544)
  148. {
  149. return TRTCVideoResolution.TRTCVideoResolution_960_540;
  150. }
  151. else if (outputWidth <= 960 && outputHeight <= 720)
  152. {
  153. return TRTCVideoResolution.TRTCVideoResolution_960_720;
  154. }
  155. else if (outputWidth <= 1280 && outputHeight <= 720)
  156. {
  157. return TRTCVideoResolution.TRTCVideoResolution_1280_720;
  158. }
  159. else if (outputWidth <= 1920 && outputHeight <= 1080)
  160. {
  161. return TRTCVideoResolution.TRTCVideoResolution_1920_1080;
  162. }
  163. return TRTCVideoResolution.TRTCVideoResolution_640_480;
  164. }
  165. /// <summary>
  166. /// Get the Correct Id
  167. /// </summary>
  168. /// <param name="type"></param>
  169. /// <param name="deviceId"></param>
  170. /// <returns></returns>
  171. private string GetCorrectId(HardwareType type, string deviceId)
  172. {
  173. switch (type)
  174. {
  175. case HardwareType.Camera:
  176. var cameraList = _deviceManager.getDevicesList(TXMediaDeviceType.TXMediaDeviceTypeCamera);
  177. try
  178. {
  179. var count = cameraList.getCount();
  180. for (uint i = 0; i < count; i++)
  181. {
  182. var id = cameraList.getDevicePID(i);
  183. if (deviceId.Contains(id) || id.Contains(deviceId))
  184. {
  185. return id;
  186. }
  187. }
  188. }
  189. finally
  190. {
  191. cameraList.release();
  192. }
  193. break;
  194. case HardwareType.Mic:
  195. var micList = _deviceManager.getDevicesList(TXMediaDeviceType.TXMediaDeviceTypeMic);
  196. try
  197. {
  198. var count = micList.getCount();
  199. for (uint i = 0; i < count; i++)
  200. {
  201. var id = micList.getDevicePID(i);
  202. if (deviceId.Contains(id) || id.Contains(deviceId))
  203. {
  204. return id;
  205. }
  206. }
  207. }
  208. finally
  209. {
  210. micList.release();
  211. }
  212. break;
  213. case HardwareType.Speaker:
  214. var speakerList = _deviceManager.getDevicesList(TXMediaDeviceType.TXMediaDeviceTypeSpeaker);
  215. try
  216. {
  217. var count = speakerList.getCount();
  218. for (uint i = 0; i < count; i++)
  219. {
  220. var id = speakerList.getDevicePID(i);
  221. if (deviceId.Contains(id) || id.Contains(deviceId))
  222. {
  223. return id;
  224. }
  225. }
  226. }
  227. finally
  228. {
  229. speakerList.release();
  230. }
  231. break;
  232. }
  233. return string.Empty;
  234. }
  235. public void Dispose()
  236. {
  237. try
  238. {
  239. ExitRoom();
  240. _cloud.removeCallback(this);
  241. _cloud.setLogCallback(null);
  242. _cloud = null;
  243. ITRTCCloud.destroyTRTCShareInstance();
  244. }
  245. catch (Exception ex)
  246. {
  247. Logger.WriteLineError($"TRTCPusher Dispose Error:{ex}");
  248. }
  249. }
  250. #region callback
  251. public void onAudioEffectFinished(int effectId, int code)
  252. {
  253. }
  254. public void onCameraDidReady()
  255. {
  256. Logger.WriteLineInfo($"onCameraDidReady");
  257. }
  258. public void onConnectionLost()
  259. {
  260. Logger.WriteLineInfo($"onConnectionLost");
  261. }
  262. public void onConnectionRecovery()
  263. {
  264. Logger.WriteLineInfo($"onConnectionRecovery");
  265. }
  266. public void onConnectOtherRoom(string userId, TXLiteAVError errCode, string errMsg)
  267. {
  268. }
  269. public void onDeviceChange(string deviceId, TXMediaDeviceType type, TRTCDeviceState state)
  270. {
  271. }
  272. public void onDisconnectOtherRoom(TXLiteAVError errCode, string errMsg)
  273. {
  274. }
  275. public void onEnterRoom(int result)
  276. {
  277. Logger.WriteLineInfo($"Enter room :{result}");
  278. }
  279. public void onError(TXLiteAVError errCode, string errMsg, IntPtr arg)
  280. {
  281. Logger.WriteLineError($"onError errCode:{errCode}, msg:{errMsg}, arg:{arg}");
  282. }
  283. public void onExitRoom(int reason)
  284. {
  285. Logger.WriteLineInfo($"Exit room success:{reason}");
  286. _exitResetEvent.Set();
  287. }
  288. public void onFirstAudioFrame(string userId)
  289. {
  290. Logger.WriteLineInfo($"onFirstAudioFrame userId: {userId}");
  291. }
  292. public void onFirstVideoFrame(string userId, TRTCVideoStreamType streamType, int width, int height)
  293. {
  294. Logger.WriteLineInfo($"onFirstVideoFrame userId: {userId}, streamType: {streamType}, width:{width}, height:{height}");
  295. }
  296. public void onMicDidReady()
  297. {
  298. Logger.WriteLineInfo($"onMicDidReady");
  299. }
  300. public void onMissCustomCmdMsg(string userId, int cmdId, int errCode, int missed)
  301. {
  302. }
  303. public void onNetworkQuality(TRTCQualityInfo localQuality, TRTCQualityInfo[] remoteQuality, uint remoteQualityCount)
  304. {
  305. }
  306. public void onPlayBGMBegin(TXLiteAVError errCode)
  307. {
  308. }
  309. public void onPlayBGMComplete(TXLiteAVError errCode)
  310. {
  311. }
  312. public void onPlayBGMProgress(uint progressMS, uint durationMS)
  313. {
  314. }
  315. public void onRecvCustomCmdMsg(string userId, int cmdID, uint seq, byte[] msg, uint msgSize)
  316. {
  317. }
  318. public void onRecvSEIMsg(string userId, byte[] message, uint msgSize)
  319. {
  320. }
  321. public void onRemoteUserEnterRoom(string userId)
  322. {
  323. }
  324. public void onRemoteUserLeaveRoom(string userId, int reason)
  325. {
  326. }
  327. public void onScreenCaptureCovered()
  328. {
  329. }
  330. public void onScreenCapturePaused(int reason)
  331. {
  332. }
  333. public void onScreenCaptureResumed(int reason)
  334. {
  335. }
  336. public void onScreenCaptureStarted()
  337. {
  338. }
  339. public void onScreenCaptureStoped(int reason)
  340. {
  341. }
  342. public void onSendFirstLocalAudioFrame()
  343. {
  344. }
  345. public void onSendFirstLocalVideoFrame(TRTCVideoStreamType streamType)
  346. {
  347. Logger.WriteLineInfo($"onSendFirstLocalVideoFrame successed");
  348. SendFirstLocalVideoFrame?.Invoke(this, true);
  349. }
  350. public void onSetMixTranscodingConfig(int errCode, string errMsg)
  351. {
  352. }
  353. public void onSpeedTest(TRTCSpeedTestResult currentResult, uint finishedCount, uint totalCount)
  354. {
  355. }
  356. public void onStartPublishCDNStream(int errCode, string errMsg)
  357. {
  358. }
  359. public void onStartPublishing(int errCode, string errMsg)
  360. {
  361. }
  362. public void onStatistics(TRTCStatistics statis)
  363. {
  364. }
  365. public void onStopPublishCDNStream(int errCode, string errMsg)
  366. {
  367. }
  368. public void onStopPublishing(int errCode, string errMsg)
  369. {
  370. }
  371. public void onSwitchRole(TXLiteAVError errCode, string errMsg)
  372. {
  373. }
  374. public void onTestMicVolume(uint volume)
  375. {
  376. }
  377. public void onTestSpeakerVolume(uint volume)
  378. {
  379. }
  380. public void onTryToReconnect()
  381. {
  382. }
  383. public void onUserAudioAvailable(string userId, bool available)
  384. {
  385. }
  386. public void onUserEnter(string userId)
  387. {
  388. }
  389. public void onUserExit(string userId, int reason)
  390. {
  391. }
  392. public void onUserSubStreamAvailable(string userId, bool available)
  393. {
  394. }
  395. public void onUserVideoAvailable(string userId, bool available)
  396. {
  397. }
  398. public void onUserVoiceVolume(TRTCVolumeInfo[] userVolumes, uint userVolumesCount, uint totalVolume)
  399. {
  400. }
  401. public void onWarning(TXLiteAVWarning warningCode, string warningMsg, IntPtr arg)
  402. {
  403. Logger.WriteLineWarn($"onWarning errCode: {warningCode}, msg:{warningMsg}, arg:{arg}");
  404. }
  405. public void onSwitchRoom(TXLiteAVError errCode, string errMsg)
  406. {
  407. }
  408. public void onAudioDeviceCaptureVolumeChanged(uint volume, bool muted)
  409. {
  410. }
  411. public void onAudioDevicePlayoutVolumeChanged(uint volume, bool muted)
  412. {
  413. }
  414. internal void SetMute(bool isMute)
  415. {
  416. if (_cloud != null)
  417. {
  418. _cloud.muteLocalAudio(isMute);
  419. }
  420. }
  421. internal void SwitchMic(string micId)
  422. {
  423. if (_cloud != null)
  424. {
  425. _cloud.stopLocalAudio();
  426. var correctMicId = GetCorrectId(HardwareType.Mic, micId);
  427. if (!string.IsNullOrEmpty(correctMicId))
  428. {
  429. var deviceManager = _cloud.getDeviceManager();
  430. deviceManager.setCurrentDevice(TXMediaDeviceType.TXMediaDeviceTypeMic, correctMicId);
  431. _cloud.startLocalAudio(TRTCAudioQuality.TRTCAudioQualityDefault);//开启本地音频的采集和上行
  432. }
  433. }
  434. }
  435. public void onRemoteVideoStatusUpdated(string userId, TRTCVideoStreamType streamType, TRTCAVStatusType status, TRTCAVStatusChangeReason reason, IntPtr extrainfo)
  436. {
  437. }
  438. public void onSpeedTestResult(TRTCSpeedTestResult result)
  439. {
  440. }
  441. public void onStartPublishMediaStream(string taskId, int code, string message, IntPtr extraInfo)
  442. {
  443. }
  444. public void onUpdatePublishMediaStream(string taskId, int code, string message, IntPtr extraInfo)
  445. {
  446. }
  447. public void onStopPublishMediaStream(string taskId, int code, string message, IntPtr extraInfo)
  448. {
  449. }
  450. public void onCdnStreamStateChanged(string cdnUrl, int status, int code, string msg, IntPtr extraInfo)
  451. {
  452. }
  453. public void onLocalRecordBegin(int errCode, string storagePath)
  454. {
  455. }
  456. public void onLocalRecording(int duration, string storagePath)
  457. {
  458. }
  459. public void onLocalRecordComplete(int errCode, string storagePath)
  460. {
  461. }
  462. public void onSnapshotComplete(string userId, TRTCVideoStreamType type, byte[] data, uint length, uint width, uint height, TRTCVideoPixelFormat format)
  463. {
  464. }
  465. #endregion callback
  466. public enum HardwareType
  467. {
  468. Mic,
  469. Camera,
  470. Speaker
  471. }
  472. }
  473. }