BucketLiveChannelTest.php 12 KB

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