Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/media/cast_remoting_connector.h

Issue 2951523002: Media Remoting: Add mojo interfaces between browser and extension. (Closed)
Patch Set: Addressed imcheng's comments. Removed OnStarted/Failed interface. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
miu 2017/06/27 00:23:49 Looks like you meant to take this out (you're not
xjz 2017/06/28 02:09:39 Changed it to "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 Cast Media
miu 2017/06/27 00:23:49 nit: /in Cast Media/in the Cast Media/
xjz 2017/06/28 02:09:39 Done.
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 the tab ID. When a mirroring route is
miu 2017/06/27 00:23:49 nit: s/with the tab ID/with a tab ID that uniquely
xjz 2017/06/28 02:09:39 Done.
38 // indicates that the provider has found a sink that is capable of remoting and 47 // created and avilable for remoting, the Cast MRP will create a MediaRemoter
39 // is available for use. At this point, CastRemotingConnector notifies all 48 // and notify MediaRouter, which notifies the CastRemotingConnector registered
40 // RemotingSources that a sink is available, and some time later a 49 // under the tab ID being remoted. At this point, the CastRemotingConnector can
41 // RemotingSource can request the start of a remoting session. Once the sink is 50 // communicate with the MediaRemoter. When CastRemotingConnector gets notified
42 // no longer available, the provider terminates the route and 51 // that a sink is available, it notifies all RemotingSources, and some time
43 // CastRemotingConnector notifies all RemotingSources that the sink is gone. 52 // later a RemotingSource can request the start of a remoting session.
44 // 53 //
45 // Note that only one RemotingSource can remote media at a time. Therefore, 54 // Note that only one RemotingSource can remote media at a time. Therefore,
46 // CastRemotingConnector must mediate among simultaneous requests for media 55 // CastRemotingConnector must mediate among simultaneous requests for media
47 // remoting, and only allow one at a time. Currently, the policy is "first come, 56 // remoting, and only allow one at a time. Currently, the policy is "first come,
48 // first served." 57 // first served."
49 // 58 //
50 // When starting a remoting session, the CastRemotingConnector sends control 59 // 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 60 // 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 61 // is done, the MediaRemoter notifies the CastRemotingConnector. Then,
53 // on the remote device). Among its other duties, the provider will also set up 62 // 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 63 // handles to CastRemotingSenders, and the remoting session will be fully active
55 // this is done, the provider sends notification messages back to 64 // The CastRemotingConnector is responsible for passing small binary messages
56 // CastRemotingConnector to acknowledge this. Then, CastRemotingConnector knows 65 // between the source and sink, while the CastRemotingSender handles the
57 // it can look-up and pass the mojo data pipe handles to CastRemotingSenders, 66 // 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 // 67 //
62 // Please see the unit tests in cast_remoting_connector_unittest.cc as a 68 // Please see the unit tests in cast_remoting_connector_unittest.cc as a
63 // reference for how CastRemotingConnector and a Cast Provider interact to 69 // reference for how CastRemotingConnector and a MediaRemoter interact to
64 // start/execute/stop remoting sessions. 70 // start/execute/stop remoting sessions.
65 class CastRemotingConnector 71 class CastRemotingConnector : public base::SupportsUserData::Data,
66 : public base::SupportsUserData::Data, 72 public media::mojom::MirrorServiceRemotingSource {
67 public media_router::MediaRoutesObserver {
68 public: 73 public:
69 ~CastRemotingConnector() final; 74 ~CastRemotingConnector() final;
70 75
71 // Returns the instance of the CastRemotingConnector associated with 76 // Returns the instance of the CastRemotingConnector associated with
72 // |source_contents|, creating a new instance if needed. 77 // |source_contents|, creating a new instance if needed.
73 static CastRemotingConnector* Get(content::WebContents* source_contents); 78 static CastRemotingConnector* Get(content::WebContents* source_contents);
74 79
75 // Used by ChromeContentBrowserClient to request a binding to a new 80 // Used by ChromeContentBrowserClient to request a binding to a new
76 // Remoter for each new source in a render frame. 81 // Remoter for each new source in a render frame.
77 static void CreateMediaRemoter(content::RenderFrameHost* render_frame_host, 82 static void CreateMediaRemoter(content::RenderFrameHost* render_frame_host,
78 media::mojom::RemotingSourcePtr source, 83 media::mojom::RemotingSourcePtr source,
79 media::mojom::RemoterRequest request); 84 media::mojom::RemoterRequest request);
80 85
86 // Called when a MediaRemoter is created and started in Cast MRP. This call
miu 2017/06/27 00:23:49 nit: s/in Cast MRP/in the Cast MRP/
xjz 2017/06/28 02:09:39 Done.
87 // connects the CastRemotingConnector with the MediaRemoter. Remoting sessions
88 // can only be started after this is called.
89 void ConnectToService(
90 media::mojom::MirrorServiceRemotingSourceRequest source_request,
91 media::mojom::MirrorServiceRemoterPtr remoter);
92
81 private: 93 private:
82 // Allow unit tests access to the private constructor and CreateBridge() 94 // 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 95 // method, since unit tests don't have a complete browser (i.e., with a
84 // WebContents and RenderFrameHost) to work with. 96 // WebContents and RenderFrameHost) to work with.
85 friend class CastRemotingConnectorTest; 97 friend class CastRemotingConnectorTest;
86 98
87 // Implementation of the media::mojom::Remoter service for a single source in 99 // 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 100 // a render frame. This is just a "lightweight bridge" that delegates calls
89 // back-and-forth between a CastRemotingConnector and a 101 // back-and-forth between a CastRemotingConnector and a
90 // media::mojom::RemotingSource. An instance of this class is owned by its 102 // media::mojom::RemotingSource. An instance of this class is owned by its
91 // mojo message pipe. 103 // mojo message pipe.
92 class RemotingBridge; 104 class RemotingBridge;
93 105
94 // A RouteMessageObserver for the remoting route that passes messages from the 106 // 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). 107 // by this instance (i.e., any remoted content from one tab/WebContents).
101 CastRemotingConnector(media_router::MediaRouter* router, 108 CastRemotingConnector(media_router::MediaRouter* router, int32_t tab_id);
102 const media_router::MediaSource::Id& media_source_id);
103 109
104 // Creates a RemotingBridge that implements the requested Remoter service, and 110 // Creates a RemotingBridge that implements the requested Remoter service, and
105 // binds it to the interface |request|. 111 // binds it to the interface |request|.
106 void CreateBridge(media::mojom::RemotingSourcePtr source, 112 void CreateBridge(media::mojom::RemotingSourcePtr source,
107 media::mojom::RemoterRequest request); 113 media::mojom::RemoterRequest request);
108 114
109 // Called by the RemotingBridge constructor/destructor to register/deregister 115 // Called by the RemotingBridge constructor/destructor to register/deregister
110 // an instance. This allows this connector to broadcast notifications to all 116 // an instance. This allows this connector to broadcast notifications to all
111 // active sources. 117 // active sources.
112 void RegisterBridge(RemotingBridge* bridge); 118 void RegisterBridge(RemotingBridge* bridge);
113 void DeregisterBridge(RemotingBridge* bridge, 119 void DeregisterBridge(RemotingBridge* bridge,
114 media::mojom::RemotingStopReason reason); 120 media::mojom::RemotingStopReason reason);
115 121
122 // media::mojom::MirrorServiceRemotingSource implementations.
123 void OnSinkAvailable(
124 media::mojom::RemotingSinkCapabilities capabilities) override;
125 void OnMessageFromSink(const std::vector<uint8_t>& message) override;
126 void OnStopped(media::mojom::RemotingStopReason reason) override;
127 void OnDataStreamsStarted(int32_t audio_stream_id,
128 int32_t video_stream_id) override;
129 void OnError() override;
130
116 // These methods are called by RemotingBridge to forward media::mojom::Remoter 131 // 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 132 // 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 133 // source is allowed to be in a remoting session at a time, and that no source
119 // may interfere with any other. 134 // may interfere with any other.
120 void StartRemoting(RemotingBridge* bridge); 135 void StartRemoting(RemotingBridge* bridge);
121 void StartRemotingDataStreams( 136 void StartRemotingDataStreams(
122 RemotingBridge* bridge, 137 RemotingBridge* bridge,
123 mojo::ScopedDataPipeConsumerHandle audio_pipe, 138 mojo::ScopedDataPipeConsumerHandle audio_pipe,
124 mojo::ScopedDataPipeConsumerHandle video_pipe, 139 mojo::ScopedDataPipeConsumerHandle video_pipe,
125 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, 140 media::mojom::RemotingDataStreamSenderRequest audio_sender_request,
126 media::mojom::RemotingDataStreamSenderRequest video_sender_request); 141 media::mojom::RemotingDataStreamSenderRequest video_sender_request);
127 void StopRemoting(RemotingBridge* bridge, 142 void StopRemoting(RemotingBridge* bridge,
128 media::mojom::RemotingStopReason reason); 143 media::mojom::RemotingStopReason reason);
129 void SendMessageToSink(RemotingBridge* bridge, 144 void SendMessageToSink(RemotingBridge* bridge,
130 const std::vector<uint8_t>& message); 145 const std::vector<uint8_t>& message);
131 146
132 // Send a control message to the Cast Provider. This may or may not succeed, 147 // Error handlers for message sending during an active remoting session. When
133 // with the success status reported later via HandleSendMessageResult(). 148 // 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(); 149 void OnDataSendFailed();
149 150
150 // MediaRoutesObserver implementation: Scans |routes| to check whether the 151 // 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 152 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 153
158 // The MediaSource ID referring to any remoted content managed by this 154 media_router::MediaRouter* const media_router_;
159 // CastRemotingConnector. 155
160 const media_router::MediaSource::Id media_source_id_; 156 const int32_t tab_id_;
161 157
162 // Describes the sink's capabilities according to what has been enabled via 158 // Describes the sink's capabilities according to what has been enabled via
163 // |features::kMediaRemoting|. These are controlled manually via 159 // |features::kMediaRemoting|. These are controlled manually via
164 // chrome://flags or the command line; or in-the-wild via feature experiments. 160 // chrome://flags or the command line; or in-the-wild via feature experiments.
165 const media::mojom::RemotingSinkCapabilities enabled_features_; 161 const media::mojom::RemotingSinkCapabilities enabled_features_;
166 162
167 // Set of registered RemotingBridges, maintained by RegisterBridge() and 163 // Set of registered RemotingBridges, maintained by RegisterBridge() and
168 // DeregisterBridge(). These pointers are always valid while they are in this 164 // DeregisterBridge(). These pointers are always valid while they are in this
169 // set. 165 // set.
170 std::set<RemotingBridge*> bridges_; 166 std::set<RemotingBridge*> bridges_;
171 167
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 168 // When non-null, an active remoting session is taking place, with this
185 // pointing to the RemotingBridge being used to communicate with the source. 169 // pointing to the RemotingBridge being used to communicate with the source.
186 RemotingBridge* active_bridge_; 170 RemotingBridge* active_bridge_;
187 171
188 // These temporarily hold the mojo data pipe handles and interface requests 172 // These temporarily hold the mojo data pipe handles and interface requests
189 // until hand-off to the CastRemotingSenders. 173 // until hand-off to the CastRemotingSenders.
190 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_; 174 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_;
191 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_; 175 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_;
192 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_; 176 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_;
193 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_; 177 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_;
194 178
179 mojo::Binding<media::mojom::MirrorServiceRemotingSource> binding_;
180 media::mojom::MirrorServiceRemoterPtr remoter_;
181
195 // Produces weak pointers that are only valid for the current remoting 182 // Produces weak pointers that are only valid for the current remoting
196 // session. This is used to cancel any outstanding callbacks when a remoting 183 // session. This is used to cancel any outstanding callbacks when a remoting
197 // session is stopped. 184 // session is stopped.
198 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; 185 base::WeakPtrFactory<CastRemotingConnector> weak_factory_;
199 186
200 // Key used with the base::SupportsUserData interface to search for an 187 // Key used with the base::SupportsUserData interface to search for an
201 // instance of CastRemotingConnector owned by a WebContents. 188 // instance of CastRemotingConnector owned by a WebContents.
202 static const void* const kUserDataKey; 189 static const void* const kUserDataKey;
203 190
204 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); 191 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector);
205 }; 192 };
206 193
207 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ 194 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698