LiveService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  14. use App\Models\MemberModel;
  15. use Darabonba\OpenApi\Models\Config;
  16. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  17. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  18. /**
  19. * 在线直播服务管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * Class LiveService
  23. * @package App\Services
  24. */
  25. class LiveService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 静态入口
  31. * @return SmsService|static|null
  32. */
  33. public static function make()
  34. {
  35. if (!self::$instance) {
  36. self::$instance = new static();
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * 获取直播推流/拉流地址
  42. * @param $streamName
  43. * @param string $appName
  44. * @param string $playType
  45. * @param int $expireTime
  46. * @return array
  47. */
  48. public function getLiveUrl($streamName, $appName = 'xlapp', $playType = 'rtmp', $expireTime = 1440)
  49. {
  50. $playUrls = [];
  51. //未开启鉴权Key的情况下
  52. $pushDomain = ConfigService::make()->getConfigByCode('live_push_url');
  53. $playDomain = ConfigService::make()->getConfigByCode('live_play_url');
  54. $pushKey = ConfigService::make()->getConfigByCode('push_url_key');
  55. $playKey = ConfigService::make()->getConfigByCode('play_url_key');
  56. $timeStamp = time() + $expireTime * 60;
  57. if ($pushKey == '') {
  58. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName;
  59. } else {
  60. $sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $pushKey;
  61. $md5hash = md5($sstring);
  62. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $md5hash;
  63. }
  64. if ($playKey == '') {
  65. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName;
  66. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv';
  67. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8';
  68. } else {
  69. $rtmpSstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $playKey;
  70. $rtmpMd5hash = md5($rtmpSstring);
  71. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $rtmpMd5hash;
  72. $flvSstring = '/' . $appName . '/' . $streamName . '.flv-' . $timeStamp . '-0-0-' . $playKey;
  73. $flvMd5hash = md5($flvSstring);
  74. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv?auth_key=' . $timeStamp . '-0-0-' . $flvMd5hash;
  75. $hlsSstring = '/' . $appName . '/' . $streamName . '.m3u8-' . $timeStamp . '-0-0-' . $playKey;
  76. $hlsMd5hash = md5($hlsSstring);
  77. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8?auth_key=' . $timeStamp . '-0-0-' . $hlsMd5hash;
  78. }
  79. return [
  80. 'push_url' => $pushUrl,
  81. 'play_url' => isset($playUrls[$playType]) ? $playUrls[$playType] : '',
  82. 'play_urls' => $playUrls,
  83. ];
  84. }
  85. /**
  86. * 创建直播间
  87. * @param $userId 直逼用户
  88. * @param $params
  89. * @return array|false
  90. */
  91. public function create($userId, $params)
  92. {
  93. $liveLevel = ConfigService::make()->getConfigByCode('live_open_level');
  94. $liveLevel = $liveLevel > 0 ? $liveLevel : 0;
  95. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->select(['id', 'nickname', 'member_level', 'status'])->first();
  96. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  97. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  98. $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : 0;
  99. if (empty($userInfo) || $status != 1) {
  100. $this->error = 2024;
  101. return false;
  102. }
  103. if($memberLevel < $liveLevel){
  104. $this->error = 2040;
  105. return false;
  106. }
  107. $data = [
  108. 'type' => isset($params['type']) ? intval($params['type']) : 1,
  109. 'title' => isset($params['title']) && $params['title'] ? trim($params['title']) : $nickname.'正在直播',
  110. 'description' => isset($params['description']) && $params['description'] ? trim($params['description']) : '',
  111. 'category' => isset($params['category']) ? intval($params['category']) : 0,
  112. 'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 0,
  113. 'visible_users' => isset($params['visible_users']) ? trim($params['visible_users']) : '',
  114. 'chat_type' => isset($params['chat_type']) ? intval($params['chat_type']) : 0,
  115. 'chat_status' => isset($params['chat_status']) ? intval($params['chat_status']) : 1,
  116. 'pay_status' => isset($params['pay_status']) ? intval($params['pay_status']) : 1,
  117. 'open_area' => isset($params['open_area']) ? intval($params['open_area']) : 1,
  118. 'view_allow' => isset($params['view_allow']) ? intval($params['view_allow']) : 1,
  119. 'push_url' => isset($params['push_url']) ? trim($params['push_url']) : '',
  120. 'play_url' => isset($params['play_url']) ? trim($params['play_url']) : '',
  121. 'create_time'=> time(),
  122. 'update_time'=> time(),
  123. 'status' =>1,
  124. 'mark'=>1,
  125. ];
  126. if(!$liveId = $this->model->insertGetId($data)){
  127. $this->error = 2042;
  128. return false;
  129. }
  130. $data['id'] = $liveId;
  131. $this->error = 2041;
  132. return $data;
  133. }
  134. }