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