Skip to content

Commit

Permalink
Merge pull request #18 from aliyun/live-channel
Browse files Browse the repository at this point in the history
Add live channel API
  • Loading branch information
baiyubin2020 committed Nov 12, 2016
2 parents 8b71992 + a52866f commit 534aee2
Show file tree
Hide file tree
Showing 24 changed files with 1,841 additions and 33 deletions.
Binary file added example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions samples/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
require_once __DIR__ . '/Common.php';

use OSS\OssClient;
use OSS\Core\OssException;

$bucketName = Common::getBucketName();
$object = "example.jpg";
$ossClient = Common::getOssClient();
$download_file = "download.jpg";
if (is_null($ossClient)) exit(1);
//*******************************简单使用***************************************************************
// 先把本地的example.jpg上传到指定$bucket, 命名为$object
$ossClient->uploadFile($bucketName, $object, "example.jpg");

// 图片缩放
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100", );
$ossClient->getObject($bucketName, $object, $options);
printImage("imageResize",$download_file);

// 图片裁剪
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/crop,w_100,h_100,x_100,y_100,r_1", );
$ossClient->getObject($bucketName, $object, $options);
printImage("iamgeCrop", $download_file);

// 图片旋转
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/rotate,90", );
$ossClient->getObject($bucketName, $object, $options);
printImage("imageRotate", $download_file);

// 图片锐化
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/sharpen,100", );
$ossClient->getObject($bucketName, $object, $options);
printImage("imageSharpen", $download_file);

// 图片水印
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ", );
$ossClient->getObject($bucketName, $object, $options);
printImage("imageWatermark", $download_file);

// 图片格式转换
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/format,png", );
$ossClient->getObject($bucketName, $object, $options);
printImage("imageFormat", $download_file);

// 获取图片信息
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => "image/info", );
$ossClient->getObject($bucketName, $object, $options);
printImage("imageInfo", $download_file);


/**
生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒
*/
$timeout = 3600;
$options = array(
OssClient::OSS_PROCESS => "image/resize,m_lfit,h_100,w_100",
);
$signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, "GET", $options);
Common::println("rtmp url: \n" . $signedUrl);

//最后删除上传的$object
$ossClient->deleteObject($bucketName, $object);

function printImage($func, $imageFile)
{
$array = getimagesize($imageFile);
Common::println("$func, image width: " . $array[0]);
Common::println("$func, image height: " . $array[1]);
Common::println("$func, image type: " . ($array[2] === 2 ? 'jpg' : 'png'));
Common::println("$func, image size: " . ceil(filesize($imageFile)));
}
126 changes: 126 additions & 0 deletions samples/LiveChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
require_once __DIR__ . '/Common.php';

use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\LiveChannelConfig;

$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);

//******************************* 简单使用 *******************************************************

