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