| 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> |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 * A callback definition, registered by users of the adapter. The callback | 63 * A callback definition, registered by users of the adapter. The callback |
| 64 * will be called any time the adapter is in the signalled state, either | 64 * will be called any time the adapter is in the signalled state, either |
| 65 * because there is room for more data in the pipe, or because the pipe has | 65 * because there is room for more data in the pipe, or because the pipe has |
| 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 /** |
| 74 * Constructor |
| 75 * |
| 76 * Create an adapter which will take ownership of the provided MediaPipe |
| 77 * interface and assist in the process of generating MediaPackets and |
| 78 * marshalling them to the other side of the MediaPipe. |
| 79 * |
| 80 * @param pipe A pointer to the MediaPipe interface which will be used as the |
| 81 * target for MediaPackets. |
| 82 */ |
| 73 explicit CircularBufferMediaPipeAdapter(MediaPipePtr pipe); | 83 explicit CircularBufferMediaPipeAdapter(MediaPipePtr pipe); |
| 84 |
| 85 /** |
| 86 * Destructor |
| 87 */ |
| 74 ~CircularBufferMediaPipeAdapter(); | 88 ~CircularBufferMediaPipeAdapter(); |
| 75 | 89 |
| 76 /** | 90 /** |
| 91 * Init |
| 92 * |
| 93 * Allocate a shared memory buffer of the specified size and begin the process |
| 94 * of marshalling it to the other side of the MediaPipe. |
| 95 * |
| 96 * @param size The size in bytes of the shared memory buffer to allocate. |
| 97 */ |
| 98 void Init(uint64_t size); |
| 99 |
| 100 /** |
| 77 * Set the signal callback for this media pipe adapter. This callback will be | 101 * Set the signal callback for this media pipe adapter. This callback will be |
| 78 * called when the adapter transitions from un-signalled to signalled and has | 102 * called when the adapter transitions from un-signalled to signalled and has |
| 79 * a valid callback, or when a valid callback is assigned (via a call to | 103 * a valid callback, or when a valid callback is assigned (via a call to |
| 80 * Setllback) and the adapter is currently in the signalled state. | 104 * Setllback) and the adapter is currently in the signalled state. |
| 81 * | 105 * |
| 82 * Any callbacks in flight are guaranteed to be canceled or executed to | 106 * Any callbacks in flight are guaranteed to be canceled or executed to |
| 83 * completion when SetCallback returns. | 107 * completion when SetCallback returns. |
| 84 * | 108 * |
| 85 * It is the adapter's user's responsibility to manage the lifetime of the | 109 * It is the adapter's user's responsibility to manage the lifetime of the |
| 86 * callback and the adapter, and ensure that the callback is valid as long as | 110 * callback and the adapter, and ensure that the callback is valid as long as |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 uint32_t seq_num, | 222 uint32_t seq_num, |
| 199 const MediaPipe::SendPacketCallback& cbk); | 223 const MediaPipe::SendPacketCallback& cbk); |
| 200 ~PacketState(); | 224 ~PacketState(); |
| 201 | 225 |
| 202 uint64_t post_consume_rd_; | 226 uint64_t post_consume_rd_; |
| 203 uint32_t seq_num_; | 227 uint32_t seq_num_; |
| 204 MediaPipe::SendPacketCallback cbk_; | 228 MediaPipe::SendPacketCallback cbk_; |
| 205 }; | 229 }; |
| 206 using PacketStateQueue = std::deque<PacketState>; | 230 using PacketStateQueue = std::deque<PacketState>; |
| 207 | 231 |
| 208 void HandleGetState(MediaPipeStatePtr state); | |
| 209 void HandleSendPacket(uint32_t seq_num, MediaPipe::SendResult result); | 232 void HandleSendPacket(uint32_t seq_num, MediaPipe::SendResult result); |
| 210 void HandleFlush(); | 233 void HandleFlush(); |
| 211 void HandleSignalCallback(); | 234 void HandleSignalCallback(); |
| 212 | 235 |
| 213 void UpdateSignalled(); | 236 void UpdateSignalled(); |
| 214 void Fault(MediaResult reason); | 237 void Fault(MediaResult reason); |
| 215 void Cleanup(); | 238 void Cleanup(); |
| 216 | 239 |
| 217 bool Faulted() const { | 240 bool Faulted() const { |
| 218 return (MediaResult::OK != internal_state_); | 241 return (MediaResult::OK != internal_state_); |
| 219 } | 242 } |
| 220 | 243 |
| 221 bool Busy() const { | 244 bool Busy() const { |
| 222 return (get_state_in_progress_ || flush_in_progress_); | 245 return flush_in_progress_; |
| 223 } | 246 } |
| 224 | 247 |
| 225 // Pipe interface callbacks | 248 // Pipe interface callbacks |
| 226 MediaPipePtr pipe_; | 249 MediaPipePtr pipe_; |
| 227 MediaPipe::GetStateCallback pipe_get_state_cbk_; | |
| 228 MediaPipe::FlushCallback pipe_flush_cbk_; | 250 MediaPipe::FlushCallback pipe_flush_cbk_; |
| 229 Closure signalled_callback_; | 251 Closure signalled_callback_; |
| 230 | 252 |
| 231 // A small helper which lets us nerf callbacks we may have directly scheduled | 253 // A small helper which lets us nerf callbacks we may have directly scheduled |
| 232 // on the main run loop which may be in flight as we get destroyed. | 254 // on the main run loop which may be in flight as we get destroyed. |
| 233 std::shared_ptr<CircularBufferMediaPipeAdapter*> thiz_; | 255 std::shared_ptr<CircularBufferMediaPipeAdapter*> thiz_; |
| 234 | 256 |
| 235 // State for managing signalled/un-signalled status and signal callbacks. | 257 // State for managing signalled/un-signalled status and signal callbacks. |
| 236 SignalCbk signal_cbk_; | 258 SignalCbk signal_cbk_; |
| 237 bool get_state_in_progress_ = true; | |
| 238 bool flush_in_progress_ = false; | 259 bool flush_in_progress_ = false; |
| 239 bool fault_cbk_made_ = false; | 260 bool fault_cbk_made_ = false; |
| 240 bool cbk_scheduled_ = false; | 261 bool cbk_scheduled_ = false; |
| 241 uint64_t hi_water_mark_ = 0u; | 262 uint64_t hi_water_mark_ = 0u; |
| 242 uint64_t lo_water_mark_ = 0u; | 263 uint64_t lo_water_mark_ = 0u; |
| 243 bool signalled_ = false; | 264 bool signalled_ = false; |
| 244 | 265 |
| 245 MediaResult internal_state_ = MediaResult::OK; | 266 MediaResult internal_state_ = MediaResult::OK; |
| 246 | 267 |
| 247 ScopedSharedBufferHandle buffer_handle_; | 268 ScopedSharedBufferHandle buffer_handle_; |
| 248 void* buffer_ = nullptr; | 269 void* buffer_ = nullptr; |
| 249 uint64_t buffer_size_; | 270 uint64_t buffer_size_ = 0; |
| 250 uint64_t rd_, wr_; | 271 uint64_t rd_, wr_; |
| 251 | 272 |
| 252 // Packet queue state | 273 // Packet queue state |
| 253 std::atomic<uint32_t> flush_generation_; | 274 std::atomic<uint32_t> flush_generation_; |
| 254 std::atomic<uint32_t> seq_num_gen_; | 275 std::atomic<uint32_t> seq_num_gen_; |
| 255 PacketStateQueue in_flight_queue_; | 276 PacketStateQueue in_flight_queue_; |
| 256 }; | 277 }; |
| 257 | 278 |
| 258 } // namespace media | 279 } // namespace media |
| 259 } // namespace mojo | 280 } // namespace mojo |
| 260 | 281 |
| 261 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ | 282 #endif // MOJO_SERVICES_MEDIA_COMMON_CPP_CIRCULAR_BUFFER_MEDIA_PIPE_ADAPTER_H_ |
| OLD | NEW |