Qiniu.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Live\Lib;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'live');
  5. class Qiniu
  6. {
  7. private $stream;
  8. private $config;
  9. private $space;
  10. # 连接
  11. public function __construct($config)
  12. {
  13. $this->config = $config;
  14. $mac = new \Qiniu\Pili\Mac($this->config['appkey'], $this->config['appsecret']);
  15. $client = new \Qiniu\Pili\Client($mac);
  16. $this->space = $client->hub($this->config['space']);
  17. }
  18. # 创建新流
  19. public function create($name)
  20. {
  21. $result = $this->space->create($name);
  22. return $this->getInfo($name);
  23. }
  24. # 查询流
  25. public function get($name)
  26. {
  27. if (!$this->stream) {
  28. $this->stream = $this->space->stream($name);
  29. }
  30. return $this->stream;
  31. }
  32. # 查询流的信息
  33. public function getInfo($name)
  34. {
  35. $stream = $this->get($name);
  36. return $stream->info();
  37. }
  38. # 获取直播状态
  39. public function getLiveStatus($name)
  40. {
  41. $stream = $this->get($name);
  42. return $stream->liveStatus();
  43. }
  44. # 列出所有流
  45. public function gets($prefix = '', $limit = 10000)
  46. {
  47. return $this->space->listStreams($prefix, $limit, '');
  48. }
  49. # 列出所有正在直播的流
  50. public function getLives($prefix = '', $limit = 10000)
  51. {
  52. return $this->space->listLiveStreams($prefix, $limit, '');
  53. }
  54. # 启动流
  55. public function start($name = '')
  56. {
  57. $stream = $this->get($name);
  58. $stream->enable();
  59. return $this->getLiveStatus($name);
  60. }
  61. # 禁用流
  62. public function stop($name = '', $num = 120)
  63. {
  64. $stream = $this->get($name);
  65. $stream->disable(time() + $num);
  66. return $this->getLiveStatus($name);
  67. }
  68. # 保存直播数据
  69. public function save($name, $start = 0, $end = 0, $format = false)
  70. {
  71. $stream = $this->get($name);
  72. if ($end <= 0) {
  73. $end = time();
  74. }
  75. if ($format) {
  76. $option = array('format' => $format, 'start' => $start, 'end' => $end);
  77. $data = $stream->saveas($option);
  78. } else {
  79. $data = $stream->save($start, $end);
  80. }
  81. if (isset($data['fname'])) {
  82. return $data['fname'];
  83. }
  84. return '';
  85. }
  86. # 保存直播截图
  87. public function savePic($name)
  88. {
  89. $stream = $this->get($name);
  90. return $stream->snapshot(array("format" => "jpg"));
  91. }
  92. # 查看历史直播流
  93. public function history($name)
  94. {
  95. $stream = $this->get($name);
  96. return $stream->historyActivity(0, 0);
  97. }
  98. # 查询推流历史
  99. public function update($name, $convert)
  100. {
  101. $stream = $this->get($name);
  102. $stream->updateConverts($convert);
  103. return $stream->info();
  104. }
  105. # 获取推流地址
  106. public function getRtmp($name, $time = 3600)
  107. {
  108. $url = \Qiniu\Pili\RTMPPublishURL($this->config['domain_publish'], $this->config['space'], $name, $time, $this->config['appkey'], $this->config['appsecret']);
  109. return $url;
  110. }
  111. # 获取直播地址
  112. public function getPlay($name)
  113. {
  114. //高清、标清、普清
  115. $url['rtmp'] = $this->getRtmpPlay($name);
  116. $url['hls'] = $this->getHlsPlay($name);
  117. $url['hdl'] = $this->getHdlPlay($name);
  118. $url['pic'] = $this->getPicPlay($name);
  119. return $url;
  120. }
  121. # 获取直播地址
  122. public function getRtmpPlay($name)
  123. {
  124. $url = \Qiniu\Pili\RTMPPlayURL($this->config['domain_live_rtmp'], $this->config['space'], $name);
  125. return $url;
  126. }
  127. # HLS 直播地址
  128. public function getHlsPlay($name)
  129. {
  130. $url = \Qiniu\Pili\HLSPlayURL($this->config['domain_live_hls'], $this->config['space'], $name);
  131. return $url;
  132. }
  133. # HDL 直播地址
  134. public function getHdlPlay($name)
  135. {
  136. $url = \Qiniu\Pili\HDLPlayURL($this->config['domain_live_hdl'], $this->config['space'], $name);
  137. return $url;
  138. }
  139. # 截图直播地址
  140. public function getPicPlay($name)
  141. {
  142. $url = \Qiniu\Pili\SnapshotPlayURL($this->config['domain_live_pic'], $this->config['space'], $name);
  143. return $url;
  144. }
  145. }