Open6

GCP Live Stream API

QuantumQuantum

準備1:環境変数

テキスト?

cat > ~/env_variables.txt << EOF
export PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)")
export LOCATION=us-west2
export INPUT_ID=lab-live-input
export CHANNEL_ID=lab-live-channel
EOF

環境変数を作る

export PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)")
export LOCATION=asia-northeast1
export INPUT_ID=lab-live-input
export CHANNEL_ID=lab-live-channel
環境変数を確認
env | grep -E 'DEVSHELL_PROJECT_ID=|LOGNAME|PROJECT_NUMBER|LOCATION|INPUT_ID|CHANNEL_ID'

(初回のみ) 必要なAPIを有効化する必要があります

  • ネットワークサービスAPI
  • 証明書マネージャー API
  • ライブストリームAPI
  • メディア CDN エッジ キャッシュ API
gcloud 有効化コマンドを流す
gcloud services enable networkservices.googleapis.com
gcloud services enable certificatemanager.googleapis.com
gcloud services enable livestream.googleapis.com
gcloud services enable edgecache.googleapis.com

確認コマンドを流して、4つサービス名が出れば、OK。

有効化の確認コマンド
gcloud services list | grep -E 'networkservices|certificatemanager|livestream|edgecache'
QuantumQuantum

生成した映像(ストリーム)をためるストレージを用意

gcloud
gsutil mb gs://live-streaming-storage-$LOGNAME

一般公開の場合は"公開用の権限を付けます

gcloud
gsutil iam ch allUsers:objectViewer gs://live-streaming-storage-$LOGNAME

Live Stream APIの作成

入力エンドポイント作成

ストリームデータを受け入れるエンドポイントの作成

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d '{ "type": "RTMP_PUSH" }' \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/inputs?inputId=$INPUT_ID"

{ "type": "RTMP_PUSH" } が「どのような入力を受けるか」の部分です。
今回は、RTMP(rtmp://)の入力を作ります。

値の一覧はここ。https://cloud.google.com/livestream/docs/reference/rest/v1/projects.locations.inputs


実行したら、生成が始まる。生成は10分くらいかかる。
返り値の「"done": true」になるまで待つ必要がある。
返り値の"name"キーの末尾"operation-"から始まる文字列をコピー

環境変数にして記録しておく
export OPERATION_ID_1=<OPERATION>

確認コマンドを実行して、「"done": true」になるのを待つ。

確認コマンド
curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/operations/$OPERATION_ID_1"

完了したら、入力用のURLを出力する。

curl -s -X GET -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/inputs/$INPUT_ID" | jq .uri

URLを環境変数に保存しておく。

export URI=<uri>

チャンネル(出力)の作成

設定量が多いので、一旦jsonファイルとして保存。

チャンネル構成例
cat > channel.json << EOF
{
 "inputAttachments": [
   {
     "key": "my-input",
     "input": "projects/$PROJECT_NUMBER/locations/$LOCATION/inputs/$INPUT_ID"
   }
 ],
 "output": {
   "uri": "gs://live-streaming-storage-$LOGNAME"
 },
 "elementaryStreams": [
   {
     "key": "es_video_1",
     "videoStream": {
       "h264": {
         "profile": "high",
         "widthPixels": 1280,
         "heightPixels": 720,
         "bitrateBps": 3000000,
         "frameRate": 30
       }
     }
   },
   {
     "key": "es_video_2",
     "videoStream": {
       "h264": {
         "profile": "high",
         "widthPixels": 640,
         "heightPixels": 360,
         "bitrateBps": 500000,
         "frameRate": 30
       }
     }
   },
   {
     "key": "es_audio_1",
     "audioStream": {
       "codec": "aac",
       "channelCount": 2,
       "bitrateBps": 160000
     }
   },
   {
     "key": "es_audio_2",
     "audioStream": {
       "codec": "aac",
       "channelCount": 2,
       "bitrateBps": 64000
     }
   }
 ],
 "muxStreams": [
   {
     "key": "mux_video_fmp4_1",
     "container": "fmp4",
     "elementaryStreams": ["es_video_1"],
     "segmentSettings": { "segmentDuration": "2s" }
   },
   {
     "key": "mux_video_fmp4_2",
     "container": "fmp4",
     "elementaryStreams": ["es_video_2"],
     "segmentSettings": { "segmentDuration": "2s" }
   },
   {
     "key": "mux_audio_fmp4_1",
     "container": "fmp4",
     "elementaryStreams": ["es_audio_1"],
     "segmentSettings": { "segmentDuration": "2s" }
   },
   {
     "key": "mux_audio_fmp4_2",
     "container": "fmp4",
     "elementaryStreams": ["es_audio_2"],
     "segmentSettings": { "segmentDuration": "2s" }
   }
 ],
 "manifests": [
   {
     "fileName": "hls_index.m3u8",
     "type": "HLS",
     "muxStreams": [
       "mux_video_fmp4_1",
       "mux_audio_fmp4_1",
       "mux_video_fmp4_2",
       "mux_audio_fmp4_2"
     ],
     "maxSegmentCount": 5,
     "segmentKeepDuration": "60s"
   }
 ]
}
EOF

チャンネルの内容は、ここ。https://cloud.google.com/livestream/docs/reference/rest/v1/projects.locations.channels

チャンネル作成を実行

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @channel.json \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/channels?channelId=$CHANNEL_ID"

実行したら、生成待ち。
返り値の「"done": true」になるまで待つ必要がある。
返り値の"name"キーの末尾"operation-"から始まる文字列をコピー

環境変数にして記録しておく
export OPERATION_ID_2=<OPERATION>

確認コマンドを実行して、「"done": true」になるのを待つ。

確認コマンド
curl -s -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/operations/$OPERATION_ID_2"

これで、Live Stream API の構成は完了しました

QuantumQuantum

ストリーミング受付開始

開始コマンド
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/channels/$CHANNEL_ID:start"

状況確認コマンドは以下。

確認コマンド
curl -s -X GET -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/channels/$CHANNEL_ID" | grep "streamingState"

"streamingState": "STARTING" は準備中。

ステータスが "streamingState": "AWAITING_INPUT" になると受付ている状態。

QuantumQuantum

終了とクリーンアップ

Channel を停止

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/channels/$CHANNEL_ID:stop"

Channel を削除

curl -X DELETE -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/channels/$CHANNEL_ID"

入力ストリームを削除

curl -X DELETE \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
"https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/inputs/$INPUT_ID"

バケットを削除

gsutil rm -r gs://live-streaming-storage-$LOGNAME
QuantumQuantum

一覧の取得

入力一覧
curl -s -X GET -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://livestream.googleapis.com/v1/projects/$PROJECT_NUMBER/locations/$LOCATION/inputs"