| 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_MEDIA_REMOTING_CONNECTOR_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_REMOTING_CONNECTOR_H_ |
| 7 |
| 8 #include <unordered_set> |
| 9 |
| 10 #include "content/public/browser/web_contents_user_data.h" |
| 11 #include "media/mojo/interfaces/remoter.mojom.h" |
| 12 |
| 13 namespace content { |
| 14 class RenderFrameHost; |
| 15 } |
| 16 |
| 17 namespace media_router { |
| 18 |
| 19 class MediaRemotingProvider; |
| 20 |
| 21 // MediaRemotingConnector connects a single source (a media player in a render |
| 22 // frame) with a single sink (MediaRemotingProvider). There is one instance of a |
| 23 // MediaRemotingConnector per source WebContents (representing a collection of |
| 24 // render frames), and it is created on-demand. |
| 25 // |
| 26 // Whenever a RenderFrameHost is created, ChromeContentBrowserClient will call |
| 27 // CreateRemoter() to provide a media::mojom::Remoter service to the render |
| 28 // frame. This service is how the render frame requests notifications for when |
| 29 // remote media services are available, controls the start/stop of remoting, and |
| 30 // passes messages to/from the remote. |
| 31 // |
| 32 // At any time before or after the media::mojom::Remoter services are created, |
| 33 // the Media Router extension may create a service that implements the |
| 34 // MediaRemotingProvider interface. The implementation, will notify this |
| 35 // MediaRemotingConnector when all dependencies for remoting have been |
| 36 // satisfied. The connector will then, in turn, notify all the render frames |
| 37 // that remoting is available. Finally, it is up to the render frame to decide |
| 38 // whether the source media is compatible with what the remote media services |
| 39 // provide and, if so, start remoting. |
| 40 // |
| 41 // Note that only one render frame may be remoting at a time. Therefore, |
| 42 // MediaRemotingConnector must mitigate which simultaneous requests for media |
| 43 // services are to be rejected. Currently, the policy is "first come, first |
| 44 // served." |
| 45 class MediaRemotingConnector |
| 46 : protected content::WebContentsUserData<MediaRemotingConnector> { |
| 47 public: |
| 48 // Returns an instance to the MediaRemotingConnector associated with |
| 49 // |source_contents|, creating a new instance if needed. |
| 50 static MediaRemotingConnector* Get(content::WebContents* source_contents); |
| 51 |
| 52 // Used by ChromeContentBrowserClient to request a binding to a new media |
| 53 // Remoter mojo service for each new render frame. |
| 54 static void CreateRemoter( |
| 55 content::RenderFrameHost* render_frame_host, |
| 56 mojo::InterfaceRequest<media::mojom::Remoter> request); |
| 57 |
| 58 ~MediaRemotingConnector() final; |
| 59 |
| 60 // Sets the provider of media remoting services to |provider|. This is called |
| 61 // once a sink is available and is ready to start media remoting. The pointer |
| 62 // must remain valid until after this method is called again with nullptr |
| 63 // (i.e., the provider is no longer available). |
| 64 void SetProvider(MediaRemotingProvider* provider); |
| 65 |
| 66 private: |
| 67 // Private implementation of the media::mojom::Remoter service for a single |
| 68 // render frame. A single WebContents may contain more than one render frame, |
| 69 // and so this per-RenderFrameHost object is just a "lightweight shell" that |
| 70 // delegates calls to its MediaRemotingConnector parent class. |
| 71 class FrameMediaRemoter; |
| 72 |
| 73 friend class content::WebContentsUserData<MediaRemotingConnector>; |
| 74 explicit MediaRemotingConnector(content::WebContents* source_contents); |
| 75 |
| 76 // Returns the MediaRemotingConnector associated with the WebContents |
| 77 // containing |source_render_frame_host|, or nullptr if there is none. This is |
| 78 // used by FrameMediaRemoter. |
| 79 static MediaRemotingConnector* GetIfExists( |
| 80 content::RenderFrameHost* source_render_frame_host); |
| 81 |
| 82 // These are called by FrameMediaRemoter to forward media::mojom::Remoter |
| 83 // calls from a render frame through to the MediaRemotingProvider. It is up to |
| 84 // this MediaRemotingConnector to ensure only one FrameMediaRemoter is |
| 85 // actively forwarding to the single MediaRemotingProvider at a time. |
| 86 void RegisterFrameRemoter(FrameMediaRemoter* remoter); |
| 87 void DeregisterFrameRemoter(FrameMediaRemoter* remoter); |
| 88 void StartMediaServices( |
| 89 FrameMediaRemoter* remoter, |
| 90 mojo::ScopedDataPipeConsumerHandle audio_pipe, |
| 91 mojo::ScopedDataPipeConsumerHandle video_pipe, |
| 92 const base::Callback<void(bool, uint32_t, uint32_t)>& callback); |
| 93 void StopMediaServices(FrameMediaRemoter* remoter, |
| 94 const mojo::String& error_reason); |
| 95 void SendMessageToRemote(FrameMediaRemoter* remoter, |
| 96 mojo::Array<uint8_t> message); |
| 97 void SendBufferToRemote(FrameMediaRemoter* remoter, uint32_t pipe_id, |
| 98 media::mojom::DecoderBufferPtr buffer); |
| 99 |
| 100 // List of registered FrameMediaRemoters. |
| 101 std::unordered_set<FrameMediaRemoter*> remoters_; |
| 102 |
| 103 // When non-null, a sink is available and ready for media remoting. |
| 104 MediaRemotingProvider* provider_; |
| 105 |
| 106 // When non-null, remoting is taking place, with this pointing to the |
| 107 // FrameMediaRemoter representing the source. |
| 108 FrameMediaRemoter* active_remoter_; |
| 109 }; |
| 110 |
| 111 } // namespace media_router |
| 112 |
| 113 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_REMOTING_CONNECTOR_H_ |
| OLD | NEW |