Co-Watching API 구현

이 페이지에서는 Co-Watching API를 사용하여 공동 시청 시나리오를 지원하는 방법을 설명합니다.

초기 설정

사용할 라이브러리를 준비하려면 실시간 공유 애플리케이션이 공동 시청 세션을 나타내는 CoWatchingClient 객체를 초기화해야 합니다.

Meet 실시간 공유 SDK를 사용하려면 AddonClientFactory.getClient 메서드를 호출합니다. 이렇게 하면 공동 시청 세션의 진입점 역할을 하는 AddonClient가 반환됩니다.

클라이언트를 사용하려면 AddonClient에서 newSessionBuilder 메서드를 호출하여 새 AddonSession의 빌더를 반환합니다. newSessionBuilderAddonSessionHandler 인터페이스를 구현하여 세션의 부가기능에서 제공하는 콜백을 처리합니다.

세션을 시작하려면 빌더에 withCoWatching 메서드를 추가합니다.

다음 코드 샘플은 공동 시청 클라이언트 객체의 기본 초기화를 보여줍니다.

Java

class AwesomeVideoAddonSessionHandler implements AddonSessionHandler {}

// For sample implementation, see the "Manage remote state" section below.
class AwesomeVideoCoWatchingHandler implements CoWatchingHandler {}

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

사용자 작업에 대한 알림

로컬 사용자가 기기에서 미디어 재생을 일시중지하거나 탐색하는 등의 작업을 실행할 때 이러한 작업이 공동 시청 환경의 다른 참여자에게 미러링될 수 있도록 라이브러리에 알려야 합니다. 라이브러리에 여러 상태를 알리는 방법의 예는 시작하기를 참고하세요.

다음 방법을 사용하여 공동 시청 상태를 제어할 수 있습니다.

  • CoWatchingClient.notifyBuffering: 이전 미디어 전환, 미디어 탐색 또는 일반적인 네트워크 정체로 인해 버퍼링으로 인해 미디어를 재생할 준비가 되지 않았음을 Meet에 알립니다.
  • CoWatchingClient.notifyEnded: 미디어 플레이어가 현재 미디어 끝에 도달했음을 Meet에 알립니다.
  • CoWatchingClient.notifyPauseState Meet에서 다른 사용자를 위해 해당 작업을 미러링할 수 있도록 사용자가 미디어 재생을 일시중지하거나 일시중지를 해제했음을 Meet에 알립니다.
  • CoWatchingClient.notifyPlayoutRate: 사용자가 미디어 재생 속도를 새 값 (예: 1.25x)으로 업데이트했음을 Meet에 알립니다.
  • CoWatchingClient.notifyQueueUpdate: 대기열이 변경되었음을 Meet에 알립니다. 따라서 Meet에서 대기열을 다른 사용자에게 미러링할 수 있습니다.
  • CoWatchingClient.notifyReady: 제공된 타임스탬프부터 버퍼링이 완료되고 미디어를 재생할 준비가 되었음을 Meet에 알립니다.
  • CoWatchingClient.notifySeekToTimestamp: Meet에서 다른 사용자를 위해 해당 작업을 미러링할 수 있도록 사용자가 미디어의 재생 지점을 찾았음을 Meet에 알립니다.
  • CoWatchingClient.notifySwitchedToMedia: Meet에서 사용자가 미디어를 전환했음을 Meet에 알리므로 Meet에서 다른 사용자에게 미디어를 전달할 수 있습니다. 동시 큐 업데이트 옵션도 있습니다.

다음 코드 샘플은 사용자에게 알리는 방법을 보여줍니다.

Java

public void onVideoPaused(Duration currentTimestamp) {
  // Use Meet to broadcast the pause state to ensure other participants also pause.
  this.session.getCoWatching().notifyPauseState(/* paused= */ true, currentTimestamp);
};

원격 상태 관리

원격 참여자의 수신 업데이트를 적용하려면 CoWatchingHandler.onCoWatchingStateChanged() 콜백을 사용하여 로컬 미디어 재생 상태를 직접 관리하는 방법을 Meet에 제공해야 합니다.

또한 Meet은 CoWatchingHandler.onStateQuery() 콜백을 호출하여 미디어 재생의 현재 위치를 가져와야 합니다. 이는 정기적으로 호출되므로 성능 기준에 맞게 작성해야 합니다(예: <100ms).

다음 코드 샘플은 CoWatchingHandler의 구현을 보여줍니다.

Java

class AwesomeVideoCoWatchingHandler implements CoWatchingHandler {
  /** Applies incoming playback state to the local video. */
  public void onCoWatchingStateChanged(CoWatchingState newState) {
    // Handle transition to new video.
    if (!newState.mediaId().equals(this.videoPlayer.videoUrl)) {
      this.videoPlayer.loadVideo(newState.mediaId());
    }

    // Only adjust the local video playout if it's sufficiently diverged from the timestamp in the
    // applied update.
    if (newState
            .mediaPlayoutPosition()
            .minus(this.videoPlayer.videoTimestamp)
            .compareTo(Duration.ofMillis(500))
        > 0) {
      this.videoPlayer.seek(newState.mediaPlayoutPosition());
    }

    // Update pause state, if necessary.
    if (newState.playbackState().equals(PLAY) && this.videoPlayer.isPaused) {
      this.videoPlayer.unpause();
    } else if (newState.playbackState().equals(PAUSE) && !this.videoPlayer.isPaused) {
      this.videoPlayer.pause();
    }
  }

  /** Returns local video playback state. */
  public Optional<QueriedCoWatchingState> onStateQuery() {
    return Optional.of(QueriedCoWatchingState.of(
      /* mediaPlayoutPosition= */ this.videoPlayer.videoTimestamp));
  }
}