OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_REMOTING_REMOTING_CONTROLLER_H_ | 5 #ifndef MEDIA_REMOTING_REMOTING_RENDERER_CONTROLLER_H_ |
6 #define MEDIA_REMOTING_REMOTING_CONTROLLER_H_ | 6 #define MEDIA_REMOTING_REMOTING_RENDERER_CONTROLLER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "media/base/media_observer.h" | 10 #include "media/base/media_observer.h" |
11 #include "media/mojo/interfaces/remoting.mojom.h" | 11 #include "media/remoting/remoting_source_impl.h" |
12 #include "mojo/public/cpp/bindings/binding.h" | |
13 | 12 |
14 namespace base { | |
15 class SingleThreadTaskRunner; | |
16 } | |
17 | |
18 // This class does the following: | |
19 // 1) Sends/Receives messages from/to Remoter; | |
20 // 2) Monitors player events as a MediaObserver; | |
21 // 3) May trigger the switch of the media renderer between local playback | |
22 // and remoting. | |
23 // | |
24 namespace media { | 13 namespace media { |
25 | 14 |
| 15 struct PipelineMetadata; |
26 namespace remoting { | 16 namespace remoting { |
27 class RpcBroker; | 17 class RpcBroker; |
28 } | 18 } |
29 | 19 |
30 class RemotingController final : public MediaObserver, | 20 // This class: |
31 public mojom::RemotingSource { | 21 // 1) Implements the RemotingSourceImpl::Client; |
| 22 // 2) Monitors player events as a MediaObserver; |
| 23 // 3) May trigger the switch of the media renderer between local playback |
| 24 // and remoting. |
| 25 class RemotingRendererController final : public RemotingSourceImpl::Client, |
| 26 public MediaObserver { |
32 public: | 27 public: |
33 RemotingController(mojom::RemotingSourceRequest source_request, | 28 explicit RemotingRendererController( |
34 mojom::RemoterPtr remoter); | 29 scoped_refptr<RemotingSourceImpl> remoting_source); |
35 ~RemotingController() override; | 30 ~RemotingRendererController() override; |
36 | 31 |
37 using DataPipeStartCallback = | 32 // RemotingSourceImpl::Client implemenations. |
38 base::Callback<void(mojom::RemotingDataStreamSenderPtrInfo audio, | 33 void OnStarted(bool success) override; |
39 mojom::RemotingDataStreamSenderPtrInfo video, | 34 void OnSessionStateChanged() override; |
40 mojo::ScopedDataPipeProducerHandle audio_handle, | |
41 mojo::ScopedDataPipeProducerHandle video_handle)>; | |
42 void StartDataPipe(std::unique_ptr<mojo::DataPipe> audio_data_pipe, | |
43 std::unique_ptr<mojo::DataPipe> video_data_pipe, | |
44 const DataPipeStartCallback& done_callback); | |
45 | |
46 // RemotingSource implementations. | |
47 void OnSinkAvailable() override; | |
48 void OnSinkGone() override; | |
49 void OnStarted() override; | |
50 void OnStartFailed(mojom::RemotingStartFailReason reason) override; | |
51 void OnMessageFromSink(const std::vector<uint8_t>& message) override; | |
52 void OnStopped(mojom::RemotingStopReason reason) override; | |
53 | 35 |
54 // MediaObserver implementations. | 36 // MediaObserver implementations. |
55 // This is called when the video element or its ancestor enters full screen. | |
56 // We currently use this as an indicator for immersive playback. May add other | |
57 // criteria (e.g. the actual display width/height of the video element) in | |
58 // future. | |
59 void OnEnteredFullscreen() override; | 37 void OnEnteredFullscreen() override; |
60 void OnExitedFullscreen() override; | 38 void OnExitedFullscreen() override; |
61 void OnSetCdm(CdmContext* cdm_context) override; | 39 void OnSetCdm(CdmContext* cdm_context) override; |
62 void OnMetadataChanged(const PipelineMetadata& metadata) override; | 40 void OnMetadataChanged(const PipelineMetadata& metadata) override; |
63 | 41 |
64 using SwitchRendererCallback = base::Callback<void()>; | 42 void SetSwitchRendererCallback(const base::Closure& cb); |
65 void SetSwitchRendererCallback(const SwitchRendererCallback& cb); | |
66 | 43 |
67 // Tells which renderer should be used. | 44 base::WeakPtr<RemotingRendererController> GetWeakPtr() { |
68 bool is_remoting() const { | |
69 DCHECK(task_runner_->BelongsToCurrentThread()); | |
70 return is_remoting_; | |
71 } | |
72 | |
73 base::WeakPtr<RemotingController> GetWeakPtr() { | |
74 return weak_factory_.GetWeakPtr(); | 45 return weak_factory_.GetWeakPtr(); |
75 } | 46 } |
76 | 47 |
| 48 // Used by RemotingRendererFactory to query whether to create Media Remoting |
| 49 // Renderer. |
| 50 bool remote_rendering_started() const { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 return remote_rendering_started_; |
| 53 } |
| 54 |
| 55 void StartDataPipe( |
| 56 std::unique_ptr<mojo::DataPipe> audio_data_pipe, |
| 57 std::unique_ptr<mojo::DataPipe> video_data_pipe, |
| 58 const RemotingSourceImpl::DataPipeStartCallback& done_callback); |
| 59 |
| 60 // Used by RemotingRendererImpl to query the session state. |
| 61 RemotingSourceImpl* remoting_source() const { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 return remoting_source_.get(); |
| 64 } |
| 65 |
77 base::WeakPtr<remoting::RpcBroker> GetRpcBroker() const; | 66 base::WeakPtr<remoting::RpcBroker> GetRpcBroker() const; |
78 | 67 |
79 private: | 68 private: |
80 bool IsVideoCodecSupported(); | 69 bool IsVideoCodecSupported(); |
81 bool IsAudioCodecSupported(); | 70 bool IsAudioCodecSupported(); |
82 | 71 |
83 // Helper to decide whether to enter or leave Remoting mode. | 72 // Helper to decide whether to enter or leave Remoting mode. |
84 bool ShouldBeRemoting(); | 73 bool ShouldBeRemoting(); |
85 | 74 |
86 // Determines whether to enter or leave Remoting mode and switches if | 75 // Determines whether to enter or leave Remoting mode and switches if |
87 // necessary. | 76 // necessary. |
88 void UpdateAndMaybeSwitch(); | 77 void UpdateAndMaybeSwitch(); |
89 | 78 |
90 // Callback from RpcBroker when sending message to remote sink. | 79 // Indicates whether this media element or its ancestor is in full screen. |
91 void OnSendMessageToSink(std::unique_ptr<std::vector<uint8_t>> message); | |
92 | |
93 // Handle incomging and outgoing RPC message. | |
94 std::unique_ptr<remoting::RpcBroker> rpc_broker_; | |
95 | |
96 // Indicates if this media element or its ancestor enters full screen. | |
97 bool is_fullscreen_ = false; | 80 bool is_fullscreen_ = false; |
98 | 81 |
99 // Indicates the remoting sink availablity. | 82 // Indicates whether remoting is started. |
100 bool is_sink_available_ = false; | 83 bool remote_rendering_started_ = false; |
101 | |
102 // Indicates if remoting is started. | |
103 bool is_remoting_ = false; | |
104 | 84 |
105 // Indicates whether audio or video is encrypted. | 85 // Indicates whether audio or video is encrypted. |
106 bool is_encrypted_ = false; | 86 bool is_encrypted_ = false; |
107 | 87 |
108 // Current audio/video config. | 88 // Current audio/video config. |
109 VideoDecoderConfig video_decoder_config_; | 89 VideoDecoderConfig video_decoder_config_; |
110 AudioDecoderConfig audio_decoder_config_; | 90 AudioDecoderConfig audio_decoder_config_; |
111 bool has_audio_ = false; | 91 bool has_audio_ = false; |
112 bool has_video_ = false; | 92 bool has_video_ = false; |
113 | 93 |
114 // The callback to switch the media renderer. | 94 // The callback to switch the media renderer. |
115 SwitchRendererCallback switch_renderer_cb_; | 95 base::Closure switch_renderer_cb_; |
116 | 96 |
117 mojo::Binding<mojom::RemotingSource> binding_; | 97 // This is initially the RemotingSourceImpl passed to the ctor, and might be |
118 mojom::RemoterPtr remoter_; | 98 // replaced with a different instance later if OnSetCdm() is called. |
| 99 scoped_refptr<RemotingSourceImpl> remoting_source_; |
119 | 100 |
120 // TODO(xjz): Add a media thread task runner for the received RPC messages for | 101 // This is used to check all the methods are called on the current thread in |
121 // remoting media renderer in the up-coming change. | 102 // debug builds. |
122 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 103 base::ThreadChecker thread_checker_; |
123 | 104 |
124 base::WeakPtrFactory<RemotingController> weak_factory_; | 105 base::WeakPtrFactory<RemotingRendererController> weak_factory_; |
125 | 106 |
126 DISALLOW_COPY_AND_ASSIGN(RemotingController); | 107 DISALLOW_COPY_AND_ASSIGN(RemotingRendererController); |
127 }; | 108 }; |
128 | 109 |
129 } // namespace media | 110 } // namespace media |
130 | 111 |
131 #endif // MEDIA_REMOTING_REMOTING_CONTROLLER_H_ | 112 #endif // MEDIA_REMOTING_REMOTING_RENDERER_CONTROLLER_H_ |
OLD | NEW |