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_REMOTE_DEMUXER_STREAM_ADAPTER_H_ | 5 #ifndef MEDIA_REMOTING_DEMUXER_STREAM_ADAPTER_H_ |
6 #define MEDIA_REMOTING_REMOTE_DEMUXER_STREAM_ADAPTER_H_ | 6 #define MEDIA_REMOTING_DEMUXER_STREAM_ADAPTER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/optional.h" | 16 #include "base/optional.h" |
17 #include "media/base/audio_decoder_config.h" | 17 #include "media/base/audio_decoder_config.h" |
18 #include "media/base/demuxer_stream.h" | 18 #include "media/base/demuxer_stream.h" |
19 #include "media/base/video_decoder_config.h" | 19 #include "media/base/video_decoder_config.h" |
20 #include "media/mojo/interfaces/remoting.mojom.h" | 20 #include "media/mojo/interfaces/remoting.mojom.h" |
21 #include "media/remoting/rpc/rpc_broker.h" | 21 #include "media/remoting/rpc_broker.h" |
22 #include "media/remoting/triggers.h" | 22 #include "media/remoting/triggers.h" |
23 #include "mojo/public/cpp/system/data_pipe.h" | 23 #include "mojo/public/cpp/system/data_pipe.h" |
24 | 24 |
25 namespace base { | 25 namespace base { |
26 class SingleThreadTaskRunner; | 26 class SingleThreadTaskRunner; |
27 } | 27 } |
28 | 28 |
29 namespace media { | 29 namespace media { |
30 | 30 |
31 class DemuxerStream; | 31 class DemuxerStream; |
32 | 32 |
33 namespace remoting { | 33 namespace remoting { |
34 | 34 |
35 // Class to fetch audio/video buffer from demuxer and send it to browser process | 35 // Class to fetch audio/video buffer from demuxer and send it to browser process |
36 // via mojo::Remoting interface. Note the class is created and run on media | 36 // via mojo::Remoting interface. Note the class is created and run on media |
37 // thread using |media_task_runner|, Mojo data pipe should run on media thread, | 37 // thread using |media_task_runner|, Mojo data pipe should run on media thread, |
38 // while RPC message should be sent on main thread using |main_task_runner|. | 38 // while RPC message should be sent on main thread using |main_task_runner|. |
39 class RemoteDemuxerStreamAdapter { | 39 class DemuxerStreamAdapter { |
40 public: | 40 public: |
41 using ErrorCallback = base::Callback<void(StopTrigger)>; | 41 using ErrorCallback = base::Callback<void(StopTrigger)>; |
42 | 42 |
43 // |main_task_runner|: Task runner to post RPC message on main thread | 43 // |main_task_runner|: Task runner to post RPC message on main thread |
44 // |media_task_runner|: Task runner to run whole class on media thread. | 44 // |media_task_runner|: Task runner to run whole class on media thread. |
45 // |name|: Demuxer stream name. For troubleshooting purposes. | 45 // |name|: Demuxer stream name. For troubleshooting purposes. |
46 // |demuxer_stream|: Demuxer component. | 46 // |demuxer_stream|: Demuxer component. |
47 // |rpc_broker|: Broker class to handle incoming and outgoing RPC message. It | 47 // |rpc_broker|: Broker class to handle incoming and outgoing RPC message. It |
48 // is used only on the main thread. | 48 // is used only on the main thread. |
49 // |rpc_handle|: Unique value that references this RemoteDemuxerStreamAdapter. | 49 // |rpc_handle|: Unique value that references this DemuxerStreamAdapter. |
50 // |stream_sender_info|: Transfer of pipe binding on the media thread. It is | 50 // |stream_sender_info|: Transfer of pipe binding on the media thread. It is |
51 // to access mojo interface for sending data stream. | 51 // to access mojo interface for sending data stream. |
52 // |producer_handle|: handle to send data using mojo data pipe. | 52 // |producer_handle|: handle to send data using mojo data pipe. |
53 // |error_callback|: Run if a fatal runtime error occurs and remoting should | 53 // |error_callback|: Run if a fatal runtime error occurs and remoting should |
54 // be shut down. | 54 // be shut down. |
55 RemoteDemuxerStreamAdapter( | 55 DemuxerStreamAdapter( |
56 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 56 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
57 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, | 57 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner, |
58 const std::string& name, | 58 const std::string& name, |
59 ::media::DemuxerStream* demuxer_stream, | 59 DemuxerStream* demuxer_stream, |
60 const base::WeakPtr<RpcBroker>& rpc_broker, | 60 const base::WeakPtr<RpcBroker>& rpc_broker, |
61 int rpc_handle, | 61 int rpc_handle, |
62 mojom::RemotingDataStreamSenderPtrInfo stream_sender_info, | 62 mojom::RemotingDataStreamSenderPtrInfo stream_sender_info, |
63 mojo::ScopedDataPipeProducerHandle producer_handle, | 63 mojo::ScopedDataPipeProducerHandle producer_handle, |
64 const ErrorCallback& error_callback); | 64 const ErrorCallback& error_callback); |
65 ~RemoteDemuxerStreamAdapter(); | 65 ~DemuxerStreamAdapter(); |
66 | 66 |
67 // Rpc handle for this class. This is used for sending/receiving RPC message | 67 // Rpc handle for this class. This is used for sending/receiving RPC message |
68 // with specific hanle using Rpcbroker. | 68 // with specific hanle using Rpcbroker. |
69 int rpc_handle() const { return rpc_handle_; } | 69 int rpc_handle() const { return rpc_handle_; } |
70 | 70 |
71 // Returns the number of bytes that have been written to the data pipe since | 71 // Returns the number of bytes that have been written to the data pipe since |
72 // the last call to this method. This is polled periodically by | 72 // the last call to this method. This is polled periodically by |
73 // RemoteRendererImpl for metrics purposes. | 73 // CourierRenderer for metrics purposes. |
74 int64_t GetBytesWrittenAndReset(); | 74 int64_t GetBytesWrittenAndReset(); |
75 | 75 |
76 // Signals if system is in flushing state. The caller uses |flushing| to | 76 // Signals if system is in flushing state. The caller uses |flushing| to |
77 // signal when flush starts and when is done. During flush operation, all | 77 // signal when flush starts and when is done. During flush operation, all |
78 // fetching data actions will be discarded. The return value indicates frame | 78 // fetching data actions will be discarded. The return value indicates frame |
79 // count in order to signal receiver what frames are in flight before flush, | 79 // count in order to signal receiver what frames are in flight before flush, |
80 // or base::nullopt if the flushing state was unchanged. | 80 // or base::nullopt if the flushing state was unchanged. |
81 base::Optional<uint32_t> SignalFlush(bool flushing); | 81 base::Optional<uint32_t> SignalFlush(bool flushing); |
82 | 82 |
| 83 // Creates a Mojo data pipe configured appropriately for use with a |
| 84 // DemuxerStreamAdapter. |
| 85 static mojo::DataPipe* CreateDataPipe(); |
| 86 |
83 private: | 87 private: |
84 friend class MockRemoteDemuxerStreamAdapter; | 88 friend class MockDemuxerStreamAdapter; |
85 | 89 |
86 // Receives RPC message from RpcBroker. | 90 // Receives RPC message from RpcBroker. |
87 void OnReceivedRpc(std::unique_ptr<remoting::pb::RpcMessage> message); | 91 void OnReceivedRpc(std::unique_ptr<pb::RpcMessage> message); |
88 | 92 |
89 // RPC message tasks. | 93 // RPC message tasks. |
90 void Initialize(int remote_callback_handle); | 94 void Initialize(int remote_callback_handle); |
91 void ReadUntil(std::unique_ptr<remoting::pb::RpcMessage> message); | 95 void ReadUntil(std::unique_ptr<pb::RpcMessage> message); |
92 void EnableBitstreamConverter(); | 96 void EnableBitstreamConverter(); |
93 void RequestBuffer(); | 97 void RequestBuffer(); |
94 void SendReadAck(); | 98 void SendReadAck(); |
95 | 99 |
96 // Callback function when retrieving data from demuxer. | 100 // Callback function when retrieving data from demuxer. |
97 void OnNewBuffer(::media::DemuxerStream::Status status, | 101 void OnNewBuffer(DemuxerStream::Status status, |
98 const scoped_refptr<::media::DecoderBuffer>& input); | 102 const scoped_refptr<DecoderBuffer>& input); |
99 void TryWriteData(MojoResult result); | 103 void TryWriteData(MojoResult result); |
100 void ResetPendingFrame(); | 104 void ResetPendingFrame(); |
101 bool IsProcessingReadRequest() const { | 105 bool IsProcessingReadRequest() const { |
102 // |read_until_callback_handle_| is set when RPC_DS_READUNTIL message is | 106 // |read_until_callback_handle_| is set when RPC_DS_READUNTIL message is |
103 // received, and will be reset to invalid value after | 107 // received, and will be reset to invalid value after |
104 // RPC_DS_READUNTIL_CALLBACK is sent back to receiver. Therefore it can be | 108 // RPC_DS_READUNTIL_CALLBACK is sent back to receiver. Therefore it can be |
105 // used to determine if the class is in the reading state or not. | 109 // used to determine if the class is in the reading state or not. |
106 return read_until_callback_handle_ != kInvalidHandle; | 110 return read_until_callback_handle_ != RpcBroker::kInvalidHandle; |
107 } | 111 } |
108 | 112 |
109 // Callback function when a fatal runtime error occurs. | 113 // Callback function when a fatal runtime error occurs. |
110 void OnFatalError(StopTrigger stop_trigger); | 114 void OnFatalError(StopTrigger stop_trigger); |
111 | 115 |
112 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 116 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
113 const scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 117 const scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
114 | 118 |
115 // Name of demuxer stream. Debug only. | 119 // Name of demuxer stream. Debug only. |
116 const std::string name_; | 120 const std::string name_; |
117 | 121 |
118 // Weak pointer of RpcBroker. It should use |main_task_runner_| to access the | 122 // Weak pointer of RpcBroker. It should use |main_task_runner_| to access the |
119 // interfaces. | 123 // interfaces. |
120 const base::WeakPtr<RpcBroker> rpc_broker_; | 124 const base::WeakPtr<RpcBroker> rpc_broker_; |
121 | 125 |
122 // RPC handle for this demuxer stream service. | 126 // RPC handle for this demuxer stream service. |
123 const int rpc_handle_; | 127 const int rpc_handle_; |
124 | 128 |
125 // Demuxer stream and stream type. | 129 // Demuxer stream and stream type. |
126 ::media::DemuxerStream* const demuxer_stream_; | 130 DemuxerStream* const demuxer_stream_; |
127 const ::media::DemuxerStream::Type type_; | 131 const DemuxerStream::Type type_; |
128 | 132 |
129 // Run by OnFatalError to propagate StopTriggers back to the | 133 // Run by OnFatalError to propagate StopTriggers back to the |
130 // RemoteRendererImpl that owns this instance. This is not-null at | 134 // CourierRenderer that owns this instance. This is not-null at |
131 // construction time, and set to null the first time OnFatalError() is called. | 135 // construction time, and set to null the first time OnFatalError() is called. |
132 ErrorCallback error_callback_; | 136 ErrorCallback error_callback_; |
133 | 137 |
134 // Remote RPC handle for demuxer initialization. The value is provided by | 138 // Remote RPC handle for demuxer initialization. The value is provided by |
135 // receiver from RPC_DS_INITIALIZE message and will be used as handle in | 139 // receiver from RPC_DS_INITIALIZE message and will be used as handle in |
136 // RPC_DS_INITIALIZE_CALLBACK message. | 140 // RPC_DS_INITIALIZE_CALLBACK message. |
137 int remote_callback_handle_; | 141 int remote_callback_handle_; |
138 | 142 |
139 // Remote RPC handle for reading data from demuxer. The value is provided by | 143 // Remote RPC handle for reading data from demuxer. The value is provided by |
140 // receiver from RPC_DS_READUNTIL message and will be used as handle in | 144 // receiver from RPC_DS_READUNTIL message and will be used as handle in |
(...skipping 18 matching lines...) Expand all Loading... |
159 // Frame buffer and its information that is currently in process of writing to | 163 // Frame buffer and its information that is currently in process of writing to |
160 // Mojo data pipe. | 164 // Mojo data pipe. |
161 std::vector<uint8_t> pending_frame_; | 165 std::vector<uint8_t> pending_frame_; |
162 uint32_t current_pending_frame_offset_; | 166 uint32_t current_pending_frame_offset_; |
163 bool pending_frame_is_eos_; | 167 bool pending_frame_is_eos_; |
164 | 168 |
165 // Monitor if data pipe is available to write data. | 169 // Monitor if data pipe is available to write data. |
166 mojo::Watcher write_watcher_; | 170 mojo::Watcher write_watcher_; |
167 | 171 |
168 // Keeps latest demuxer stream status and audio/video decoder config. | 172 // Keeps latest demuxer stream status and audio/video decoder config. |
169 ::media::DemuxerStream::Status media_status_; | 173 DemuxerStream::Status media_status_; |
170 ::media::AudioDecoderConfig audio_config_; | 174 AudioDecoderConfig audio_config_; |
171 ::media::VideoDecoderConfig video_config_; | 175 VideoDecoderConfig video_config_; |
172 | 176 |
173 ::media::mojom::RemotingDataStreamSenderPtr stream_sender_; | 177 mojom::RemotingDataStreamSenderPtr stream_sender_; |
174 mojo::ScopedDataPipeProducerHandle producer_handle_; | 178 mojo::ScopedDataPipeProducerHandle producer_handle_; |
175 | 179 |
176 // Tracks the number of bytes written to the pipe. | 180 // Tracks the number of bytes written to the pipe. |
177 int64_t bytes_written_to_pipe_; | 181 int64_t bytes_written_to_pipe_; |
178 | 182 |
179 // WeakPtrFactory only for reading buffer from demuxer stream. This is used | 183 // WeakPtrFactory only for reading buffer from demuxer stream. This is used |
180 // for canceling all read callbacks provided to the |demuxer_stream_| before a | 184 // for canceling all read callbacks provided to the |demuxer_stream_| before a |
181 // flush. | 185 // flush. |
182 base::WeakPtrFactory<RemoteDemuxerStreamAdapter> request_buffer_weak_factory_; | 186 base::WeakPtrFactory<DemuxerStreamAdapter> request_buffer_weak_factory_; |
183 // WeakPtrFactory for normal usage. | 187 // WeakPtrFactory for normal usage. |
184 base::WeakPtrFactory<RemoteDemuxerStreamAdapter> weak_factory_; | 188 base::WeakPtrFactory<DemuxerStreamAdapter> weak_factory_; |
185 | 189 |
186 DISALLOW_COPY_AND_ASSIGN(RemoteDemuxerStreamAdapter); | 190 DISALLOW_COPY_AND_ASSIGN(DemuxerStreamAdapter); |
187 }; | 191 }; |
188 | 192 |
189 mojo::DataPipe* CreateDataPipe(); | |
190 | |
191 } // namespace remoting | 193 } // namespace remoting |
192 } // namespace media | 194 } // namespace media |
193 | 195 |
194 #endif // MEDIA_REMOTING_REMOTE_DEMUXER_STREAM_ADAPTER_H_ | 196 #endif // MEDIA_REMOTING_DEMUXER_STREAM_ADAPTER_H_ |
OLD | NEW |