

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Connect 到存储会话
<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 信令频道控制台](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)。

   使用以下命令运行以下 D [escribe-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"
       }
   }
   ```

   您将在对象中找到信令通道 ARN。`ChannelInfo`

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

   此代码片段向您展示了如何使用适用于 v2 的 SDK 创建带有 WebRTC 信令通道的 Kinesis Video Streams。 AWS JavaScript 语法将与其他语法不同 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 终端节点。必须向其指定端点发出[JoinStorageSessionAsViewer](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_webrtc_JoinStorageSessionAsViewer.html)对特定信令通道的请求[JoinStorageSession](https://docs.aws.amazon.com//kinesisvideostreams/latest/dg/API_webrtc_JoinStorageSession.html)或针对该信令通道的请求。

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

**注意**  
Kinesis Vid AWS 管理控制台 eo Streams 目前不支持此操作。

   打开已 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 ]

   此代码片段向您展示了如何使用适用于 v2 的 SDK 调用带有 WebRTC 信令通道的 Kinesis Video Streams 的 `GetSignalingChannelEndpoint` API。 AWS JavaScript 语法将与其他语法不同 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``ConnectAsViewer` WebSocket APIs或使用WebRTC信号正确连接到Kinesis Video Streams，则存储会话中将向参与者发送SDP优惠和ICE候选消息流。 WebSocket 

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

**注意**  
Kinesis Vid AWS 管理控制台 eo Streams 目前不支持此操作。

   打开已 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"
   ```

   成功运行后，返回空响应且不打印任何内容。

   如果是观众参与者，请 AWS CLI 使用之前的频道 ARN 和 WEBRTC 端点在中运行以下`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 ]

   此代码片段向您展示了如何使用适用于 v2 的软件开发工具包调用 Kinesis Video Streams 和 WebRTC 信令通道的 Kinesis Video Streams 的`JoinStorageSession`或 `JoinStorageSessionAsViewer` API。 AWS JavaScript 语法将与其他语法不同 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 摄取和存储节点，取消**选中自动确定摄取模式。**

   将手动覆盖切换为开**启**并选择**显示按钮以手动调用 JoinStorageSession API and/or ** **显示按钮来手动调用 JoinStorageSessionAsViewer API**。

   当您选择 “**启动主**服务器” 或 “**启动查看器**” 时，在应用程序通过`ConnectAsMaster`或连接到信令后`ConnectAsViewer`，将出现一个按钮，让存储会话启动与对等方的 WebRTC 连接，而不是应用程序在连接到信令后立即调用 API。

------