From 8ff611eb4c817925b4f6918675e943ea730d66a5 Mon Sep 17 00:00:00 2001 From: "qiankun.yqk" Date: Mon, 24 Oct 2016 09:41:34 +0800 Subject: [PATCH] add live channel interface --- config4Test.cfg | 6 + samples/.LiveChannel.php.swp | Bin 0 -> 12288 bytes samples/Config.php | 8 +- samples/LiveChannel.php | 89 +++++++++ samples/Object.php | 1 + samples/test4Url.php | 48 +++++ src/OSS/Model/GetLiveChannelHistory.php | 35 ++++ src/OSS/Model/GetLiveChannelInfo.php | 69 +++++++ src/OSS/Model/GetLiveChannelStatus.php | 107 +++++++++++ src/OSS/Model/LiveChannelHistory.php | 59 ++++++ src/OSS/Model/LiveChannelListInfo.php | 2 +- src/OSS/OssClient.php | 166 +++++++++++++++- .../Result/GetLiveChannelHistoryResult.php | 19 ++ src/OSS/Result/GetLiveChannelInfoResult.php | 19 ++ src/OSS/Result/GetLiveChannelStatusResult.php | 19 ++ tests/OSS/Tests/BucketLiveChannelTest.php | 180 +++++++++++++++++- tests/OSS/Tests/LiveChannelXmlTest.php | 112 +++++++++-- 17 files changed, 908 insertions(+), 31 deletions(-) create mode 100755 config4Test.cfg create mode 100644 samples/.LiveChannel.php.swp create mode 100644 samples/LiveChannel.php create mode 100644 samples/test4Url.php create mode 100644 src/OSS/Model/GetLiveChannelHistory.php create mode 100644 src/OSS/Model/GetLiveChannelInfo.php create mode 100644 src/OSS/Model/GetLiveChannelStatus.php create mode 100644 src/OSS/Model/LiveChannelHistory.php create mode 100644 src/OSS/Result/GetLiveChannelHistoryResult.php create mode 100644 src/OSS/Result/GetLiveChannelInfoResult.php create mode 100644 src/OSS/Result/GetLiveChannelStatusResult.php diff --git a/config4Test.cfg b/config4Test.cfg new file mode 100755 index 00000000..ff6084ce --- /dev/null +++ b/config4Test.cfg @@ -0,0 +1,6 @@ +#!/bin/bash +export OSS_ACCESS_KEY_ID=mFuuivpsrtPlLPcT +export OSS_ACCESS_KEY_SECRET=AiiUWXVcAf2I92vkhZg31K9Cx2TTeH +export OSS_ENDPOINT=oss-cn-shenzhen.aliyuncs.com +export OSS_BUCKET=php-sdk-test-bucket-6 +echo "done..." diff --git a/samples/.LiveChannel.php.swp b/samples/.LiveChannel.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..d5bcbb7d18af14ae217883cb7eba31ac01d1eb69 GIT binary patch literal 12288 zcmeI2O=uHA6vwB62ennvi-LzS26ih=HmOLpu@y{(23jk|>ZK4Tn@O{DGrQdn+f-0N z4_?Gey?ar_gC7?~K@@L#)uTtHM?DBaPrlh~zSBm$Ro=j#P3FCsdG9xG7D6VUxRjZu zBk=@*wwDm~-tWu(+aGLuw2cr}C>uid_h^ngl20ZQiIF(>hTTQBk^A*vLT$-%7F?U@ zT(ev^C%*+BbgRHMW*QY=#J~`+&)2SEowFLo319P|uy2UG%`1RVmc4-oPd^b+(0BtT`*r+z{{ zf>uF4cMr)j%c%l^G(4N z(xO|w@YKePE#^VFxpJ|#%8*?hVCuCYdH!o=25fbZStF-<*93QU$FM!a5yJ zHcpiiH%&`v^(s1SJ}t=yF2{#DtrNb(WTt^bGSTMYRqSx~$*HW;W-K%~RU?pPMD^-z zZJKO%7xhT1CmQTwp1?-%CE8T{=^%fVIS#9+@Yd9{qnkIn_VniWyZ67neYdvym~QU7 zp_LG$NL9nl3EzZMRDVUmCi7*(Qjb4blR(pUpRhN-?N%;)e$-*9-&5;~{3+(hO=E(f)#Xw@L) O?Rh0dPMwyw=>Gt-?f`ZG literal 0 HcmV?d00001 diff --git a/samples/Config.php b/samples/Config.php index 35c0dc7c..072140ce 100644 --- a/samples/Config.php +++ b/samples/Config.php @@ -8,8 +8,8 @@ */ final class Config { - const OSS_ACCESS_ID = ''; - const OSS_ACCESS_KEY = ''; - const OSS_ENDPOINT = ''; - const OSS_TEST_BUCKET = ''; + const OSS_ACCESS_ID = 'mFuuivpsrtPlLPcT'; + const OSS_ACCESS_KEY = 'AiiUWXVcAf2I92vkhZg31K9Cx2TTeH'; + const OSS_ENDPOINT = 'oss-cn-shenzhen.aliyuncs.com'; + const OSS_TEST_BUCKET = 'yueqiankun-rtmp'; } diff --git a/samples/LiveChannel.php b/samples/LiveChannel.php new file mode 100644 index 00000000..41d26107 --- /dev/null +++ b/samples/LiveChannel.php @@ -0,0 +1,89 @@ + 'live-1', + 'description' => 'live channel 1', + 'type' => 'HLS', + 'fragDuration' => 10, + 'fragCount' => 5, + 'playListName' => 'hello' + )); +$info = $ossClient->putBucketLiveChannel($bucket, $config); +Common::println("bucket $bucket liveChannel created:\n" . +"live channel name: ". $info->getName() . "\n" . +"live channel description: ". $info->getDescription() . "\n" . +"publishurls: ". $info->getPublishUrls()[0] . "\n" . +"playurls: ". $info->getPlayUrls()[0] . "\n"); + +//listLiveChannel +$list = $ossClient->listBucketLiveChannels($bucket); +Common::println("bucket $bucket listLiveChannel:\n" . +"list live channel prefix: ". $list->getPrefix() . "\n" . +"list live channel marker: ". $list->getMarker() . "\n" . +"list live channel maxkey: ". $list->getMaxKeys() . "\n" . +"list live channel IsTruncated: ". $list->getIsTruncated() . "\n" . +"list live channel getNextMarker: ". $list->getNextMarker() . "\n" . +"list live channel list: ". $list->getChannelList()[0]->getName() . "\n"); + +//getLiveChannelUrl +$url = $ossClient->getLiveChannelUrl($bucket, "live-1"); +Common::println("bucket $bucket rtmp url: \n" . $url); + +//putLiveChannelStatus +$resp = $ossClient->putLiveChannelStatus($bucket, "live-1", "enabled"); + +//getLiveChannelInfo +$info = $ossClient->getLiveChannelInfo($bucket, 'live-1'); +Common::println("bucket $bucket listLiveChannel:\n" . +"live channel info description: ". $info->getDescription() . "\n" . +"live channel info status: ". $info->getStatus() . "\n" . +"live channel info type: ". $info->getType() . "\n" . +"live channel info fragDuration: ". $info->getFragDuration() . "\n" . +"live channel info fragCount: ". $info->getFragCount() . "\n" . +"live channel info playListName: ". $info->getPlayListName() . "\n"); + +//getLiveChannelHistory +$history = $ossClient->getLiveChannelHistory($bucket, "live-1"); +if (count($history->getLiveRecordList()) != 0) +{ + Common::println("bucket $bucket liveChannelHistory:\n" . + "live channel history startTime: ". $history->getLiveRecordList()[0]->getStartTime() . "\n" . + "live channel history endTime: ". $history->getLiveRecordList()[0]->getEndTime() . "\n" . + "live channel history remoteAddr: ". $history->getLiveRecordList()[0]->getRemoteAddr() . "\n"); +} + +//getLiveChannelStatus +$status = $ossClient->getLiveChannelStatus($bucket, "live-1"); +Common::println("bucket $bucket listLiveChannel:\n" . +"live channel status status: ". $status->getStatus() . "\n" . +"live channel status ConnectedTime: ". $status->getConnectedTime() . "\n" . +"live channel status VideoWidth: ". $status->getVideoWidth() . "\n" . +"live channel status VideoHeight: ". $status->getVideoHeight() . "\n" . +"live channel status VideoFrameRate: ". $status->getVideoFrameRate() . "\n" . +"live channel status VideoBandwidth: ". $status->getVideoBandwidth() . "\n" . +"live channel status VideoCodec: ". $status->getVideoCodec() . "\n" . +"live channel status AudioBandwidth: ". $status->getAudioBandwidth() . "\n" . +"live channel status AudioSampleRate: ". $status->getAudioSampleRate() . "\n" . +"live channel status AdioCodec: ". $status->getAudioCodec() . "\n"); + +//postVodPlaylist +$info = $ossClient->postVodPlaylist($bucket, + "live-1", "playback.m3u8", + array('StartTime' => '1476844172', + 'EndTime' => '1476864172') +); + +//deleteLiveChannel +$info = $ossClient->deleteBucketLiveChannel($bucket, "live-1"); diff --git a/samples/Object.php b/samples/Object.php index d0fef472..8edcd4ab 100644 --- a/samples/Object.php +++ b/samples/Object.php @@ -214,6 +214,7 @@ function listAllObjects($ossClient, $bucket) printf($e->getMessage() . "\n"); return; } + print("\n***********\n"); // 得到nextMarker,从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表 $nextMarker = $listObjectInfo->getNextMarker(); $listObject = $listObjectInfo->getObjectList(); diff --git a/samples/test4Url.php b/samples/test4Url.php new file mode 100644 index 00000000..8754f4b1 --- /dev/null +++ b/samples/test4Url.php @@ -0,0 +1,48 @@ + 'live-1', + 'description' => 'live channel 1', + 'type' => 'HLS', + 'fragDuration' => 10, + 'fragCount' => 5, + 'playListName' => 'hello' + )); +$info = $ossClient->putBucketLiveChannel($bucket, $config); +Common::println("bucket $bucket liveChannel created:\n" . +"live channel name: ". $info->getName() . "\n" . +"live channel description: ". $info->getDescription() . "\n" . +"publishurls: ". $info->getPublishUrls()[0] . "\n" . +"playurls: ". $info->getPlayUrls()[0] . "\n"); + +//listLiveChannel +$list = $ossClient->listBucketLiveChannels($bucket); +Common::println("bucket $bucket listLiveChannel:\n" . +"list live channel prefix: ". $list->getPrefix() . "\n" . +"list live channel marker: ". $list->getMarker() . "\n" . +"list live channel maxkey: ". $list->getMaxKeys() . "\n" . +"list live channel IsTruncated: ". $list->getIsTruncated() . "\n" . +"list live channel getNextMarker: ". $list->getNextMarker() . "\n" . +"list live channel count: ". count($list->getChannelList()) . "\n" . +"list live channel list: ". $list->getChannelList()[0]->getName() . "\n"); + +//getLiveChannelUrl +$url = $ossClient->getLiveChannelUrl($bucket, "live-1"); +Common::println("bucket $bucket rtmp url: \n" . $url); + +system(" sudo ffmpeg \-re \-i ./allstar.flv \-c copy \-f flv \"$url\" "); + +//deleteLiveChannel +$info = $ossClient->deleteBucketLiveChannel($bucket, "live-1"); diff --git a/src/OSS/Model/GetLiveChannelHistory.php b/src/OSS/Model/GetLiveChannelHistory.php new file mode 100644 index 00000000..7b7abaaf --- /dev/null +++ b/src/OSS/Model/GetLiveChannelHistory.php @@ -0,0 +1,35 @@ +liveRecordList; + } + + public function parseFromXml($strXml) + { + $xml = simplexml_load_string($strXml); + + if (isset($xml->LiveRecord)) { + foreach ($xml->LiveRecord as $record) { + $liveRecord = new LiveChannelHistory(); + $liveRecord->parseFromXmlNode($record); + $this->liveRecordList[] = $liveRecord; + } + } + } + + public function serializeToXml() + { + throw new OssException("Not implemented."); + } +} diff --git a/src/OSS/Model/GetLiveChannelInfo.php b/src/OSS/Model/GetLiveChannelInfo.php new file mode 100644 index 00000000..2cb2f38b --- /dev/null +++ b/src/OSS/Model/GetLiveChannelInfo.php @@ -0,0 +1,69 @@ +description; + } + + public function getStatus() + { + return $this->status; + } + + public function getType() + { + return $this->type; + } + + public function getFragDuration() + { + return $this->fragDuration; + } + + public function getFragCount() + { + return $this->fragCount; + } + + public function getPlayListName() + { + return $this->playlistName; + } + + public function parseFromXml($strXml) + { + $xml = simplexml_load_string($strXml); + + $this->description = strval($xml->Description); + $this->status = strval($xml->Status); + + if (isset($xml->Target)) { + foreach ($xml->Target as $target) { + $this->type = strval($target->Type); + $this->fragDuration = strval($target->FragDuration); + $this->fragCount = strval($target->FragCount); + $this->playlistName = strval($target->PlaylistName); + } + } + } + + public function serializeToXml() + { + throw new OssException("Not implemented."); + } +} diff --git a/src/OSS/Model/GetLiveChannelStatus.php b/src/OSS/Model/GetLiveChannelStatus.php new file mode 100644 index 00000000..a0b1f94b --- /dev/null +++ b/src/OSS/Model/GetLiveChannelStatus.php @@ -0,0 +1,107 @@ +status; + } + + public function getConnectedTime() + { + return $this->connectedTime; + } + + public function getRemoteAddr() + { + return $this->remoteAddr; + } + + public function getVideoWidth() + { + return $this->videoWidth; + } + public function getVideoHeight() + { + return $this->videoHeight; + } + public function getVideoFrameRate() + { + return $this->videoFrameRate; + } + public function getVideoBandwidth() + { + return $this->videoBandwidth; + } + public function getVideoCodec() + { + return $this->videoCodec; + } + + public function getAudioBandwidth() + { + return $this->audioBandwidth; + } + public function getAudioSampleRate() + { + return $this->audioSampleRate; + } + public function getAudioCodec() + { + return $this->audioCodec; + } + + + public function parseFromXml($strXml) + { + $xml = simplexml_load_string($strXml); + $this->status = strval($xml->Status); + $this->connectedTime = strval($xml->ConnectedTime); + $this->remoteAddr = strval($xml->RemoteAddr); + + if (isset($xml->Video)) { + foreach ($xml->Video as $video) { + $this->videoWidth = intval($video->Width); + $this->videoHeight = intval($video->Height); + $this->videoFrameRate = intval($video->FrameRate); + $this->videoBandwidth = intval($video->Bandwidth); + $this->videoCodec = strval($video->Codec); + } + } + + if (isset($xml->Video)) { + foreach ($xml->Audio as $audio) { + $this->audioBandwidth = intval($audio->Bandwidth); + $this->audioSampleRate = intval($audio->SampleRate); + $this->audioCodec = strval($audio->Codec); + } + } + + } + + public function serializeToXml() + { + throw new OssException("Not implemented."); + } +} diff --git a/src/OSS/Model/LiveChannelHistory.php b/src/OSS/Model/LiveChannelHistory.php new file mode 100644 index 00000000..ed166bd8 --- /dev/null +++ b/src/OSS/Model/LiveChannelHistory.php @@ -0,0 +1,59 @@ +startTime; + } + + public function getEndTime() + { + return $this->endTime; + } + + public function getRemoteAddr() + { + return $this->remoteAddr; + } + + public function parseFromXmlNode($xml) + { + if (isset($xml->StartTime)) { + $this->startTime = strval($xml->StartTime); + } + + if (isset($xml->EndTime)) { + $this->endTime = strval($xml->EndTime); + } + + if (isset($xml->RemoteAddr)) { + $this->remoteAddr = strval($xml->RemoteAddr); + } + } + + public function parseFromXml($strXml) + { + $xml = simplexml_load_string($strXml); + $this->parseFromXmlNode($xml); + } + + public function serializeToXml() + { + throw new OssException("Not implemented."); + } +} diff --git a/src/OSS/Model/LiveChannelListInfo.php b/src/OSS/Model/LiveChannelListInfo.php index 6be16e4e..7a6a7f73 100644 --- a/src/OSS/Model/LiveChannelListInfo.php +++ b/src/OSS/Model/LiveChannelListInfo.php @@ -105,4 +105,4 @@ public function serializeToXml() { throw new OssException("Not implemented."); } -} \ No newline at end of file +} diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index cf45592b..71e5cbe0 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -30,6 +30,9 @@ use OSS\Result\PutSetDeleteResult; use OSS\Result\ExistResult; use OSS\Result\PutLiveChannelResult; +use OSS\Result\GetLiveChannelHistoryResult; +use OSS\Result\GetLiveChannelInfoResult; +use OSS\Result\GetLiveChannelStatusResult; use OSS\Result\ListLiveChannelResult; use OSS\Model\ObjectListInfo; use OSS\Result\UploadPartResult; @@ -546,6 +549,98 @@ public function putBucketLiveChannel($bucket, $channelConfig, $options = NULL) return $info; } + /** + * 设置LiveChannel的status + * + * @param string $bucket bucket名称 + * @param string $channelId 操作的channelName + * @param string $channelStatus 为 enabled或disabled + * @param array $options + * @throws OssException + * @return null + */ + public function putLiveChannelStatus($bucket, $channelId, $channelStatus, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_PUT; + $options[self::OSS_OBJECT] = $channelId; + $options[self::OSS_SUB_RESOURCE] = 'live'; + $options[self::OSS_LIVE_CHANNEL_STATUS] = $channelStatus; + + $response = $this->auth($options); + $result = new PutSetDeleteResult($response); + return $result->getData(); + } + + /** + * 获取LiveChannel信息 + * + * @param string $bucket bucket名称 + * @param string $channelId 指定的LiveChannel + * @param array $options + * @throws OssException + * @return GetLiveChannelInfo + */ + public function getLiveChannelInfo($bucket, $channelId, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_GET; + $options[self::OSS_OBJECT] = $channelId; + $options[self::OSS_SUB_RESOURCE] = 'live'; + + $response = $this->auth($options); + $result = new GetLiveChannelInfoResult($response); + return $result->getData(); + } + + /** + * 获取LiveChannel状态信息 + * + * @param string $bucket bucket名称 + * @param string $channelId 指定的LiveChannel + * @param array $options + * @throws OssException + * @return GetLiveChannelStatus + */ + public function getLiveChannelStatus($bucket, $channelId, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_GET; + $options[self::OSS_OBJECT] = $channelId; + $options[self::OSS_SUB_RESOURCE] = 'live'; + $options[self::OSS_CNAME_COMP] = 'stat'; + + $response = $this->auth($options); + $result = new GetLiveChannelStatusResult($response); + return $result->getData(); + } + + /** + * 获取LiveChannel历史信息 + * + * @param string $bucket bucket名称 + * @param string $channelId 指定的LiveChannel + * @param array $options + * @throws OssException + * @return GetLiveChannelHistory + */ + public function getLiveChannelHistory($bucket, $channelId, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_GET; + $options[self::OSS_OBJECT] = $channelId; + $options[self::OSS_SUB_RESOURCE] = 'live'; + $options[self::OSS_CNAME_COMP] = 'history'; + + $response = $this->auth($options); + $result = new GetLiveChannelHistoryResult($response); + return $result->getData(); + } + /** * 获取指定Bucket的直播流列表 * @@ -574,6 +669,31 @@ public function listBucketLiveChannels($bucket, $options = NULL) return $list; } + /** + * 为指定LiveChannel生成播放列表 + * + * @param string $bucket bucket名称 + * @param string $channelId 指定的LiveChannel + * @param string $playlistName 指定生成的点播播放列表的名称,必须以“.m3u8”结尾 + * @param array $options + * @throws OssException + * @return GetLiveChannelStatus + */ + public function postVodPlaylist($bucket, $channelId, $playlistName, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_POST; + $options[self::OSS_OBJECT] = $channelId . '/' . $playlistName; + $options[self::OSS_SUB_RESOURCE] = 'vod'; + $options[self::OSS_LIVE_CHANNEL_END_TIME] = $options['EndTime']; + $options[self::OSS_LIVE_CHANNEL_START_TIME] = $options['StartTime']; + + $response = $this->auth($options); + $result = new PutSetDeleteResult($response); + return $result->getData(); + } + /** * 删除指定Bucket的直播流 * @@ -623,8 +743,11 @@ public function getLiveChannelUrl($bucket, $channelId, $options = NULL) $resource = '/' . $bucket . '/' . $channelId; $string_to_sign = $expires . '\n' . $cano_params . $resource; + printf("\n********\n"); + print($string_to_sign); + printf("\n********\n"); $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true)); - $query_items[] = 'AccessKeyId=' . rawurlencode($this->accessKeyId); + $query_items[] = 'OSSAccessKeyId=' . rawurlencode($this->accessKeyId); $query_items[] = 'Expires=' . rawurlencode($expires); $query_items[] = 'Signature=' . rawurlencode($signature); @@ -1736,7 +1859,11 @@ private function auth($options) // 生成 signable_resource $signable_resource = $this->generateSignableResource($options); $string_to_sign .= rawurldecode($signable_resource) . urldecode($signable_query_string); - $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true)); + + //对?后面的要签名的string字母序排序 + $string_to_sign_ordered = $this->stringToSignSorted($string_to_sign); + + $signature = base64_encode(hash_hmac('sha1', $string_to_sign_ordered, $this->accessKeySecret, true)); $request->add_header('Authorization', 'OSS ' . $this->accessKeyId . ':' . $signature); if (isset($options[self::OSS_PREAUTH]) && (integer)$options[self::OSS_PREAUTH] > 0) { @@ -1775,7 +1902,7 @@ private function auth($options) $data = $this->auth($options); } } - + $this->redirects = 0; return $data; } @@ -1964,7 +2091,10 @@ private function generateSignableQueryStringParam($options) 'response-expires', 'response-content-disposition', self::OSS_UPLOAD_ID, - self::OSS_CNAME_COMP + self::OSS_CNAME_COMP, + self::OSS_LIVE_CHANNEL_STATUS, + self::OSS_LIVE_CHANNEL_START_TIME, + self::OSS_LIVE_CHANNEL_END_TIME ); foreach ($signableList as $item) { @@ -2025,6 +2155,27 @@ private function generateQueryString($options) return OssUtil::toQueryString($queryStringParams); } + private function stringToSignSorted($string_to_sign) + { + $queryStringSorted = ''; + $explodeResult = explode('?', $string_to_sign); + $index = count($explodeResult); + if ($index === 1) + return $string_to_sign; + + $queryStringParams = explode('&', $explodeResult[$index - 1]); + sort($queryStringParams); + + foreach($queryStringParams as $params) + { + $queryStringSorted .= $params . '&'; + } + + $queryStringSorted = substr($queryStringSorted, 0, -1); + + return $explodeResult[0] . '?' . $queryStringSorted; + } + /** * 初始化headers * @@ -2121,7 +2272,7 @@ public static function checkEnv() } /** - * 设置http库的请求超时时间,单位秒 + //* 设置http库的请求超时时间,单位秒 * * @param int $timeout */ @@ -2155,6 +2306,9 @@ public function setConnectTimeout($connectTimeout) const OSS_UPLOAD_ID = 'uploadId'; const OSS_PART_NUM = 'partNumber'; const OSS_CNAME_COMP = 'comp'; + const OSS_LIVE_CHANNEL_STATUS = 'status'; + const OSS_LIVE_CHANNEL_START_TIME = 'startTime'; + const OSS_LIVE_CHANNEL_END_TIME = 'endTime'; const OSS_MAX_KEYS_VALUE = 100; const OSS_MAX_OBJECT_GROUP_VALUE = OssUtil::OSS_MAX_OBJECT_GROUP_VALUE; const OSS_MAX_PART_SIZE = OssUtil::OSS_MAX_PART_SIZE; @@ -2260,4 +2414,4 @@ public function setConnectTimeout($connectTimeout) private $enableStsInUrl = false; private $timeout = 0; private $connectTimeout = 0; -} \ No newline at end of file +} diff --git a/src/OSS/Result/GetLiveChannelHistoryResult.php b/src/OSS/Result/GetLiveChannelHistoryResult.php new file mode 100644 index 00000000..202a6681 --- /dev/null +++ b/src/OSS/Result/GetLiveChannelHistoryResult.php @@ -0,0 +1,19 @@ +rawResponse->body; + $channelList = new GetLiveChannelHistory(); + $channelList->parseFromXml($content); + return $channelList; + } +} diff --git a/src/OSS/Result/GetLiveChannelInfoResult.php b/src/OSS/Result/GetLiveChannelInfoResult.php new file mode 100644 index 00000000..d5a9005e --- /dev/null +++ b/src/OSS/Result/GetLiveChannelInfoResult.php @@ -0,0 +1,19 @@ +rawResponse->body; + $channelList = new GetLiveChannelInfo(); + $channelList->parseFromXml($content); + return $channelList; + } +} diff --git a/src/OSS/Result/GetLiveChannelStatusResult.php b/src/OSS/Result/GetLiveChannelStatusResult.php new file mode 100644 index 00000000..6b8a60f5 --- /dev/null +++ b/src/OSS/Result/GetLiveChannelStatusResult.php @@ -0,0 +1,19 @@ +rawResponse->body; + $channelList = new GetLiveChannelStatus(); + $channelList->parseFromXml($content); + return $channelList; + } +} diff --git a/tests/OSS/Tests/BucketLiveChannelTest.php b/tests/OSS/Tests/BucketLiveChannelTest.php index 7a8c506b..ad7add9f 100644 --- a/tests/OSS/Tests/BucketLiveChannelTest.php +++ b/tests/OSS/Tests/BucketLiveChannelTest.php @@ -18,12 +18,49 @@ class BucketLiveChannelTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->client = Common::getOssClient(); - $this->bucketName = 'php-sdk-test-bucket-' . strval(rand(0, 10)); + $this->bucketName = 'php-sdk-test-bucket-name-' . strval(rand(0, 10)); $this->client->createBucket($this->bucketName); - } + $this->client->putBucketAcl($this->bucketName, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE); + } public function tearDown() { + ////to delete created bucket + //1. delele live channel + $list = $this->client->listBucketLiveChannels($this->bucketName); + if (count($list->getChannelList()) != 0) + { + foreach($list->getChannelList() as $list) + { + $this->client->deleteBucketLiveChannel($this->bucketName, $list->getName()); + } + } + //2. delete exsited object + $prefix = 'live-test/'; + $delimiter = '/'; + $nextMarker = ''; + $maxkeys = 1000; + $options = array( + 'delimiter' => $delimiter, + 'prefix' => $prefix, + 'max-keys' => $maxkeys, + 'marker' => $nextMarker, + ); + + try { + $listObjectInfo = $this->client->listObjects($this->bucketName, $options); + } catch (OssException $e) { + printf($e->getMessage() . "\n"); + return; + } + + $objectList = $listObjectInfo->getObjectList(); // 文件列表 + if (!empty($objectList)) + { + foreach($objectList as $objectInfo) + $this->client->deleteObject($this->bucketName, $objectInfo->getKey()); + } + //3. delete the bucket $this->client->deleteBucket($this->bucketName); } @@ -38,6 +75,7 @@ public function testPutLiveChannel() 'playListName' => 'hello' )); $info = $this->client->putBucketLiveChannel($this->bucketName, $config); + $this->client->deleteBucketLiveChannel($this->bucketName, 'live-1'); $this->assertEquals('live-1', $info->getName()); $this->assertEquals('live channel 1', $info->getDescription()); @@ -47,7 +85,7 @@ public function testPutLiveChannel() public function testListLiveChannels() { - $config = new LiveChannelConfig(array( + $config = new LiveChannelConfig(array( 'name' => 'live-1', 'description' => 'live channel 1', 'type' => 'HLS', @@ -98,9 +136,15 @@ public function testListLiveChannels() $this->assertEquals('live channel 2', $chan2->getDescription()); $this->assertEquals(1, count($chan2->getPublishUrls())); $this->assertEquals(1, count($chan2->getPlayUrls())); - } - /* + $this->client->deleteBucketLiveChannel($this->bucketName, 'live-1'); + $this->client->deleteBucketLiveChannel($this->bucketName, 'live-2'); + $list = $this->client->listBucketLiveChannels($this->bucketName, array( + 'prefix' => 'live-' + )); + $this->assertEquals(0, count($list->getChannelList())); + } + public function testDeleteLiveChannel() { $channelId = 'live-to-delete'; @@ -114,14 +158,13 @@ public function testDeleteLiveChannel() )); $this->client->putBucketLiveChannel($this->bucketName, $config); - $this->client->deleteBucketLiveChannel($channelId); - $list = $this->listLiveChannels($this->bucketName, array( + $this->client->deleteBucketLiveChannel($this->bucketName, $channelId); + $list = $this->client->listBucketLiveChannels($this->bucketName, array( 'prefix' => $channelId )); $this->assertEquals(0, count($list->getChannelList())); } - */ public function testGetLiveChannelUrl() { @@ -140,10 +183,129 @@ public function testGetLiveChannelUrl() $this->assertEquals('rtmp', $ret['scheme']); parse_str($ret['query'], $query); - $this->assertTrue(isset($query['AccessKeyId'])); + $this->assertTrue(isset($query['OSSAccessKeyId'])); $this->assertTrue(isset($query['Signature'])); $this->assertTrue(intval($query['Expires']) - ($now + 900) < 3); $this->assertEquals('hello', $query['a']); $this->assertEquals('world', $query['b']); } + +/**** + public function testLiveChannelStatus() + { + $channelId = 'live-1'; + $config = new LiveChannelConfig(array( + 'name' => $channelId, + 'description' => 'live channel to delete', + 'type' => 'HLS', + 'fragDuration' => 10, + 'fragCount' => 5, + 'playListName' => 'hello' + )); + $this->client->putBucketLiveChannel($this->bucketName, $config); + + $status = $this->client->getLiveChannelStatus($this->bucketName, $channelId); + $this->assertEquals('', $status->getStatus()); + $this->assertEquals('', $status->getConnectedTime()); + $this->assertEquals(672, $status->getVideoWidth()); + $this->assertEquals(378, $status->getVideoHeight()); + $this->assertEquals(29, $status->getVideoFrameRate()); + $this->assertEquals(72513, $status->getVideoBandwidth()); + $this->assertEquals('H264', $status->getVideoCodec()); + $this->assertEquals(6519, $status->getAudioBandwidth()); + $this->assertEquals(22050, $status->getAudioSampleRate()); + $this->assertEquals('AAC', $status->getAudioCodec()); + + $this->client->deleteBucketLiveChannel($this->bucketName, $channelId); + $list = $this->client->listBucketLiveChannels($this->bucketName, array( + 'prefix' => $channelId + )); + + $this->assertEquals(0, count($list->getChannelList())); + } +****/ + public function testLiveChannelInfo() + { + $channelId = 'live-to-put-status'; + $config = new LiveChannelConfig(array( + 'name' => $channelId, + 'description' => 'test live channel info', + 'type' => 'HLS', + 'fragDuration' => 10, + 'fragCount' => 5, + 'playListName' => 'hello' + )); + $this->client->putBucketLiveChannel($this->bucketName, $config); + + //getLiveChannelInfo + $info = $this->client->getLiveChannelInfo($this->bucketName, $channelId); + $this->assertEquals('test live channel info', $info->getDescription()); + $this->assertEquals('enabled', $info->getStatus()); + $this->assertEquals('HLS', $info->getType()); + $this->assertEquals(10, $info->getFragDuration()); + $this->assertEquals(5, $info->getFragCount()); + $this->assertEquals('playlist.m3u8', $info->getPlayListName()); + + $this->client->deleteBucketLiveChannel($this->bucketName, $channelId); + $list = $this->client->listBucketLiveChannels($this->bucketName, array( + 'prefix' => $channelId + )); + $this->assertEquals(0, count($list->getChannelList())); + } + + public function testLiveChannelHistory() + { + $channelId = 'live-test'; + $config = new LiveChannelConfig(array( + 'name' => $channelId, + 'description' => 'test live channel info', + 'type' => 'HLS', + 'fragDuration' => 10, + 'fragCount' => 5, + 'playListName' => 'hello' + )); + $this->client->putBucketLiveChannel($this->bucketName, $config); + + + system(" sudo ffmpeg \-re \-i ./allstar.flv \-c copy \-f flv \"rtmp://$this->bucketName.oss-cn-shenzhen.aliyuncs.com/live/live-test?playlistName=test.m3u8\" "); + sleep(2); + system(" sudo ffmpeg \-re \-i ./allstar.flv \-c copy \-f flv \"rtmp://$this->bucketName.oss-cn-shenzhen.aliyuncs.com/live/live-test?playlistName=test.m3u8\" "); + + $history = $this->client->getLiveChannelHistory($this->bucketName, $channelId); + $this->assertEquals(2, count($history->getLiveRecordList())); + $this->assertNotEquals('', $history->getLiveRecordList()[0]->getStartTime()); + $this->assertNotEquals('', $history->getLiveRecordList()[0]->getEndTime()); + $this->assertNotEquals('', $history->getLiveRecordList()[0]->getRemoteAddr()); + $this->client->deleteBucketLiveChannel($this->bucketName, $channelId); + } + + public function testPostVodPlayList() + { + $channelId = 'live-test'; + $config = new LiveChannelConfig(array( + 'name' => $channelId, + 'description' => 'live channel to delete', + 'type' => 'HLS', + 'fragDuration' => 10, + 'fragCount' => 5, + 'playListName' => 'hello' + )); + $this->client->putBucketLiveChannel($this->bucketName, $config); + + system(" sudo ffmpeg \-re \-i ./allstar.flv \-c copy \-f flv \"rtmp://$this->bucketName.oss-cn-shenzhen.aliyuncs.com/live/live-test?playlistName=test.m3u8\" "); + sleep(1); + + $ts = time(); + $info = $this->client->postVodPlaylist($this->bucketName, $channelId, "playback.m3u8", + array('StartTime' => $ts - 86400, + 'EndTime' => $ts) + ); + + $this->client->deleteBucketLiveChannel($this->bucketName, $channelId); + $list = $this->client->listBucketLiveChannels($this->bucketName, array( + 'prefix' => $channelId + )); + + $this->assertEquals(0, count($list->getChannelList())); + } } diff --git a/tests/OSS/Tests/LiveChannelXmlTest.php b/tests/OSS/Tests/LiveChannelXmlTest.php index 3b3fc79a..f40c7602 100644 --- a/tests/OSS/Tests/LiveChannelXmlTest.php +++ b/tests/OSS/Tests/LiveChannelXmlTest.php @@ -7,6 +7,8 @@ use OSS\Model\LiveChannelInfo; use OSS\Model\LiveChannelListInfo; use OSS\Model\LiveChannelConfig; +use OSS\Model\GetLiveChannelStatus; +use OSS\Model\GetLiveChannelHistory; use OSS\Core\OssException; class LiveChannelXmlTest extends \PHPUnit_Framework_TestCase @@ -28,7 +30,7 @@ class LiveChannelXmlTest extends \PHPUnit_Framework_TestCase private $info = << - live-1 + live-1 xxx rtmp://bucket.oss-cn-hangzhou.aliyuncs.com/live/213443245345 @@ -50,7 +52,7 @@ class LiveChannelXmlTest extends \PHPUnit_Framework_TestCase false 121312132 - 12123214323431 + 12123214323431 xxx rtmp://bucket.oss-cn-hangzhou.aliyuncs.com/live/1 @@ -62,7 +64,7 @@ class LiveChannelXmlTest extends \PHPUnit_Framework_TestCase 2015-11-24T14:25:31.000Z - 432423432423 + 432423432423 yyy rtmp://bucket.oss-cn-hangzhou.aliyuncs.com/live/2 @@ -76,12 +78,99 @@ class LiveChannelXmlTest extends \PHPUnit_Framework_TestCase BBBB; + private $status = << + + Live + 2016-10-20T14:25:31.000Z + 10.1.2.4.:47745 + + + +BBBB; + + private $history = << + + + 2013-11-24T14:25:31.000Z + 2013-11-24T15:25:31.000Z + 10.101.194.148:56861 + + + 2014-11-24T14:25:31.000Z + 2014-11-24T15:25:31.000Z + 10.101.194.148:56862 + + + 2015-11-24T14:25:31.000Z + 2015-11-24T15:25:31.000Z + 10.101.194.148:56863 + + +BBBB; + + public function testStatus() + { + $stat = new GetLiveChannelStatus(); + $stat->parseFromXml($this->status); + + $this->assertEquals('Live', $stat->getStatus()); + $this->assertEquals('2016-10-20T14:25:31.000Z', $stat->getConnectedTime()); + $this->assertEquals('10.1.2.4.:47745', $stat->getRemoteAddr()); + + $this->assertEquals(1280, $stat->getVideoWidth()); + $this->assertEquals(536, $stat->getVideoHeight()); + $this->assertEquals(24, $stat->getVideoFrameRate()); + $this->assertEquals(72513, $stat->getVideoBandwidth()); + $this->assertEquals('H264', $stat->getVideoCodec()); + $this->assertEquals(6519, $stat->getAudioBandwidth()); + $this->assertEquals(44100, $stat->getAudioSampleRate()); + $this->assertEquals('AAC', $stat->getAudioCodec()); + + } + + public function testHistory() + { + $history = new GetLiveChannelHistory(); + $history->parseFromXml($this->history); + + $recordList = $history->getLiveRecordList(); + $this->assertEquals(3, count($recordList)); + + $list0 = $recordList[0]; + $this->assertEquals('2013-11-24T14:25:31.000Z', $list0->getStartTime()); + $this->assertEquals('2013-11-24T15:25:31.000Z', $list0->getEndTime()); + $this->assertEquals('10.101.194.148:56861', $list0->getRemoteAddr()); + + $list1 = $recordList[1]; + $this->assertEquals('2014-11-24T14:25:31.000Z', $list1->getStartTime()); + $this->assertEquals('2014-11-24T15:25:31.000Z', $list1->getEndTime()); + $this->assertEquals('10.101.194.148:56862', $list1->getRemoteAddr()); + + $list2 = $recordList[2]; + $this->assertEquals('2015-11-24T14:25:31.000Z', $list2->getStartTime()); + $this->assertEquals('2015-11-24T15:25:31.000Z', $list2->getEndTime()); + $this->assertEquals('10.101.194.148:56863', $list2->getRemoteAddr()); + + } + public function testConfig() { - $config = new LiveChannelConfig(array('id' => 'live-1')); + $config = new LiveChannelConfig(array('name' => 'live-1')); $config->parseFromXml($this->config); - $this->assertEquals('live-1', $config->getId()); + $this->assertEquals('live-1', $config->getName()); $this->assertEquals('xxx', $config->getDescription()); $this->assertEquals('enabled', $config->getStatus()); $this->assertEquals('hls', $config->getType()); @@ -90,9 +179,9 @@ public function testConfig() $this->assertEquals('hello', $config->getPlayListName()); $xml = $config->serializeToXml(); - $config2 = new LiveChannelConfig(array('id' => 'live-2')); + $config2 = new LiveChannelConfig(array('name' => 'live-2')); $config2->parseFromXml($xml); - $this->assertEquals('live-2', $config2->getId()); + $this->assertEquals('live-2', $config2->getName()); $this->assertEquals('xxx', $config2->getDescription()); $this->assertEquals('enabled', $config2->getStatus()); $this->assertEquals('hls', $config2->getType()); @@ -103,10 +192,10 @@ public function testConfig() public function testInfo() { - $info = new LiveChannelInfo(); + $info = new LiveChannelInfo(array('name' => 'live-1')); $info->parseFromXml($this->info); - $this->assertEquals('live-1', $info->getId()); + $this->assertEquals('live-1', $info->getName()); $this->assertEquals('xxx', $info->getDescription()); $this->assertEquals('enabled', $info->getStatus()); $this->assertEquals('2015-11-24T14:25:31.000Z', $info->getLastModified()); @@ -134,7 +223,7 @@ public function testList() $this->assertEquals(2, count($channels)); $chan1 = $channels[0]; - $this->assertEquals('12123214323431', $chan1->getId()); + $this->assertEquals('12123214323431', $chan1->getName()); $this->assertEquals('xxx', $chan1->getDescription()); $this->assertEquals('enabled', $chan1->getStatus()); $this->assertEquals('2015-11-24T14:25:31.000Z', $chan1->getLastModified()); @@ -147,7 +236,7 @@ public function testList() $this->assertEquals('http://bucket.oss-cn-hangzhou.aliyuncs.com/1/播放列表.m3u8', $plays[0]); $chan2 = $channels[1]; - $this->assertEquals('432423432423', $chan2->getId()); + $this->assertEquals('432423432423', $chan2->getName()); $this->assertEquals('yyy', $chan2->getDescription()); $this->assertEquals('enabled', $chan2->getStatus()); $this->assertEquals('2016-11-24T14:25:31.000Z', $chan2->getLastModified()); @@ -159,4 +248,5 @@ public function testList() $this->assertEquals(1, count($plays)); $this->assertEquals('http://bucket.oss-cn-hangzhou.aliyuncs.com/2/播放列表.m3u8', $plays[0]); } + }