| 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_ROUTER_MOJO_MEDIA_REMOTING_SESSION_IMPL_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_REMOTING_SESSION_IMPL_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "chrome/browser/media/router/media_remoting_provider.h" |
| 10 #include "chrome/browser/media/router/mojo/media_remoter.mojom.h" |
| 11 #include "content/public/browser/web_contents_observer.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" |
| 13 #include "media/mojo/interfaces/remoter.mojom.h" |
| 14 |
| 15 namespace media_router { |
| 16 |
| 17 class MediaRemotingConnector; |
| 18 |
| 19 // Manages the dependencies and execution of a media remoting session. This is |
| 20 // created by a MediaRemotingProvider (mojo) in the Media Router extension after |
| 21 // a route is established and the sink is determined to provide remote media |
| 22 // services. |
| 23 // |
| 24 // Once all dependencies are satisfied, this MediaRemotingSessionImpl notifies a |
| 25 // MediaRemotingConnector that the session is ready for remoting. It is then up |
| 26 // to the connector to delegate requests from a single source to start remoting |
| 27 // and pass messages and bitstreams via this MediaRemotingSessionImpl. |
| 28 // |
| 29 // media_router::MediaRouterMojoImpl owns instances of this class. |
| 30 class MediaRemotingSessionImpl : public interfaces::MediaRemotingSession, |
| 31 public MediaRemotingProvider { |
| 32 public: |
| 33 // |connector| is used to register this MediaRemotingProvider once media |
| 34 // services become available. It must remain valid until Terminate() is called |
| 35 // (or destruction of this instance). |
| 36 // |
| 37 // |provider| is bound to a mojo instance that provides the implementation for |
| 38 // sending/receiving to/from the remote. Also, |this| is bound to the given |
| 39 // Mojo interface |request|. |
| 40 // |
| 41 // If |did_terminate_callback| is not a null Closure, it will be run one time |
| 42 // after a call to Terminate(). This is used to notify the owner of this |
| 43 // MediaRemotingSessionImpl that this instance can now be destroyed. |
| 44 MediaRemotingSessionImpl( |
| 45 MediaRemotingConnector* connector, |
| 46 interfaces::MediaRemotingProviderPtr provider, |
| 47 mojo::InterfaceRequest<interfaces::MediaRemotingSession> request, |
| 48 const base::Closure& did_terminate_callback); |
| 49 |
| 50 ~MediaRemotingSessionImpl() override; |
| 51 |
| 52 // interfaces::MediaRemotingSession implementation. |
| 53 void OnRemoteServicesAvailable( |
| 54 media::mojom::RemoteCapabilitiesPtr capabilities) final; |
| 55 void OnRemoteServicesGone() final; |
| 56 void OnBitstreamTransportAvailable( |
| 57 const mojo::String& transport_session_id) final; |
| 58 void OnBitstreamTransportGone() final; |
| 59 void OnMessageFromRemote(mojo::Array<uint8_t> message) final; |
| 60 void Terminate(const mojo::String& error_reason) final; |
| 61 |
| 62 // MediaRemotingProvider implementation. |
| 63 const media::mojom::RemoteCapabilities* GetRemoteCapabilities() const final; |
| 64 bool StartMediaServices(mojo::ScopedDataPipeConsumerHandle audio_pipe, |
| 65 mojo::ScopedDataPipeConsumerHandle video_pipe, |
| 66 const RemoteMessageCallback& message_callback) final; |
| 67 void StopMediaServices(const mojo::String& error_reason) final; |
| 68 void SendMessageToRemote(mojo::Array<uint8_t> message) final; |
| 69 void SendBufferToRemote(uint32_t pipe_id, |
| 70 media::mojom::DecoderBufferPtr buffer) final; |
| 71 |
| 72 private: |
| 73 // Called whenever something potentially affecting the availability of remote |
| 74 // media services changes. If necessary, the MediaRemotingConnection will be |
| 75 // notified, and this could cause active remoting to be stopped. |
| 76 void UpdateAvailability(); |
| 77 |
| 78 // A mojo connection error forces termination of this remoting session. |
| 79 void OnConnectionError(); |
| 80 |
| 81 // The MediaRemotingConnector to register with when remote media services |
| 82 // become available and ready, or are no longer available. The connector will |
| 83 // call this implementation's MediaRemotingProvider methods. This is set to |
| 84 // nullptr when Terminate() is called. |
| 85 MediaRemotingConnector* connector_; |
| 86 |
| 87 // Mojo bindings. These are closed by a call to Terminate(), which itself is |
| 88 // called either by the provider, when the source WebContents goes away, or |
| 89 // when a mojo message pipe connection error occurs. |
| 90 interfaces::MediaRemotingProviderPtr remoting_provider_; |
| 91 mojo::Binding<interfaces::MediaRemotingSession> binding_; |
| 92 |
| 93 // Run once, at the end of Terminate() to notify the owner that this object |
| 94 // can be destroyed. |
| 95 base::Closure did_terminate_callback_; |
| 96 |
| 97 // This is null until OnRemoteServicesAvailable() is called by the |
| 98 // provider. It is set to null once OnRemoteServicesGone() is called. |
| 99 media::mojom::RemoteCapabilitiesPtr remote_capabilities_; |
| 100 |
| 101 mojo::String transport_session_id_; // TODO(miu): This is a stub. |
| 102 |
| 103 // Set to true once all dependencies have been satisfied and the |
| 104 // MediaRemotingConnector has been notified that remoting is available. |
| 105 bool can_remote_media_; |
| 106 |
| 107 // During remoting, the content bitstream is read from these pipes (as |
| 108 // media::mojom::DecoderBuffers). |
| 109 mojo::ScopedDataPipeConsumerHandle audio_pipe_; |
| 110 mojo::ScopedDataPipeConsumerHandle video_pipe_; |
| 111 |
| 112 // During remoting, this callback is run to dispatch messages back to the |
| 113 // source. |
| 114 RemoteMessageCallback message_callback_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(MediaRemotingSessionImpl); |
| 117 }; |
| 118 |
| 119 // A subclass of MediaRemotingSessionImpl whose lifetime is associated with a |
| 120 // specific "content source" WebContents. It observes the WebContents and |
| 121 // automatically self-terminates when the WebContents is destroyed. |
| 122 class AutoTerminatingRemotingSession : public MediaRemotingSessionImpl, |
| 123 protected content::WebContentsObserver { |
| 124 public: |
| 125 AutoTerminatingRemotingSession( |
| 126 content::WebContents* source_contents, |
| 127 interfaces::MediaRemotingProviderPtr provider, |
| 128 mojo::InterfaceRequest<interfaces::MediaRemotingSession> request, |
| 129 const base::Closure& did_terminate_callback); |
| 130 ~AutoTerminatingRemotingSession() final; |
| 131 |
| 132 private: |
| 133 // content::WebContentsObserver override. |
| 134 void WebContentsDestroyed() final; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(AutoTerminatingRemotingSession); |
| 137 }; |
| 138 |
| 139 } // namespace media_router |
| 140 |
| 141 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_REMOTING_SESSION_IMPL_H_ |
| OLD | NEW |