Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ |
| 6 #define CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | 6 #define CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/supports_user_data.h" | 11 #include "base/supports_user_data.h" |
| 12 #include "chrome/browser/media/router/media_routes_observer.h" | 12 #include "media/mojo/interfaces/mirror_service_remoting.mojom.h" |
| 13 #include "chrome/common/media_router/route_message.h" | |
| 14 #include "media/mojo/interfaces/remoting.mojom.h" | 13 #include "media/mojo/interfaces/remoting.mojom.h" |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 class RenderFrameHost; | 17 class RenderFrameHost; |
| 18 class WebContents; | 18 class WebContents; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace media_router { | |
| 22 class MediaRouter; | |
| 23 } | |
| 24 | |
| 21 // CastRemotingConnector connects a single source (a media element in a render | 25 // CastRemotingConnector connects a single source (a media element in a render |
| 22 // frame) with a single sink (a media player in a remote device). There is one | 26 // frame) with a single sink (a media player in a remote device). There is one |
| 23 // instance of a CastRemotingConnector per source WebContents (representing a | 27 // instance of a CastRemotingConnector per source WebContents (representing a |
| 24 // collection of render frames), and it is created on-demand. The source in the | 28 // collection of render frames), and it is created on-demand. The source in the |
| 25 // render process represents itself by providing a media::mojom::RemotingSource | 29 // render process represents itself by providing a media::mojom::RemotingSource |
| 26 // service instance. The sink is represented by the Media Router Cast Provider. | 30 // service instance. The sink is represented by a MediaRemoter in Media Route |
|
imcheng
2017/06/22 01:13:27
The MR extension is desktop-specific. How about "
xjz
2017/06/23 19:02:41
Done.
| |
| 31 // extension, which handles the communication with the remote device. The | |
| 32 // CastRemotingConnector and the MediaRemoter are connected when a sink that is | |
| 33 // capable of remoting is available. Once connected, they can communicate with | |
| 34 // each other through the media::mojom::MirrorServiceRemoter and | |
| 35 // media::mojom::MirrorServiceRemotingSource interfaces. | |
| 27 // | 36 // |
| 28 // Whenever a candidate media source is created in a render frame, | 37 // Whenever a candidate media source is created in a render frame, |
| 29 // ChromeContentBrowserClient will call CreateMediaRemoter() to instantiate a | 38 // ChromeContentBrowserClient will call CreateMediaRemoter() to instantiate a |
| 30 // media::mojom::Remoter associated with the connector. A corresponding | 39 // media::mojom::Remoter associated with the connector. A corresponding |
| 31 // media::mojom::RemotingSource is provided by the caller for communications | 40 // media::mojom::RemotingSource is provided by the caller for communications |
| 32 // back to the media source in the render frame: The connector uses this to | 41 // back to the media source in the render frame: The connector uses this to |
| 33 // notify when a sink becomes available for remoting, and to pass binary | 42 // notify when a sink becomes available for remoting, and to pass binary |
| 34 // messages from the sink back to the source. | 43 // messages from the sink back to the source. |
| 35 // | 44 // |
| 36 // At any time before or after the CastRemotingConnector is created, the | 45 // When the CastRemotingConnector is created, it will register itself in the |
| 37 // Media Router Cast Provider may create a tab remoting media route. This | 46 // media_router::MediaRouterMojoImpl with the tab ID. When a mirroring route is |
|
imcheng
2017/06/22 01:13:27
s/MojoImpl//
xjz
2017/06/23 19:02:41
Done.
| |
| 38 // indicates that the provider has found a sink that is capable of remoting and | 47 // created and avilable for remoting, a MediaRemoter is created and started in |
| 39 // is available for use. At this point, CastRemotingConnector notifies all | 48 // the extension, and it notifies the media_router::MediaRouterMojoImpl through |
|
imcheng
2017/06/22 01:13:27
ditto for "extension". How about something along t
xjz
2017/06/23 19:02:41
Done.
| |
| 40 // RemotingSources that a sink is available, and some time later a | 49 // the MediaRouter mojo interface, and connects to the CastRemotingConnector |
| 41 // RemotingSource can request the start of a remoting session. Once the sink is | 50 // with the same source tab Id. At this point, the CastRemotingConnector can |
| 42 // no longer available, the provider terminates the route and | 51 // compunicate with the MediaRemoter. When CastRemotingConnector gets notified |
|
imcheng
2017/06/22 01:13:27
typo in "communicate"
xjz
2017/06/23 19:02:41
Done.
| |
| 43 // CastRemotingConnector notifies all RemotingSources that the sink is gone. | 52 // that a sink is available, it notifies all RemotingSources, and some time |
| 53 // later a RemotingSource can request the start of a remoting session. | |
| 44 // | 54 // |
| 45 // Note that only one RemotingSource can remote media at a time. Therefore, | 55 // Note that only one RemotingSource can remote media at a time. Therefore, |
| 46 // CastRemotingConnector must mediate among simultaneous requests for media | 56 // CastRemotingConnector must mediate among simultaneous requests for media |
| 47 // remoting, and only allow one at a time. Currently, the policy is "first come, | 57 // remoting, and only allow one at a time. Currently, the policy is "first come, |
| 48 // first served." | 58 // first served." |
| 49 // | 59 // |
| 50 // When starting a remoting session, the CastRemotingConnector sends control | 60 // When starting a remoting session, the MediaRemoter will also set up a Cast |
| 51 // messages to the Cast Provider. These control messages direct the provider to | 61 // Streaming session to provide a bitstream transport for the media. Once this |
| 52 // start/stop remoting with the remote device (e.g., launching apps or services | 62 // is done, the MediaRemoter notifies the CastRemotingConnector. Then, |
| 53 // on the remote device). Among its other duties, the provider will also set up | 63 // CastRemotingConnector knows it can look-up and pass the mojo data pipe |
| 54 // a Cast Streaming session to provide a bitstream transport for the media. Once | 64 // handles to CastRemotingSenders, and the remoting session will be fully active |
| 55 // this is done, the provider sends notification messages back to | 65 // The CastRemotingConnector is responsible for passing small binary messages |
| 56 // CastRemotingConnector to acknowledge this. Then, CastRemotingConnector knows | 66 // between the source and sink, while the CastRemotingSender handles the |
| 57 // it can look-up and pass the mojo data pipe handles to CastRemotingSenders, | 67 // high-volume media data transfer. |
| 58 // and the remoting session will be fully active. The CastRemotingConnector is | |
| 59 // responsible for passing small binary messages between the source and sink, | |
| 60 // while the CastRemotingSender handles the high-volume media data transfer. | |
| 61 // | 68 // |
| 62 // Please see the unit tests in cast_remoting_connector_unittest.cc as a | 69 // Please see the unit tests in cast_remoting_connector_unittest.cc as a |
| 63 // reference for how CastRemotingConnector and a Cast Provider interact to | 70 // reference for how CastRemotingConnector and a MediaRemoter interact to |
| 64 // start/execute/stop remoting sessions. | 71 // start/execute/stop remoting sessions. |
| 65 class CastRemotingConnector | 72 class CastRemotingConnector : public base::SupportsUserData::Data, |
|
imcheng
2017/06/22 01:13:27
Was it a deliberate decision to not put this class
xjz
2017/06/23 19:02:41
It sgtm to put this class under the cast namespace
| |
| 66 : public base::SupportsUserData::Data, | 73 public media::mojom::MirrorServiceRemotingSource { |
| 67 public media_router::MediaRoutesObserver { | |
| 68 public: | 74 public: |
| 69 ~CastRemotingConnector() final; | 75 ~CastRemotingConnector() final; |
| 70 | 76 |
| 71 // Returns the instance of the CastRemotingConnector associated with | 77 // Returns the instance of the CastRemotingConnector associated with |
| 72 // |source_contents|, creating a new instance if needed. | 78 // |source_contents|, creating a new instance if needed. |
| 73 static CastRemotingConnector* Get(content::WebContents* source_contents); | 79 static CastRemotingConnector* Get(content::WebContents* source_contents); |
| 74 | 80 |
| 75 // Used by ChromeContentBrowserClient to request a binding to a new | 81 // Used by ChromeContentBrowserClient to request a binding to a new |
| 76 // Remoter for each new source in a render frame. | 82 // Remoter for each new source in a render frame. |
| 77 static void CreateMediaRemoter(content::RenderFrameHost* render_frame_host, | 83 static void CreateMediaRemoter(content::RenderFrameHost* render_frame_host, |
| 78 media::mojom::RemotingSourcePtr source, | 84 media::mojom::RemotingSourcePtr source, |
| 79 media::mojom::RemoterRequest request); | 85 media::mojom::RemoterRequest request); |
| 80 | 86 |
| 87 // Called when a MediaRemoter is created and started in extension. This call | |
| 88 // connects the CastRemotingConnector with the MediaRemoter in extension. | |
| 89 // Remoting sessions can only be started after this is called. | |
| 90 void ConnectToService( | |
| 91 media::mojom::MirrorServiceRemotingSourceRequest source_request, | |
| 92 media::mojom::MirrorServiceRemoterPtr remoter); | |
| 93 | |
| 81 private: | 94 private: |
| 82 // Allow unit tests access to the private constructor and CreateBridge() | 95 // Allow unit tests access to the private constructor and CreateBridge() |
| 83 // method, since unit tests don't have a complete browser (i.e., with a | 96 // method, since unit tests don't have a complete browser (i.e., with a |
| 84 // WebContents and RenderFrameHost) to work with. | 97 // WebContents and RenderFrameHost) to work with. |
| 85 friend class CastRemotingConnectorTest; | 98 friend class CastRemotingConnectorTest; |
| 86 | 99 |
| 87 // Implementation of the media::mojom::Remoter service for a single source in | 100 // Implementation of the media::mojom::Remoter service for a single source in |
| 88 // a render frame. This is just a "lightweight bridge" that delegates calls | 101 // a render frame. This is just a "lightweight bridge" that delegates calls |
| 89 // back-and-forth between a CastRemotingConnector and a | 102 // back-and-forth between a CastRemotingConnector and a |
| 90 // media::mojom::RemotingSource. An instance of this class is owned by its | 103 // media::mojom::RemotingSource. An instance of this class is owned by its |
| 91 // mojo message pipe. | 104 // mojo message pipe. |
| 92 class RemotingBridge; | 105 class RemotingBridge; |
| 93 | 106 |
| 94 // A RouteMessageObserver for the remoting route that passes messages from the | 107 // Main constructor. |tab_id| refers to any remoted content managed |
| 95 // Cast Provider back to this connector. An instance of this class only exists | |
| 96 // while a remoting route is available, and is owned by CastRemotingConnector. | |
| 97 class MessageObserver; | |
| 98 | |
| 99 // Main constructor. |media_source_id| refers to any remoted content managed | |
| 100 // by this instance (i.e., any remoted content from one tab/WebContents). | 108 // by this instance (i.e., any remoted content from one tab/WebContents). |
| 101 CastRemotingConnector(media_router::MediaRouter* router, | 109 CastRemotingConnector(media_router::MediaRouter* router, int32_t tab_id); |
| 102 const media_router::MediaSource::Id& media_source_id); | |
| 103 | 110 |
| 104 // Creates a RemotingBridge that implements the requested Remoter service, and | 111 // Creates a RemotingBridge that implements the requested Remoter service, and |
| 105 // binds it to the interface |request|. | 112 // binds it to the interface |request|. |
| 106 void CreateBridge(media::mojom::RemotingSourcePtr source, | 113 void CreateBridge(media::mojom::RemotingSourcePtr source, |
| 107 media::mojom::RemoterRequest request); | 114 media::mojom::RemoterRequest request); |
| 108 | 115 |
| 109 // Called by the RemotingBridge constructor/destructor to register/deregister | 116 // Called by the RemotingBridge constructor/destructor to register/deregister |
| 110 // an instance. This allows this connector to broadcast notifications to all | 117 // an instance. This allows this connector to broadcast notifications to all |
| 111 // active sources. | 118 // active sources. |
| 112 void RegisterBridge(RemotingBridge* bridge); | 119 void RegisterBridge(RemotingBridge* bridge); |
| 113 void DeregisterBridge(RemotingBridge* bridge, | 120 void DeregisterBridge(RemotingBridge* bridge, |
| 114 media::mojom::RemotingStopReason reason); | 121 media::mojom::RemotingStopReason reason); |
| 115 | 122 |
| 123 // media::mojom::MirrorServiceRemotingSource implementations. | |
| 124 void OnSinkAvailable( | |
| 125 media::mojom::RemotingSinkCapabilities capabilities) override; | |
| 126 void OnStarted() override; | |
| 127 void OnStartFailed(media::mojom::RemotingStartFailReason reason) override; | |
| 128 void OnMessageFromSink(const std::vector<uint8_t>& message) override; | |
| 129 void OnStopped(media::mojom::RemotingStopReason reason) override; | |
| 130 void OnDataStreamsStarted(int32_t audio_stream_id, | |
| 131 int32_t video_stream_id) override; | |
| 132 void OnError() override; | |
| 133 | |
| 116 // These methods are called by RemotingBridge to forward media::mojom::Remoter | 134 // These methods are called by RemotingBridge to forward media::mojom::Remoter |
| 117 // calls from a source through to this connector. They ensure that only one | 135 // calls from a source through to this connector. They ensure that only one |
| 118 // source is allowed to be in a remoting session at a time, and that no source | 136 // source is allowed to be in a remoting session at a time, and that no source |
| 119 // may interfere with any other. | 137 // may interfere with any other. |
| 120 void StartRemoting(RemotingBridge* bridge); | 138 void StartRemoting(RemotingBridge* bridge); |
| 121 void StartRemotingDataStreams( | 139 void StartRemotingDataStreams( |
| 122 RemotingBridge* bridge, | 140 RemotingBridge* bridge, |
| 123 mojo::ScopedDataPipeConsumerHandle audio_pipe, | 141 mojo::ScopedDataPipeConsumerHandle audio_pipe, |
| 124 mojo::ScopedDataPipeConsumerHandle video_pipe, | 142 mojo::ScopedDataPipeConsumerHandle video_pipe, |
| 125 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, | 143 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, |
| 126 media::mojom::RemotingDataStreamSenderRequest video_sender_request); | 144 media::mojom::RemotingDataStreamSenderRequest video_sender_request); |
| 127 void StopRemoting(RemotingBridge* bridge, | 145 void StopRemoting(RemotingBridge* bridge, |
| 128 media::mojom::RemotingStopReason reason); | 146 media::mojom::RemotingStopReason reason); |
| 129 void SendMessageToSink(RemotingBridge* bridge, | 147 void SendMessageToSink(RemotingBridge* bridge, |
| 130 const std::vector<uint8_t>& message); | 148 const std::vector<uint8_t>& message); |
| 131 | 149 |
| 132 // Send a control message to the Cast Provider. This may or may not succeed, | 150 // Error handlers for message sending during an active remoting session. When |
| 133 // with the success status reported later via HandleSendMessageResult(). | 151 // a failure occurs, these immediately force-stop remoting. |
| 134 void SendMessageToProvider(const std::string& message); | |
| 135 | |
| 136 // Called by the current MessageObserver to process messages observed on the | |
| 137 // remoting route. There are two types of messages: 1) TEXT notification | |
| 138 // messages from the Cast Provider, to report on the current state of a | |
| 139 // remoting session between Chrome and the remote device, and 2) BINARY | |
| 140 // messages, to be passed directly to the active remoting source during a | |
| 141 // remoting session. | |
| 142 void ProcessMessagesFromRoute( | |
| 143 const std::vector<media_router::RouteMessage>& messages); | |
| 144 | |
| 145 // Error handlers for message/data sending during an active remoting | |
| 146 // session. When a failure occurs, these immediately force-stop remoting. | |
| 147 void HandleSendMessageResult(bool success); | |
| 148 void OnDataSendFailed(); | 152 void OnDataSendFailed(); |
| 149 | 153 |
| 150 // MediaRoutesObserver implementation: Scans |routes| to check whether the | 154 // Called when any connection error/lost occurs with the MediaRemoter. |
| 151 // existing remoting route has gone away and/or there is a new remoting route | 155 void OnMirrorServiceStopped(); |
| 152 // established, and take the necessary actions to notify sources and/or | |
| 153 // shutdown an active remoting session. | |
| 154 void OnRoutesUpdated( | |
| 155 const std::vector<media_router::MediaRoute>& routes, | |
| 156 const std::vector<media_router::MediaRoute::Id>& ignored) final; | |
| 157 | 156 |
| 158 // The MediaSource ID referring to any remoted content managed by this | 157 media_router::MediaRouter* const media_router_; |
| 159 // CastRemotingConnector. | 158 |
| 160 const media_router::MediaSource::Id media_source_id_; | 159 const int32_t tab_id_; |
| 161 | 160 |
| 162 // Describes the sink's capabilities according to what has been enabled via | 161 // Describes the sink's capabilities according to what has been enabled via |
| 163 // |features::kMediaRemoting|. These are controlled manually via | 162 // |features::kMediaRemoting|. These are controlled manually via |
| 164 // chrome://flags or the command line; or in-the-wild via feature experiments. | 163 // chrome://flags or the command line; or in-the-wild via feature experiments. |
| 165 const media::mojom::RemotingSinkCapabilities enabled_features_; | 164 const media::mojom::RemotingSinkCapabilities enabled_features_; |
| 166 | 165 |
| 167 // Set of registered RemotingBridges, maintained by RegisterBridge() and | 166 // Set of registered RemotingBridges, maintained by RegisterBridge() and |
| 168 // DeregisterBridge(). These pointers are always valid while they are in this | 167 // DeregisterBridge(). These pointers are always valid while they are in this |
| 169 // set. | 168 // set. |
| 170 std::set<RemotingBridge*> bridges_; | 169 std::set<RemotingBridge*> bridges_; |
| 171 | 170 |
| 172 // Created when the Media Router Cast Provider has created a media remoting | |
| 173 // route to a sink that supports remoting and is available for use. This | |
| 174 // observer simply dispatches messages from the Cast Provider and sink back to | |
| 175 // this connector. Once the route is gone, this is reset to null. | |
| 176 std::unique_ptr<MessageObserver> message_observer_; | |
| 177 | |
| 178 // Incremented each time StartRemoting() is called, and used as a "current | |
| 179 // session ID" to ensure that control messaging between this connector and the | |
| 180 // Cast Provider are referring to the same remoting session. This allows both | |
| 181 // this connector and the provider to ignore stale messages. | |
| 182 unsigned int session_counter_; | |
| 183 | |
| 184 // When non-null, an active remoting session is taking place, with this | 171 // When non-null, an active remoting session is taking place, with this |
| 185 // pointing to the RemotingBridge being used to communicate with the source. | 172 // pointing to the RemotingBridge being used to communicate with the source. |
| 186 RemotingBridge* active_bridge_; | 173 RemotingBridge* active_bridge_; |
| 187 | 174 |
| 188 // These temporarily hold the mojo data pipe handles and interface requests | 175 // These temporarily hold the mojo data pipe handles and interface requests |
| 189 // until hand-off to the CastRemotingSenders. | 176 // until hand-off to the CastRemotingSenders. |
| 190 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_; | 177 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_; |
| 191 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_; | 178 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_; |
| 192 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_; | 179 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_; |
| 193 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_; | 180 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_; |
| 194 | 181 |
| 182 mojo::Binding<media::mojom::MirrorServiceRemotingSource> binding_; | |
| 183 media::mojom::MirrorServiceRemoterPtr remoter_; | |
| 184 | |
| 195 // Produces weak pointers that are only valid for the current remoting | 185 // Produces weak pointers that are only valid for the current remoting |
| 196 // session. This is used to cancel any outstanding callbacks when a remoting | 186 // session. This is used to cancel any outstanding callbacks when a remoting |
| 197 // session is stopped. | 187 // session is stopped. |
| 198 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; | 188 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; |
| 199 | 189 |
| 200 // Key used with the base::SupportsUserData interface to search for an | 190 // Key used with the base::SupportsUserData interface to search for an |
| 201 // instance of CastRemotingConnector owned by a WebContents. | 191 // instance of CastRemotingConnector owned by a WebContents. |
| 202 static const void* const kUserDataKey; | 192 static const void* const kUserDataKey; |
| 203 | 193 |
| 204 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); | 194 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); |
| 205 }; | 195 }; |
| 206 | 196 |
| 207 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | 197 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ |
| OLD | NEW |