RoomWebRecordService.php 4.3 KB

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