本页介绍了如何以极低的延迟预览输入源,以便近乎实时地检测潜在问题或事件。这样一来,您就可以采取纠正措施,例如暂停直播、插播广告或显示板,从而为观看者提供优质的观看体验。
预览内容以以下格式提供:
- 格式:MPEG-TS
- 视频:
- 分辨率:640x480
- 编解码器:H.264
- 帧速率:每秒 25 帧 (FPS)
- 音频
- 编解码器:AAC
- 采样率:48 kHz
- 协议:HTTPS
准备工作
本页面假定您已完成 HLS 直播快速入门的准备工作部分中的步骤。
创建输入端点
如需创建输入端点,请使用 projects.locations.inputs.create
方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:创建输入端点的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
INPUT_ID
:要创建的新输入端点(您将输入流发送到该端点)的用户定义标识符。此值必须为 1-63 个字符,以[a-z0-9]
开头和结尾,并且可以在字符之间包含短划线 (-)。例如my-input
。
请求 JSON 正文:
{ "type": "RTMP_PUSH" }
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
此命令会创建一个长时间运行的操作 (LRO),您可以使用该操作来跟踪请求的进度。如需了解详情,请参阅管理长时间运行的操作 。
获取输入端点详细信息
如需获取输入源端点的详细信息,请使用 projects.locations.inputs.get
方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:输入端点所在的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
INPUT_ID
:输入端点的用户定义标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "createTime": CREATE_TIME, "updateTime": UPDATE_TIME, "type": "RTMP_PUSH", "uri": "INPUT_STREAM_URI", # For example, "rtmp://1.2.3.4/live/b8ebdd94-c8d9-4d88-a16e-b963c43a953b", "tier": "HD" }
找到 uri
字段,然后复制返回的 INPUT_STREAM_URI 以供稍后在发送输入流部分中使用。
创建频道
如需创建渠道,请使用 projects.locations.channels.create
方法。以下示例会创建一个生成 HLS 直播的频道。直播包含单个高清 (1280x720) 呈现版本。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:创建渠道的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:要创建的渠道的用户定义标识符;此值必须为 1-63 个字符,以[a-z0-9]
开头和结尾,并且可以在字符之间包含短划线 (-)。INPUT_ID
:输入端点的用户定义标识符BUCKET_NAME
:您创建的用于存储直播清单和分段文件的 Cloud Storage 存储桶的名称
请求 JSON 正文:
{ "inputAttachments": [ { "key": "my-input", "input": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID" } ], "output": { "uri": "gs://BUCKET_NAME" }, "elementaryStreams": [ { "key": "es_video", "videoStream": { "h264": { "profile": "high", "widthPixels": 1280, "heightPixels": 720, "bitrateBps": 3000000, "frameRate": 30 } } }, { "key": "es_audio", "audioStream": { "codec": "aac", "channelCount": 2, "bitrateBps": 160000 } } ], "muxStreams": [ { "key": "mux_video_ts", "container": "ts", "elementaryStreams": ["es_video", "es_audio"], "segmentSettings": { "segmentDuration": "2s" } } ], "manifests": [ { "key": "manifest_hls", "fileName": "main.m3u8", "type": "HLS", "muxStreams": [ "mux_video_ts" ], "maxSegmentCount": 5 } ] }
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
此命令会创建一个长时间运行的操作 (LRO),您可以使用该操作来跟踪请求的进度。如需了解详情,请参阅管理长时间运行的操作 。
创建频道
如需启动频道,请使用 projects.locations.channels.start
方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "start", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
此命令会创建一个长时间运行的操作 (LRO),您可以使用该操作来跟踪请求的进度。如需了解详情,请参阅管理长时间运行的操作 。
安装编码器
如需使用该 API,您需要使用编码器来生成 API 需处理的输入流。
安装 ffmpeg
,因为本页介绍了如何使用 ffmpeg
生成输入流。您可以使用以下命令在 Cloud Shell 中安装此工具。
sudo apt install ffmpeg
发送输入流
打开一个新终端窗口。运行以下命令,使用获取输入端点详细信息部分中的 INPUT_STREAM_URI。此命令会生成测试流。
ffmpeg -re -f lavfi -i "testsrc=size=1280x720 [out0]; sine=frequency=500 [out1]" \ -acodec aac -vcodec h264 -f flv INPUT_STREAM_URI
获取端点网址和不记名令牌
如需获取预览内容的端点网址和身份验证所需的不记名令牌,请使用 projects.locations.inputs.preview
方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:输入端点所在的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
INPUT_ID
:输入端点的用户定义标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "uri": ENDPOINT_URL, # For example, "preview.k3fhpvei3vvf.us-central1.livestream.goog/inputs/my-preview-input", "bearerToken": "BEARER_TOKEN" }
您将在下一部分中使用 uri
和 bearerToken
字段中的值。
显示预览内容
您可以使用以下主要选项来显示预览内容:
mpegts.js
mpegts.js 是 GitHub 上提供的一个库,用于在 Web 浏览器中播放 MPEG-TS 流。
将您在上一步中收到的 bearerToken
和 uri
字段中的值复制到以下 HTML 中。根据以下 HTML 代码创建一个 HTML 文件,然后在浏览器中打开该文件。
<html>
<head>
<script src="https://xqq.im/mpegts.js/dist/mpegts.js"></script>
<video id="video" autoplay controls muted></video>
<script>
function main() {
if (!mpegts.getFeatureList().mseLivePlayback) {
console.log("MSE live playback is not supported by the browswer!")
return;
}
var videoElement = document.getElementById("video");
player = mpegts.createPlayer(
{
type: "mse",
isLive: true,
<!-- Placeholder: Replace ENDPOINT_URL with the actual endpoint URL -->
url: "ENDPOINT_URL",
},
{
headers: {
<!-- Placeholder: Replace BEARER_TOKEN with the actual bearer token -->
Authorization: "BEARER_TOKEN",
},
}
);
player.attachMediaElement(videoElement);
player.load();
player.play();
}
main();
</script>
</body>
</html>
您应该会看到测试模式以比相关联的直播更低的延迟播放。此串流播放器只能播放串流中的第一个音轨。
FFplay
FFplay 是一款多用途的命令行媒体播放器,使用 FFmpeg 库。您可以在下载 FFmpeg 页面中找到 FFplay 的下载链接。
打开一个新终端窗口。将您在上一步中收到的 bearerToken
和 uri
字段中的值复制到以下命令中,然后运行该命令。
ffplay -headers "Authorization: Bearer BEARER_TOKEN" \ -fflags nobuffer -flags low_delay -probesize 32 -analyzeduration 0 \ ENDPOINT_URL
您应该会看到测试模式以比相关联的直播更低的延迟播放。ffplay
支持通过按“A”键在音轨之间切换。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
停止频道
使用 projects.locations.channels.stop
方法停止渠道。您必须先停止频道,然后才能将其删除。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "stop", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
此命令会创建一个长时间运行的操作 (LRO),您可以使用该操作来跟踪请求的进度。如需了解详情,请参阅管理长时间运行的操作 。
停止输入流
如果您使用 ffmpeg
发送输入源,则在停止频道后,连接会自动断开。
删除频道
使用 projects.locations.channels.delete
方法删除频道。您必须先删除频道,然后才能删除该频道使用的输入端点。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:频道所在的地理位置;请使用支持的地区之一显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
CHANNEL_ID
:用户定义的渠道标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/channels/CHANNEL_ID", "verb": "delete", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
此命令会创建一个长时间运行的操作 (LRO),您可以使用该操作来跟踪请求的进度。如需了解详情,请参阅管理长时间运行的操作 。
删除输入端点
使用 projects.locations.inputs.delete
方法删除输入端点。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_NUMBER
:您的 Google Cloud 项目编号;该编号位于 IAM 设置页面上的项目编号字段中LOCATION
:输入端点所在的位置;请使用某个受支持的地区显示位置us-central1
us-east1
us-east4
us-west1
us-west2
northamerica-northeast1
southamerica-east1
asia-east1
asia-east2
asia-south1
asia-northeast1
asia-southeast1
australia-southeast1
europe-north1
europe-west1
europe-west2
europe-west3
europe-west4
INPUT_ID
:输入端点的用户定义标识符
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.video.livestream.v1.OperationMetadata", "createTime": CREATE_TIME, "target": "projects/PROJECT_NUMBER/locations/LOCATION/inputs/INPUT_ID", "verb": "delete", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }
此命令会创建一个长时间运行的操作 (LRO),您可以使用该操作来跟踪请求的进度。如需了解详情,请参阅管理长时间运行的操作 。
删除 Cloud Storage 存储桶
在 Google Cloud 控制台中,前往 Cloud Storage 浏览器页面。
选中所创建存储分区旁边的复选框。
点击删除。
在随即显示的对话框窗口中,点击删除以删除相应存储桶及其内容。