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