

# IVS Web Broadcast SDK を使用してパブリッシュおよびサブスクライブする
<a name="getting-started-pub-sub-web"></a>

このセクションでは、ウェブアプリケーションを使用してステージに発行およびサブスクライブする手順について説明します。

## HTML 共通スクリプトの作成
<a name="getting-started-pub-sub-web-html"></a>

最初に、HTML 共通スクリプトを作成し、ライブラリをスクリプトタグとしてインポートします。

```
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <!-- Import the SDK -->
  <script src="https://web-broadcast.live-video.net/1.33.0/amazon-ivs-web-broadcast.js"></script>
</head>

<body>

<!-- TODO - fill in with next sections -->
<script src="./app.js"></script>

</body>
</html>
```

## トークンの入力の受け入れと、参加/退出ボタンの追加
<a name="getting-started-pub-sub-web-join"></a>

ここでは、Body に入力コントロールを入力します。これらはトークンを入力として受け取り、**[参加]** および **[退出]** ボタンを設定します。通常、アプリケーションはアプリケーションの API からトークンをリクエストしますが、この例では、トークンをコピーしてトークン入力に貼り付けます。

```
<h1>IVS Real-Time Streaming</h1>
<hr />

<label for="token">Token</label>
<input type="text" id="token" name="token" />
<button class="button" id="join-button">Join</button>
<button class="button" id="leave-button" style="display: none;">Leave</button>
<hr />
```

## メディアコンテナ要素の追加
<a name="getting-started-pub-sub-web-media"></a>

これらの要素は、ローカル参加者およびリモート参加者向けのメディアを保持します。スクリプトタグを追加して、`app.js` で定義されているアプリケーションのロジックを読み込みます。

```
<!-- Local Participant -->
<div id="local-media"></div>

<!-- Remote Participants -->
<div id="remote-media"></div>

<!-- Load Script -->
<script src="./app.js"></script>
```

これで HTML ページが完成しました。ブラウザに `index.html` を読み込むとこれが表示されるはずです。

