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