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 MEDIA_REMOTING_REMOTING_CONTROLLER_H_ | |
6 #define MEDIA_REMOTING_REMOTING_CONTROLLER_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "media/base/media_observer.h" | |
11 #include "media/mojo/interfaces/remoting.mojom.h" | |
12 #include "mojo/public/cpp/bindings/binding.h" | |
13 | |
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 { | |
25 | |
26 namespace remoting { | |
27 class RpcBroker; | |
28 } | |
29 | |
30 class RemotingController final : public MediaObserver, | |
31 public mojom::RemotingSource { | |
32 public: | |
33 RemotingController(mojom::RemotingSourceRequest source_request, | |
34 mojom::RemoterPtr remoter); | |
35 ~RemotingController() override; | |
36 | |
37 using DataPipeStartCallback = | |
38 base::Callback<void(mojom::RemotingDataStreamSenderPtrInfo audio, | |
39 mojom::RemotingDataStreamSenderPtrInfo video, | |
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 | |
54 // 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; | |
60 void OnExitedFullscreen() override; | |
61 void OnSetCdm(CdmContext* cdm_context) override; | |
62 void OnMetadataChanged(const PipelineMetadata& metadata) override; | |
63 | |
64 using SwitchRendererCallback = base::Callback<void()>; | |
65 void SetSwitchRendererCallback(const SwitchRendererCallback& cb); | |
66 | |
67 // Tells which renderer should be used. | |
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(); | |
75 } | |
76 | |
77 base::WeakPtr<remoting::RpcBroker> GetRpcBroker() const; | |
78 | |
79 private: | |
80 bool IsVideoCodecSupported(); | |
81 bool IsAudioCodecSupported(); | |
82 | |
83 // Helper to decide whether to enter or leave Remoting mode. | |
84 bool ShouldBeRemoting(); | |
85 | |
86 // Determines whether to enter or leave Remoting mode and switches if | |
87 // necessary. | |
88 void UpdateAndMaybeSwitch(); | |
89 | |
90 // Callback from RpcBroker when sending message to remote sink. | |
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; | |
98 | |
99 // Indicates the remoting sink availablity. | |
100 bool is_sink_available_ = false; | |
101 | |
102 // Indicates if remoting is started. | |
103 bool is_remoting_ = false; | |
104 | |
105 // Indicates whether audio or video is encrypted. | |
106 bool is_encrypted_ = false; | |
107 | |
108 // Current audio/video config. | |
109 VideoDecoderConfig video_decoder_config_; | |
110 AudioDecoderConfig audio_decoder_config_; | |
111 bool has_audio_ = false; | |
112 bool has_video_ = false; | |
113 | |
114 // The callback to switch the media renderer. | |
115 SwitchRendererCallback switch_renderer_cb_; | |
116 | |
117 mojo::Binding<mojom::RemotingSource> binding_; | |
118 mojom::RemoterPtr remoter_; | |
119 | |
120 // TODO(xjz): Add a media thread task runner for the received RPC messages for | |
121 // remoting media renderer in the up-coming change. | |
122 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
123 | |
124 base::WeakPtrFactory<RemotingController> weak_factory_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(RemotingController); | |
127 }; | |
128 | |
129 } // namespace media | |
130 | |
131 #endif // MEDIA_REMOTING_REMOTING_CONTROLLER_H_ | |
OLD | NEW |