No Description

LiveChannel.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Model\LiveChannelConfig;
  5. $bucket = Common::getBucketName();
  6. $ossClient = Common::getOssClient();
  7. if (is_null($ossClient)) exit(1);
  8. //******************************* 简单使用 *******************************************************
  9. /**
  10. 创建一个直播频道
  11. 频道的名称是test_rtmp_live。直播生成的m3u8文件叫做test.m3u8,该索引文件包含3片ts文件,每片ts文件的时长为5秒(这只是一个建议值,具体的时长取决于关键帧)。
  12. */
  13. $config = new LiveChannelConfig(array(
  14. 'description' => 'live channel test',
  15. 'type' => 'HLS',
  16. 'fragDuration' => 10,
  17. 'fragCount' => 5,
  18. 'playListName' => 'hello.m3u8'
  19. ));
  20. $info = $ossClient->putBucketLiveChannel($bucket, 'test_rtmp_live', $config);
  21. Common::println("bucket $bucket liveChannel created:\n" .
  22. "live channel name: ". $info->getName() . "\n" .
  23. "live channel description: ". $info->getDescription() . "\n" .
  24. "publishurls: ". $info->getPublishUrls()[0] . "\n" .
  25. "playurls: ". $info->getPlayUrls()[0] . "\n");
  26. /**
  27. 对创建好的频道,可以使用listBucketLiveChannels来进行列举已达到管理的目的。
  28. prefix可以按照前缀过滤list出来的频道。
  29. max_keys表示迭代器内部一次list出来的频道的最大数量,这个值最大不能超过1000,不填写的话默认为100。
  30. */
  31. $list = $ossClient->listBucketLiveChannels($bucket);
  32. Common::println("bucket $bucket listLiveChannel:\n" .
  33. "list live channel prefix: ". $list->getPrefix() . "\n" .
  34. "list live channel marker: ". $list->getMarker() . "\n" .
  35. "list live channel maxkey: ". $list->getMaxKeys() . "\n" .
  36. "list live channel IsTruncated: ". $list->getIsTruncated() . "\n" .
  37. "list live channel getNextMarker: ". $list->getNextMarker() . "\n");
  38. foreach($list->getChannelList() as $list)
  39. {
  40. Common::println("bucket $bucket listLiveChannel:\n" .
  41. "list live channel IsTruncated: ". $list->getName() . "\n" .
  42. "list live channel Description: ". $list->getDescription() . "\n" .
  43. "list live channel Status: ". $list->getStatus() . "\n" .
  44. "list live channel getNextMarker: ". $list->getLastModified() . "\n");
  45. }
  46. /**
  47. 创建直播频道之后拿到推流用的play_url(rtmp推流的url,如果Bucket不是公共读写权限那么还需要带上签名,见下文示例)和推流用的publish_url(推流产生的m3u8文件的url)
  48. */
  49. $play_url = $ossClient->signRtmpUrl($bucket, "test_rtmp_live", 3600, array('params' => array('playlistName' => 'playlist.m3u8')));
  50. Common::println("bucket $bucket rtmp url: \n" . $play_url);
  51. $play_url = $ossClient->signRtmpUrl($bucket, "test_rtmp_live", 3600);
  52. Common::println("bucket $bucket rtmp url: \n" . $play_url);
  53. /**
  54. 创建好直播频道,如果想把这个频道禁用掉(断掉正在推的流或者不再允许向一个地址推流),应该使用putLiveChannelStatus接口,将频道的status改成“Disabled”,如果要将一个禁用状态的频道启用,那么也是调用这个接口,将status改成“Enabled”
  55. */
  56. $resp = $ossClient->putLiveChannelStatus($bucket, "test_rtmp_live", "enabled");
  57. /**
  58. 创建好直播频道之后调用getLiveChannelInfo可以得到频道相关的信息
  59. */
  60. $info = $ossClient->getLiveChannelInfo($bucket, 'test_rtmp_live');
  61. Common::println("bucket $bucket LiveChannelInfo:\n" .
  62. "live channel info description: ". $info->getDescription() . "\n" .
  63. "live channel info status: ". $info->getStatus() . "\n" .
  64. "live channel info type: ". $info->getType() . "\n" .
  65. "live channel info fragDuration: ". $info->getFragDuration() . "\n" .
  66. "live channel info fragCount: ". $info->getFragCount() . "\n" .
  67. "live channel info playListName: ". $info->getPlayListName() . "\n");
  68. /**
  69. 如果想查看一个频道历史推流记录,可以调用getLiveChannelHistory。目前最多可以看到10次推流的记录
  70. */
  71. $history = $ossClient->getLiveChannelHistory($bucket, "test_rtmp_live");
  72. if (count($history->getLiveRecordList()) != 0)
  73. {
  74. foreach($history->getLiveRecordList() as $recordList)
  75. {
  76. Common::println("bucket $bucket liveChannelHistory:\n" .
  77. "live channel history startTime: ". $recordList->getStartTime() . "\n" .
  78. "live channel history endTime: ". $recordList->getEndTime() . "\n" .
  79. "live channel history remoteAddr: ". $recordList->getRemoteAddr() . "\n");
  80. }
  81. }
  82. /**
  83. 对于正在推流的频道调用get_live_channel_stat可以获得流的状态信息。
  84. 如果频道正在推流,那么stat_result中的所有字段都有意义。
  85. 如果频道闲置或者处于“Disabled”状态,那么status为“Idle”或“Disabled”,其他字段无意义。
  86. */
  87. $status = $ossClient->getLiveChannelStatus($bucket, "test_rtmp_live");
  88. Common::println("bucket $bucket listLiveChannel:\n" .
  89. "live channel status status: ". $status->getStatus() . "\n" .
  90. "live channel status ConnectedTime: ". $status->getConnectedTime() . "\n" .
  91. "live channel status VideoWidth: ". $status->getVideoWidth() . "\n" .
  92. "live channel status VideoHeight: ". $status->getVideoHeight() . "\n" .
  93. "live channel status VideoFrameRate: ". $status->getVideoFrameRate() . "\n" .
  94. "live channel status VideoBandwidth: ". $status->getVideoBandwidth() . "\n" .
  95. "live channel status VideoCodec: ". $status->getVideoCodec() . "\n" .
  96. "live channel status AudioBandwidth: ". $status->getAudioBandwidth() . "\n" .
  97. "live channel status AudioSampleRate: ". $status->getAudioSampleRate() . "\n" .
  98. "live channel status AdioCodec: ". $status->getAudioCodec() . "\n");
  99. /**
  100. * 如果希望利用直播推流产生的ts文件生成一个点播列表,可以使用postVodPlaylist方法。
  101. * 指定起始时间为当前时间减去60秒,结束时间为当前时间,这意味着将生成一个长度为60秒的点播视频。
  102. * 播放列表指定为“vod_playlist.m3u8”,也就是说这个接口调用成功之后会在OSS上生成一个名叫“vod_playlist.m3u8”的播放列表文件。
  103. */
  104. $current_time = time();
  105. $ossClient->postVodPlaylist($bucket,
  106. "test_rtmp_live", "vod_playlist.m3u8",
  107. array('StartTime' => $current_time - 60,
  108. 'EndTime' => $current_time)
  109. );
  110. /**
  111. * 如果一个直播频道已经不打算再使用了,那么可以调用delete_live_channel来删除频道。
  112. */
  113. $ossClient->deleteBucketLiveChannel($bucket, "test_rtmp_live");