audio_buffer.h revision 010d83a9304c5a91596085d917d248abff47903a
17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file. 47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#ifndef MEDIA_BASE_AUDIO_BUFFER_H_ 67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#define MEDIA_BASE_AUDIO_BUFFER_H_ 77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <vector> 97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/aligned_memory.h" 117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/ref_counted.h" 127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 1423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "media/base/channel_layout.h" 157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "media/base/media_export.h" 167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "media/base/sample_format.h" 177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace media { 197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class AudioBus; 207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// An audio buffer that takes a copy of the data passed to it, holds it, and 227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// copies it into an AudioBus when needed. Also supports an end of stream 237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// marker. 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class MEDIA_EXPORT AudioBuffer 257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) : public base::RefCountedThreadSafe<AudioBuffer> { 267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public: 273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Alignment of each channel's data; this must match what ffmpeg expects 283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // (which may be 0, 16, or 32, depending on the processor). Selecting 32 in 293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // order to work on all processors. 303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) enum { kChannelAlignment = 32 }; 313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Create an AudioBuffer whose channel data is copied from |data|. For 337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // interleaved data, only the first buffer is used. For planar data, the 347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // number of buffers must be equal to |channel_count|. |frame_count| is the 357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // number of frames in each buffer. |data| must not be null and |frame_count| 367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // must be >= 0. 377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static scoped_refptr<AudioBuffer> CopyFrom(SampleFormat sample_format, 3823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) ChannelLayout channel_layout, 39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch int channel_count, 4023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int sample_rate, 417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) int frame_count, 427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const uint8* const* data, 43010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const base::TimeDelta timestamp); 447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 45a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Create an AudioBuffer with |frame_count| frames. Buffer is allocated, but 46a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // not initialized. Timestamp and duration are set to kNoTimestamp(). 47a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) static scoped_refptr<AudioBuffer> CreateBuffer(SampleFormat sample_format, 4823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) ChannelLayout channel_layout, 49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch int channel_count, 5023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int sample_rate, 51a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) int frame_count); 52a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Create an empty AudioBuffer with |frame_count| frames. 547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch static scoped_refptr<AudioBuffer> CreateEmptyBuffer( 5523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) ChannelLayout channel_layout, 56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch int channel_count, 5723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int sample_rate, 587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch int frame_count, 59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const base::TimeDelta timestamp); 607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Create a AudioBuffer indicating we've reached end of stream. 627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Calling any method other than end_of_stream() on the resulting buffer 637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // is disallowed. 647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static scoped_refptr<AudioBuffer> CreateEOSBuffer(); 657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Copy frames into |dest|. |frames_to_copy| is the number of frames to copy. 677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // |source_frame_offset| specifies how many frames in the buffer to skip 687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // first. |dest_frame_offset| is the frame offset in |dest|. The frames are 697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // converted from their source format into planar float32 data (which is all 707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // that AudioBus handles). 717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void ReadFrames(int frames_to_copy, 727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) int source_frame_offset, 737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) int dest_frame_offset, 747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) AudioBus* dest); 757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Trim an AudioBuffer by removing |frames_to_trim| frames from the start. 77a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Timestamp and duration are adjusted to reflect the fewer frames. 787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Note that repeated calls to TrimStart() may result in timestamp() and 797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // duration() being off by a few microseconds due to rounding issues. 807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch void TrimStart(int frames_to_trim); 817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 82a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Trim an AudioBuffer by removing |frames_to_trim| frames from the end. 83a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Duration is adjusted to reflect the fewer frames. 84a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) void TrimEnd(int frames_to_trim); 85a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Trim an AudioBuffer by removing |end - start| frames from [|start|, |end|). 87010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Even if |start| is zero, timestamp() is not adjusted, only duration(). 88010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) void TrimRange(int start, int end); 89010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Return the number of channels. 917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch int channel_count() const { return channel_count_; } 927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Return the number of frames held. 947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch int frame_count() const { return adjusted_frame_count_; } 957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 9623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // Return the sample rate. 9723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int sample_rate() const { return sample_rate_; } 9823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 9923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // Return the channel layout. 10023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) ChannelLayout channel_layout() const { return channel_layout_; } 10123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::TimeDelta timestamp() const { return timestamp_; } 1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::TimeDelta duration() const { return duration_; } 1047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch void set_timestamp(base::TimeDelta timestamp) { timestamp_ = timestamp; } 1057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // If there's no data in this buffer, it represents end of stream. 1077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch bool end_of_stream() const { return end_of_stream_; } 1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 109a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Access to the raw buffer for ffmpeg to write directly to. Data for planar 1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // data is grouped by channel. There is only 1 entry for interleaved formats. 1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::vector<uint8*>& channel_data() const { return channel_data_; } 112a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private: 1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) friend class base::RefCountedThreadSafe<AudioBuffer>; 1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Allocates aligned contiguous buffer to hold all channel data (1 block for 1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // interleaved data, |channel_count| blocks for planar data), copies 118a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // [data,data+data_size) to the allocated buffer(s). If |data| is null, no 119a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // data is copied. If |create_buffer| is false, no data buffer is created (or 120a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // copied to). 1217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) AudioBuffer(SampleFormat sample_format, 12223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) ChannelLayout channel_layout, 123effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch int channel_count, 12423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) int sample_rate, 1257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) int frame_count, 126a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) bool create_buffer, 1277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const uint8* const* data, 128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const base::TimeDelta timestamp); 1297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) virtual ~AudioBuffer(); 1317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const SampleFormat sample_format_; 13323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) const ChannelLayout channel_layout_; 1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const int channel_count_; 13523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) const int sample_rate_; 1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch int adjusted_frame_count_; 1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch int trim_start_; 1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const bool end_of_stream_; 1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::TimeDelta timestamp_; 1407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::TimeDelta duration_; 1417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Contiguous block of channel data. 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_ptr<uint8, base::AlignedFreeDeleter> data_; 1447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // For planar data, points to each channels data. 1467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) std::vector<uint8*> channel_data_; 1477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) DISALLOW_IMPLICIT_CONSTRUCTORS(AudioBuffer); 1497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}; 1507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} // namespace media 1527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif // MEDIA_BASE_AUDIO_BUFFER_H_ 154