OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | |
6 #define CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | |
7 | |
8 #include <set> | |
9 | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "chrome/browser/media/router/media_routes_observer.h" | |
12 #include "chrome/browser/media/router/route_message.h" | |
13 #include "content/public/browser/web_contents_user_data.h" | |
14 #include "media/mojo/interfaces/remoting.mojom.h" | |
15 | |
16 namespace content { | |
17 class RenderFrameHost; | |
18 } | |
19 | |
20 // CastRemotingConnector connects a single source (a media element in a render | |
21 // frame) with a single sink (a media player in a remote device). There is one | |
22 // instance of a CastRemotingConnector per source WebContents (representing a | |
23 // collection of render frames), and it is created on-demand. The source in the | |
24 // render process represents itself by providing a media::mojom::RemotingSource | |
25 // service instance. The sink is represented by the Media Router Cast Provider. | |
26 // | |
27 // Whenever a RenderFrameHost is created, ChromeContentBrowserClient will call | |
28 // CreateRemoterFactory() to instantiate an implementation of | |
29 // media::mojom::RemoterFactory associated with the connector. This allows one | |
30 // or more media sources in a render frame to create a media::mojom::Remoter to | |
31 // start/stop and execute media remoting. Media sources also provide a | |
32 // media::mojom::RemotingSource so that the connector may notify them when a | |
33 // sink becomes available for remoting and pass binary messages from the sink | |
34 // back to the source. | |
35 // | |
36 // At any time before or after the CastRemotingConnector is created, the | |
37 // Media Router Cast Provider may create a tab remoting media route. This | |
38 // indicates that the provider has found a sink that is capable of remoting and | |
39 // is available for use. At this point, CastRemotingConnector notifies all | |
40 // RemotingSources that a sink is available, and some time later a | |
41 // RemotingSource can request the start of a remoting session. Once the sink is | |
42 // no longer available, the provider terminates the route and | |
43 // CastRemotingConnector notifies all RemotingSources that the sink is gone. | |
44 // | |
45 // Note that only one RemotingSource can remote media at a time. Therefore, | |
46 // CastRemotingConnector must mitigate among simultaneous requests for media | |
dcheng
2016/09/20 08:20:12
mitigate => mediate?
miu
2016/09/21 03:15:50
Done.
| |
47 // remoting, and only allow one at a time. Currently, the policy is "first come, | |
48 // first served." | |
49 // | |
50 // When starting a remoting session, the CastRemotingConnector sends control | |
51 // messages to the Cast Provider. These control messages direct the provider to | |
52 // start/stop remoting with the remote device (e.g., launching apps or services | |
53 // on the remote device). Among its other duties, the provider will also set up | |
54 // a Cast Streaming session to provide a bitstream transport for the media. Once | |
55 // this is done, the provider sends notification messages back to | |
56 // CastRemotingConnector to acknowledge this. Then, CastRemotingConnector knows | |
57 // it can look-up and pass the mojo data pipe handles to CastRemotingSenders, | |
dcheng
2016/09/20 08:20:12
CastRemotingSender? presumably there's only one, s
miu
2016/09/21 03:15:50
There is one source consisting of one audio track
| |
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 // | |
62 // Please see the unit tests in cast_remoting_connector_unittest.cc as a | |
63 // reference for how CastRemotingConnector and a Cast Provider interact to | |
64 // start/execute/stop remoting sessions. | |
65 class CastRemotingConnector | |
66 : protected content::WebContentsUserData<CastRemotingConnector>, | |
dcheng
2016/09/20 08:20:12
All inheritance should be public: https://google.g
miu
2016/09/21 03:15:50
Done. Changed to inherit from base::SupportsUserDa
| |
67 public media_router::MediaRoutesObserver { | |
68 public: | |
69 // Returns the instance of the CastRemotingConnector associated with | |
70 // |source_contents|, creating a new instance if needed. | |
71 static CastRemotingConnector* Get(content::WebContents* source_contents); | |
72 | |
73 // Used by ChromeContentBrowserClient to request a binding to a new | |
74 // RemoterFactory for each new render frame. The |render_frame_host| is | |
75 // expected to outlive the mojo message pipe in |request| that the service | |
76 // binds to. | |
77 static void CreateRemoterFactory(content::RenderFrameHost* render_frame_host, | |
78 media::mojom::RemoterFactoryRequest request); | |
79 | |
80 protected: | |
81 friend class content::WebContentsUserData<CastRemotingConnector>; | |
82 | |
83 // Constructor used only by WebContentsUserData. | |
84 explicit CastRemotingConnector(content::WebContents* source_contents); | |
85 | |
86 ~CastRemotingConnector() final; | |
87 | |
88 private: | |
89 // Allow unit tests access to the private constructor and CreateBridge() | |
90 // method, since unit tests don't have a complete browser (i.e., with a | |
91 // WebContents and RenderFrameHost) to work with. | |
dcheng
2016/09/20 08:20:12
Does TestWebContents help this? I think it'd allow
miu
2016/09/21 03:15:50
No longer an issue (no longer using WebContentsUse
| |
92 friend class CastRemotingConnectorTest; | |
93 | |
94 // Implementation of the media::mojom::RemoterFactory, with one instance | |
95 // created per content::RenderFrameHost. An instance of this class is owned by | |
96 // its mojo message pipe. | |
97 class FrameRemoterFactory; | |
98 | |
99 // Implementation of the media::mojom::Remoter service for a single source in | |
100 // a render frame. This is just a "lightweight bridge" that delegates calls | |
101 // back-and-forth between a CastRemotingConnector and a | |
102 // media::mojom::RemotingSource. An instance of this class is owned by its | |
103 // mojo message pipe. | |
104 class RemotingBridge; | |
105 | |
106 // A RouteMessageObserver for the remoting route that passes messages from the | |
107 // Cast Provider back to this connector. An instance of this class only exists | |
108 // while a remoting route is available, and is owned by CastRemotingConnector. | |
109 class MessageObserver; | |
110 | |
111 // Main constructor. |route_source_id| is the remoting route source specific | |
112 // to one tab (WebContents). | |
113 CastRemotingConnector(media_router::MediaRouter* router, | |
114 const media_router::MediaSource::Id& route_source_id); | |
115 | |
116 // Creates a RemotingBridge that implements the requested Remoter service, and | |
117 // binds it to the interface |request|. | |
118 void CreateBridge(media::mojom::RemotingSourcePtr source, | |
119 media::mojom::RemoterRequest request); | |
120 | |
121 // Called by the RemotingBridge constructor/destructor to register/deregister | |
122 // an instance. This allows this connector to broadcast notifications to all | |
123 // active sources. | |
124 void RegisterBridge(RemotingBridge* bridge); | |
125 void DeregisterBridge(RemotingBridge* bridge, | |
126 media::mojom::RemotingStopReason reason); | |
127 | |
128 // These methods are called by RemotingBridge to forward media::mojom::Remoter | |
129 // calls from a source through to this connector. They ensure that only one | |
130 // source is allowed to be in a remoting session at a time, and that no source | |
131 // may interfere with any other. | |
132 void StartRemoting(RemotingBridge* bridge); | |
133 void StartRemotingDataStreams( | |
134 RemotingBridge* bridge, | |
135 mojo::ScopedDataPipeConsumerHandle audio_pipe, | |
136 mojo::ScopedDataPipeConsumerHandle video_pipe, | |
137 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, | |
138 media::mojom::RemotingDataStreamSenderRequest video_sender_request); | |
139 void StopRemoting(RemotingBridge* bridge, | |
140 media::mojom::RemotingStopReason reason); | |
141 void SendMessageToSink(RemotingBridge* bridge, | |
142 const std::vector<uint8_t>& message); | |
143 | |
144 // Send a control message to the Cast Provider. This may or may not succeed, | |
145 // with the success status reported later via HandleSendMessageResult(). | |
146 void SendMessageToProvider(const std::string& message); | |
147 | |
148 // Called by the current MessageObserver to process messages observed on the | |
149 // remoting route. There are two types of messages: 1) TEXT notification | |
150 // messages from the Cast Provider, to report on the current state of a | |
151 // remoting session between Chrome and the remote device, and 2) BINARY | |
152 // messages, to be passed directly to the active remoting source during a | |
153 // remoting session. | |
154 void ProcessMessagesFromRoute( | |
155 const std::vector<media_router::RouteMessage>& messages); | |
156 | |
157 // Error handlers for message/data sending during an active remoting | |
158 // session. When a failure occurs, these immediately force-stop remoting. | |
159 void HandleSendMessageResult(bool success); | |
160 void OnDataSendFailed(); | |
161 | |
162 // MediaRoutesObserver implementation: Scans |routes| to check whether the | |
163 // existing remoting route has gone away and/or there is a new remoting route | |
164 // established, and take the necessary actions to notify sources and/or | |
165 // shutdown an active remoting session. | |
dcheng
2016/09/20 08:20:12
Nit: document |ignored| here too?
miu
2016/09/21 03:15:50
The 2nd arg is described in the interface. It is n
dcheng
2016/09/22 02:29:15
Ah I missed that this was an interface method, tha
| |
166 void OnRoutesUpdated( | |
167 const std::vector<media_router::MediaRoute>& routes, | |
168 const std::vector<media_router::MediaRoute::Id>& ignored) final; | |
169 | |
170 // Set of registered RemotingBridges, maintained by RegisterBridge() and | |
171 // DeregisterBridge(). These pointers are always valid while they are in this | |
172 // set. | |
173 std::set<RemotingBridge*> bridges_; | |
174 | |
175 // Created when the Media Router Cast Provider has created a media remoting | |
176 // route to a sink that supports remoting and is available for use. This | |
177 // observer simply dispatches messages from the Cast Provider and sink back to | |
178 // this connector. Once the route is gone, this is reset to null. | |
179 std::unique_ptr<MessageObserver> message_observer_; | |
180 | |
181 // Incremented each time StartRemoting() is called, and used as a "current | |
182 // session ID" to ensure that control messaging between this connector and the | |
183 // Cast Provider are referring to the same remoting session. This allows both | |
184 // this connector and the provider to ignore stale messages. | |
185 unsigned int session_counter_; | |
186 | |
187 // When non-null, an active remoting session is taking place, with this | |
188 // pointing to the RemotingBridge being used to communicate with the source. | |
189 RemotingBridge* active_bridge_; | |
190 | |
191 // These temporarily hold the mojo data pipe handles and interface requests | |
192 // until hand-off to the CastRemotingSenders. | |
193 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_; | |
194 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_; | |
195 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_; | |
196 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_; | |
197 | |
198 // Produces weak pointers that are only valid for the current remoting | |
199 // session. This is used to cancel any outstanding callbacks when a remoting | |
200 // session is stopped. | |
201 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; | |
202 | |
203 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); | |
204 }; | |
205 | |
206 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ | |
OLD | NEW |