/**
创建一个直播频道
频道的名称是test_rtmp_live。直播生成的m3u8文件叫做test.m3u8,该索引文件包含3片ts文件,每片ts文件的时长为5秒(这只是一个建议值,具体的时长取决于关键帧)。
*/
$config = new LiveChannelConfig(array(
'description' => 'live channel test',
'type' => 'HLS',
'fragDuration' => 10,
'fragCount' => 5,
'playListName' => 'hello.m3u8'
));
$info = $ossClient->putBucketLiveChannel($bucket, 'test_rtmp_live', $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");

/**
对创建好的频道,可以使用listBucketLiveChannels来进行列举已达到管理的目的。
prefix可以按照前缀过滤list出来的频道。
max_keys表示迭代器内部一次list出来的频道的最大数量,这个值最大不能超过1000,不填写的话默认为100。
*/
$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");

foreach($list->getChannelList() as $list)
{
Common::println("bucket $bucket listLiveChannel:\n" .
"list live channel IsTruncated: ". $list->getName() . "\n" .
"list live channel Description: ". $list->getDescription() . "\n" .
"list live channel Status: ". $list->getStatus() . "\n" .
"list live channel getNextMarker: ". $list->getLastModified() . "\n");
}
/**
创建直播频道之后拿到推流用的play_url(rtmp推流的url,如果Bucket不是公共读写权限那么还需要带上签名,见下文示例)和推流用的publish_url(推流产生的m3u8文件的url)
*/
$play_url = $ossClient->signRtmpUrl($bucket, "test_rtmp_live", 3600, array('params' => array('playlistName' => 'playlist.m3u8')));
Common::println("bucket $bucket rtmp url: \n" . $play_url);
$play_url = $ossClient->signRtmpUrl($bucket, "test_rtmp_live", 3600);
Common::println("bucket $bucket rtmp url: \n" . $play_url);

/**
创建好直播频道,如果想把这个频道禁用掉(断掉正在推的流或者不再允许向一个地址推流),应该使用putLiveChannelStatus接口,将频道的status改成“Disabled”,如果要将一个禁用状态的频道启用,那么也是调用这个接口,将status改成“Enabled”
*/
$resp = $ossClient->putLiveChannelStatus($bucket, "test_rtmp_live", "enabled");

/**
创建好直播频道之后调用getLiveChannelInfo可以得到频道相关的信息
*/
$info = $ossClient->getLiveChannelInfo($bucket, 'test_rtmp_live');
Common::println("bucket $bucket LiveChannelInfo:\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。目前最多可以看到10次推流的记录
*/
$history = $ossClient->getLiveChannelHistory($bucket, "test_rtmp_live");
if (count($history->getLiveRecordList()) != 0)
{
foreach($history->getLiveRecordList() as $recordList)
{
Common::println("bucket $bucket liveChannelHistory:\n" .
"live channel history startTime: ". $recordList->getStartTime() . "\n" .
"live channel history endTime: ". $recordList->getEndTime() . "\n" .
"live channel history remoteAddr: ". $recordList->getRemoteAddr() . "\n");
}
}

/**
对于正在推流的频道调用get_live_channel_stat可以获得流的状态信息。
如果频道正在推流,那么stat_result中的所有字段都有意义。
如果频道闲置或者处于“Disabled”状态,那么status为“Idle”或“Disabled”,其他字段无意义。
*/
$status = $ossClient->getLiveChannelStatus($bucket, "test_rtmp_live");
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");

/**
如果希望利用直播推流产生的ts文件生成一个点播列表,可以使用postVodPlaylist方法。
指定起始时间为当前时间减去60秒,结束时间为当前时间,这意味着将生成一个长度为60秒的点播视频。
播放列表指定为“vod_playlist.m3u8”,也就是说这个接口调用成功之后会在OSS上生成一个名叫“vod_playlist.m3u8”的播放列表文件。
*/
$current_time = time();
$ossClient->postVodPlaylist($bucket,
"test_rtmp_live", "vod_playlist.m3u8",
array('StartTime' => $current_time - 60,
'EndTime' => $current_time)
);

/**
如果一个直播频道已经不打算再使用了,那么可以调用delete_live_channel来删除频道。
*/
$ossClient->deleteBucketLiveChannel($bucket, "test_rtmp_live");
34 changes: 34 additions & 0 deletions src/OSS/Model/GetLiveChannelHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OSS\Model;
/**
* Class GetLiveChannelHistory
* @package OSS\Model
*/
class GetLiveChannelHistory implements XmlConfig
{
public function getLiveRecordList()
{
return $this->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.");
}

private $liveRecordList = array();
}
68 changes: 68 additions & 0 deletions src/OSS/Model/GetLiveChannelInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace OSS\Model;
/**
* Class GetLiveChannelInfo
* @package OSS\Model
*/
class GetLiveChannelInfo implements XmlConfig
{
public function getDescription()
{
return $this->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.");
}

private $description;
private $status;
private $type;
private $fragDuration;
private $fragCount;
private $playlistName;
}
Loading

0 comments on commit 534aee2

Please sign in to comment.