video_ksyun.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. fanwe_require(APP_ROOT_PATH . 'vendor/autoload.php');
  3. use Ksyun\Service\Cdn;
  4. use GuzzleHttp\Client;
  5. define("KS3_API_LOG", false);
  6. define("KS3_API_DISPLAY_LOG", false);
  7. /**
  8. * 文档地址 https://v.ksyun.com/doc.html#/doc/livesdk.md
  9. */
  10. class VideoKsyun
  11. {
  12. private $m_config;
  13. function __construct($m_config)
  14. {
  15. $this->m_config = $m_config;
  16. }
  17. function getExpireTime()
  18. {
  19. $tz = 'Asia/Shanghai';
  20. $dt = new DateTime("now", new DateTimeZone($tz));
  21. // 流时长6小时之内,且主动正常断流会触发拼接
  22. $dt->add(new DateInterval('PT6H'));
  23. return $dt->getTimestamp();
  24. }
  25. public function Create($video_id)
  26. {
  27. // 流时长6小时之内,且主动正常断流会触发拼接
  28. $time = $this->getExpireTime();
  29. $nonce = md5($video_id . rand(1, 9999999));
  30. $signature = base64_encode(hash_hmac('sha1', "GET\{$time}\nnonce={$nonce}&vdoid={$video_id}",
  31. $this->m_config['ks3_secretkey'], true));
  32. $stream_id = $video_id . "_" . substr(md5($video_id . microtime_float()), 12);
  33. $params = http_build_query(array(
  34. 'signature' => $signature,
  35. 'accesskey' => $this->m_config['ks3_accesskey'],
  36. 'expire' => $time,
  37. 'nonce' => $nonce,
  38. 'vdoid' => $video_id,
  39. ));
  40. if (empty($this->m_config['ksyun_domain'])) {
  41. $upstream_address = "rtmp://{$this->m_config['ksyun_app']}.uplive.ks-cdn.com/live/{$stream_id}?" . $params;
  42. return array(
  43. 'stream_id' => $stream_id,
  44. 'push_rtmp' => $upstream_address,
  45. 'play_rtmp' => "rtmp://{$this->m_config['ksyun_app']}.rtmplive.ks-cdn.com/live/" . $stream_id,
  46. 'play_flv' => "http://{$this->m_config['ksyun_app']}.hdllive.ks-cdn.com/live/" . $stream_id . ".flv",
  47. 'play_hls' => "http://{$this->m_config['ksyun_app']}.hlslive.ks-cdn.com/live/" . $stream_id . "/index.m3u8",
  48. );
  49. } else {
  50. $upstream_address = "rtmp://uplive.{$this->m_config['ksyun_domain']}/live/{$stream_id}?" . $params;
  51. return array(
  52. 'stream_id' => $stream_id,
  53. 'push_rtmp' => $upstream_address,
  54. 'play_rtmp' => "rtmp://rtmplive.{$this->m_config['ksyun_domain']}/live/" . $stream_id,
  55. 'play_flv' => "http://hdllive.{$this->m_config['ksyun_domain']}/live/" . $stream_id . ".flv",
  56. 'play_hls' => "http://hlslive.{$this->m_config['ksyun_domain']}/live/" . $stream_id . "/index.m3u8",
  57. );
  58. }
  59. }
  60. public function Query($stream_id)
  61. {
  62. $time = $this->getExpireTime();
  63. $signature = base64_encode(hash_hmac('sha1', "GET\n{$time}\napp=live&name=" . $stream_id,
  64. $this->m_config['ks3_secretkey'], true));
  65. $url = "http://{$this->m_config['ksyun_app']}.dashboard.ks-cdn.com/v2/stat?" . http_build_query(array(
  66. 'signature' => $signature,
  67. 'accesskey' => $this->m_config['ks3_accesskey'],
  68. 'expire' => $time,
  69. 'app' => 'live',
  70. 'name' => $stream_id,
  71. ));
  72. $client = new Client();
  73. $response = $client->request('GET', $url);
  74. $result = json_decode((string)$response->getBody(), true);
  75. return array(
  76. 'stream_status' => isset($result['app']['live'][$stream_id]) ? 1 : 0
  77. );
  78. }
  79. public function Stop($stream_id)
  80. {
  81. $time = $this->getExpireTime();
  82. $signature = base64_encode(hash_hmac('sha1', "GET\n{$time}\napp=live&method=add&name=" . $stream_id,
  83. $this->m_config['ks3_secretkey'], true));
  84. $url = "http://{$this->m_config['ksyun_app']}.dashboard.ks-cdn.com/blacklist?" . http_build_query(array(
  85. 'signature' => $signature,
  86. 'accesskey' => $this->m_config['ks3_accesskey'],
  87. 'expire' => $time,
  88. 'method' => 'add',
  89. 'app' => 'live',
  90. 'name' => $stream_id,
  91. ));
  92. $client = new Client();
  93. $response = $client->request('GET', $url);
  94. return $response->getStatuscode() == 200;
  95. }
  96. public function GetRecord($stream_id, $video_id)
  97. {
  98. fanwe_require(APP_ROOT_PATH . "system/ks3-php-sdk/Ks3Client.class.php");
  99. $client = new Ks3Client($this->m_config['ks3_accesskey'], $this->m_config['ks3_secretkey'],
  100. "ks3-cn-beijing.ksyun.com");//!!第三个参数endpoint需要对应bucket所在region!! 详见http://ks3.ksyun.com/doc/api/index.html Region(区域)一节
  101. // ks3-cn-beijing-internal.ksyun.com
  102. $bucket = "fanwelive2";
  103. $mp4_key = $video_id . ".mp4";
  104. $args = array(
  105. "Bucket" => $bucket,
  106. "Key" => $mp4_key,
  107. );
  108. if ($client->objectExists($args)) {
  109. $ks_url = $client->generatePresignedUrl(
  110. array(
  111. "Bucket" => $bucket,
  112. "Key" => $mp4_key,
  113. "Options" => array(
  114. "Expires" => 60 * 60 * 24 * 10,
  115. )
  116. )
  117. );
  118. return array(
  119. '20' => $ks_url,
  120. );
  121. } else {
  122. return array();
  123. }
  124. }
  125. }