![\[リアルタイムストリーミングをブラウザで表示 : HTML の設定が完了しました。\]](http://docs.aws.amazon.com/ja_jp/ivs/latest/RealTimeUserGuide/images/RT_Browser_View.png)


## app.js の作成
<a name="getting-started-pub-sub-web-appjs"></a>

次に、`app.js` ファイルのコンテンツを定義します。まず、SDK のグローバルから必要なすべてのプロパティをインポートします。

```
const {
  Stage,
  LocalStageStream,
  SubscribeType,
  StageEvents,
  ConnectionState,
  StreamType
} = IVSBroadcastClient;
```

## アプリケーション変数の作成
<a name="getting-started-pub-sub-web-vars"></a>

変数を構成し、**[参加]** および **[退出]** ボタンの HTML 要素への参照を保持し、アプリケーションの状態を保存します。

```
let joinButton = document.getElementById("join-button");
let leaveButton = document.getElementById("leave-button");

// Stage management
let stage;
let joining = false;
let connected = false;
let localCamera;
let localMic;
let cameraStageStream;
let micStageStream;
```

## joinStage 1 の作成: 関数の定義と入力の検証
<a name="getting-started-pub-sub-web-joinstage1"></a>

`joinStage` 関数は入力トークンを受け取り、ステージへの接続を作成して、`getUserMedia` から取得したビデオとオーディオの公開を開始します。

まず関数を定義し、状態とトークンの入力を検証します。この機能については、この後のいくつかのセクションで説明します。

```
const joinStage = async () => {
  if (connected || joining) {
    return;
  }
  joining = true;

  const token = document.getElementById("token").value;

  if (!token) {
    window.alert("Please enter a participant token");
    joining = false;
    return;
  }

  // Fill in with the next sections
};
```

## joinStage 2 の作成: メディアを公開する
<a name="getting-started-pub-sub-web-joinstage2"></a>

次のメディアをステージに公開します。

```
async function getCamera() {
  // Use Max Width and Height
  return navigator.mediaDevices.getUserMedia({
    video: true,
    audio: false
  });
}

async function getMic() {
  return navigator.mediaDevices.getUserMedia({
    video: false,
    audio: true
  });
}

// Retrieve the User Media currently set on the page
localCamera = await getCamera();
localMic = await getMic();

// Create StageStreams for Audio and Video
cameraStageStream = new LocalStageStream(localCamera.getVideoTracks()[0]);
micStageStream = new LocalStageStream(localMic.getAudioTracks()[0]);
```

## joinStage 3 の作成: ステージ戦略の定義とステージの作成
<a name="getting-started-pub-sub-web-joinstage3"></a>

このステージ戦略は、何を公開し、どの参加者にサブスクライブするかを決める際に SDK が使用する、決定ロジックの要となります。関数の目的の詳細については、「[戦略](web-publish-subscribe.md#web-publish-subscribe-concepts-strategy)」を参照してください。

この戦略はシンプルです。ステージに参加したら、直前に取得したストリームを公開し、リモート参加者全員のオーディオとビデオにサブスクライブします。

```
const strategy = {
  stageStreamsToPublish() {
    return [cameraStageStream, micStageStream];
  },
  shouldPublishParticipant() {
    return true;
  },
  shouldSubscribeToParticipant() {
    return SubscribeType.AUDIO_VIDEO;
  }
};

stage = new Stage(token, strategy);
```

## joinStage 4 の作成: ステージイベントの処理とメディアのレンダリング
<a name="getting-started-pub-sub-web-joinstage4"></a>

ステージでは多くのイベントが発生します。`STAGE_PARTICIPANT_STREAMS_ADDED` および `STAGE_PARTICIPANT_LEFT` をリッスンして、ページ間でメディアをレンダリングしたり削除したりする必要があります。より詳細なイベントの一覧については、「[イベント](web-publish-subscribe.md#web-publish-subscribe-concepts-events)」にあるリストを参照してください。

ここでは、必要となる DOM 要素の管理に役立つヘルパー関数を 4 つ作成しています：`setupParticipant`、`teardownParticipant`、`createVideoEl`、`createContainer`。

```
stage.on(StageEvents.STAGE_CONNECTION_STATE_CHANGED, (state) => {
  connected = state === ConnectionState.CONNECTED;

  if (connected) {
    joining = false;
    joinButton.style = "display: none";
    leaveButton.style = "display: inline-block";
  }
});

stage.on(
  StageEvents.STAGE_PARTICIPANT_STREAMS_ADDED,
  (participant, streams) => {
    console.log("Participant Media Added: ", participant, streams);

    let streamsToDisplay = streams;

    if (participant.isLocal) {
      // Ensure to exclude local audio streams, otherwise echo will occur
      streamsToDisplay = streams.filter(
        (stream) => stream.streamType === StreamType.VIDEO
      );
    }

    const videoEl = setupParticipant(participant);
    streamsToDisplay.forEach((stream) =>
      videoEl.srcObject.addTrack(stream.mediaStreamTrack)
    );
  }
);

stage.on(StageEvents.STAGE_PARTICIPANT_LEFT, (participant) => {
  console.log("Participant Left: ", participant);
  teardownParticipant(participant);
});


// Helper functions for managing DOM

function setupParticipant({ isLocal, id }) {
  const groupId = isLocal ? "local-media" : "remote-media";
  const groupContainer = document.getElementById(groupId);

  const participantContainerId = isLocal ? "local" : id;
  const participantContainer = createContainer(participantContainerId);
  const videoEl = createVideoEl(participantContainerId);

  participantContainer.appendChild(videoEl);
  groupContainer.appendChild(participantContainer);

  return videoEl;
}

function teardownParticipant({ isLocal, id }) {
  const groupId = isLocal ? "local-media" : "remote-media";
  const groupContainer = document.getElementById(groupId);
  const participantContainerId = isLocal ? "local" : id;

  const participantDiv = document.getElementById(
    participantContainerId + "-container"
  );
  if (!participantDiv) {
    return;
  }
  groupContainer.removeChild(participantDiv);
}

function createVideoEl(id) {
  const videoEl = document.createElement("video");
  videoEl.id = id;
  videoEl.autoplay = true;
  videoEl.playsInline = true;
  videoEl.srcObject = new MediaStream();
  return videoEl;
}

function createContainer(id) {
  const participantContainer = document.createElement("div");
  participantContainer.classList = "participant-container";
  participantContainer.id = id + "-container";

  return participantContainer;
}
```

## joinStage 5 の作成: ステージへの参加
<a name="getting-started-pub-sub-web-joinstage5"></a>

ステージに参加して、`joinStage` 関数を完成させましょう。

```
try {
  await stage.join();
} catch (err) {
  joining = false;
  connected = false;
  console.error(err.message);
}
```

## leaveStage の作成
<a name="getting-started-pub-sub-web-leavestage"></a>

[退出] ボタンで呼び出す `leaveStage` 関数を定義します。

```
const leaveStage = async () => {
  stage.leave();

  joining = false;
  connected = false;
};
```

## 入力イベントハンドラーの初期化
<a name="getting-started-pub-sub-web-handlers"></a>

最後にもう 1 つ、関数を `app.js` ファイルに追加します。この関数は、ページが読み込まれ、ステージへの参加と退出のためのイベントハンドラーが確立され次第すぐに呼び出されます。

```
const init = async () => {
  try {
    // Prevents issues on Safari/FF so devices are not blank
    await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
  } catch (e) {
    alert(
      "Problem retrieving media! Enable camera and microphone permissions."
    );
  }

  joinButton.addEventListener("click", () => {
    joinStage();
  });

  leaveButton.addEventListener("click", () => {
    leaveStage();
    joinButton.style = "display: inline-block";
    leaveButton.style = "display: none";
  });
};

init(); // call the function
```

## アプリケーションの実行とトークンの提供
<a name="getting-started-pub-sub-run-app"></a>

この時点でウェブページをローカルまたは他のユーザーと共有し、[ページを開き](#getting-started-pub-sub-web-media)、参加者トークンを入力してステージに参加できます。

## 次のステップ
<a name="getting-started-pub-sub-next"></a>

npm や React などに関する詳細な例については、「[IVS Broadcast SDK: Web ガイド (リアルタイムストリーミングガイド](broadcast-web.md)」を参照してください。