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