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 |
| 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. Among its other duties, the provider must then |
| 52 // set up a Cast Streaming session to provide a bitstream transport for the |
| 53 // media. Once this is done, the provider sends a control message back to |
| 54 // CastRemotingConnector to acknowledge this. Then, CastRemotingConnector knows |
| 55 // it can look-up and pass the mojo data pipe handles to CastRemotingSenders, |
| 56 // and the remoting session will be fully active. The CastRemotingConnector is |
| 57 // responsible for passing small binary messages between the source and sink, |
| 58 // while the CastRemotingSender handles the high-volume media data transfer. |
| 59 class CastRemotingConnector |
| 60 : protected content::WebContentsUserData<CastRemotingConnector>, |
| 61 public media_router::MediaRoutesObserver { |
| 62 public: |
| 63 // Returns the instance of the CastRemotingConnector associated with |
| 64 // |source_contents|, creating a new instance if needed. |
| 65 static CastRemotingConnector* Get(content::WebContents* source_contents); |
| 66 |
| 67 // Used by ChromeContentBrowserClient to request a binding to a new |
| 68 // RemoterFactory for each new render frame. The |render_frame_host| is |
| 69 // expected to outlive the mojo message pipe in |request| that the service |
| 70 // binds to. |
| 71 static void CreateRemoterFactory(content::RenderFrameHost* render_frame_host, |
| 72 media::mojom::RemoterFactoryRequest request); |
| 73 |
| 74 protected: |
| 75 friend class content::WebContentsUserData<CastRemotingConnector>; |
| 76 |
| 77 // Constructor used only by WebContentsUserData. |
| 78 explicit CastRemotingConnector(content::WebContents* source_contents); |
| 79 |
| 80 ~CastRemotingConnector() final; |
| 81 |
| 82 private: |
| 83 // Allow unit tests access to the private constructor and CreateBridge() |
| 84 // method, since unit tests don't have a complete browser (i.e., with a |
| 85 // WebContents and RenderFrameHost) to work with. |
| 86 friend class CastRemotingConnectorTest; |
| 87 |
| 88 // Implementation of the media::mojom::RemoterFactory, with one instance |
| 89 // created per content::RenderFrameHost. An instance of this class is owned by |
| 90 // its mojo message pipe. |
| 91 class FrameRemoterFactory; |
| 92 |
| 93 // Implementation of the media::mojom::Remoter service for a single source in |
| 94 // a render frame. This is just a "lightweight bridge" that delegates calls |
| 95 // back-and-forth between a CastRemotingConnector and a |
| 96 // media::mojom::RemotingSource. An instance of this class is owned by its |
| 97 // mojo message pipe. |
| 98 class RemotingBridge; |
| 99 |
| 100 // A RouteMessageObserver for the remoting route that passes messages from the |
| 101 // Cast Provider back to this connector. An instance of this class only exists |
| 102 // while a remoting route is available, and is owned by CastRemotingConnector. |
| 103 class MessageObserver; |
| 104 |
| 105 // Main constructor. |route_source_id| is the remoting route source specific |
| 106 // to one tab (WebContents). |
| 107 CastRemotingConnector(media_router::MediaRouter* router, |
| 108 const media_router::MediaSource::Id& route_source_id); |
| 109 |
| 110 // Creates a RemotingBridge that implements the requested Remoter service, and |
| 111 // binds it to the interface |request|. |
| 112 void CreateBridge(media::mojom::RemotingSourcePtr source, |
| 113 media::mojom::RemoterRequest request); |
| 114 |
| 115 // Called by the RemotingBridge constructor/destructor to register/deregister |
| 116 // an instance. This allows this connector to broadcast notifications to all |
| 117 // active sources. |
| 118 void RegisterBridge(RemotingBridge* bridge); |
| 119 void DeregisterBridge(RemotingBridge* bridge, |
| 120 media::mojom::RemotingStopReason reason); |
| 121 |
| 122 // These methods are called by RemotingBridge to forward media::mojom::Remoter |
| 123 // calls from a source through to this connector. They ensure that only one |
| 124 // source is allowed to be in a remoting session at a time, and that no source |
| 125 // may interfere with any other. |
| 126 void StartRemoting(RemotingBridge* bridge); |
| 127 void StartRemotingDataStreams( |
| 128 RemotingBridge* bridge, |
| 129 mojo::ScopedDataPipeConsumerHandle audio_pipe, |
| 130 mojo::ScopedDataPipeConsumerHandle video_pipe, |
| 131 media::mojom::RemotingDataStreamSenderRequest audio_sender_request, |
| 132 media::mojom::RemotingDataStreamSenderRequest video_sender_request); |
| 133 void StopRemoting(RemotingBridge* bridge, |
| 134 media::mojom::RemotingStopReason reason); |
| 135 void SendMessageToSink(RemotingBridge* bridge, |
| 136 const std::vector<uint8_t>& message); |
| 137 |
| 138 // Send a control message to the Cast Provider. This may or may not succeed, |
| 139 // with the success status reported later via HandleSendMessageResult(). |
| 140 void SendMessageToProvider(const std::string& message); |
| 141 |
| 142 // Called by the current MessageObserver to process messages observed on the |
| 143 // remoting route. There are two types of messages: 1) TEXT control messages |
| 144 // from the Cast Provider to set-up and tear-down remoting sessions, and |
| 145 // 2) BINARY messages meant to be passed to the active remoting source during |
| 146 // a remoting session. |
| 147 void ProcessMessagesFromRoute( |
| 148 const std::vector<media_router::RouteMessage>& messages); |
| 149 |
| 150 // Error handlers for message/data sending during an active remoting |
| 151 // session. When a failure occurs, these immediately force-stop remoting. |
| 152 void HandleSendMessageResult(bool success); |
| 153 void OnDataSendFailed(); |
| 154 |
| 155 // MediaRoutesObserver implementation: Scans |routes| to check whether the |
| 156 // existing remoting route has gone away and/or there is a new remoting route |
| 157 // established, and take the necessary actions to notify sources and/or |
| 158 // shutdown an active remoting session. |
| 159 void OnRoutesUpdated( |
| 160 const std::vector<media_router::MediaRoute>& routes, |
| 161 const std::vector<media_router::MediaRoute::Id>& ignored) final; |
| 162 |
| 163 // Set of registered RemotingBridges, maintained by RegisterBridge() and |
| 164 // DeregisterBridge(). These pointers are always valid while they are in this |
| 165 // set. |
| 166 std::set<RemotingBridge*> bridges_; |
| 167 |
| 168 // Created when the Media Router Cast Provider has created a media remoting |
| 169 // route to a sink that supports remoting and is available for use. This |
| 170 // observer simply dispatches messages from the Cast Provider and sink back to |
| 171 // this connector. Once the route is gone, this is reset to null. |
| 172 std::unique_ptr<MessageObserver> message_observer_; |
| 173 |
| 174 // Incremented each time StartRemoting() is called, and used as a "current |
| 175 // session ID" to ensure that control messaging between this connector and the |
| 176 // Cast Provider are referring to the same remoting session. This allows both |
| 177 // this connector and the provider to ignore stale messages. |
| 178 unsigned int session_counter_; |
| 179 |
| 180 // When non-null, an active remoting session is taking place, with this |
| 181 // pointing to the RemotingBridge being used to communicate with the source. |
| 182 RemotingBridge* active_bridge_; |
| 183 |
| 184 // These temporarily hold the mojo data pipe handles and interface requests |
| 185 // until hand-off to the CastRemotingSenders. |
| 186 mojo::ScopedDataPipeConsumerHandle pending_audio_pipe_; |
| 187 mojo::ScopedDataPipeConsumerHandle pending_video_pipe_; |
| 188 media::mojom::RemotingDataStreamSenderRequest pending_audio_sender_request_; |
| 189 media::mojom::RemotingDataStreamSenderRequest pending_video_sender_request_; |
| 190 |
| 191 // Produces weak pointers that are only valid for the current remoting |
| 192 // session. This is used to cancel any outstanding callbacks when a remoting |
| 193 // session is stopped. |
| 194 base::WeakPtrFactory<CastRemotingConnector> weak_factory_; |
| 195 |
| 196 DISALLOW_COPY_AND_ASSIGN(CastRemotingConnector); |
| 197 }; |
| 198 |
| 199 #endif // CHROME_BROWSER_MEDIA_CAST_REMOTING_CONNECTOR_H_ |
OLD | NEW |