Co-Watching API 구현

이 페이지에서는 Co-Watching API를 사용하여 시나리오를 보여줍니다

초기 설정

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

Meet 실시간 공유 SDK를 사용하려면 AddonClientFactory.getClient 메서드를 사용하여 축소하도록 요청합니다. 그러면 AddonClient 공동 시청 세션의 진입점 역할을 합니다.

클라이언트를 사용하려면 newSessionBuilder AddonClient의 메서드를 호출하여 새 AddonSession입니다. newSessionBuilder는 다음을 구현합니다. AddonSessionHandler 인터페이스를 사용하여 세션을 위한 부가기능입니다.

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

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

자바

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();
}

사용자 작업 시 알림

로컬 사용자가 미디어를 일시중지 또는 찾는 등의 작업을 실행할 때 재생되며 이러한 작업이 실행될 수 있도록 라이브러리에 알려야 합니다. 공동 시청 환경의 다른 참여자에게 미러링됩니다. 예를 들어 라이브러리에 여러 상태를 알리는 방법은 Get 시작됨

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

  • CoWatchingClient.notifyBuffering: 미디어를 재생할 준비가 되지 않았음을 Meet에 알립니다. 이전 미디어 전환, 미디어 탐색 또는 일반 네트워크로 인한 버퍼링에 대해 걱정할 필요가 없습니다. 정체가 생깁니다.
  • CoWatchingClient.notifyEnded: 미디어 플레이어가 확인할 수 있습니다.
  • CoWatchingClient.notifyPauseState 사용자가 재생을 일시중지하거나 일시중지를 해제했다고 Meet에 알립니다. Meet에서 해당 작업을 다른 사용자에게 미러링할 수 있습니다.
  • CoWatchingClient.notifyPlayoutRate: 사용자가 재생률을 다음과 같이 업데이트했음을 Meet에 알립니다. 새 값 (예: 1.25x)으로 변경합니다.
  • CoWatchingClient.notifyQueueUpdate: 대기열이 변경되었음을 Meet에 알립니다. Meet에서 이를 다른 사용자에게 미러링할 수 있습니다.
  • CoWatchingClient.notifyReady: Meet에 버퍼링이 완료되었으며 미디어가 사용 중임을 알립니다. 제공된 타임스탬프에서 시작하여 재생할 준비가 되었습니다.
  • CoWatchingClient.notifySeekToTimestamp: 사용자가 다음의 재생 지점을 탐색했음을 Meet에 알립니다. Meet에서 해당 작업을 다른 사용자에게 미러링할 수 있습니다.
  • CoWatchingClient.notifySwitchedToMedia: 사용자가 미디어를 전환했다고 Meet에 알립니다. Meet에서 이 정보를 다른 사용자에게 전달할 수 있습니다. 옵션도 제공 동시 대기열 업데이트.

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

자바

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에서 미디어의 현재 위치를 가져와야 합니다. 디코더로 설정하여 CoWatchingHandler.onStateQuery() 있습니다. 이는 정기적으로 호출되므로 성능 기준에 맞게 작성되어야 합니다. (예: 100ms 미만)

다음 코드 샘플은 CoWatchingHandler:

자바

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));
  }
}