LiveWebRecordService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by KLXQ
  4. * user: youyi
  5. * Date:2024/6/12 11:36.
  6. */
  7. namespace App\Services;
  8. use App\Http\Helpers\Tencent\MediaTencent;
  9. use App\Http\Helpers\Tencent\SignTencent;
  10. use App\Http\Helpers\Tencent\TrtcTencent;
  11. use App\Models\LectureRecord;
  12. use App\Models\LiveRecord;
  13. use App\Models\MeetingCentre;
  14. use App\Models\Room as RoomModel;
  15. use App\Models\RoomWebRecord;
  16. class LiveWebRecordService
  17. {
  18. public array $storageParams = [
  19. 'CloudVod' => [
  20. 'TencentVod' => [
  21. 'StorageRegion' => 'ap-chongqing',
  22. 'Procedure' => 'STD-H264-MP4-1080P',
  23. 'ClassId' => 1174322,
  24. 'SubAppId' => 1327013090,
  25. 'UserDefineRecordId' => '',
  26. ],
  27. ],
  28. ];
  29. public function startWebRecord($request)
  30. {
  31. $roomId = $request['room_id'];
  32. $roomName = $request['room_name'];
  33. $otherParams = $request['other_params'];
  34. $userId = $request['account_id'];
  35. // 录制机器人的UserId,用于进房发起录制任务
  36. $recordUserId = 'recorder_'.$roomId.'_'.$userId;
  37. $this->storageParams['CloudVod']['TencentVod']['UserDefineRecordId'] = $recordUserId;
  38. $sign = new SignTencent();
  39. $userSign = $sign->genUserSign($recordUserId);
  40. $recordInsertData = [
  41. 'lecture_id' => $roomId,
  42. 'room_name' => $roomName,
  43. 'user_id' => $userId,
  44. 'record_user_id' => $recordUserId,
  45. 'user_sign' => $userSign,
  46. ];
  47. $maxDurationLimit = config('tencent.web.max_duration_limit.region');
  48. $tParams = [
  49. 'RecordUrl' => $this->webRecordUrl($recordInsertData, $otherParams),
  50. 'MaxDurationLimit' => $maxDurationLimit,
  51. 'StorageParams' => $this->storageParams,
  52. ];
  53. // print_r($tParams);exit;
  54. $tRoom = new TrtcTencent('StartWebRecord', $tParams);
  55. $retData = $tRoom->resultArr;
  56. $taskId = $retData['data']['TaskId'] ?? '';
  57. if (!$taskId) {
  58. return false;
  59. }
  60. $recordInsertData['task_id'] = $taskId;
  61. $roomRecord = new LectureRecord();
  62. $roomRecord->fill($recordInsertData)->save();
  63. return $taskId;
  64. }
  65. public function describeWebRecord($taskId)
  66. {
  67. $params['TaskId'] = $taskId;
  68. $tRoom = new TrtcTencent('DescribeWebRecord', $params);
  69. $retData = $tRoom->resultArr;
  70. return $retData['data'];
  71. }
  72. public function stopWebRecord($taskId)
  73. {
  74. $params['TaskId'] = $taskId;
  75. $tRoom = new TrtcTencent('StopWebRecord', $params);
  76. $retData = $tRoom->resultArr;
  77. return $retData['data'];
  78. }
  79. public function deleteMedia($fileId)
  80. {
  81. if (!$fileId) {
  82. return false;
  83. }
  84. $params['FileId'] = $fileId;
  85. $tMedia = new MediaTencent('DeleteMedia', $params);
  86. $retData = $tMedia->resultArr;
  87. return $retData['data'];
  88. }
  89. // 构建Web录制页面的URL
  90. public function webRecordUrl($data, $otherParams): string
  91. {
  92. $url = getenv('WEB_URL').'Room?';
  93. $params = [
  94. 'user_id' => $data['record_user_id'],
  95. 'username' => $data['record_user_id'],
  96. 'room_id' => (string) $data['lecture_id'],
  97. 'room_name' => $data['room_name'],
  98. 'user_sign' => $data['user_sign'],
  99. 'sdk_app_id' => (int)config('tencent.trtc.app_id'),
  100. 'room_exist' => true,
  101. 'is_record' => true,
  102. ];
  103. $extraInfo = [
  104. ];
  105. // 其他参数,主要控制页面录制时,页面需要渲染成什么样子
  106. if (\is_array($otherParams)) {
  107. foreach ($otherParams as $k => $v) {
  108. $params[$k] = $v;
  109. }
  110. }
  111. // print_r($params);exit;
  112. $url .= 'roomInfo='.urlencode(json_encode($params, JSON_UNESCAPED_UNICODE));
  113. $url .= '&extraInfo='.urlencode(json_encode($extraInfo, JSON_UNESCAPED_UNICODE));
  114. $url.='&is_record=true&room_id='.$data['lecture_id'];
  115. return $url;
  116. }
  117. }