session.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_SESSION_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_SESSION_H_
  6. #include "base/component_export.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/task/single_thread_task_runner.h"
  9. #include "components/mirroring/mojom/cast_message_channel.mojom.h"
  10. #include "components/mirroring/mojom/resource_provider.mojom.h"
  11. #include "components/mirroring/mojom/session_observer.mojom.h"
  12. #include "components/mirroring/mojom/session_parameters.mojom.h"
  13. #include "components/mirroring/service/media_remoter.h"
  14. #include "components/mirroring/service/message_dispatcher.h"
  15. #include "components/mirroring/service/mirror_settings.h"
  16. #include "components/mirroring/service/receiver_setup_querier.h"
  17. #include "components/mirroring/service/rtp_stream.h"
  18. #include "gpu/config/gpu_info.h"
  19. #include "media/capture/video/video_capture_feedback.h"
  20. #include "media/cast/cast_environment.h"
  21. #include "media/cast/net/cast_transport_defines.h"
  22. #include "media/mojo/mojom/video_encode_accelerator.mojom.h"
  23. #include "mojo/public/cpp/bindings/pending_receiver.h"
  24. #include "mojo/public/cpp/bindings/pending_remote.h"
  25. #include "mojo/public/cpp/bindings/remote.h"
  26. #include "services/network/public/mojom/network_context.mojom.h"
  27. namespace media {
  28. class AudioInputDevice;
  29. namespace cast {
  30. class CastTransport;
  31. } // namespace cast
  32. } // namespace media
  33. namespace viz {
  34. class Gpu;
  35. } // namespace viz
  36. namespace mirroring {
  37. class ReceiverResponse;
  38. class VideoCaptureClient;
  39. class ReceiverSetupQuerier;
  40. // Controls a mirroring session, including audio/video capturing, Cast
  41. // Streaming, and the switching to/from media remoting. When constructed, it
  42. // does OFFER/ANSWER exchange with the mirroring receiver. Mirroring starts when
  43. // the exchange succeeds and stops when this class is destructed or error
  44. // occurs. Specifically, a session is torn down when (1) a new session starts,
  45. // (2) the mirroring service note a disconnection.
  46. // |observer| will get notified when status changes. |outbound_channel| is
  47. // responsible for sending messages to the mirroring receiver through Cast
  48. // Channel. |inbound_channel| receives message sent from the mirroring receiver.
  49. class COMPONENT_EXPORT(MIRRORING_SERVICE) Session final
  50. : public RtpStreamClient,
  51. public MediaRemoter::Client {
  52. public:
  53. Session(mojom::SessionParametersPtr session_params,
  54. const gfx::Size& max_resolution,
  55. mojo::PendingRemote<mojom::SessionObserver> observer,
  56. mojo::PendingRemote<mojom::ResourceProvider> resource_provider,
  57. mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel,
  58. mojo::PendingReceiver<mojom::CastMessageChannel> inbound_channel,
  59. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
  60. ~Session() override;
  61. using AsyncInitializeDoneCB = base::OnceCallback<void()>;
  62. void AsyncInitialize(AsyncInitializeDoneCB done_cb);
  63. // RtpStreamClient implementation.
  64. void OnError(const std::string& message) override;
  65. void RequestRefreshFrame() override;
  66. void CreateVideoEncodeAccelerator(
  67. media::cast::ReceiveVideoEncodeAcceleratorCallback callback) override;
  68. // Callbacks by media::cast::CastTransport::Client.
  69. void OnTransportStatusChanged(media::cast::CastTransportStatus status);
  70. void OnLoggingEventsReceived(
  71. std::unique_ptr<std::vector<media::cast::FrameEvent>> frame_events,
  72. std::unique_ptr<std::vector<media::cast::PacketEvent>> packet_events);
  73. // Helper method for setting constraints from the ANSWER response.
  74. void SetConstraints(const openscreen::cast::Answer& answer,
  75. media::cast::FrameSenderConfig* audio_config,
  76. media::cast::FrameSenderConfig* video_config);
  77. // Callback for ANSWER response. If the ANSWER is invalid, |observer_| will
  78. // get notified with error, and session is stopped. Otherwise, capturing and
  79. // streaming are started with the selected configs.
  80. void OnAnswer(
  81. const std::vector<media::cast::FrameSenderConfig>& audio_configs,
  82. const std::vector<media::cast::FrameSenderConfig>& video_configs,
  83. const ReceiverResponse& response);
  84. // Called by |message_dispatcher_| when error occurs while parsing the
  85. // responses.
  86. void OnResponseParsingError(const std::string& error_message);
  87. // Creates an audio input stream through Audio Service. |client| will be
  88. // called after the stream is created.
  89. void CreateAudioStream(
  90. mojo::PendingRemote<mojom::AudioStreamCreatorClient> client,
  91. const media::AudioParameters& params,
  92. uint32_t shared_memory_count);
  93. // Callback for CAPABILITIES_RESPONSE.
  94. void OnCapabilitiesResponse(const ReceiverResponse& response);
  95. private:
  96. class AudioCapturingCallback;
  97. using SupportedProfiles = media::VideoEncodeAccelerator::SupportedProfiles;
  98. // MediaRemoter::Client implementation.
  99. void ConnectToRemotingSource(
  100. mojo::PendingRemote<media::mojom::Remoter> remoter,
  101. mojo::PendingReceiver<media::mojom::RemotingSource> source_receiver)
  102. override;
  103. void RequestRemotingStreaming() override;
  104. void RestartMirroringStreaming() override;
  105. // Stops the current streaming session. If not called from StopSession(), a
  106. // new streaming session will start later after exchanging OFFER/ANSWER
  107. // messages with the receiver. This could happen any number of times before
  108. // StopSession() shuts down everything permanently.
  109. void StopStreaming();
  110. void StopSession(); // Shuts down the entire mirroring session.
  111. // Notify |observer_| that error occurred and close the session.
  112. void ReportError(mojom::SessionError error);
  113. // Send logging messages to |observer_|.
  114. void LogInfoMessage(const std::string& message);
  115. void LogErrorMessage(const std::string& message);
  116. // Callback by Audio/VideoSender to indicate encoder status change.
  117. void OnEncoderStatusChange(media::cast::OperationalStatus status);
  118. // Callback by media::cast::VideoSender to set a new target playout delay.
  119. void SetTargetPlayoutDelay(base::TimeDelta playout_delay);
  120. // Callback by media::cast::VideoSender to report resource utilization.
  121. void ProcessFeedback(const media::VideoCaptureFeedback& feedback);
  122. // Create and send OFFER message.
  123. void CreateAndSendOffer();
  124. // Send GET_CAPABILITIES message.
  125. void QueryCapabilitiesForRemoting();
  126. void OnAsyncInitializeDone(const SupportedProfiles& profiles);
  127. // Provided by client.
  128. const mojom::SessionParameters session_params_;
  129. // State transition:
  130. // INITIALIZING
  131. // |
  132. // \./
  133. // MIRRORING <-------> REMOTING
  134. // | |
  135. // .---> STOPPED <----.
  136. enum {
  137. INITIALIZING, // The session is initializing, and can't be used yet.
  138. MIRRORING, // A mirroring streaming session is starting or started.
  139. REMOTING, // A remoting streaming session is starting or started.
  140. STOPPED, // The session is stopped due to user's request or errors.
  141. } state_ = INITIALIZING;
  142. mojo::Remote<mojom::SessionObserver> observer_;
  143. mojo::Remote<mojom::ResourceProvider> resource_provider_;
  144. MirrorSettings mirror_settings_;
  145. std::unique_ptr<MessageDispatcher> message_dispatcher_;
  146. mojo::Remote<network::mojom::NetworkContext> network_context_;
  147. std::unique_ptr<ReceiverSetupQuerier> setup_querier_;
  148. // Created after OFFER/ANSWER exchange succeeds.
  149. std::unique_ptr<AudioRtpStream> audio_stream_;
  150. std::unique_ptr<VideoRtpStream> video_stream_;
  151. std::unique_ptr<VideoCaptureClient> video_capture_client_;
  152. scoped_refptr<media::cast::CastEnvironment> cast_environment_;
  153. std::unique_ptr<media::cast::CastTransport> cast_transport_;
  154. scoped_refptr<base::SingleThreadTaskRunner> audio_encode_thread_;
  155. scoped_refptr<base::SingleThreadTaskRunner> video_encode_thread_;
  156. std::unique_ptr<AudioCapturingCallback> audio_capturing_callback_;
  157. scoped_refptr<media::AudioInputDevice> audio_input_device_;
  158. std::unique_ptr<MediaRemoter> media_remoter_;
  159. std::unique_ptr<viz::Gpu> gpu_;
  160. SupportedProfiles supported_profiles_;
  161. mojo::Remote<media::mojom::VideoEncodeAcceleratorProvider> vea_provider_;
  162. // A callback to call after initialization is completed
  163. AsyncInitializeDoneCB init_done_cb_;
  164. base::WeakPtrFactory<Session> weak_factory_{this};
  165. };
  166. } // namespace mirroring
  167. #endif // COMPONENTS_MIRRORING_SERVICE_SESSION_H_