video_aliyun.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. fanwe_require(APP_ROOT_PATH . 'system/aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php');
  3. use live\Request\V20161101 as Live;
  4. /**
  5. * 文档地址 https://help.aliyun.com/document_detail/48207.html?spm=5176.doc35411.6.570.AeuNzq
  6. */
  7. class VideoAliyun
  8. {
  9. private $m_config;
  10. function __construct($m_config)
  11. {
  12. $this->m_config = $m_config;
  13. }
  14. private function build_auth_key($uri, $time)
  15. {
  16. $auth_key = md5("{$uri}-{$time}-0-0-" . $this->m_config['aliyun_private_key']);
  17. return "{$uri}?auth_key={$time}-0-0-" . $auth_key;
  18. }
  19. private function get_vhost()
  20. {
  21. $max = 10;
  22. $videos = $GLOBALS['db']->getAll("select vhost , count(*) as num from " . DB_PREFIX . "video_aliyun group by vhost");
  23. $vhost_max = array();
  24. foreach ($videos as $video) {
  25. if ($video['num'] < $max) {
  26. return $video['vhost'];
  27. }
  28. $vhost_max[] = $video['vhost'];
  29. }
  30. $vhosts = preg_split("/[\r\n]+/", $this->m_config['aliyun_vhost']);
  31. foreach ($vhosts as $vhost) {
  32. if (!in_array($vhost, $vhost_max)) {
  33. return trim($vhost);
  34. }
  35. }
  36. return false;
  37. }
  38. /** https://help.aliyun.com/document_detail/29957.html?spm=5176.doc35409.6.546.IMpk1g
  39. * @param $video_id
  40. * @return array
  41. */
  42. public function Create($video_id)
  43. {
  44. $vhost = $this->get_vhost();
  45. if (!$vhost) {
  46. return array('status' => 0,"error" => '推流数量已超出最大值');
  47. }
  48. $stream_id = $video_id . "_" . substr(md5($video_id . microtime_float()), 12);
  49. $app_name = 'live';
  50. $upstream_time = NOW_TIME + 8 * 3600 + 1800;
  51. $download_time = $upstream_time + 6 * 3600;
  52. $GLOBALS['db']->autoExecute(DB_PREFIX . "video_aliyun", array(
  53. 'vhost' => $vhost,
  54. 'stream_id' => $stream_id,
  55. 'create_time' => NOW_TIME,
  56. ));
  57. return array(
  58. 'status' => 1,
  59. 'stream_id' => $stream_id,
  60. 'push_rtmp' => "rtmp://video-center.alivecdn.com" . $this->build_auth_key("/{$app_name}/{$stream_id}",
  61. $upstream_time) . "&vhost={$vhost}",
  62. 'play_rtmp' => "rtmp://{$vhost}" . $this->build_auth_key("/{$app_name}/{$stream_id}", $download_time),
  63. 'play_flv' => "http://{$vhost}" . $this->build_auth_key("/{$app_name}/{$stream_id}.flv", $download_time),
  64. 'play_hls' => "http://{$vhost}" . $this->build_auth_key("/{$app_name}/{$stream_id}.m3u8", $download_time),
  65. );
  66. }
  67. /** https://help.aliyun.com/document_detail/35409.html?spm=5176.doc35413.6.581.U1UYO6
  68. * @param $stream_id
  69. * @return array
  70. */
  71. public function Query($stream_id)
  72. {
  73. $iClientProfile = DefaultProfile::getProfile($this->m_config['aliyun_region'],
  74. $this->m_config['aliyun_access_key'],
  75. $this->m_config['aliyun_access_secret']);
  76. $client = new DefaultAcsClient($iClientProfile);
  77. $vhost = $GLOBALS['db']->getOne("select vhost from " . DB_PREFIX . "video_aliyun where stream_id = '{$stream_id}'");
  78. $request = new Live\DescribeLiveStreamsOnlineListRequest();
  79. $request->setDomainName($vhost);
  80. $request->setAppName('live');
  81. $response = $client->getAcsResponse($request);
  82. $status = 0;
  83. foreach ($response->OnlineInfo->LiveStreamOnlineInfo as $stream) {
  84. if ($stream->StreamName == $stream_id) {
  85. $status = 1;
  86. }
  87. }
  88. return array(
  89. 'stream_status' => $status
  90. );
  91. }
  92. /** https://help.aliyun.com/document_detail/35413.html?spm=5176.doc48207.2.6.Gk4oAS
  93. * @param $stream_id
  94. * @return bool
  95. */
  96. public function Stop($stream_id,$vhost='')
  97. {
  98. $iClientProfile = DefaultProfile::getProfile($this->m_config['aliyun_region'],
  99. $this->m_config['aliyun_access_key'],
  100. $this->m_config['aliyun_access_secret']);
  101. $client = new DefaultAcsClient($iClientProfile);
  102. if(empty($vhost)){
  103. $vhost = $GLOBALS['db']->getOne("select vhost from " . DB_PREFIX . "video_aliyun where stream_id = '{$stream_id}'");
  104. }
  105. try {
  106. $request = new Live\ForbidLiveStreamRequest();
  107. $request->setDomainName($vhost);
  108. $request->setAppName('live');
  109. $request->setStreamName($stream_id);
  110. $request->setLiveStreamType("publisher");
  111. $response = $client->getAcsResponse($request);
  112. if (!empty($response->RequestId)) {
  113. $GLOBALS['db']->query("delete from " . DB_PREFIX . "video_aliyun where stream_id = '{$stream_id}'");
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. } catch (Exception $e) {
  119. log_err_file($e->getMessage());
  120. return false;
  121. }
  122. }
  123. public function GetRecord($stream_id)
  124. {
  125. return array();
  126. }
  127. }