作者:張醫博
場景描述
目前有很多直播愛好者使用的是 OSS + RTMP 做的推流直播,其中不乏一些企業級别應用,由于 OSS 作為推流的接收略微有一些複雜,故單獨開篇講一下。其實不建議使用 OSS+RTMP 做直播推流,因為功能性相比專業的阿裡雲直播産品來說,OSS 的推流适合監管備份等特定場景 ,用戶端對直播推流的延遲要求不是很敏感。如果對直播的拉流推流延遲有高敏感的場景,建議大家使用阿裡雲視訊直播服務,可以做到上下行鍊路加速,且支援多樣化的直播功能适配;
使用基礎
簡單的直播知識
您需要了解甚至掌握簡單的直播知識技巧才能熟練的使用 OSS 直播,不僅是針對 OSS 還涉及到用戶端的推流等相關問題,是以需要明白直播的相關基礎
【音視訊頭介紹】
OSS 的直播功能是建立在 RTMP 直播傳輸協定的基礎上,是以需要指導一些基礎的 RMTP 知識:RTMP 的音視訊流的封裝形式和 FLV 格式相似, 流媒體伺服器向用戶端發送包含 H264 和 AAC 的 RTMP 直播流,需要首先發送這兩個 header,沒有這些資訊播放端是無法解碼音視訊流的,其中音頻 tag 格式如下
1)AVC sequence header
2)AAC sequence header

