1 // Copyright 2013 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_BASE_AUDIO_BUFFER_QUEUE_H_ 6 #define MEDIA_BASE_AUDIO_BUFFER_QUEUE_H_ 7 8 #include <deque> 9 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "media/base/audio_buffer.h" 13 #include "media/base/media_export.h" 14 15 namespace media { 16 17 class AudioBus; 18 19 // A queue of AudioBuffers to support reading of arbitrary chunks of a media 20 // data source. Audio data can be copied into an AudioBus for output. The 21 // current position can be forwarded to anywhere in the buffered data. 22 // 23 // This class is not inherently thread-safe. Concurrent access must be 24 // externally serialized. 25 class MEDIA_EXPORT AudioBufferQueue { 26 public: 27 AudioBufferQueue(); 28 ~AudioBufferQueue(); 29 30 // Clears the buffer queue. 31 void Clear(); 32 33 // Appends |buffer_in| to this queue. 34 void Append(const scoped_refptr<AudioBuffer>& buffer_in); 35 36 // Reads a maximum of |frames| frames into |dest| from the current position. 37 // Returns the number of frames read. The current position will advance by the 38 // amount of frames read. |dest_frame_offset| specifies a starting offset into 39 // |dest|. On each call, the frames are converted from their source format 40 // into the destination AudioBus. 41 int ReadFrames(int frames, int dest_frame_offset, AudioBus* dest); 42 43 // Copies up to |frames| frames from current position to |dest|. Returns 44 // number of frames copied. Doesn't advance current position. Starts at 45 // |source_frame_offset| from current position. |dest_frame_offset| specifies 46 // a starting offset into |dest|. On each call, the frames are converted from 47 // their source format into the destination AudioBus. 48 int PeekFrames(int frames, 49 int source_frame_offset, 50 int dest_frame_offset, 51 AudioBus* dest); 52 53 // Moves the current position forward by |frames| frames. If |frames| exceeds 54 // frames available, the seek operation will fail. 55 void SeekFrames(int frames); 56 57 // Returns the number of frames buffered beyond the current position. 58 int frames() const { return frames_; } 59 60 // Returns the current timestamp, taking into account current offset. The 61 // value calculated based on the timestamp of the current buffer. If timestamp 62 // for the current buffer is set to 0, then returns value that corresponds to 63 // the last position in a buffer that had timestamp set. kNoTimestamp() is 64 // returned if no buffers we read from had timestamp set. 65 base::TimeDelta current_time() const { return current_time_; } 66 67 private: 68 // Definition of the buffer queue. 69 typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue; 70 71 // An internal method shared by ReadFrames() and SeekFrames() that actually 72 // does reading. It reads a maximum of |frames| frames into |dest|. Returns 73 // the number of frames read. The current position will be moved forward by 74 // the number of frames read if |advance_position| is set. If |dest| is NULL, 75 // only the current position will advance but no data will be copied. 76 // |source_frame_offset| can be used to skip frames before reading. 77 // |dest_frame_offset| specifies a starting offset into |dest|. 78 int InternalRead(int frames, 79 bool advance_position, 80 int source_frame_offset, 81 int dest_frame_offset, 82 AudioBus* dest); 83 84 // Updates |current_time_| with the time that corresponds to the specified 85 // position in the buffer. 86 void UpdateCurrentTime(BufferQueue::iterator buffer, int offset); 87 88 BufferQueue::iterator current_buffer_; 89 BufferQueue buffers_; 90 int current_buffer_offset_; 91 92 // Number of frames available to be read in the buffer. 93 int frames_; 94 95 // Keeps track of the most recent time we've seen in case the |buffers_| is 96 // empty when our owner asks what time it is. 97 base::TimeDelta current_time_; 98 99 DISALLOW_COPY_AND_ASSIGN(AudioBufferQueue); 100 }; 101 102 } // namespace media 103 104 #endif // MEDIA_BASE_AUDIO_BUFFER_QUEUE_H_ 105