

# IVS Real-Time Streaming の最適化
<a name="real-time-streaming-optimization"></a>

ユーザーが IVS Real-Time Streaming を使用して動画をストリーミングおよび視聴するときに、最高のエクスペリエンスを得られるように、現在提供されている機能を使用して、エクスペリエンスの一部を向上/最適化する方法が何通りかあります。

## 序章
<a name="real-time-streaming-optimization-intro"></a>

ユーザーエクスペリエンスの質を最適化するには、ユーザーが希望するエクスペリエンスを考慮することが重要です。このようなエクスペリエンスは、視聴するコンテンツやネットワークの状態によって異なる可能性があります。

このガイドでは、ストリームのパブリッシャーまたはサブスクライバーであるユーザーに焦点を当て、それらのユーザーに望ましいアクションとエクスペリエンスを考慮します。

IVS SDK を使用すると、ストリームの最大ビットレート、フレームレート、解像度を設定できます。パブリッシャーにネットワーク輻輳が発生すると、SDK は自動的に調整してビットレート、フレームレート、解像度を下げることによってビデオ品質を低下させます。Android および iOS では、輻輳が発生した際にデグレデーション設定を選択できます。サイマルキャストでレイヤードエンコーディングを有効にしても、デフォルト設定のままにしても、同じ動作が適用されます。

## アダプティブストリーミング: サイマルキャストによるレイヤードエンコーディング
<a name="real-time-streaming-optimization-adaptive"></a>

この機能は、以下のクライアントバージョンでのみサポートされています。
+ iOS とAndroid 1.18.0 以降
+ Web 1.12.0 以降

IVS [リアルタイム Broadcast SDK](broadcast.md) を使用する場合、パブリッシャーは複数のレイヤーのビデオをエンコードし、サブスクライバーはネットワークに最適な品質に自動的に適応または変更します。これをサイマルキャストによるレイヤードエンコーディングと呼びます。

サイマルキャストによるレイヤードエンコーディングは Android と iOS、および Chrome と Edge デスクトップブラウザ (Windows と macOS 用) でサポートされています。他のブラウザではレイヤードエンコーディングはサポートされていません。

下の図では、ホストは 3 つのビデオ品質 (高、中、低) を送信しています。IVS は、利用可能な帯域幅に基づいて最高品質のビデオを各視聴者に転送し、各視聴者に最適な体験を提供します。Viewer 1 のネットワーク接続が正常から不良に変わると、IVS は自動的に低画質のビデオを Viewer 1 に送信し始めるため、Viewer 1 はストリームを中断されることなく (可能な限り最高の品質で) 視聴し続けることができます。

![\[サイマルキャストでレイヤードエンコーディングを使用すると、視聴者のネットワーク接続の品質に基づいて画質を調整できます。\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/images/Layered_Encoding_1.png)


### デフォルトのレイヤー、画質、フレームレート
<a name="real-time-streaming-optimization-default-layers"></a>

モバイルユーザーと Web ユーザーに提供されるデフォルトの画質とレイヤーは次のとおりです。


| モバイル (Android、iOS) | Web (Chrome) | 
| --- | --- | 
| 高レイヤー (またはカスタム): [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/real-time-streaming-optimization.html)  | 高レイヤー (またはカスタム): [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/real-time-streaming-optimization.html)  | 
| 中間レイヤー: なし (モバイルでは高レイヤーと低レイヤーのビットレートの差が小さいため不要) | 中間レイヤー: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/real-time-streaming-optimization.html)  | 
| 低レイヤー: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/real-time-streaming-optimization.html)  | 低レイヤー: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/real-time-streaming-optimization.html)  | 

### レイヤーの解像度
<a name="real-time-streaming-optimization-layer-resolution"></a>

中レイヤーと低レイヤー解像度は、同じアスペクト比を維持するために、高レイヤーから自動的にスケールダウンされます。

