Move chrome://proximity-auth to only be accessible on ChromeOS.
[chromium-blink-merge.git] / media / base / audio_buffer.h
blob63390d4781cacca98c636d465b0e75c28fecd50f
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.
5 #ifndef MEDIA_BASE_AUDIO_BUFFER_H_
6 #define MEDIA_BASE_AUDIO_BUFFER_H_
8 #include <vector>
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/channel_layout.h"
15 #include "media/base/media_export.h"
16 #include "media/base/sample_format.h"
18 namespace media {
19 class AudioBus;
21 // An audio buffer that takes a copy of the data passed to it, holds it, and
22 // copies it into an AudioBus when needed. Also supports an end of stream
23 // marker.
24 class MEDIA_EXPORT AudioBuffer
25 : public base::RefCountedThreadSafe<AudioBuffer> {
26 public:
27 // Alignment of each channel's data; this must match what ffmpeg expects
28 // (which may be 0, 16, or 32, depending on the processor). Selecting 32 in
29 // order to work on all processors.
30 enum { kChannelAlignment = 32 };
32 // Create an AudioBuffer whose channel data is copied from |data|. For
33 // interleaved data, only the first buffer is used. For planar data, the
34 // number of buffers must be equal to |channel_count|. |frame_count| is the
35 // number of frames in each buffer. |data| must not be null and |frame_count|
36 // must be >= 0.
37 static scoped_refptr<AudioBuffer> CopyFrom(SampleFormat sample_format,
38 ChannelLayout channel_layout,
39 int channel_count,
40 int sample_rate,
41 int frame_count,
42 const uint8* const* data,
43 const base::TimeDelta timestamp);
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 ChannelLayout channel_layout,
49 int channel_count,
50 int sample_rate,
51 int frame_count);
53 // Create an empty AudioBuffer with |frame_count| frames.
54 static scoped_refptr<AudioBuffer> CreateEmptyBuffer(
55 ChannelLayout channel_layout,
56 int channel_count,
57 int sample_rate,
58 int frame_count,
59 const base::TimeDelta timestamp);
61 // Create a AudioBuffer indicating we've reached end of stream.
62 // Calling any method other than end_of_stream() on the resulting buffer
63 // is disallowed.
64 static scoped_refptr<AudioBuffer> CreateEOSBuffer();
66 // Copy frames into |dest|. |frames_to_copy| is the number of frames to copy.
67 // |source_frame_offset| specifies how many frames in the buffer to skip
68 // first. |dest_frame_offset| is the frame offset in |dest|. The frames are
69 // converted from their source format into planar float32 data (which is all
70 // that AudioBus handles).
71 void ReadFrames(int frames_to_copy,
72 int source_frame_offset,
73 int dest_frame_offset,
74 AudioBus* dest);
76 // Copy |frames_to_copy| frames into |dest|, |frames_to_copy| is the number of
77 // frames to copy. The frames are converted from their source format into
78 // interleaved int32.
79 void ReadFramesInterleavedS32(int frames_to_copy, int32* dest);
81 // Copy |frames_to_copy| frames into |dest|, |frames_to_copy| is the number of
82 // frames to copy. The frames are converted from their source format into
83 // interleaved int16.
84 void ReadFramesInterleavedS16(int frames_to_copy, int16* dest);
86 // Trim an AudioBuffer by removing |frames_to_trim| frames from the start.
87 // Timestamp and duration are adjusted to reflect the fewer frames.
88 // Note that repeated calls to TrimStart() may result in timestamp() and
89 // duration() being off by a few microseconds due to rounding issues.
90 void TrimStart(int frames_to_trim);
92 // Trim an AudioBuffer by removing |frames_to_trim| frames from the end.
93 // Duration is adjusted to reflect the fewer frames.
94 void TrimEnd(int frames_to_trim);
96 // Trim an AudioBuffer by removing |end - start| frames from [|start|, |end|).
97 // Even if |start| is zero, timestamp() is not adjusted, only duration().
98 void TrimRange(int start, int end);
100 // Return the number of channels.
101 int channel_count() const { return channel_count_; }
103 // Return the number of frames held.
104 int frame_count() const { return adjusted_frame_count_; }
106 // Return the sample rate.
107 int sample_rate() const { return sample_rate_; }
109 // Return the channel layout.
110 ChannelLayout channel_layout() const { return channel_layout_; }
112 base::TimeDelta timestamp() const { return timestamp_; }
113 base::TimeDelta duration() const { return duration_; }
114 void set_timestamp(base::TimeDelta timestamp) { timestamp_ = timestamp; }
116 // If there's no data in this buffer, it represents end of stream.
117 bool end_of_stream() const { return end_of_stream_; }
119 // Access to the raw buffer for ffmpeg to write directly to. Data for planar
120 // data is grouped by channel. There is only 1 entry for interleaved formats.
121 const std::vector<uint8*>& channel_data() const { return channel_data_; }
123 private:
124 friend class base::RefCountedThreadSafe<AudioBuffer>;
126 // Allocates aligned contiguous buffer to hold all channel data (1 block for
127 // interleaved data, |channel_count| blocks for planar data), copies
128 // [data,data+data_size) to the allocated buffer(s). If |data| is null, no
129 // data is copied. If |create_buffer| is false, no data buffer is created (or
130 // copied to).
131 AudioBuffer(SampleFormat sample_format,
132 ChannelLayout channel_layout,
133 int channel_count,
134 int sample_rate,
135 int frame_count,
136 bool create_buffer,
137 const uint8* const* data,
138 const base::TimeDelta timestamp);
140 virtual ~AudioBuffer();
142 const SampleFormat sample_format_;
143 const ChannelLayout channel_layout_;
144 const int channel_count_;
145 const int sample_rate_;
146 int adjusted_frame_count_;
147 int trim_start_;
148 const bool end_of_stream_;
149 base::TimeDelta timestamp_;
150 base::TimeDelta duration_;
152 // Contiguous block of channel data.
153 scoped_ptr<uint8, base::AlignedFreeDeleter> data_;
155 // For planar data, points to each channels data.
156 std::vector<uint8*> channel_data_;
158 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioBuffer);
161 } // namespace media
163 #endif // MEDIA_BASE_AUDIO_BUFFER_H_