audio_buffer_queue.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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
15namespace media {
16
17class 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.
25class 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.
39  int ReadFrames(int frames, AudioBus* dest);
40
41  // Copies up to |frames| frames from current position to |dest|. Returns
42  // number of frames copied. Doesn't advance current position. Starts at
43  // |forward_offset| from current position.
44  int PeekFrames(int frames, int forward_offset, AudioBus* dest);
45
46  // Moves the current position forward by |frames| frames. If |frames| exceeds
47  // frames available, the seek operation will fail.
48  void SeekFrames(int frames);
49
50  // Returns the number of frames buffered beyond the current position.
51  int frames() const { return frames_; }
52
53  // Returns the current timestamp, taking into account current offset. The
54  // value calculated based on the timestamp of the current buffer. If timestamp
55  // for the current buffer is set to 0, then returns value that corresponds to
56  // the last position in a buffer that had timestamp set. kNoTimestamp() is
57  // returned if no buffers we read from had timestamp set.
58  base::TimeDelta current_time() const { return current_time_; }
59
60 private:
61  // Definition of the buffer queue.
62  typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue;
63
64  // An internal method shared by ReadFrames() and SeekFrames() that actually
65  // does reading. It reads a maximum of |frames| frames into |dest|. Returns
66  // the number of frames read. The current position will be moved forward by
67  // the number of frames read if |advance_position| is set. If |dest| is NULL,
68  // only the current position will advance but no data will be copied.
69  // |forward_offset| can be used to skip frames before reading.
70  int InternalRead(int frames,
71                   bool advance_position,
72                   int forward_offset,
73                   AudioBus* dest);
74
75  // Updates |current_time_| with the time that corresponds to the specified
76  // position in the buffer.
77  void UpdateCurrentTime(BufferQueue::iterator buffer, int offset);
78
79  BufferQueue::iterator current_buffer_;
80  BufferQueue buffers_;
81  int current_buffer_offset_;
82
83  // Number of frames available to be read in the buffer.
84  int frames_;
85
86  // Keeps track of the most recent time we've seen in case the |buffers_| is
87  // empty when our owner asks what time it is.
88  base::TimeDelta current_time_;
89
90  DISALLOW_COPY_AND_ASSIGN(AudioBufferQueue);
91};
92
93}  // namespace media
94
95#endif  // MEDIA_BASE_AUDIO_BUFFER_QUEUE_H_
96