從上面推論出 AAC sequence header 内容的前 2 個位元組是 0xAF 0x00,我們來看一個示例:
3)ADIF:Audio Data Interchange Format 音頻資料交換格式。這種格式的特征是可以确定的找到這個音頻資料的開始,不需進行在音頻資料流中間開始的解碼,即它的解碼必須在明确定義的開始處進行。故這種格式常用在磁盤檔案中。
4)ADTS:Audio Data Transport Stream 音頻資料傳輸流。這種格式的特征是它是一個有同步字的比特流,解碼可以在這個流中任何位置開始。它的特征類似于 mp3 資料流格式。
【RTMP 内容介紹】
以下是我對 RTMP 總結的一張完整描述圖
對象存儲推流架構
看下官網對于 OSS 推流的過程定義:
1)隻能使用RTMP推流的方式,不支援拉流。
2)必須包含視訊流,且視訊流格式為H264。
3)音頻流是可選的,并且隻支援AAC格式,其他格式的音頻流會被丢棄。
4)轉儲隻支援HLS協定。
5)一個LiveChannel同時隻能有一個用戶端向其推流。
RTMP 的推流格式:
- demo :rtmp://your-bucket.oss-cn-- hangzhou.aliyuncs.com/live/test-channel
- live 等同于 RTMP 的 APP 挂載點
- test-channel 等同于 RTMP 的 stream name
RTMP URL 推流簽名:
- demo:
rtmp://${bucket}.${host}/live/${channel}?OSSAccessKeyId=xxx&Expires=yyy&Signature=zzz&${params}
- 推流前 LiveChannel有兩種Status:enabled和disabled,使用者可以使用本接口在兩種Status之間進行切換。處于disabled狀态時,OSS會禁止使用者向該LiveChannel進行推流操作;如果有使用者正在向該LiveChannel推流,那麼推流的用戶端會被強制斷開(可能會有10s左右的延遲)
對象存儲推流的流程彙總圖如下
生成推流 URL API 設定推流狀态API使用 JAVA SDK 生成推流位址
我們現在用 java 的 SDK 示範一下如上的推理過程,在跑 SDK 之前,需要先搭建好一套本地的 eclipse 環境,如下是我用的 eclipse,如果有沒搭建請網上搜尋一下 eclipse 的搭建方式(之前需要安裝 JDK 且配置環境變量)
環境要求
- Eclipse 版本:Version: Neon.3 Release (4.6.3)
- JDK 版本:jdk1.8.0_144
- OSS:公開讀(為了驗證推流功能是否正常,我們用公開讀的方式快速測試)
- 我們采用主函數入口的方式,執行個體化其他類進行調用,這樣友善類的拆分,不用都集合在主函數中。
- 主函數 domain,執行個體化 OSSClient 對象傳入到 RtmpTest 類中測試。
- 所有的 jar 都會通過官方的 maven 解決的依賴關系, https://help.aliyun.com/document_detail/32009.html
package javasdk;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import com.aliyun.oss.OSSClient;
public class domain {
public static void main( String[] args ) throws ParseException, FileNotFoundException
{
System.out.println( "Hello World!" );
String accessid = "AK";
String secretkey = "SK";
String objectpath = "C://Users//hanli.zyb//Desktop//running.png";
String bucket = "bucket";
String object = "running";
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// OSS + rtmp 推流
String bucketName = "ali-hangzhou";
RtmpTest pushoss = new RtmpTest();
OSSClient ossClient = new OSSClient(endpoint, AK, SK);
pushoss.testCreateLiveChannel(bucketName,ossClient);
}
}
package javasdk;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import junit.framework.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSErrorCode;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.DateUtil;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateLiveChannelRequest;
import com.aliyun.oss.model.CreateLiveChannelResult;
import com.aliyun.oss.model.ListLiveChannelsRequest;
import com.aliyun.oss.model.LiveChannel;
import com.aliyun.oss.model.LiveChannelInfo;
import com.aliyun.oss.model.LiveChannelListing;
import com.aliyun.oss.model.LiveChannelStat;
import com.aliyun.oss.model.LiveChannelStatus;
import com.aliyun.oss.model.LiveChannelTarget;
import com.aliyun.oss.model.LiveRecord;
import com.aliyun.oss.model.PushflowStatus;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class RtmpTest {
String bucketName = "bucket";
final String liveChannel = "stream name";
@Test
public void testCreateLiveChannelDefault(String bucketname,OSSClient ossClient) {
try {
CreateLiveChannelRequest createLiveChannelRequest = new CreateLiveChannelRequest(
bucketName, liveChannel);
CreateLiveChannelResult createLiveChannelResult = ossClient.createLiveChannel(createLiveChannelRequest);
LiveChannelInfo liveChannelInfo = ossClient.getLiveChannelInfo(bucketName, liveChannel);
ossClient.deleteLiveChannel(bucketName, liveChannel);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
@Test
public void testCreateLiveChannel(String bucketname,OSSClient ossClient) {
final String liveChannel = "normal-create-live-channel";
final String liveChannelDesc = "my test live channel";
try {
LiveChannelTarget target = new LiveChannelTarget("HLS", 100, 99, "myplaylist.m3u8");
CreateLiveChannelRequest createLiveChannelRequest = new CreateLiveChannelRequest(
bucketName, liveChannel, liveChannelDesc, LiveChannelStatus.Enabled, target);
CreateLiveChannelResult createLiveChannelResult = ossClient.createLiveChannel(createLiveChannelRequest);
System.out.println(createLiveChannelResult.getPublishUrls());
/*Assert.assertEquals(createLiveChannelResult.getPublishUrls().size(), 1);
Assert.assertTrue(createLiveChannelResult.getPublishUrls().get(0).startsWith("rtmp://"));
Assert.assertTrue(createLiveChannelResult.getPublishUrls().get(0).endsWith("live/" + liveChannel));
Assert.assertEquals(createLiveChannelResult.getPlayUrls().size(), 1);
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).startsWith("http://"));
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).endsWith(liveChannel + "/myplaylist.m3u8"));*/
/* LiveChannelInfo liveChannelInfo = ossClient.getLiveChannelInfo(bucketName, liveChannel);
Assert.assertEquals(liveChannelInfo.getDescription(), liveChannelDesc);
Assert.assertEquals(liveChannelInfo.getStatus(), LiveChannelStatus.Disabled);
Assert.assertEquals(liveChannelInfo.getTarget().getType(), "HLS");
Assert.assertEquals(liveChannelInfo.getTarget().getFragDuration(), 100);
Assert.assertEquals(liveChannelInfo.getTarget().getFragCount(), 99);
Assert.assertEquals(liveChannelInfo.getTarget().getPlaylistName(), "myplaylist.m3u8");*/
// ossClient.deleteLiveChannel(bucketName, liveChannel);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
}
其中我們最關注的是 testCreateLiveChannel 類,建立了推流位址,其中參數 LiveChannelStatus.enable 是要讓推流變為可用狀态。
running 主函數,列印出推流位址。
rtmp://hangzhou.oss-cn-hangzhou.aliyuncs.com/live/normal-create-live-channel
public void testCreateLiveChannel(String bucketname,OSSClient ossClient) {
final String liveChannel = "normal-create-live-channel";
final String liveChannelDesc = "my test live channel";
try {
LiveChannelTarget target = new LiveChannelTarget("HLS", 100, 99, "myplaylist.m3u8");
CreateLiveChannelRequest createLiveChannelRequest = new CreateLiveChannelRequest(
bucketName, liveChannel, liveChannelDesc, LiveChannelStatus.Enabled, target);
CreateLiveChannelResult createLiveChannelResult = ossClient.createLiveChannel(createLiveChannelRequest);
System.out.println(createLiveChannelResult.getPublishUrls());
/*Assert.assertEquals(createLiveChannelResult.getPublishUrls().size(), 1);
Assert.assertTrue(createLiveChannelResult.getPublishUrls().get(0).startsWith("rtmp://"));
Assert.assertTrue(createLiveChannelResult.getPublishUrls().get(0).endsWith("live/" + liveChannel));
Assert.assertEquals(createLiveChannelResult.getPlayUrls().size(), 1);
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).startsWith("http://"));
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).endsWith(liveChannel + "/myplaylist.m3u8"));*/
// ossClient.deleteLiveChannel(bucketName, liveChannel);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}