audio_buffer.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 // Create an AudioBuffer whose channel data is copied from |data|. For 27 // interleaved data, only the first buffer is used. For planar data, the 28 // number of buffers must be equal to |channel_count|. |frame_count| is the 29 // number of frames in each buffer. |data| must not be null and |frame_count| 30 // must be >= 0. 31 // 32 // TODO(jrummell): Compute duration rather than pass it in. 33 static scoped_refptr<AudioBuffer> CopyFrom(SampleFormat sample_format, 34 int channel_count, 35 int frame_count, 36 const uint8* const* data, 37 const base::TimeDelta timestamp, 38 const base::TimeDelta duration); 39 40 // Create a AudioBuffer indicating we've reached end of stream. 41 // Calling any method other than end_of_stream() on the resulting buffer 42 // is disallowed. 43 static scoped_refptr<AudioBuffer> CreateEOSBuffer(); 44 45 // Copy frames into |dest|. |frames_to_copy| is the number of frames to copy. 46 // |source_frame_offset| specified how many frames in the buffer to skip 47 // first. |dest_frame_offset| is the frame offset in |dest|. The frames are 48 // converted from their source format into planar float32 data (which is all 49 // that AudioBus handles). 50 void ReadFrames(int frames_to_copy, 51 int source_frame_offset, 52 int dest_frame_offset, 53 AudioBus* dest); 54 55 // Return the number of frames held. 56 int frame_count() const { return frame_count_; } 57 58 // Access to constructor parameters. 59 base::TimeDelta timestamp() const { return timestamp_; } 60 base::TimeDelta duration() const { return duration_; } 61 62 // If there's no data in this buffer, it represents end of stream. 63 bool end_of_stream() const { return data_ == NULL; } 64 65 private: 66 friend class base::RefCountedThreadSafe<AudioBuffer>; 67 68 // Allocates aligned contiguous buffer to hold all channel data (1 block for 69 // interleaved data, |channel_count| blocks for planar data), copies 70 // [data,data+data_size) to the allocated buffer(s). If |data| is null an end 71 // of stream buffer is created. 72 AudioBuffer(SampleFormat sample_format, 73 int channel_count, 74 int frame_count, 75 const uint8* const* data, 76 const base::TimeDelta timestamp, 77 const base::TimeDelta duration); 78 79 virtual ~AudioBuffer(); 80 81 SampleFormat sample_format_; 82 int channel_count_; 83 int frame_count_; 84 base::TimeDelta timestamp_; 85 base::TimeDelta duration_; 86 87 // Contiguous block of channel data. 88 scoped_ptr_malloc<uint8, base::ScopedPtrAlignedFree> data_; 89 90 // For planar data, points to each channels data. 91 std::vector<uint8*> channel_data_; 92 93 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioBuffer); 94}; 95 96} // namespace media 97 98#endif // MEDIA_BASE_AUDIO_BUFFER_H_ 99