

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 連線至儲存工作階段
<a name="ingestion-initiate"></a>

請依照這些程序建立儲存工作階段並啟動 WebRTC 連線程序。主要參與者應呼叫 [JoinStorageSession](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_webrtc_JoinStorageSession.html)。觀眾參與者應呼叫 [JoinStorageSessionAsViewer](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_webrtc_JoinStorageSessionAsViewer.html)。

這可讓儲存工作階段透過向透過 連線的主要參與者，或透過 連線的指定檢視器參與者發出訊號[ConnectAsMaster](ConnectAsMaster.md)，來傳送 SDP 優惠和 ICE 候選者[ConnectAsViewer](ConnectAsViewer.md)。

1. 取得訊號頻道的 ARN，因為它是下一個步驟所需的輸入。如果您已經知道 ARN，請繼續下一個步驟。

------
#### [ AWS 管理主控台 ]

   1. 開啟 [Kinesis Video Streams Signaling Channels 主控台](https://console.aws.amazon.com//kinesisvideo/home/#/signalingChannels)。

   1. 選擇訊號頻道的名稱。

   1. 在**訊號頻道資訊**索引標籤下，找到訊號頻道的 ARN。

------
#### [ AWS CLI ]

   確認您已安裝 AWS CLI 並設定 。如需詳細資訊，請參閱[「AWS Command Line Interface 使用者指南」](https://docs.aws.amazon.com//cli/latest/userguide/)。

   如需安裝說明，請參閱[AWS Command Line Interface 《 使用者指南》](https://docs.aws.amazon.com//cli/latest/userguide/getting-started-install.html#getting-started-install-instructions)。安裝後，使用登入資料和區域[設定 AWS CLI](https://docs.aws.amazon.com//cli/latest/userguide/getting-started-quickstart.html#getting-started-quickstart-new)。

   或者，開啟已安裝並設定 AWS CLI 的 AWS CloudShell 終端機。如需詳細資訊，請參閱 [AWS CloudShell 使用者指南](https://docs.aws.amazon.com//cloudshell/latest/userguide/welcome.html#how-to-get-started)。

   使用 執行下列 [Describe-Signaling-Channel](https://docs.aws.amazon.com//cli/latest/reference/kinesisvideo/describe-signaling-channel.html) 命令 AWS CLI：

   ```
   aws kinesisvideo describe-signaling-channel \
     --channel-name "YourChannelName" \
   ```

   回應如下所示：

   ```
   {
       "ChannelInfo": {
           "ChannelName": "YourChannelName",
           "ChannelARN": "arn:aws:kinesisvideo:us-west-2:123456789012:channel/YourChannelName/1234567890123",
           "ChannelType": "SINGLE_MASTER", 
           "ChannelStatus": "ACTIVE",
           "CreationTime": "2024-07-07T23:28:24.941000-07:00",
           "SingleMasterConfiguration": {
           "MessageTtlSeconds": 60
       },
       "Version": "Ws0fZvFGXzEpuZ2CE1s9"
       }
   }
   ```

   您可以在 `ChannelInfo` 物件中找到訊號頻道 ARN。

------
#### [ AWS SDK ]

   此程式碼片段說明如何使用適用於 JavaScript 的 AWS SDK v2 建立 Kinesis Video Streams with WebRTC 訊號頻道。語法將與其他 AWS SDKs 不同，但一般流程將相同。

   您可以檢視 [JoinStorageSession](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/master/examples/joinStorageSession.js) 或 [JoinStorageSessionAsViewer](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/master/examples/joinStorageSessionAsViewer.js) 的完整程式碼範例。

   建立 Kinesis Video Streams 用戶端。這用於呼叫 [DescribeSignalingChannel API](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_DescribeSignalingChannel.html)。

   ```
   const clientConfig = {
       accessKeyId: 'YourAccessKey',
       secretAccessKey: 'YourSecretKey',
       region: 'us-west-2'
   };
   const kinesisVideoClient = new AWS.KinesisVideo(clientConfig);
   ```

   使用 用戶端呼叫 `DescribeSignalingChannel` API。

   ```
   const describeSignalingChannelResponse = await kinesisVideoClient
       .describeSignalingChannel({
           ChannelName: 'YourChannelName',
       })
       .promise();
   ```

   儲存回應。

   ```
   const channelARN =
       describeSignalingChannelResponse.ChannelInfo.ChannelARN;
   ```

------

1. 取得 WEBRTC 端點。對於特定訊號頻道的 [JoinStorageSession](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_webrtc_JoinStorageSession.html) 或 [JoinStorageSessionAsViewer](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_webrtc_JoinStorageSessionAsViewer.html) 請求，必須向指定的端點提出。

------
#### [ AWS 管理主控台 ]

**注意**  
Kinesis Video Streams 目前不支援此操作 AWS 管理主控台。

   開啟 AWS CLI 已安裝並設定 的 AWS CloudShell 終端機。如需詳細資訊，請參閱 [AWS CloudShell 使用者指南](https://docs.aws.amazon.com//cloudshell/latest/userguide/welcome.html#how-to-get-started)。

   遵循 AWS CLI 索引標籤中的指示。

------
#### [ AWS CLI ]

   確認您已安裝 AWS CLI 並設定 。如需詳細資訊，請參閱[AWS Command Line Interface](https://docs.aws.amazon.com//cli/latest/userguide/)文件。

   如需安裝說明，請參閱[AWS Command Line Interface 《 使用者指南》](https://docs.aws.amazon.com//cli/latest/userguide/getting-started-install.html#getting-started-install-instructions)。安裝後，使用登入資料和區域[設定 AWS CLI](https://docs.aws.amazon.com//cli/latest/userguide/getting-started-quickstart.html#getting-started-quickstart-new)。

   或者，開啟已安裝並設定 AWS CLI 的 AWS CloudShell 終端機。如需詳細資訊，請參閱 [AWS CloudShell 使用者指南](https://docs.aws.amazon.com//cloudshell/latest/userguide/welcome.html#how-to-get-started)。

   在 中執行 `Get-Signaling-Channel-Endpoint`命令 AWS CLI：

   ```
   aws kinesisvideo get-signaling-channel-endpoint \
     --channel-arn "arn:aws:kinesisvideo:us-west-2:123456789012:channel/YourChannelName/1234567890123" \
     --single-master-channel-endpoint-configuration "Protocols=['WEBRTC'],Role=MASTER" \
     --region "us-west-2"
   ```

   回應看起來類似以下的內容。

   ```
   {
       "ResourceEndpointList": [
           {
               "Protocol": "WEBRTC",
               "ResourceEndpoint": "https://w-abcd1234.kinesisvideo.aws-region.amazonaws.com"
           } 
       ]
   }
   ```

------
#### [ AWS SDK ]

   此程式碼片段說明如何使用適用於 JavaScript 的 AWS SDK v2 呼叫 Kinesis Video Streams with WebRTC 訊號頻道的 `GetSignalingChannelEndpoint` API。語法將與其他 AWS SDKs不同，但一般流程將相同。檢視 [JoinStorageSession](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/master/examples/joinStorageSession.js) 或 [JoinStorageSessionAsViewer](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/master/examples/joinStorageSessionAsViewer.js) 的完整程式碼範例。

   建立 Kinesis Video Streams 用戶端。這是用來呼叫 [DescribeSignalingChannel API](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_DescribeSignalingChannel.html) 的用戶端。

   如果您先前已建立 Kinesis Video Streams 用戶端來呼叫 `DescribeSignalingChannel`，則可以重複使用相同的用戶端。

   ```
   const clientConfig = {
       accessKeyId: 'YourAccessKey',
       secretAccessKey: 'YourSecretKey',
       region: 'us-west-2'
   };
   const kinesisVideoClient = new AWS.KinesisVideo(clientConfig);
   ```

   使用 用戶端呼叫 `GetSignalingChannelEndpoint` API。

   ```
   const getSignalingChannelEndpointResponse = await kinesisVideoClient
       .getSignalingChannelEndpoint({
           ChannelARN: channelARN,
           SingleMasterChannelEndpointConfiguration: {
               Protocols: ['WEBRTC'],
               Role: 'MASTER',
           },
       })
       .promise();
   ```

   儲存回應：

   ```
   const webrtcEndpoint = getSignalingChannelEndpointResponse.ResourceEndpointList[0].ResourceEndpoint;
   ```

------

1. 使用頻道 ARN 和 WEBRTC 端點進行 API 呼叫。如果參與者透過 `ConnectAsMaster`或 WebSocket API 使用 WebRTC Signaling 正確連接到 Kinesis Video Streams，則 SDP 優惠和 ICE 候選訊息串流將沿著 WebSocket 從儲存工作階段傳送給參與者。 `ConnectAsViewer` WebSocket APIs

------
#### [ AWS 管理主控台 ]

**注意**  
Kinesis Video Streams 目前不支援此操作 AWS 管理主控台。

   開啟 AWS CLI 已安裝並設定 的 AWS CloudShell 終端機。如需詳細資訊，請參閱 [AWS CloudShell 使用者指南](https://docs.aws.amazon.com//cloudshell/latest/userguide/welcome.html#how-to-get-started)。

   遵循 AWS CLI 索引標籤中的指示。

------
#### [ AWS CLI ]

   確認您已安裝 AWS CLI 並設定 。如需詳細資訊，請參閱[AWS Command Line Interface](https://docs.aws.amazon.com//cli/latest/userguide/)文件。

   如需安裝說明，請參閱[AWS Command Line Interface 《 使用者指南》](https://docs.aws.amazon.com//cli/latest/userguide/getting-started-install.html#getting-started-install-instructions)。安裝後，使用登入資料和區域[設定 AWS CLI](https://docs.aws.amazon.com//cli/latest/userguide/getting-started-quickstart.html#getting-started-quickstart-new)。

   或者，開啟已安裝並設定 AWS CLI 的 AWS CloudShell 終端機。如需詳細資訊，請參閱 [AWS CloudShell 使用者指南](https://docs.aws.amazon.com//cloudshell/latest/userguide/welcome.html#how-to-get-started)。

    AWS CLI 使用先前步驟中的頻道 ARN 和 WEBRTC 端點，在 中為主要參與者執行 `Join-Storage-Session`命令：

   ```
   aws kinesis-video-webrtc-storage join-storage-session \
     --endpoint-url https://w-abcd1234.kinesisvideo.us-west-2.amazonaws.com \
     --channel-arn arn:aws:kinesisvideo:us-west-2:123456789012:channel/YourChannelName/1234567890123 \
     --region "us-west-2"
   ```

   成功執行後，會傳回空的回應，而且不會列印任何內容。

   如果是瀏覽者參與者，請使用 之前的頻道 ARN 和 WEBRTC 端點，在 AWS CLI 中執行下列`Join-Storage-Session-As-Viewer`命令：

   ```
   aws kinesis-video-webrtc-storage join-storage-session-as-viewer \
     --endpoint-url https://w-abcd1234.kinesisvideo.us-west-2.amazonaws.com \
     --channel-arn arn:aws:kinesisvideo:us-west-2:123456789012:channel/YourChannelName/1234567890123 \
     --client-id "ExampleViewerClientID"
     --region "us-west-2"
   ```

   成功執行後，會傳回空的回應，而且不會列印任何內容。

------
#### [ AWS SDK ]

   此程式碼片段說明如何使用適用於 JavaScript 的 AWS SDK v2 呼叫 Kinesis Video Streams with WebRTC 訊號頻道的 `JoinStorageSession`或 `JoinStorageSessionAsViewer` API。語法將與其他 AWS SDKs 不同，但一般流程將相同。檢視 [JoinStorageSession](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/master/examples/joinStorageSession.js) 或 [JoinStorageSessionAsViewer](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/master/examples/joinStorageSessionAsViewer.js) 的完整程式碼範例。

   建立 Kinesis Video Streams WebRTC 儲存用戶端。這是用來呼叫 `JoinStorageSession`或 的用戶端，`JoinStorageSessionAsViewer`與先前步驟中建立的 Kinesis Video Streams 用戶端不同。

   ```
   const webrtcStorageClientConfig = {
       accessKeyId: 'YourAccessKey',
       secretAccessKey: 'YourSecretKey',
       region: 'us-west-2',
       endpoint: webrtcEndpoint
   };
   const kinesisVideoWebRTCStorageClient = new AWS.KinesisVideoWebRTCStorage(clientConfig);
   ```

   使用 用戶端呼叫主要參與者的 `JoinStorageSession` API。

   ```
   await kinesisVideoWebRTCStorageClient
       .joinStorageSession({
           channelArn: channelARN,
       })
       .promise();
   ```

   如果是瀏覽者參與者，請使用 用戶端呼叫 `JoinStorageSessionAsViewer` API。

   ```
   await kinesisVideoWebRTCStorageClient
       .joinStorageSessionAsViewer({
           channelArn: channelARN,
           clientId: "ExampleViewerClientID",
       })
       .promise();
   ```

   具有此程式碼範例的即時網頁可在 [GitHub](https://awslabs.github.io/amazon-kinesis-video-streams-webrtc-sdk-js/examples/index.html) 上取得。輸入您的區域、 AWS 憑證和訊號頻道的名稱。展開 **WebRTC 擷取和儲存**節點，然後取消核取**自動判斷擷取模式**。

   切換手動覆寫至 **ON**，然後選取**顯示按鈕以手動呼叫 JoinStorageSession API** 和/或**顯示按鈕以手動呼叫 JoinStorageSessionAsViewer API**。

   當您選取 **Start Master** 或 **Start Viewer** 時，在應用程式透過 `ConnectAsMaster`或 連線至訊號後`ConnectAsViewer`，會顯示一個按鈕，讓儲存工作階段啟動與對等的 WebRTC 連線，而不是在連線至訊號後立即呼叫 API 的應用程式。

------