LiveService.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Darabonba\OpenApi\Models\Config;
  15. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  16. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  17. /**
  18. * 在线直播服务管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class LiveService
  22. * @package App\Services
  23. */
  24. class LiveService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 静态入口
  30. * @return SmsService|static|null
  31. */
  32. public static function make(){
  33. if(!self::$instance){
  34. self::$instance = new static();
  35. }
  36. return self::$instance;
  37. }
  38. /**
  39. * 获取直播推流/拉流地址
  40. * @param $streamName
  41. * @param string $appName
  42. * @param string $playType
  43. * @param int $expireTime
  44. * @return array
  45. */
  46. public function getLiveUrl($streamName, $appName='xlapp', $playType='rtmp',$expireTime=1440)
  47. {
  48. $playUrls = [];
  49. //未开启鉴权Key的情况下
  50. $pushDomain = ConfigService::make()->getConfigByCode('live_push_url');
  51. $playDomain = ConfigService::make()->getConfigByCode('live_play_url');
  52. $pushKey = ConfigService::make()->getConfigByCode('push_url_key');
  53. $playKey = ConfigService::make()->getConfigByCode('play_url_key');
  54. $timeStamp = time() + $expireTime * 60;
  55. if($pushKey==''){
  56. $pushUrl = 'rtmp://'.$pushDomain.'/'.$appName.'/'.$streamName;
  57. }else{
  58. $sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$pushKey;
  59. $md5hash = md5($sstring);
  60. $pushUrl = 'rtmp://'.$pushDomain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$md5hash;
  61. }
  62. if($playKey==''){
  63. $playUrls['rtmp'] = 'rtmp://'.$playDomain.'/'.$appName.'/'.$streamName;
  64. $playUrls['flv'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.flv';
  65. $playUrls['hls'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.m3u8';
  66. }else{
  67. $rtmpSstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$playKey;
  68. $rtmpMd5hash = md5($rtmpSstring);
  69. $playUrls['rtmp'] = 'rtmp://'.$playDomain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$rtmpMd5hash;
  70. $flvSstring = '/'.$appName.'/'.$streamName.'.flv-'.$timeStamp.'-0-0-'.$playKey;
  71. $flvMd5hash = md5($flvSstring);
  72. $playUrls['flv'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$timeStamp.'-0-0-'.$flvMd5hash;
  73. $hlsSstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$timeStamp.'-0-0-'.$playKey;
  74. $hlsMd5hash = md5($hlsSstring);
  75. $playUrls['hls'] = 'http://'.$playDomain.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$timeStamp.'-0-0-'.$hlsMd5hash;
  76. }
  77. return [
  78. 'push_url'=> $pushUrl,
  79. 'play_url'=> isset($playUrls[$playType])? $playUrls[$playType] : '',
  80. 'play_urls'=> $playUrls,
  81. ];
  82. }
  83. }