389607ae2c2cc1046b6ba6ac79711a5b315907c5
[platform/framework/web/crosswalk.git] / src / media / base / audio_splicer.h
1 // Copyright (c) 2012 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_SPLICER_H_
6 #define MEDIA_BASE_AUDIO_SPLICER_H_
7
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "media/audio/audio_parameters.h"
12 #include "media/base/buffers.h"
13 #include "media/base/media_export.h"
14
15 namespace media {
16
17 class AudioBuffer;
18 class AudioBus;
19 class AudioStreamSanitizer;
20
21 // Helper class that handles filling gaps and resolving overlaps.
22 class MEDIA_EXPORT AudioSplicer {
23  public:
24   explicit AudioSplicer(int samples_per_second);
25   ~AudioSplicer();
26
27   enum {
28     // The number of ms to crossfade before trimming when buffers overlap.
29     kCrossfadeDurationInMilliseconds = 5,
30   };
31
32   // Resets the splicer state by clearing the output buffers queue and resetting
33   // the timestamp helper.
34   void Reset();
35
36   // Adds a new buffer full of samples or end of stream buffer to the splicer.
37   // Returns true if the buffer was accepted.  False is returned if an error
38   // occurred.
39   bool AddInput(const scoped_refptr<AudioBuffer>& input);
40
41   // Returns true if the splicer has a buffer to return.
42   bool HasNextBuffer() const;
43
44   // Removes the next buffer from the output buffer queue and returns it; this
45   // should only be called if HasNextBuffer() returns true.
46   scoped_refptr<AudioBuffer> GetNextBuffer();
47
48   // Indicates an upcoming splice point.  All buffers overlapping or after the
49   // |splice_timestamp| will be considered as "before the splice."  Clients must
50   // then call SetSpliceTimestamp(kNoTimestamp()) to signal that future buffers
51   // should be considered as "after the splice."
52   //
53   // Once |kCrossfadeDurationInMilliseconds| of buffers "after the splice" or
54   // end of stream has been received, the "after" buffers will be crossfaded
55   // with all "before" buffers which overlap them.  "before" buffers outside
56   // of the overlap range will be discarded.
57   void SetSpliceTimestamp(base::TimeDelta splice_timestamp);
58
59  private:
60   friend class AudioSplicerTest;
61
62   // Extracts frames to be crossfaded from |pre_splice_sanitizer_|.  Transfers
63   // all frames before |splice_timestamp_| into |output_sanitizer_| and drops
64   // frames outside of the crossfade duration.
65   //
66   // The size of the returned AudioBus is the crossfade duration in frames.
67   // Crossfade duration is calculated based on the number of frames available
68   // after |splice_timestamp_| in each sanitizer and capped by
69   // |max_crossfade_duration_|.
70   //
71   // |pre_splice_sanitizer_| will be empty after this operation.
72   scoped_ptr<AudioBus> ExtractCrossfadeFromPreSplice(
73       scoped_refptr<AudioBuffer>* crossfade_buffer);
74
75   // Crossfades |pre_splice_bus->frames()| frames from
76   // |post_splice_sanitizer_|
77   // with those from |pre_splice_bus|.  Adds the crossfaded buffer to
78   // |output_sanitizer_| along with all buffers in |post_splice_sanitizer_|.
79   //
80   // |post_splice_sanitizer_| will be empty after this operation.
81   void CrossfadePostSplice(scoped_ptr<AudioBus> pre_splice_bus,
82                            scoped_refptr<AudioBuffer> crossfade_buffer);
83
84   // Reset the splice and splice end timestamps.
85   void reset_splice_timestamps() {
86     splice_timestamp_ = max_splice_end_timestamp_ = kNoTimestamp();
87   }
88
89   const base::TimeDelta max_crossfade_duration_;
90   base::TimeDelta splice_timestamp_;
91   base::TimeDelta max_splice_end_timestamp_;
92
93   // The various sanitizers for each stage of the crossfade process.  Buffers in
94   // |output_sanitizer_| are immediately available for consumption by external
95   // callers.
96   //
97   // Overlapped buffers go into the |pre_splice_sanitizer_| while overlapping
98   // buffers go into the |post_splice_sanitizer_|.  Once enough buffers for
99   // crossfading are received the pre and post sanitizers are drained into
100   // |output_sanitizer_| by the two ExtractCrossfadeFromXXX methods above.
101   //
102   // |pre_splice_sanitizer_| is not constructed until the first splice frame is
103   // encountered.  At which point it is constructed based on the timestamp state
104   // of |output_sanitizer_|.  It is destructed once the splice is finished.
105   scoped_ptr<AudioStreamSanitizer> output_sanitizer_;
106   scoped_ptr<AudioStreamSanitizer> pre_splice_sanitizer_;
107   scoped_ptr<AudioStreamSanitizer> post_splice_sanitizer_;
108
109   // Whether all buffers which should go into |pre_splice_sanitizer_| have been
110   // received.  If true, buffers should now be put in |post_splice_sanitizer_|.
111   bool have_all_pre_splice_buffers_;
112
113   DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSplicer);
114 };
115
116 }  // namespace media
117
118 #endif