audio_buffer.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_H_
6#define MEDIA_BASE_AUDIO_BUFFER_H_
7
8#include <vector>
9
10#include "base/memory/aligned_memory.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/time/time.h"
14#include "media/base/media_export.h"
15#include "media/base/sample_format.h"
16
17namespace media {
18class AudioBus;
19
20// An audio buffer that takes a copy of the data passed to it, holds it, and
21// copies it into an AudioBus when needed. Also supports an end of stream
22// marker.
23class MEDIA_EXPORT AudioBuffer
24    : public base::RefCountedThreadSafe<AudioBuffer> {
25 public:
26  // Alignment of each channel's data; this must match what ffmpeg expects
27  // (which may be 0, 16, or 32, depending on the processor). Selecting 32 in
28  // order to work on all processors.
29  enum { kChannelAlignment = 32 };
30
31  // Create an AudioBuffer whose channel data is copied from |data|. For
32  // interleaved data, only the first buffer is used. For planar data, the
33  // number of buffers must be equal to |channel_count|. |frame_count| is the
34  // number of frames in each buffer. |data| must not be null and |frame_count|
35  // must be >= 0.
36  //
37  // TODO(jrummell): Compute duration rather than pass it in.
38  static scoped_refptr<AudioBuffer> CopyFrom(SampleFormat sample_format,
39                                             int channel_count,
40                                             int frame_count,
41                                             const uint8* const* data,
42                                             const base::TimeDelta timestamp,
43                                             const base::TimeDelta duration);
44
45  // Create an AudioBuffer with |frame_count| frames. Buffer is allocated, but
46  // not initialized. Timestamp and duration are set to kNoTimestamp().
47  static scoped_refptr<AudioBuffer> CreateBuffer(SampleFormat sample_format,
48                                                 int channel_count,
49                                                 int frame_count);
50
51  // Create an empty AudioBuffer with |frame_count| frames.
52  static scoped_refptr<AudioBuffer> CreateEmptyBuffer(
53      int channel_count,
54      int frame_count,
55      const base::TimeDelta timestamp,
56      const base::TimeDelta duration);
57
58  // Create a AudioBuffer indicating we've reached end of stream.
59  // Calling any method other than end_of_stream() on the resulting buffer
60  // is disallowed.
61  static scoped_refptr<AudioBuffer> CreateEOSBuffer();
62
63  // Copy frames into |dest|. |frames_to_copy| is the number of frames to copy.
64  // |source_frame_offset| specifies how many frames in the buffer to skip
65  // first. |dest_frame_offset| is the frame offset in |dest|. The frames are
66  // converted from their source format into planar float32 data (which is all
67  // that AudioBus handles).
68  void ReadFrames(int frames_to_copy,
69                  int source_frame_offset,
70                  int dest_frame_offset,
71                  AudioBus* dest);
72
73  // Trim an AudioBuffer by removing |frames_to_trim| frames from the start.
74  // Timestamp and duration are adjusted to reflect the fewer frames.
75  // Note that repeated calls to TrimStart() may result in timestamp() and
76  // duration() being off by a few microseconds due to rounding issues.
77  void TrimStart(int frames_to_trim);
78
79  // Trim an AudioBuffer by removing |frames_to_trim| frames from the end.
80  // Duration is adjusted to reflect the fewer frames.
81  void TrimEnd(int frames_to_trim);
82
83  // Return the number of channels.
84  int channel_count() const { return channel_count_; }
85
86  // Return the number of frames held.
87  int frame_count() const { return adjusted_frame_count_; }
88
89  // Access to constructor parameters.
90  base::TimeDelta timestamp() const { return timestamp_; }
91  base::TimeDelta duration() const { return duration_; }
92
93  // TODO(jrummell): Remove set_timestamp() and set_duration() once
94  // DecryptingAudioDecoder::EnqueueFrames() is changed to set them when
95  // creating the buffer. See http://crbug.com/255261.
96  void set_timestamp(base::TimeDelta timestamp) { timestamp_ = timestamp; }
97  void set_duration(base::TimeDelta duration) { duration_ = duration; }
98
99  // If there's no data in this buffer, it represents end of stream.
100  bool end_of_stream() const { return end_of_stream_; }
101
102  // Access to the raw buffer for ffmpeg to write directly to. Data for planar
103  // data is grouped by channel. There is only 1 entry for interleaved formats.
104  const std::vector<uint8*>& channel_data() const { return channel_data_; }
105
106 private:
107  friend class base::RefCountedThreadSafe<AudioBuffer>;
108
109  // Allocates aligned contiguous buffer to hold all channel data (1 block for
110  // interleaved data, |channel_count| blocks for planar data), copies
111  // [data,data+data_size) to the allocated buffer(s). If |data| is null, no
112  // data is copied. If |create_buffer| is false, no data buffer is created (or
113  // copied to).
114  AudioBuffer(SampleFormat sample_format,
115              int channel_count,
116              int frame_count,
117              bool create_buffer,
118              const uint8* const* data,
119              const base::TimeDelta timestamp,
120              const base::TimeDelta duration);
121
122  virtual ~AudioBuffer();
123
124  const SampleFormat sample_format_;
125  const int channel_count_;
126  int adjusted_frame_count_;
127  int trim_start_;
128  const bool end_of_stream_;
129  base::TimeDelta timestamp_;
130  base::TimeDelta duration_;
131
132  // Contiguous block of channel data.
133  scoped_ptr<uint8, base::AlignedFreeDeleter> data_;
134
135  // For planar data, points to each channels data.
136  std::vector<uint8*> channel_data_;
137
138  DISALLOW_IMPLICIT_CONSTRUCTORS(AudioBuffer);
139};
140
141}  // namespace media
142
143#endif  // MEDIA_BASE_AUDIO_BUFFER_H_
144