LiveChannel.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //******************************* Simple Usage *******************************************************
  9. /**
  10. * Create a Live Channel
  11. * The live channel's name is test_rtmp_live.
  12. * The play url file is named as test.m3u8, which includes 3 ts files.
  13. * The time period of each file is 5 seconds.(It is recommneded value only for demo purpose, the actual period depends on the key frame.)
  14. *
  15. */
  16. $config = new LiveChannelConfig(array(
  17. 'description' => 'live channel test',
  18. 'type' => 'HLS',
  19. 'fragDuration' => 10,
  20. 'fragCount' => 5,
  21. 'playListName' => 'hello.m3u8'
  22. ));
  23. $info = $ossClient->putBucketLiveChannel($bucket, 'test_rtmp_live', $config);
  24. Common::println("bucket $bucket liveChannel created:\n" .
  25. "live channel name: ". $info->getName() . "\n" .
  26. "live channel description: ". $info->getDescription() . "\n" .
  27. "publishurls: ". $info->getPublishUrls()[0] . "\n" .
  28. "playurls: ". $info->getPlayUrls()[0] . "\n");
  29. /**
  30. * You can use listBucketLiveChannels to list and manage all existing live channels.
  31. * Prefix can be used to filter listed live channels by prefix.
  32. * Max_keys indicates the maximum numbers of live channels that can be listed in an iterator at one time. Its value is 1000 in maximum and 100 by default.
  33. */
  34. $list = $ossClient->listBucketLiveChannels($bucket);
  35. Common::println("bucket $bucket listLiveChannel:\n" .
  36. "list live channel prefix: ". $list->getPrefix() . "\n" .
  37. "list live channel marker: ". $list->getMarker() . "\n" .
  38. "list live channel maxkey: ". $list->getMaxKeys() . "\n" .
  39. "list live channel IsTruncated: ". $list->getIsTruncated() . "\n" .
  40. "list live channel getNextMarker: ". $list->getNextMarker() . "\n");
  41. foreach($list->getChannelList() as $list)
  42. {
  43. Common::println("bucket $bucket listLiveChannel:\n" .
  44. "list live channel IsTruncated: ". $list->getName() . "\n" .
  45. "list live channel Description: ". $list->getDescription() . "\n" .
  46. "list live channel Status: ". $list->getStatus() . "\n" .
  47. "list live channel getNextMarker: ". $list->getLastModified() . "\n");
  48. }
  49. /**
  50. * Obtain the play_url (url used for rtmp stream pushing.
  51. * If the the bucket is not globally readable and writable,
  52. * the url must be signed as shown in the following.) and pulish_url (the url included in the m3u8 file generated in stream pushing) used to push streams.
  53. */
  54. $play_url = $ossClient->signRtmpUrl($bucket, "test_rtmp_live", 3600, array('params' => array('playlistName' => 'playlist.m3u8')));
  55. Common::println("bucket $bucket rtmp url: \n" . $play_url);
  56. $play_url = $ossClient->signRtmpUrl($bucket, "test_rtmp_live", 3600);
  57. Common::println("bucket $bucket rtmp url: \n" . $play_url);
  58. /**
  59. * If you want to disable a created live channel (disable the pushing streaming or do not allow stream pushing to an IP address), call putLiveChannelStatus to change the channel status to "Disabled".
  60. * If you want to enable a disabled live channel, call PutLiveChannelStatus to chanage the channel status to "Enabled".
  61. */
  62. $resp = $ossClient->putLiveChannelStatus($bucket, "test_rtmp_live", "enabled");
  63. /**
  64. * You can callLiveChannelInfo to get the information about a live channel.
  65. */
  66. $info = $ossClient->getLiveChannelInfo($bucket, 'test_rtmp_live');
  67. Common::println("bucket $bucket LiveChannelInfo:\n" .
  68. "live channel info description: ". $info->getDescription() . "\n" .
  69. "live channel info status: ". $info->getStatus() . "\n" .
  70. "live channel info type: ". $info->getType() . "\n" .
  71. "live channel info fragDuration: ". $info->getFragDuration() . "\n" .
  72. "live channel info fragCount: ". $info->getFragCount() . "\n" .
  73. "live channel info playListName: ". $info->getPlayListName() . "\n");
  74. /**
  75. * Gets the historical pushing streaming records by calling getLiveChannelHistory. Now the max records to return is 10.
  76. */
  77. $history = $ossClient->getLiveChannelHistory($bucket, "test_rtmp_live");
  78. if (count($history->getLiveRecordList()) != 0)
  79. {
  80. foreach($history->getLiveRecordList() as $recordList)
  81. {
  82. Common::println("bucket $bucket liveChannelHistory:\n" .
  83. "live channel history startTime: ". $recordList->getStartTime() . "\n" .
  84. "live channel history endTime: ". $recordList->getEndTime() . "\n" .
  85. "live channel history remoteAddr: ". $recordList->getRemoteAddr() . "\n");
  86. }
  87. }
  88. /**
  89. * Get the live channel's status by calling getLiveChannelStatus.
  90. * If the live channel is receiving the pushing stream, all attributes in stat_result are valid.
  91. * If the live channel is idle or disabled, then the status is idle or Disabled and other attributes have no meaning.
  92. */
  93. $status = $ossClient->getLiveChannelStatus($bucket, "test_rtmp_live");
  94. Common::println("bucket $bucket listLiveChannel:\n" .
  95. "live channel status status: ". $status->getStatus() . "\n" .
  96. "live channel status ConnectedTime: ". $status->getConnectedTime() . "\n" .
  97. "live channel status VideoWidth: ". $status->getVideoWidth() . "\n" .
  98. "live channel status VideoHeight: ". $status->getVideoHeight() . "\n" .
  99. "live channel status VideoFrameRate: ". $status->getVideoFrameRate() . "\n" .
  100. "live channel status VideoBandwidth: ". $status->getVideoBandwidth() . "\n" .
  101. "live channel status VideoCodec: ". $status->getVideoCodec() . "\n" .
  102. "live channel status AudioBandwidth: ". $status->getAudioBandwidth() . "\n" .
  103. "live channel status AudioSampleRate: ". $status->getAudioSampleRate() . "\n" .
  104. "live channel status AdioCodec: ". $status->getAudioCodec() . "\n");
  105. /**
  106. * If you want to generate a play url from the ts files generated from pushing streaming, call postVodPlayList.
  107. * Specify the start time to 60 seconds before the current time and the end time to the current time, which means that a video of 60 seconds is generated.
  108. * The playlist file is specified to “vod_playlist.m3u8”, which means that a palylist file named vod_playlist.m3u8 is created after the interface is called.
  109. */
  110. $current_time = time();
  111. $ossClient->postVodPlaylist($bucket,
  112. "test_rtmp_live", "vod_playlist.m3u8",
  113. array('StartTime' => $current_time - 60,
  114. 'EndTime' => $current_time)
  115. );
  116. /**
  117. * Call delete_live_channel to delete a live channel if it will no longer be in used.
  118. */
  119. $ossClient->deleteBucketLiveChannel($bucket, "test_rtmp_live");