summaryrefslogtreecommitdiffstats
path: root/media/base/audio_splicer.h
blob: 22cce4728687443fae8b6f6299fac8911fcec906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_AUDIO_SPLICER_H_
#define MEDIA_BASE_AUDIO_SPLICER_H_

#include <deque>

#include "base/memory/ref_counted.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/media_export.h"

namespace media {

class AudioDecoderConfig;
class DataBuffer;

// Helper class that handles filling gaps and resolving overlaps.
class MEDIA_EXPORT AudioSplicer {
 public:
  AudioSplicer(int bytes_per_frame, int samples_per_second);
  ~AudioSplicer();

  // Resets the splicer state by clearing the output buffers queue,
  // and resetting the timestamp helper.
  void Reset();

  // Adds a new buffer full of samples or end of stream buffer to the splicer.
  // Returns true if the buffer was accepted. False is returned if an error
  // occurred.
  bool AddInput(const scoped_refptr<DataBuffer>& input);

  // Returns true if the splicer has a buffer to return.
  bool HasNextBuffer() const;

  // Removes the next buffer from the output buffer queue and returns it.
  // This should only be called if HasNextBuffer() returns true.
  scoped_refptr<DataBuffer> GetNextBuffer();

 private:
  void AddOutputBuffer(const scoped_refptr<DataBuffer>& buffer);

  AudioTimestampHelper output_timestamp_helper_;

  // Minimum gap size needed before the splicer will take action to
  // fill a gap. This avoids periodically inserting and then dropping samples
  // when the buffer timestamps are slightly off because of timestamp rounding
  // in the source content.
  int min_gap_size_;

  std::deque<scoped_refptr<DataBuffer> > output_buffers_;
  bool received_end_of_stream_;

  DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSplicer);
};

}  // namespace media

#endif