| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ | 5 #ifndef MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ |
| 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ | 6 #define MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ |
| 7 | 7 |
| 8 #include <atomic> | 8 #include <atomic> |
| 9 #include <deque> | 9 #include <deque> |
| 10 #include <functional> | 10 #include <functional> |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "mojo/public/cpp/bindings/callback.h" | 13 #include "mojo/public/cpp/bindings/callback.h" |
| 14 #include "mojo/public/cpp/environment/logging.h" | 14 #include "mojo/public/cpp/environment/logging.h" |
| 15 #include "mojo/services/media/common/interfaces/media_common.mojom.h" | 15 #include "mojo/services/media/common/interfaces/media_common.mojom.h" |
| 16 #include "mojo/services/media/common/interfaces/media_transport.mojom.h" | 16 #include "mojo/services/media/common/interfaces/media_transport.mojom.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 namespace media { | 19 namespace media { |
| 20 | 20 |
| 21 // A class to help producers of media with the bookkeeping involved in using the | 21 // A class to help producers of media with the bookkeeping involved in using the |
| 22 // shared buffer provided by a MediaConsumer mojo interface in a circular buffer | 22 // shared buffer provided by a MediaPacketConsumer mojo interface in a circular |
| 23 // fashion. | 23 // buffer fashion. |
| 24 // | 24 // |
| 25 class CircularBufferMediaPipeAdapter { | 25 class CircularBufferMediaPipeAdapter { |
| 26 public: | 26 public: |
| 27 class MappedPacket { | 27 class MappedPacket { |
| 28 public: | 28 public: |
| 29 static constexpr size_t kMaxRegions = 2; | 29 static constexpr size_t kMaxRegions = 2; |
| 30 MappedPacket(); | 30 MappedPacket(); |
| 31 ~MappedPacket(); | 31 ~MappedPacket(); |
| 32 | 32 |
| 33 const MediaPacketPtr& packet() const { return packet_; } | 33 const MediaPacketPtr& packet() const { return packet_; } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 * entered into a fatal, unrecoverable state. | 66 * entered into a fatal, unrecoverable state. |
| 67 * | 67 * |
| 68 * @param state The current state of the adapter. If the state is anything | 68 * @param state The current state of the adapter. If the state is anything |
| 69 * but MediaResult::OK, the pipe is in a fatal, unrecoverable error state. | 69 * but MediaResult::OK, the pipe is in a fatal, unrecoverable error state. |
| 70 */ | 70 */ |
| 71 using SignalCbk = std::function<void(MediaResult state)>; | 71 using SignalCbk = std::function<void(MediaResult state)>; |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * Constructor | 74 * Constructor |
| 75 * | 75 * |
| 76 * Create an adapter which will take ownership of the provided MediaConsumer | 76 * Create an adapter which will take ownership of the provided |
| 77 * interface and assist in the process of generating MediaPackets and | 77 * MediaPacketConsumer interface and assist in the process of generating |
| 78 * marshalling them to the other side of the MediaConsumer. | 78 * MediaPackets and marshalling them to the other side of the |
| 79 * MediaPacketConsumer. |
| 79 * | 80 * |
| 80 * @param pipe A pointer to the MediaConsumer interface which will be used as | 81 * @param pipe A pointer to the MediaPacketConsumer interface which will be |
| 81 * the target for MediaPackets. | 82 * used as the target for MediaPackets. |
| 82 */ | 83 */ |
| 83 explicit CircularBufferMediaPipeAdapter(MediaConsumerPtr pipe); | 84 explicit CircularBufferMediaPipeAdapter(MediaPacketConsumerPtr pipe); |
| 84 | 85 |
| 85 /** | 86 /** |
| 86 * Destructor | 87 * Destructor |
| 87 */ | 88 */ |
| 88 ~CircularBufferMediaPipeAdapter(); | 89 ~CircularBufferMediaPipeAdapter(); |
| 89 | 90 |
| 90 /** | 91 /** |
| 91 * Init | 92 * Init |
| 92 * | 93 * |
| 93 * Allocate a shared memory buffer of the specified size and begin the process | 94 * Allocate a shared memory buffer of the specified size and begin the process |
| 94 * of marshalling it to the other side of the MediaConsumer. | 95 * of marshalling it to the other side of the MediaPacketConsumer. |
| 95 * | 96 * |
| 96 * @param size The size in bytes of the shared memory buffer to allocate. | 97 * @param size The size in bytes of the shared memory buffer to allocate. |
| 97 */ | 98 */ |
| 98 void Init(uint64_t size); | 99 void Init(uint64_t size); |
| 99 | 100 |
| 100 /** | 101 /** |
| 101 * Set the signal callback for this media pipe adapter. This callback will be | 102 * Set the signal callback for this media pipe adapter. This callback will be |
| 102 * called when the adapter transitions from un-signalled to signalled and has | 103 * called when the adapter transitions from un-signalled to signalled and has |
| 103 * a valid callback, or when a valid callback is assigned (via a call to | 104 * a valid callback, or when a valid callback is assigned (via a call to |
| 104 * Setllback) and the adapter is currently in the signalled state. | 105 * Setllback) and the adapter is currently in the signalled state. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 MappedPacket* packet); | 183 MappedPacket* packet); |
| 183 | 184 |
| 184 /** | 185 /** |
| 185 * Send a previously created media packet across the pipe to the consumer. | 186 * Send a previously created media packet across the pipe to the consumer. |
| 186 * @param [in] packet The pointer to the structure which describes the packet | 187 * @param [in] packet The pointer to the structure which describes the packet |
| 187 * to be sent. | 188 * to be sent. |
| 188 * @return A media result indicating the success or failure of the operation. | 189 * @return A media result indicating the success or failure of the operation. |
| 189 */ | 190 */ |
| 190 MediaResult SendMediaPacket( | 191 MediaResult SendMediaPacket( |
| 191 MappedPacket* packet, | 192 MappedPacket* packet, |
| 192 const MediaConsumer::SendPacketCallback& cbk = | 193 const MediaPacketConsumer::SendPacketCallback& cbk = |
| 193 MediaConsumer::SendPacketCallback()); | 194 MediaPacketConsumer::SendPacketCallback()); |
| 194 | 195 |
| 195 /** | 196 /** |
| 196 * Cancel a packet previously created using CreateMediaPacket. | 197 * Cancel a packet previously created using CreateMediaPacket. |
| 197 * @note The packet canceled must not have been sent, and must be the most | 198 * @note The packet canceled must not have been sent, and must be the most |
| 198 * recent non-canceled packet created using CreateMediaPacket. | 199 * recent non-canceled packet created using CreateMediaPacket. |
| 199 * @param [in] packet The pointer to the structure which describes the packet | 200 * @param [in] packet The pointer to the structure which describes the packet |
| 200 * to be canceled. | 201 * to be canceled. |
| 201 * @return A media result indicating the success or failure of the operation. | 202 * @return A media result indicating the success or failure of the operation. |
| 202 */ | 203 */ |
| 203 MediaResult CancelMediaPacket(MappedPacket* packet); | 204 MediaResult CancelMediaPacket(MappedPacket* packet); |
| 204 | 205 |
| 205 /** | 206 /** |
| 206 * Flush the media pipe. Cancels all in flight payloads and resets the | 207 * Flush the media pipe. Cancels all in flight payloads and resets the |
| 207 * internal bookkeeping. | 208 * internal bookkeeping. |
| 208 * | 209 * |
| 209 * @note The media pipe will be un-signalled and unavailable for Create/Send | 210 * @note The media pipe will be un-signalled and unavailable for Create/Send |
| 210 * operations during the flush. | 211 * operations during the flush. |
| 211 */ | 212 */ |
| 212 MediaResult Flush(); | 213 MediaResult Flush(); |
| 213 | 214 |
| 214 uint64_t GetPending() const; | 215 uint64_t GetPending() const; |
| 215 uint64_t GetBufferSize() const; | 216 uint64_t GetBufferSize() const; |
| 216 uint64_t AboveHiWater() const { return GetPending() >= hi_water_mark_; } | 217 uint64_t AboveHiWater() const { return GetPending() >= hi_water_mark_; } |
| 217 uint64_t BelowLoWater() const { return GetPending() < lo_water_mark_; } | 218 uint64_t BelowLoWater() const { return GetPending() < lo_water_mark_; } |
| 218 | 219 |
| 219 private: | 220 private: |
| 220 struct PacketState { | 221 struct PacketState { |
| 221 PacketState(uint64_t post_consume_rd, | 222 PacketState(uint64_t post_consume_rd, |
| 222 uint32_t seq_num, | 223 uint32_t seq_num, |
| 223 const MediaConsumer::SendPacketCallback& cbk); | 224 const MediaPacketConsumer::SendPacketCallback& cbk); |
| 224 ~PacketState(); | 225 ~PacketState(); |
| 225 | 226 |
| 226 uint64_t post_consume_rd_; | 227 uint64_t post_consume_rd_; |
| 227 uint32_t seq_num_; | 228 uint32_t seq_num_; |
| 228 MediaConsumer::SendPacketCallback cbk_; | 229 MediaPacketConsumer::SendPacketCallback cbk_; |
| 229 }; | 230 }; |
| 230 using PacketStateQueue = std::deque<PacketState>; | 231 using PacketStateQueue = std::deque<PacketState>; |
| 231 | 232 |
| 232 void HandleSendPacket(uint32_t seq_num, MediaConsumer::SendResult result); | 233 void HandleSendPacket(uint32_t seq_num, |
| 234 MediaPacketConsumer::SendResult result); |
| 233 void HandleFlush(); | 235 void HandleFlush(); |
| 234 void HandleSignalCallback(); | 236 void HandleSignalCallback(); |
| 235 | 237 |
| 236 void UpdateSignalled(); | 238 void UpdateSignalled(); |
| 237 void Fault(MediaResult reason); | 239 void Fault(MediaResult reason); |
| 238 void Cleanup(); | 240 void Cleanup(); |
| 239 | 241 |
| 240 bool Faulted() const { | 242 bool Faulted() const { |
| 241 return (MediaResult::OK != internal_state_); | 243 return (MediaResult::OK != internal_state_); |
| 242 } | 244 } |
| 243 | 245 |
| 244 bool Busy() const { | 246 bool Busy() const { |
| 245 return flush_in_progress_; | 247 return flush_in_progress_; |
| 246 } | 248 } |
| 247 | 249 |
| 248 // Pipe interface callbacks | 250 // Pipe interface callbacks |
| 249 MediaConsumerPtr pipe_; | 251 MediaPacketConsumerPtr pipe_; |
| 250 MediaConsumer::FlushCallback pipe_flush_cbk_; | 252 MediaPacketConsumer::FlushCallback pipe_flush_cbk_; |
| 251 Closure signalled_callback_; | 253 Closure signalled_callback_; |
| 252 | 254 |
| 253 // A small helper which lets us nerf callbacks we may have directly scheduled | 255 // A small helper which lets us nerf callbacks we may have directly scheduled |
| 254 // on the main run loop which may be in flight as we get destroyed. | 256 // on the main run loop which may be in flight as we get destroyed. |
| 255 std::shared_ptr<CircularBufferMediaPipeAdapter*> thiz_; | 257 std::shared_ptr<CircularBufferMediaPipeAdapter*> thiz_; |
| 256 | 258 |
| 257 // State for managing signalled/un-signalled status and signal callbacks. | 259 // State for managing signalled/un-signalled status and signal callbacks. |
| 258 SignalCbk signal_cbk_; | 260 SignalCbk signal_cbk_; |
| 259 bool flush_in_progress_ = false; | 261 bool flush_in_progress_ = false; |
| 260 bool fault_cbk_made_ = false; | 262 bool fault_cbk_made_ = false; |
| 261 bool cbk_scheduled_ = false; | 263 bool cbk_scheduled_ = false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 273 // Packet queue state | 275 // Packet queue state |
| 274 std::atomic<uint32_t> flush_generation_; | 276 std::atomic<uint32_t> flush_generation_; |
| 275 std::atomic<uint32_t> seq_num_gen_; | 277 std::atomic<uint32_t> seq_num_gen_; |
| 276 PacketStateQueue in_flight_queue_; | 278 PacketStateQueue in_flight_queue_; |
| 277 }; | 279 }; |
| 278 | 280 |
| 279 } // namespace media | 281 } // namespace media |
| 280 } // namespace mojo | 282 } // namespace mojo |
| 281 | 283 |
| 282 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ | 284 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ |
| OLD | NEW |