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