解像度が上記のレイヤーに近づくと、中レイヤーと低レイヤーは除外されます。例えば、設定された解像度が 320x180 の場合、SDK は低解像度レイヤーを送信しません。

次の表は、さまざまな設定済み解像度に対して生成されたレイヤーの解像度を示しています。リストされている値は横向きですが、縦向きのコンテンツには縦横逆に適用されます。


| 入力解像度 | 出力レイヤーの解像度: モバイル | 出力レイヤーの解像度: Web | 
| --- | --- | --- | 
|  720p (1280x720)  |  高 (1280x720) 低 (320x180)  |  高 (1280x720) 中 (640x360) 低 (320x180)  | 
|  540p (960x540)  |  高 (960x540) 低 (320x180)  |  高 (960x540) 低 (320x180)  | 
|  360p (640x360)  |  高 (640x360) 低 (360x180)  |  高 (640x360) 低 (360x180)  | 
|  270p (480x270)  |  高 (480x270)  |  高 (480x270)  | 
|  180p (320x180)  |  高 (320x180)  |  高 (320x180)  | 

上記にマッピングされていないカスタム入力解像度については、[次のツールを使用して](https://codepen.io/amazon-ivs/full/ZENQQvo)計算できます。

### サイマルキャストによるレイヤードエンコーディングの設定 (パブリッシャー)
<a name="real-time-streaming-optimization-simulcast"></a>

サイマルキャストでレイヤードエンコーディングを使用するには、クライアントで[この機能を有効にしている](#real-time-streaming-optimization-adaptive)必要があります。有効にすると、パブリッシャーによるアップロード帯域幅の使用が増加し、視聴者側の動画フリーズを減らせる可能性があります。

**Android**

```
// Enable Simulcast
StageVideoConfiguration config = new StageVideoConfiguration();
config.simulcast.setEnabled(true);

ImageLocalStageStream cameraStream = new ImageLocalStageStream(frontCamera, config);

// Other Stage implementation code
```

**iOS**

```
// Enable Simulcast
let config = IVSLocalStageStreamVideoConfiguration()
config.simulcast.enabled = true

let cameraStream = IVSLocalStageStream(device: camera, configuration: config)

// Other Stage implementation code
```

**Web**

```
// Enable Simulcast
let cameraStream = new LocalStageStream(cameraDevice, {
   simulcast: { enabled: true }
})

// Other Stage implementation code
```

個々のレイヤーの設定の詳細については、各 Broadcast SDK ガイド: [Android](android-publish-subscribe.md#android-layered-encoding-simulcast-configure-publisher)、[iOS](ios-publish-subscribe.md#ios-layered-encoding-simulcast-configure-publisher)、および [Web](web-publish-subscribe.md#web-layered-encoding-simulcast-configure-publisher) の「レイヤードエンコーディングの設定 (パブリッシャー）」を参照してください。

### サイマルキャストによるレイヤードエンコーディングの設定 (サブスクライバー)
<a name="real-time-streaming-optimization-simulcast-subscriber"></a>

サブスクライバーが受信するレイヤーの設定については、リアルタイムストリーミング SDK ガイドの「サイマルキャストによるレイヤードエンコーディング」セクションを参照してください。
+ [Android Broadcast SDK](android-publish-subscribe.md#android-publish-subscribe-layered-encoding-simulcast)
+ [IVS Broadcast SDK](ios-publish-subscribe.md#ios-publish-subscribe-layered-encoding-simulcast)
+ [Web Broadcast SDK](web-publish-subscribe.md#web-publish-subscribe-layered-encoding-simulcast)

サブスクライバー設定を使用すると、`InitialLayerPreference` を定義できます。これにより、最初に配信されるビデオの品質と `preferredLayerForStream` が決定されます。これにより、ビデオの再生中に選択されるレイヤーを決定します。レイヤーの変更、適応の変更、レイヤーの選択が行われたときに通知するイベントやストリームメソッドがあります。

## ストリーミング設定
<a name="real-time-streaming-configurations"></a>

このセクションでは、ビデオストリームとオーディオストリームで行うことができるその他の設定について説明します。

### ビデオストリームビットレートの変更
<a name="real-time-streaming-changing-bitrate"></a>

ビデオストリームのビットレートを変更するには、次の設定サンプルを使用します。

**Android**

```
StageVideoConfiguration config = new StageVideoConfiguration();

// Update Max Bitrate to 1.5mbps
config.setMaxBitrate(1500000);

ImageLocalStageStream cameraStream = new ImageLocalStageStream(frontCamera, config);

// Other Stage implementation code
```

**iOS**

```
let config = IVSLocalStageStreamVideoConfiguration();

// Update Max Bitrate to 1.5mbps
try! config.setMaxBitrate(1500000);

let cameraStream = IVSLocalStageStream(device: camera, configuration: config);

// Other Stage implementation code
```

**Web**

```
let cameraStream = new LocalStageStream(camera.getVideoTracks()[0], {
   // Update Max Bitrate to 1.5mbps or 1500kbps
   maxBitrate: 1500
})

// Other Stage implementation code
```

### ビデオストリームフレームレートの変更
<a name="real-time-streaming-changing-framerate"></a>

ビデオストリームのフレームレートを変更するには、次の設定サンプルを使用します。

**Android**

```
StageVideoConfiguration config = new StageVideoConfiguration();

// Update target framerate to 10fps
config.targetFramerate(10);

ImageLocalStageStream cameraStream = new ImageLocalStageStream(frontCamera, config);

// Other Stage implementation code
```

**iOS**

```
let config = IVSLocalStageStreamVideoConfiguration();

// Update target framerate to 10fps
try! config.targetFramerate(10);

let cameraStream = IVSLocalStageStream(device: camera, configuration: config);

// Other Stage implementation code
```

**Web**

```
// Note: On web it is also recommended to configure the framerate of your device from userMedia
const camera = await navigator.mediaDevices.getUserMedia({
   video: {
      frameRate: {
         ideal: 10,
         max: 10,
      },
   },
});

let cameraStream = new LocalStageStream(camera.getVideoTracks()[0], {
   // Update Max Framerate to 10fps
   maxFramerate: 10
})
// Other Stage implementation code
```

### オーディオビットレートとステレオサポートの最適化
<a name="real-time-streaming-optimize-audio-stream"></a>

オーディオストリームのビットレートとステレオ設定を変更するには、次の設定サンプルを使用します。

**Web**

```
// Note: Disable autoGainControl, echoCancellation, and noiseSuppression when enabling stereo.
const camera = await navigator.mediaDevices.getUserMedia({
   audio: {
      autoGainControl: false,
      echoCancellation: false,
      noiseSuppression: false
   },
});

let audioStream = new LocalStageStream(camera.getAudioTracks()[0], {
   // Optional: Update Max Audio Bitrate to 96Kbps. Default is 64Kbps
   maxAudioBitrateKbps: 96,

   // Signal stereo support. Note requires dual channel input source.
   stereo: true
})

// Other Stage implementation code
```

**Android**

```
StageAudioConfiguration config = new StageAudioConfiguration();

// Update Max Bitrate to 96Kbps. Default is 64Kbps.
config.setMaxBitrate(96000);

AudioLocalStageStream microphoneStream = new AudioLocalStageStream(microphone, config);

// Other Stage implementation code
```

**iOS**

```
let config = IVSLocalStageStreamConfiguration();

// Update Max Bitrate to 96Kbps. Default is 64Kbps.
try! config.audio.setMaxBitrate(96000);

let microphoneStream = IVSLocalStageStream(device: microphone, config: config);

// Other Stage implementation code
```

### サブスクライバージッターバッファ MinDelay の変更
<a name="real-time-streaming-jitter-buffer-min-delay"></a>

サブスクライブしている参加者のジッターバッファの最小遅延を変更するには、カスタム `subscribeConfiguration` を使用できます。ジッターバッファは、再生が開始される前に保存されるパケットの数を決定します。最小遅延は、保存する必要があるデータの最小量の目標を表します。最小遅延を変更すると、パケット損失/接続の問題に直面したときに再生の回復力を高めることができます。

ジッターバッファのサイズを大きくすると、再生が開始されるまでの遅延も大きくなります。最小遅延を増やすと、回復力が向上しますが、代償としてビデオになるまでの時間に影響が出ます。再生中の最小遅延を長くすると、同様の効果があります。ジッターバッファがいっぱいになるまでの間、再生は短時間一時停止します。

耐障害性を高める必要がある場合は、再生を開始する前に、最小遅延プリセット `MEDIUM` から開始し、サブスクライブ設定を設定することをお勧めします。

最小遅延は、参加者がサブスクライブ専用である場合にのみ適用されることに注意してください。参加者が自分で公開している場合、最小遅延は適用されません。これは、複数のパブリッシャーが追加の遅延なく相互に話せるようにするために行われます。

以下の例では、最小遅延プリセット `MEDIUM` を使用しています。使用可能なすべての値については、SDK リファレンスのドキュメントを参照してください。

**Web**

```
const strategy = {  
   subscribeConfiguration: (participant) => {
      return {
         jitterBuffer: {
            minDelay: JitterBufferMinDelay.MEDIUM
         }  
      }

   // ... other strategy functions
}
```

**Android**

```
@Override
public SubscribeConfiguration subscribeConfigrationForParticipant(@NonNull Stage stage, @NonNull ParticipantInfo participantInfo) {
    SubscribeConfiguration config = new SubscribeConfiguration();

    config.jitterBuffer.setMinDelay(JitterBufferConfiguration.JitterBufferDelay.MEDIUM());

    return config;
}
```

**iOS**

```
func stage(_ stage: IVSStage, subscribeConfigurationForParticipant participant: IVSParticipantInfo) -> IVSSubscribeConfiguration {
    let config = IVSSubscribeConfiguration()

    try! config.jitterBuffer.setMinDelay(.medium())

    return config
}
```

## 推奨される最適化
<a name="real-time-streaming-optimization-suggested"></a>


| シナリオ | 推奨事項 | 
| --- | --- | 
| テキスト、またはプレゼンテーションやスライドなどの動きの遅いコンテンツを含むストリーミング  | [サイマルキャストによるレイヤードエンコーディング](#real-time-streaming-optimization-adaptive)を使用するか、[より低いフレームレートでストリームを構成](#real-time-streaming-changing-framerate)します。 | 
| アクションや多くの動きを含むストリーム | [サイマルキャストによるレイヤードエンコーディング](#real-time-streaming-optimization-adaptive)を使用します。 | 
| 会話や、とても少ない動きを含むストリーム  | [サイマルキャストによるレイヤードエンコーディング](#real-time-streaming-optimization-adaptive)を使用するか、音声のみを選択します (リアルタイムストリーミング Broadcast SDK ガイドの「参加者への登録」を以下から参照してください：[Web](web-publish-subscribe.md#web-publish-subscribe-concepts-strategy-participants)、[Android](android-publish-subscribe.md#android-publish-subscribe-concepts-strategy-participants)、[iOS](ios-publish-subscribe.md#ios-publish-subscribe-concepts-strategy-participants))。 | 
| 限られたデータでストリーミングを行うユーザー  | [サイマルキャストによるレイヤードエンコーディング](#real-time-streaming-optimization-adaptive)を使用するか、全員のデータ使用量を抑えたい場合は、[フレームレートを低く設定](#real-time-streaming-changing-framerate)して[ビットレートを手動で下げてください](#real-time-streaming-changing-bitrate)。 | 