audio_buffer_queue.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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. |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 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  // |source_frame_offset| can be used to skip frames before reading.
70  // |dest_frame_offset| specifies a starting offset into |dest|.
71  int InternalRead(int frames,
72                   bool advance_position,
73                   int source_frame_offset,
74                   int dest_frame_offset,
75                   AudioBus* dest);
76
77  BufferQueue::iterator current_buffer_;
78  BufferQueue buffers_;
79  int current_buffer_offset_;
80
81  // Number of frames available to be read in the buffer.
82  int frames_;
83
84  DISALLOW_COPY_AND_ASSIGN(AudioBufferQueue);
85};
86
87}  // namespace media
88
89#endif  // MEDIA_BASE_AUDIO_BUFFER_QUEUE_H_
90