media_remoter.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_MIRRORING_SERVICE_MEDIA_REMOTER_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_MEDIA_REMOTER_H_
  6. #include "base/component_export.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "media/cast/cast_config.h"
  9. #include "media/mojo/mojom/remoting.mojom.h"
  10. #include "media/mojo/mojom/remoting_common.mojom.h"
  11. #include "mojo/public/cpp/bindings/receiver.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. namespace media {
  14. namespace cast {
  15. class CastEnvironment;
  16. class CastTransport;
  17. } // namespace cast
  18. } // namespace media
  19. namespace mirroring {
  20. class MessageDispatcher;
  21. class ReceiverResponse;
  22. class RemotingSender;
  23. // MediaRemoter remotes media content directly to a Cast Receiver. When
  24. // MediaRemoter is started, it connects itself with a source tab in browser
  25. // through the Mirroring Service mojo interface and allows the browser to access
  26. // this MediaRemoter to start/stop individual remoting sessions, which are
  27. // caused by user actions (i.e., when they somehow indicate a desire to
  28. // enter/leave an immersive video-watching mode).
  29. //
  30. // When a remoting session is started, MediaRemoter will first request that tab
  31. // mirroring be switched into content remoting mode. If granted, it will notify
  32. // the browser that this has succeeded. At this point, two-way RPC binary
  33. // messaging begins, and the MediaRemoter simply forwards messages between the
  34. // browser and the Cast Receiver. The audio/video data streams are delivered
  35. // from the media renderer to the Mirroring Service through mojo datapipes, and
  36. // are then sent out to Cast Receiver through Cast Streaming.
  37. class COMPONENT_EXPORT(MIRRORING_SERVICE) MediaRemoter final
  38. : public media::mojom::Remoter {
  39. public:
  40. class Client {
  41. public:
  42. virtual ~Client() {}
  43. // Connects the |remoter| with a source tab.
  44. virtual void ConnectToRemotingSource(
  45. mojo::PendingRemote<media::mojom::Remoter> remoter,
  46. mojo::PendingReceiver<media::mojom::RemotingSource>
  47. source_receiver) = 0;
  48. // Requests to start remoting. StartRpcMessaging() / OnRemotingStartFailed()
  49. // will be called when starting succeeds / fails.
  50. virtual void RequestRemotingStreaming() = 0;
  51. // Requests to resume mirroring.
  52. virtual void RestartMirroringStreaming() = 0;
  53. };
  54. MediaRemoter(Client* client,
  55. const media::mojom::RemotingSinkMetadata& sink_metadata,
  56. MessageDispatcher* message_dispatcher);
  57. MediaRemoter(const MediaRemoter&) = delete;
  58. MediaRemoter& operator=(const MediaRemoter&) = delete;
  59. ~MediaRemoter() override;
  60. // Callback from |message_dispatcher_| for received RPC messages.
  61. void OnMessageFromSink(const ReceiverResponse& response);
  62. // Called when OFFER/ANSWER exchange for a remoting session succeeds.
  63. void StartRpcMessaging(
  64. scoped_refptr<media::cast::CastEnvironment> cast_environment,
  65. media::cast::CastTransport* transport,
  66. const media::cast::FrameSenderConfig& audio_config,
  67. const media::cast::FrameSenderConfig& video_config);
  68. // Called when a mirroring session is successfully resumed.
  69. void OnMirroringResumed();
  70. // Error occurred either during the start of remoting or in the middle of
  71. // remoting. In either case, this call fallbacks to mirroring, and prevents
  72. // further starting of media remoting during this mirroring session.
  73. void OnRemotingFailed();
  74. // media::mojom::Remoter implememtation. Stops the current remoting session.
  75. // This could be called either by the RemotingSource or the Session.
  76. void Stop(media::mojom::RemotingStopReason reason) override;
  77. private:
  78. // media::mojom::Remoter implememtation.
  79. void Start() override;
  80. void StartDataStreams(
  81. mojo::ScopedDataPipeConsumerHandle audio_pipe,
  82. mojo::ScopedDataPipeConsumerHandle video_pipe,
  83. mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
  84. audio_sender_receiver,
  85. mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
  86. video_sender_receiver) override;
  87. void SendMessageToSink(const std::vector<uint8_t>& message) override;
  88. void EstimateTransmissionCapacity(
  89. media::mojom::Remoter::EstimateTransmissionCapacityCallback callback)
  90. override;
  91. // Called by RemotingSender when error occurred. Will stop this remoting
  92. // session and fallback to mirroring.
  93. void OnRemotingDataStreamError();
  94. const raw_ptr<Client> client_; // Outlives this class.
  95. const media::mojom::RemotingSinkMetadata sink_metadata_;
  96. const raw_ptr<MessageDispatcher> message_dispatcher_; // Outlives this class.
  97. mojo::Receiver<media::mojom::Remoter> receiver_{this};
  98. mojo::Remote<media::mojom::RemotingSource> remoting_source_;
  99. scoped_refptr<media::cast::CastEnvironment> cast_environment_;
  100. std::unique_ptr<RemotingSender> audio_sender_;
  101. std::unique_ptr<RemotingSender> video_sender_;
  102. raw_ptr<media::cast::CastTransport> transport_; // Outlives this class;
  103. media::cast::FrameSenderConfig audio_config_;
  104. media::cast::FrameSenderConfig video_config_;
  105. // State transition diagram:
  106. //
  107. // .-----------> MIRRORING
  108. // | |
  109. // | V
  110. // | STARTING_REMOTING
  111. // | |
  112. // | V
  113. // | .-----------------------------.
  114. // | | | |
  115. // | | V V
  116. // | | REMOTING_STARTED ----> REMOTING_DISABLED
  117. // | | |
  118. // | V V
  119. // .--STOPPING_REMOTING
  120. enum {
  121. MIRRORING, // In mirroring.
  122. STARTING_REMOTING, // Starting a remoting session.
  123. REMOTING_STARTED, // Remoting started successfully.
  124. REMOTING_DISABLED, // Remoting was disabled (because of error).
  125. STOPPING_REMOTING, // Stopping the remoting session.
  126. } state_;
  127. base::WeakPtrFactory<MediaRemoter> weak_factory_{this};
  128. };
  129. } // namespace mirroring
  130. #endif // COMPONENTS_MIRRORING_SERVICE_MEDIA_REMOTER_H_