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 MOJO_MEDIA_MODELS_LPCM_FRAMES_H_ |
| 6 #define MOJO_MEDIA_MODELS_LPCM_FRAMES_H_ |
| 7 |
| 8 #include <cstdint> |
| 9 #include <functional> |
| 10 |
| 11 #include "base/logging.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace media { |
| 15 |
| 16 // References an LPCM frame buffer and implements advancement through it. |
| 17 class LpcmFrames { |
| 18 public: |
| 19 LpcmFrames(); |
| 20 |
| 21 ~LpcmFrames(); |
| 22 |
| 23 void set_bytes_per_frame(uint32_t bytes_per_frame) { |
| 24 bytes_per_frame_ = bytes_per_frame; |
| 25 } |
| 26 |
| 27 uint32_t bytes_per_frame() const { |
| 28 return bytes_per_frame_; |
| 29 } |
| 30 |
| 31 // The remaining frame buffer. |
| 32 void* buffer() const { |
| 33 return remaining_buffer_; |
| 34 } |
| 35 |
| 36 // The remaining number of frames accommodated by the frame buffer. |
| 37 uint64_t frame_count() const { |
| 38 return remaining_frame_count_; |
| 39 } |
| 40 |
| 41 // Resets the buffer and frame. |
| 42 void reset() { |
| 43 remaining_buffer_ = nullptr; |
| 44 remaining_frame_count_ = 0; |
| 45 exhausted_callback_ = nullptr; |
| 46 } |
| 47 |
| 48 // Sets the buffer and frame count. |
| 49 using ExhaustedCallback = std::function<void()>; |
| 50 void set( |
| 51 void* buffer, |
| 52 uint64_t frame_count, |
| 53 ExhaustedCallback exhausted_callback = nullptr) { |
| 54 remaining_buffer_ = buffer; |
| 55 remaining_frame_count_ = frame_count; |
| 56 exhausted_callback_ = exhausted_callback; |
| 57 } |
| 58 |
| 59 // Updates buffer and frame_count to reflect use of the buffer. |
| 60 void advance(uint64_t frame_count) { |
| 61 DCHECK(remaining_buffer_); |
| 62 DCHECK(frame_count <= remaining_frame_count_); |
| 63 remaining_buffer_ = reinterpret_cast<uint8_t*>(remaining_buffer_) + |
| 64 (frame_count * bytes_per_frame_); |
| 65 remaining_frame_count_ -= frame_count; |
| 66 if (remaining_frame_count_ == 0 && exhausted_callback_ != nullptr) { |
| 67 exhausted_callback_(); |
| 68 exhausted_callback_ = nullptr; |
| 69 } |
| 70 } |
| 71 |
| 72 private: |
| 73 uint32_t bytes_per_frame_; |
| 74 void* remaining_buffer_; |
| 75 uint64_t remaining_frame_count_; |
| 76 ExhaustedCallback exhausted_callback_; |
| 77 }; |
| 78 |
| 79 } // namespace media |
| 80 } // namespace mojo |
| 81 |
| 82 #endif // MOJO_MEDIA_MODELS_LPCM_FRAMES_H_ |
OLD | NEW |