LiveService.php 6.9 KB

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