BucketLiveChannelTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace OSS\Tests;
  3. require_once __DIR__ . '/Common.php';
  4. use OSS\Model\LiveChannelConfig;
  5. use OSS\Core\OssException;
  6. class BucketLiveChannelTest extends \PHPUnit_Framework_TestCase
  7. {
  8. private $bucketName;
  9. private $client;
  10. public function setUp()
  11. {
  12. $this->client = Common::getOssClient();
  13. $this->bucketName = 'php-sdk-test-rtmp-bucket-name-' . strval(rand(0, 10000));
  14. $this->client->createBucket($this->bucketName);
  15. Common::waitMetaSync();
  16. }
  17. public function tearDown()
  18. {
  19. ////to delete created bucket
  20. //1. delele live channel
  21. $list = $this->client->listBucketLiveChannels($this->bucketName);
  22. if (count($list->getChannelList()) != 0)
  23. {
  24. foreach($list->getChannelList() as $list)
  25. {
  26. $this->client->deleteBucketLiveChannel($this->bucketName, $list->getName());
  27. }
  28. }
  29. //2. delete exsited object
  30. $prefix = 'live-test/';
  31. $delimiter = '/';
  32. $nextMarker = '';
  33. $maxkeys = 1000;
  34. $options = array(
  35. 'delimiter' => $delimiter,
  36. 'prefix' => $prefix,
  37. 'max-keys' => $maxkeys,
  38. 'marker' => $nextMarker,
  39. );
  40. try {
  41. $listObjectInfo = $this->client->listObjects($this->bucketName, $options);
  42. } catch (OssException $e) {
  43. printf($e->getMessage() . "\n");
  44. return;
  45. }
  46. $objectList = $listObjectInfo->getObjectList(); // 文件列表
  47. if (!empty($objectList))
  48. {
  49. foreach($objectList as $objectInfo)
  50. $this->client->deleteObject($this->bucketName, $objectInfo->getKey());
  51. }
  52. //3. delete the bucket
  53. $this->client->deleteBucket($this->bucketName);
  54. }
  55. public function testPutLiveChannel()
  56. {
  57. $config = new LiveChannelConfig(array(
  58. 'description' => 'live channel 1',
  59. 'type' => 'HLS',
  60. 'fragDuration' => 10,
  61. 'fragCount' => 5,
  62. 'playListName' => 'hello.m3u8'
  63. ));
  64. $info = $this->client->putBucketLiveChannel($this->bucketName, 'live-1', $config);
  65. $this->client->deleteBucketLiveChannel($this->bucketName, 'live-1');
  66. $this->assertEquals('live-1', $info->getName());
  67. $this->assertEquals('live channel 1', $info->getDescription());
  68. $this->assertEquals(1, count($info->getPublishUrls()));
  69. $this->assertEquals(1, count($info->getPlayUrls()));
  70. }
  71. public function testPutLiveChannelWithDefaultParams()
  72. {
  73. $config = new LiveChannelConfig(array(
  74. 'description' => 'live channel 1',
  75. 'type' => 'HLS',
  76. ));
  77. $info = $this->client->putBucketLiveChannel($this->bucketName, 'live-1', $config);
  78. $this->client->deleteBucketLiveChannel($this->bucketName, 'live-1');
  79. $this->assertEquals('live-1', $info->getName());
  80. $this->assertEquals('live channel 1', $info->getDescription());
  81. $this->assertEquals(1, count($info->getPublishUrls()));
  82. $this->assertEquals(1, count($info->getPlayUrls()));
  83. }
  84. public function testListLiveChannels()
  85. {
  86. $config = new LiveChannelConfig(array(
  87. 'description' => 'live channel 1',
  88. 'type' => 'HLS',
  89. 'fragDuration' => 10,
  90. 'fragCount' => 5,
  91. 'playListName' => 'hello.m3u8'
  92. ));
  93. $this->client->putBucketLiveChannel($this->bucketName, 'live-1', $config);
  94. $config = new LiveChannelConfig(array(
  95. 'description' => 'live channel 2',
  96. 'type' => 'HLS',
  97. 'fragDuration' => 10,
  98. 'fragCount' => 5,
  99. 'playListName' => 'hello.m3u8'
  100. ));
  101. $this->client->putBucketLiveChannel($this->bucketName, 'live-2', $config);
  102. $list = $this->client->listBucketLiveChannels($this->bucketName);
  103. $this->assertEquals($this->bucketName, $list->getBucketName());
  104. $this->assertEquals(false, $list->getIsTruncated());
  105. $channels = $list->getChannelList();
  106. $this->assertEquals(2, count($channels));
  107. $chan1 = $channels[0];
  108. $this->assertEquals('live-1', $chan1->getName());
  109. $this->assertEquals('live channel 1', $chan1->getDescription());
  110. $this->assertEquals(1, count($chan1->getPublishUrls()));
  111. $this->assertEquals(1, count($chan1->getPlayUrls()));
  112. $chan2 = $channels[1];
  113. $this->assertEquals('live-2', $chan2->getName());
  114. $this->assertEquals('live channel 2', $chan2->getDescription());
  115. $this->assertEquals(1, count($chan2->getPublishUrls()));
  116. $this->assertEquals(1, count($chan2->getPlayUrls()));
  117. $list = $this->client->listBucketLiveChannels($this->bucketName, array(
  118. 'prefix' => 'live-',
  119. 'marker' => 'live-1',
  120. 'max-keys' => 10
  121. ));
  122. $channels = $list->getChannelList();
  123. $this->assertEquals(1, count($channels));
  124. $chan2 = $channels[0];
  125. $this->assertEquals('live-2', $chan2->getName());
  126. $this->assertEquals('live channel 2', $chan2->getDescription());
  127. $this->assertEquals(1, count($chan2->getPublishUrls()));
  128. $this->assertEquals(1, count($chan2->getPlayUrls()));
  129. $this->client->deleteBucketLiveChannel($this->bucketName, 'live-1');
  130. $this->client->deleteBucketLiveChannel($this->bucketName, 'live-2');
  131. $list = $this->client->listBucketLiveChannels($this->bucketName, array(
  132. 'prefix' => 'live-'
  133. ));
  134. $this->assertEquals(0, count($list->getChannelList()));
  135. }
  136. public function testDeleteLiveChannel()
  137. {
  138. $channelName = 'live-to-delete';
  139. $config = new LiveChannelConfig(array(
  140. 'description' => 'live channel to delete',
  141. 'type' => 'HLS',
  142. 'fragDuration' => 10,
  143. 'fragCount' => 5,
  144. 'playListName' => 'hello.m3u8'
  145. ));
  146. $this->client->putBucketLiveChannel($this->bucketName, $channelName, $config);
  147. $this->client->deleteBucketLiveChannel($this->bucketName, $channelName);
  148. $list = $this->client->listBucketLiveChannels($this->bucketName, array(
  149. 'prefix' => $channelName
  150. ));
  151. $this->assertEquals(0, count($list->getChannelList()));
  152. }
  153. public function testSignRtmpUrl()
  154. {
  155. $channelName = '90475';
  156. $bucket = 'douyu';
  157. $now = time();
  158. $url = $this->client->signRtmpUrl($bucket, $channelName, 900, array(
  159. 'params' => array(
  160. 'playlistName' => 'playlist.m3u8'
  161. )
  162. ));
  163. $ret = parse_url($url);
  164. $this->assertEquals('rtmp', $ret['scheme']);
  165. parse_str($ret['query'], $query);
  166. $this->assertTrue(isset($query['OSSAccessKeyId']));
  167. $this->assertTrue(isset($query['Signature']));
  168. $this->assertTrue(intval($query['Expires']) - ($now + 900) < 3);
  169. $this->assertEquals('playlist.m3u8', $query['playlistName']);
  170. }
  171. public function testLiveChannelInfo()
  172. {
  173. $channelName = 'live-to-put-status';
  174. $config = new LiveChannelConfig(array(
  175. 'description' => 'test live channel info',
  176. 'type' => 'HLS',
  177. 'fragDuration' => 10,
  178. 'fragCount' => 5,
  179. 'playListName' => 'hello.m3u8'
  180. ));
  181. $this->client->putBucketLiveChannel($this->bucketName, $channelName, $config);
  182. $info = $this->client->getLiveChannelInfo($this->bucketName, $channelName);
  183. $this->assertEquals('test live channel info', $info->getDescription());
  184. $this->assertEquals('enabled', $info->getStatus());
  185. $this->assertEquals('HLS', $info->getType());
  186. $this->assertEquals(10, $info->getFragDuration());
  187. $this->assertEquals(5, $info->getFragCount());
  188. $this->assertEquals('playlist.m3u8', $info->getPlayListName());
  189. $this->client->deleteBucketLiveChannel($this->bucketName, $channelName);
  190. $list = $this->client->listBucketLiveChannels($this->bucketName, array(
  191. 'prefix' => $channelName
  192. ));
  193. $this->assertEquals(0, count($list->getChannelList()));
  194. }
  195. public function testPutLiveChannelStatus()
  196. {
  197. $channelName = 'live-to-put-status';
  198. $config = new LiveChannelConfig(array(
  199. 'description' => 'test live channel info',
  200. 'type' => 'HLS',
  201. 'fragDuration' => 10,
  202. 'fragCount' => 5,
  203. 'playListName' => 'hello.m3u8'
  204. ));
  205. $this->client->putBucketLiveChannel($this->bucketName, $channelName, $config);
  206. $info = $this->client->getLiveChannelInfo($this->bucketName, $channelName);
  207. $this->assertEquals('test live channel info', $info->getDescription());
  208. $this->assertEquals('enabled', $info->getStatus());
  209. $this->assertEquals('HLS', $info->getType());
  210. $this->assertEquals(10, $info->getFragDuration());
  211. $this->assertEquals(5, $info->getFragCount());
  212. $this->assertEquals('playlist.m3u8', $info->getPlayListName());
  213. $status = $this->client->getLiveChannelStatus($this->bucketName, $channelName);
  214. $this->assertEquals('Idle', $status->getStatus());
  215. $resp = $this->client->putLiveChannelStatus($this->bucketName, $channelName, "disabled");
  216. $info = $this->client->getLiveChannelInfo($this->bucketName, $channelName);
  217. $this->assertEquals('test live channel info', $info->getDescription());
  218. $this->assertEquals('disabled', $info->getStatus());
  219. $this->assertEquals('HLS', $info->getType());
  220. $this->assertEquals(10, $info->getFragDuration());
  221. $this->assertEquals(5, $info->getFragCount());
  222. $this->assertEquals('playlist.m3u8', $info->getPlayListName());
  223. $status = $this->client->getLiveChannelStatus($this->bucketName, $channelName);
  224. //getLiveChannelInfo
  225. $this->assertEquals('Disabled', $status->getStatus());
  226. $this->client->deleteBucketLiveChannel($this->bucketName, $channelName);
  227. $list = $this->client->listBucketLiveChannels($this->bucketName, array(
  228. 'prefix' => $channelName
  229. ));
  230. $this->assertEquals(0, count($list->getChannelList()));
  231. }
  232. public function testLiveChannelHistory()
  233. {
  234. $channelName = 'live-test-history';
  235. $config = new LiveChannelConfig(array(
  236. 'description' => 'test live channel info',
  237. 'type' => 'HLS',
  238. 'fragDuration' => 10,
  239. 'fragCount' => 5,
  240. 'playListName' => 'hello.m3u8'
  241. ));
  242. $this->client->putBucketLiveChannel($this->bucketName, $channelName, $config);
  243. $history = $this->client->getLiveChannelHistory($this->bucketName, $channelName);
  244. $this->assertEquals(0, count($history->getLiveRecordList()));
  245. }
  246. }