1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104

fuchsia_web / webengine / browser / media_player_impl.h [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FUCHSIA_WEB_WEBENGINE_BROWSER_MEDIA_PLAYER_IMPL_H_
#define FUCHSIA_WEB_WEBENGINE_BROWSER_MEDIA_PLAYER_IMPL_H_

#include <fidl/fuchsia.media.sessions2/cpp/fidl.h>

#include <optional>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "fuchsia_web/webengine/web_engine_export.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/media_session/public/mojom/media_session.mojom.h"

namespace content {
class MediaSession;
}

class WEB_ENGINE_EXPORT MediaPlayerImpl final
    : public fidl::Server<fuchsia_media_sessions2::Player>,
      public media_session::mojom::MediaSessionObserver {
 public:
  // |media_session| must out-live |this|.
  // |on_disconnect| will be invoked when |request| disconnects, and should
  // clean up |this|, and any references to it.
  MediaPlayerImpl(content::MediaSession* media_session,
                  fidl::ServerEnd<fuchsia_media_sessions2::Player> server_end,
                  base::OnceClosure on_disconnect);
  ~MediaPlayerImpl() override;

  MediaPlayerImpl(const MediaPlayerImpl&) = delete;
  MediaPlayerImpl& operator=(const MediaPlayerImpl&) = delete;

  // fuchsia_media_sessions2::Player implementation.
  void WatchInfoChange(WatchInfoChangeCompleter::Sync& completer) override;

  // fuchsia_media_sessions2::PlayerControl implementation.
  void Play(PlayCompleter::Sync& ignored_completer) override;
  void Pause(PauseCompleter::Sync& ignored_completer) override;
  void Stop(StopCompleter::Sync& ignored_completer) override;
  void Seek(SeekRequest& request,
            SeekCompleter::Sync& ignored_completer) override;
  void SkipForward(SkipForwardCompleter::Sync& ignored_completer) override;
  void SkipReverse(SkipReverseCompleter::Sync& ignored_completer) override;
  void NextItem(NextItemCompleter::Sync& ignored_completer) override;
  void PrevItem(PrevItemCompleter::Sync& ignored_completer) override;
  void SetPlaybackRate(
      SetPlaybackRateRequest& request,
      SetPlaybackRateCompleter::Sync& ignored_completer) override;
  void SetRepeatMode(SetRepeatModeRequest& request,
                     SetRepeatModeCompleter::Sync& ignored_completer) override;
  void SetShuffleMode(
      SetShuffleModeRequest& request,
      SetShuffleModeCompleter::Sync& ignored_completer) override;
  void BindVolumeControl(
      BindVolumeControlRequest& request,
      BindVolumeControlCompleter::Sync& ignored_completer) override;

 private:
  // media_session::mojom::MediaSessionObserver implementation.
  void MediaSessionInfoChanged(
      media_session::mojom::MediaSessionInfoPtr info) override;
  void MediaSessionMetadataChanged(
      const std::optional<media_session::MediaMetadata>& metadata) override;
  void MediaSessionActionsChanged(
      const std::vector<media_session::mojom::MediaSessionAction>& action)
      override;
  void MediaSessionImagesChanged(
      const base::flat_map<media_session::mojom::MediaSessionImageType,
                           std::vector<media_session::MediaImage>>& images)
      override;
  void MediaSessionPositionChanged(
      const std::optional<media_session::MediaPosition>& position) override;

  // Sends changes accumulated in |pending_info_delta_|, if any, to the
  // |pending_info_change_callback_|, if it is set.
  void MaybeSendPlayerInfoDelta();

  void OnBindingClosure(fidl::UnbindInfo info);

  // Reports the specified |status| to the client and calls |on_disconnect_|.
  void ReportErrorAndDisconnect(zx_status_t status);

  const raw_ptr<content::MediaSession> media_session_;

  // Invoked when |binding_| becomes disconnected.
  base::OnceClosure on_disconnect_;

  // Binding through which control requests are received from the client.
  fidl::ServerBinding<fuchsia_media_sessions2::Player> binding_;

  // Binding through which notifications are received from the MediaSession.
  mojo::Receiver<media_session::mojom::MediaSessionObserver> observer_receiver_;

  // Pending PlayerInfo deltas and info-change callback.
  std::optional<WatchInfoChangeCompleter::Async> pending_info_change_callback_;
  fuchsia_media_sessions2::PlayerInfoDelta pending_info_delta_;
};

#endif  // FUCHSIA_WEB_WEBENGINE_BROWSER_MEDIA_PLAYER_IMPL_H_