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/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 the Cast Media |
| 31 // Router Provider that handles the communication with the remote device. The |
| 32 // CastRemotingConnector and the MediaRemoter can communicate with each other |
| 33 // through the media::mojom::MirrorServiceRemoter and |
| 34 // media::mojom::MirrorServiceRemotingSource interfaces when a sink that is |
| 35 // capable of remoting is available. |
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 registers itself in the |
37 // Media Router Cast Provider may create a tab remoting media route. This | 46 // media_router::MediaRouter with a tab ID that uniquely identifies it. When a |
38 // indicates that the provider has found a sink that is capable of remoting and | 47 // mirroring route is created and available for remoting, the Cast MRP will |
39 // is available for use. At this point, CastRemotingConnector notifies all | 48 // create a MediaRemoter and notify MediaRouter, which notifies the |
40 // RemotingSources that a sink is available, and some time later a | 49 // CastRemotingConnector registered under the tab ID being remoted. At this |
41 // RemotingSource can request the start of a remoting session. Once the sink is | 50 // point, the CastRemotingConnector can communicate with the MediaRemoter. When |
42 // no longer available, the provider terminates the route and | 51 // CastRemotingConnector gets notified that a sink is available, it notifies all |
43 // CastRemotingConnector notifies all RemotingSources that the sink is gone. | 52 // RemotingSources, and some time later a RemotingSource can request the start |
| 53 // 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 Cast MRP 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, |
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. Returns nullptr if |
| 79 // |source_contents| doesn't have a valid tab ID. |
73 static CastRemotingConnector* Get(content::WebContents* source_contents); | 80 static CastRemotingConnector* Get(content::WebContents* source_contents); |
74 | 81 |
75 // Used by ChromeContentBrowserClient to request a binding to a new | 82 // Used by ChromeContentBrowserClient to request a binding to a new |
76 // Remoter for each new source in a render frame. | 83 // Remoter for each new source in a render frame. |
77 static void CreateMediaRemoter(content::RenderFrameHost* render_frame_host, | 84 static void CreateMediaRemoter(content::RenderFrameHost* render_frame_host, |
78 media::mojom::RemotingSourcePtr source, | 85 media::mojom::RemotingSourcePtr source, |
79 media::mojom::RemoterRequest request); | 86 media::mojom::RemoterRequest request); |
80 | 87 |
| 88 // Called when a MediaRemoter is created and started in the Cast MRP. This |
| 89 // call connects the CastRemotingConnector with the MediaRemoter. Remoting |
| 90 // sessions can only be started after this is called. |
| 91 void ConnectToService( |
| 92 media::mojom::MirrorServiceRemotingSourceRequest source_request, |
| 93 media::mojom::MirrorServiceRemoterPtr remoter); |
| 94 |
81 private: | 95 private: |
82 // Allow unit tests access to the private constructor and CreateBridge() | 96 // 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 | 97 // method, since unit tests don't have a complete browser (i.e., with a |
84 // WebContents and RenderFrameHost) to work with. | 98 // WebContents and RenderFrameHost) to work with. |
85 friend class CastRemotingConnectorTest; | 99 friend class CastRemotingConnectorTest; |
86 | 100 |
87 // Implementation of the media::mojom::Remoter service for a single source in | 101 // 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 | 102 // a render frame. This is just a "lightweight bridge" that delegates calls |
89 // back-and-forth between a CastRemotingConnector and a | 103 // back-and-forth between a CastRemotingConnector and a |
90 // media::mojom::RemotingSource. An instance of this class is owned by its | 104 // media::mojom::RemotingSource. An instance of this class is owned by its |
91 // mojo message pipe. | 105 // mojo message pipe. |
92 class RemotingBridge; | 106 class RemotingBridge; |
93 | 107 |
94 // A RouteMessageObserver for the remoting route that passes messages from the | 108 // 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). | 109 // by this instance (i.e., any remoted content from one tab/WebContents). |
101 CastRemotingConnector(media_router::MediaRouter* router, | 110 CastRemotingConnector(media_router::MediaRouter* router, int32_t tab_id); |
102 const media_router::MediaSource::Id& media_source_id); | |
103 | 111 |
104 // Creates a RemotingBridge that implements the requested Remoter service, and | 112 // Creates a RemotingBridge that implements the requested Remoter service, and |
105 // binds it to the interface |request|. | 113 // binds it to the interface |request|. |
106 void CreateBridge(media::mojom::RemotingSourcePtr source, | 114 void CreateBridge(media::mojom::RemotingSourcePtr source, |
107 media::mojom::RemoterRequest request); | 115 media::mojom::RemoterRequest request); |
108 | 116 |
109 // Called by the RemotingBridge constructor/destructor to register/deregister | 117 // Called by the RemotingBridge constructor/destructor to register/deregister |
110 // an instance. This allows this connector to broadcast notifications to all | 118 // an instance. This allows this connector to broadcast notifications to all |
111 // active sources. | 119 // active sources. |
112 void RegisterBridge(RemotingBridge* bridge); | 120 void RegisterBridge(RemotingBridge* bridge); |
113 void DeregisterBridge(RemotingBridge* bridge, | 121 void DeregisterBridge(RemotingBridge* bridge, |
114 media::mojom::RemotingStopReason reason); | 122 media::mojom::RemotingStopReason reason); |
115 | 123 |
| 124 // media::mojom::MirrorServiceRemotingSource implementations. |
| 125 void OnSinkAvailable(media::mojom::SinkCapabilitiesPtr capabilities) override; |
| 126 void OnMessageFromSink(const std::vector<uint8_t>& message) override; |
| 127 void OnStopped(media::mojom::RemotingStopReason reason) override; |
| 128 void OnError() override; |
| 129 |
116 // These methods are called by RemotingBridge to forward media::mojom::Remoter | 130 // 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 | 131 // 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 | 132 // source is allowed to be in a remoting session at a time, and that no source |
119 // may interfere with any other. | 133 // may interfere with any other. |
120 void StartRemoting(RemotingBridge* bridge); | 134 void StartRemoting(RemotingBridge* bridge); |
121 void StartRemotingDataStreams( | 135 void StartRemotingDataStreams( |
122 RemotingBridge* bridge, | 136 RemotingBridge* bridge, |
123 mojo::ScopedDataPipeConsumerHandle audio_pipe, | 137 mojo::ScopedDataPipeConsumerHandle audio_pipe, |
124 mojo::ScopedDataPipeConsumerHandle video_pipe, | 138 mojo::ScopedDataPipeConsumerHandle video_pipe, |
125 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, | 139 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, |
126 media::mojom::RemotingDataStreamSenderRequest video_sender_request); | 140 media::mojom::RemotingDataStreamSenderRequest video_sender_request); |
127 void StopRemoting(RemotingBridge* bridge, | 141 void StopRemoting(RemotingBridge* bridge, |
128 media::mojom::RemotingStopReason reason); | 142 media::mojom::RemotingStopReason reason); |
129 void SendMessageToSink(RemotingBridge* bridge, | 143 void SendMessageToSink(RemotingBridge* bridge, |
130 const std::vector<uint8_t>& message); | 144 const std::vector<uint8_t>& message); |
131 | 145 |
132 // Send a control message to the Cast Provider. This may or may not succeed, | 146 // Called when RTP streams are started. |
133 // with the success status reported later via HandleSendMessageResult(). | 147 void OnDataStreamsStarted( |
134 void SendMessageToProvider(const std::string& message); | 148 mojo::ScopedDataPipeConsumerHandle audio_pipe, |
| 149 mojo::ScopedDataPipeConsumerHandle video_pipe, |
| 150 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, |
| 151 media::mojom::RemotingDataStreamSenderRequest video_sender_request, |
| 152 int32_t audio_stream_id, |
| 153 int32_t video_stream_id); |
135 | 154 |
136 // Called by the current MessageObserver to process messages observed on the | 155 // Error handlers for message sending during an active remoting session. When |
137 // remoting route. There are two types of messages: 1) TEXT notification | 156 // a failure occurs, these immediately force-stop remoting. |
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(); | 157 void OnDataSendFailed(); |
149 | 158 |
150 // MediaRoutesObserver implementation: Scans |routes| to check whether the | 159 // 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 | 160 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 | 161 |
158 // The MediaSource ID referring to any remoted content managed by this | 162 media_router::MediaRouter* const media_router_; |
159 // CastRemotingConnector. | 163 |
160 const media_router::MediaSource::Id media_source_id_; | 164 const int32_t tab_id_; |
161 | 165 |
162 // Describes the sink's capabilities according to what has been enabled via | 166 // Describes the sink's capabilities according to what has been enabled via |
163 // |features::kMediaRemoting|. These are controlled manually via | 167 // |features::kMediaRemoting|. These are controlled manually via |
164 // chrome://flags or the command line; or in-the-wild via feature experiments. | 168 // chrome://flags or the command line; or in-the-wild via feature experiments. |
165 const media::mojom::RemotingSinkCapabilities enabled_features_; | 169 const media::mojom::RemotingSinkCapabilities enabled_features_; |
166 | 170 |
167 // Set of registered RemotingBridges, maintained by RegisterBridge() and | 171 // Set of registered RemotingBridges, maintained by RegisterBridge() and |
168 // DeregisterBridge(). These pointers are always valid while they are in this | 172 // DeregisterBridge(). These pointers are always valid while they are in this |
169 // set. | 173 // set. |
170 std::set<RemotingBridge*> bridges_; | 174 std::set<RemotingBridge*> bridges_; |
171 | 175 |
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 | 176 // When non-null, an active remoting session is taking place, with this |
185 // pointing to the RemotingBridge being used to communicate with the source. | 177 // pointing to the RemotingBridge being used to communicate with the source. |
186 RemotingBridge* active_bridge_; | 178 RemotingBridge* active_bridge_; |
187 | 179 |
188 // These temporarily hold the mojo data pipe handles and interface requests | 180 mojo::Binding<media::mojom::MirrorServiceRemotingSource> binding_; |
189 // until hand-off to the CastRemotingSenders. | 181 media::mojom::MirrorServiceRemoterPtr remoter_; |
190 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_; | |
191 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_; | |
192 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_; | |
193 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_; | |
194 | 182 |
195 // Produces weak pointers that are only valid for the current remoting | 183 // Produces weak pointers that are only valid for the current remoting |
196 // session. This is used to cancel any outstanding callbacks when a remoting | 184 // session. This is used to cancel any outstanding callbacks when a remoting |
197 // session is stopped. | 185 // session is stopped. |
198 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; | 186 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; |
199 | 187 |
200 // Key used with the base::SupportsUserData interface to search for an | 188 // Key used with the base::SupportsUserData interface to search for an |
201 // instance of CastRemotingConnector owned by a WebContents. | 189 // instance of CastRemotingConnector owned by a WebContents. |
202 static const void* const kUserDataKey; | 190 static const void* const kUserDataKey; |
203 | 191 |
204 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); | 192 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); |
205 }; | 193 }; |
206 | 194 |
207 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | 195 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ |
OLD | NEW |