Co-Doing API を実装する

このページでは、Co-Doing API を使用して Co-Doing のシナリオをサポートする方法について説明します。

初期設定

ライブラリを使用する準備をするには、ライブ共有アプリケーションで、共同作業セッションを表す CoDoingClient オブジェクトを初期化する必要があります。

Meet ライブ共有 SDK を使用するには、AddonClientFactory.getClient メソッドを呼び出します。これにより、共同活動セッションのエントリ ポイントとして機能する AddonClient が返されます。

クライアントを使用するには、AddonClient から newSessionBuilder メソッドを呼び出して、新しい AddonSession のビルダーを返します。newSessionBuilderAddonSessionHandler インターフェースを実装し、セッションのアドオンによって提供されるコールバックを処理します。

セッションを開始するには、withCoDoing メソッドをビルダーに追加します。

次のコードサンプルは、共同実行クライアント オブジェクトの基本的な初期化を示しています。

Java

class AwesomeVideoAddonSessionHandler implements AddonSessionHandler {}

//For sample implementation, see the "Handle incoming updates" section.
class AwesomeVideoCoDoingHandler implements CoDoingHandler {}

public ListenableFuture<AddonSession> initialSetup() {
  AddonClient meetClient = AddonClientFactory.getClient();
  return meetClient
      .newSessionBuilder(
          new AwesomeVideoAddonSessionHandler())
      .withCoDoing(new AwesomeVideoCoDoingHandler())
      .begin();
}

動画を一時停止

ライブ共有に参加するときに、ユーザーがローカル動画アプリで再生を一時停止した場合、ライブ共有のすべての参加者も動画を一時停止する必要があります。

これを行うには、動画が一時停止されたことを示す CoDoingState メッセージを作成し、setGlobalState メソッドを使用して他のすべての参加者にブロードキャストするよう Google Meet に指示します。共有グローバル状態は、新しい状態が設定されるまで、既存か新規かを問わず、すべての参加者のデフォルトの状態になります。

次のコードサンプルは、ユーザーに一時停止状態を通知する方法を示しています。

Java

public void onVideoPaused(String videoUrl, Instant currentTimestamp) {
  // Create an internal state object to share with other participants. Note: It's
  // good practice to encode all metadata—even seemingly irrelevant data—into
  // ActivityState updates to guard against race conditions and other subtle
  // failures.
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(true)
    .build();

  // Create the CoDoingState object to wrap the internal state
  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  // Use Meet to broadcast internal state update to all other participants
  this.coDoingClient.setGlobalState(coDoingState);
};

コードサンプルは、シリアル化された videoState オブジェクトをトリガーし、ライブ共有に参加する他のすべての Meet インスタンスにブロードキャストします。他の参加者からブロードキャスト アップデートを受信する方法について詳しくは、アップデート受信を処理するをご覧ください。

次の図は、一時停止アクションがトリガーされた後のイベント シーケンスを示しています。

Live Sharing API の開始の図。

動画の一時停止を解除

動画の一時停止と同様に、ユーザーがローカルアプリで動画の一時停止を解除した場合、Meet はこの操作を他のライブ共有参加者にブロードキャストする必要があります。

送信側(動画の一時停止を解除したユーザー)側では、一時停止の例との唯一の違いは、isPaused ステータスが更新されることです。

次のコードサンプルは、送信側から一時停止解除状態をユーザーに通知する方法を示しています。

Java

public void onVideoUnpaused(String videoUrl, Instant currentTimestamp) {
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(false)
    .build();

  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  this.coDoingClient.setGlobalState(coDoingState);
}

動画に移動

動画の一時停止動画の一時停止解除と同様に、ユーザーがローカルアプリのタイムラインを新しいタイムスタンプにドラッグした場合、Meet はこの操作を参加者全員にブロードキャストする必要があります。

次のコードサンプルは、更新されたタイムスタンプを送信者側からユーザーに通知する方法を示しています。

Java

public void onVideoSeeked(String videoUrl, Instant currentTimestamp, bool isPaused) {
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(isPaused)
    .build();

  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  this.coDoingClient.setGlobalState(coDoingState);
}

別の動画を再生

ユーザーがローカルアプリで別の動画を選択して視聴中の動画を変更した場合、Meet はライブ共有のすべての参加者に対して新しい動画を再生する必要があります。変更された動画は videoState.videoUrl に保存されます。

次のコードサンプルは、更新された動画の URL をユーザーに通知する方法を示しています。

Java

public void onVideoChanged(String videoUrl, Duration currentTimestamp, bool isPaused) {
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(isPaused)
    .build();

  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  this.coDoingClient.setGlobalState(coDoingState);
}

共同行動を終了

ユーザーがアクティビティの終了を選択すると、endSession メソッドと Meet アプリの接続が解除されます。Meet が強制的に会議を終了することはなく、ユーザーが会議から退出させることもありません。

次のコードサンプルは、停止したセッションをユーザーに通知する方法を示しています。

Java

public void endCoDoing() {
  this.session.endSession();
}

着信アップデートを処理する

別の参加者の Meet アプリがブロードキャストを受信すると、onGlobalStateChanged() コールバックがトリガーされます。通常は、受信動画のタイムスタンプがローカルのタイムスタンプと十分に異なる場合にのみマッチングするなど、受信アップデートに応じて実行するアクションを適切に決定することが重要です。

次のコードサンプルは、さまざまな受信アップデートを処理する方法を示しています。

Java

class AwesomeVideoCoDoingHandler implements CoDoingHandler {
  public void onGlobalStateChanged(CoDoingState update) {
    AwesomeVideoState videoState = SerializationUtils.deserialize(update.state());

    // Handle transition to new video.
    if (!videoState.videoUrl.equals(this.videoPlayer.videoUrl)) {
      this.videoPlayer.loadVideo(videoState.videoUrl);
    }

    // If the timestamp in the arriving update has sufficiently diverged, adjust
    // the local video playout.
    if (videoState.videoTimestamp.minus(this.videoPlayer.videoTimestamp).abs() >
                                        Duration.ofSeconds(2)) {
      this.videoPlayer.seek(videoState.videoTimestamp);
    }

    // Update pause state, if necessary.
    if (!videoState.isPaused && this.videoPlayer.isPaused) {
      this.videoPlayer.unpause();
    } else if (videoState.isPaused && !this.videoPlayer.isPaused) {
      this.videoPlayer.pause();
    }
  }
}