| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BASE_AUDIO_SPLICER_H_ | 5 #ifndef MEDIA_BASE_AUDIO_SPLICER_H_ |
| 6 #define MEDIA_BASE_AUDIO_SPLICER_H_ | 6 #define MEDIA_BASE_AUDIO_SPLICER_H_ |
| 7 | 7 |
| 8 #include <deque> | |
| 9 | |
| 10 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 11 #include "media/base/audio_timestamp_helper.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/time.h" |
| 12 #include "media/base/media_export.h" | 11 #include "media/base/media_export.h" |
| 13 | 12 |
| 14 namespace media { | 13 namespace media { |
| 15 | 14 |
| 16 class AudioBuffer; | 15 class AudioBuffer; |
| 17 class AudioDecoderConfig; | 16 class AudioBus; |
| 17 class AudioStreamSanitizer; |
| 18 | 18 |
| 19 // Helper class that handles filling gaps and resolving overlaps. | 19 // Helper class that handles filling gaps and resolving overlaps. |
| 20 class MEDIA_EXPORT AudioSplicer { | 20 class MEDIA_EXPORT AudioSplicer { |
| 21 public: | 21 public: |
| 22 AudioSplicer(int samples_per_second); | 22 explicit AudioSplicer(int samples_per_second); |
| 23 ~AudioSplicer(); | 23 ~AudioSplicer(); |
| 24 | 24 |
| 25 // Resets the splicer state by clearing the output buffers queue, | 25 // Resets the splicer state by clearing the output buffers queue and resetting |
| 26 // and resetting the timestamp helper. | 26 // the timestamp helper. |
| 27 void Reset(); | 27 void Reset(); |
| 28 | 28 |
| 29 // Adds a new buffer full of samples or end of stream buffer to the splicer. | 29 // Adds a new buffer full of samples or end of stream buffer to the splicer. |
| 30 // Returns true if the buffer was accepted. False is returned if an error | 30 // Returns true if the buffer was accepted. False is returned if an error |
| 31 // occurred. | 31 // occurred. |
| 32 bool AddInput(const scoped_refptr<AudioBuffer>& input); | 32 bool AddInput(const scoped_refptr<AudioBuffer>& input); |
| 33 | 33 |
| 34 // Returns true if the splicer has a buffer to return. | 34 // Returns true if the splicer has a buffer to return. |
| 35 bool HasNextBuffer() const; | 35 bool HasNextBuffer() const; |
| 36 | 36 |
| 37 // Removes the next buffer from the output buffer queue and returns it. | 37 // Removes the next buffer from the output buffer queue and returns it; this |
| 38 // This should only be called if HasNextBuffer() returns true. | 38 // should only be called if HasNextBuffer() returns true. |
| 39 scoped_refptr<AudioBuffer> GetNextBuffer(); | 39 scoped_refptr<AudioBuffer> GetNextBuffer(); |
| 40 | 40 |
| 41 // Indicates that overlapping buffers are coming up and should be crossfaded. |
| 42 // Once set, all buffers encountered after |splice_timestamp| will be queued |
| 43 // internally until at least 5ms of overlapping buffers are received (or end |
| 44 // of stream, whichever comes first). |
| 45 void SetSpliceTimestamp(base::TimeDelta splice_timestamp); |
| 46 |
| 41 private: | 47 private: |
| 42 void AddOutputBuffer(const scoped_refptr<AudioBuffer>& buffer); | 48 friend class AudioSplicerTest; |
| 43 | 49 |
| 44 AudioTimestampHelper output_timestamp_helper_; | 50 // Extracts frames to be crossfaded from |pre_splice_sanitizer_|. Transfers |
| 51 // all frames before |splice_timestamp_| into |output_sanitizer_| and drops |
| 52 // frames outside of the crossfade duration. |
| 53 // |
| 54 // The size of the returned AudioBus is the crossfade duration in frames. |
| 55 // Crossfade duration is calculated based on the number of frames available |
| 56 // after |splice_timestamp_| in each sanitizer and capped by |
| 57 // |max_crossfade_duration_|. |
| 58 // |
| 59 // |pre_splice_sanitizer_| will be empty after this operation. |
| 60 scoped_ptr<AudioBus> ExtractCrossfadeFromPreSplice(); |
| 45 | 61 |
| 46 // Minimum gap size needed before the splicer will take action to | 62 // Crossfades |pre_splice_bus->frames()| frames from |post_splice_sanitizer_| |
| 47 // fill a gap. This avoids periodically inserting and then dropping samples | 63 // with those from |pre_splice_bus|. Adds the crossfaded buffer to |
| 48 // when the buffer timestamps are slightly off because of timestamp rounding | 64 // |output_sanitizer_| along with all buffers in |post_splice_sanitizer_|. |
| 49 // in the source content. Unit is frames. | 65 // |
| 50 int min_gap_size_; | 66 // |post_splice_sanitizer_| will be empty after this operation. |
| 67 void CrossfadePostSplice(scoped_ptr<AudioBus> pre_splice_bus); |
| 51 | 68 |
| 52 std::deque<scoped_refptr<AudioBuffer> > output_buffers_; | 69 const base::TimeDelta max_crossfade_duration_; |
| 53 bool received_end_of_stream_; | 70 base::TimeDelta splice_timestamp_; |
| 71 |
| 72 // The various sanitizers for each stage of the crossfade process. Buffers in |
| 73 // |output_sanitizer_| are immediately available for consumption by external |
| 74 // callers. |
| 75 // |
| 76 // Overlapped buffers go into the |pre_splice_sanitizer_| while overlapping |
| 77 // buffers go into the |post_splice_sanitizer_|. Once enough buffers for |
| 78 // crossfading are received the pre and post sanitizers are drained into |
| 79 // |output_sanitizer_| by the two ExtractCrossfadeFromXXX methods above. |
| 80 // |
| 81 // |pre_splice_sanitizer_| is not constructed until the first splice frame is |
| 82 // encountered. At which point it is constructed based on the timestamp state |
| 83 // of |output_sanitizer_|. It is destructed once the splice is finished. |
| 84 scoped_ptr<AudioStreamSanitizer> output_sanitizer_; |
| 85 scoped_ptr<AudioStreamSanitizer> pre_splice_sanitizer_; |
| 86 scoped_ptr<AudioStreamSanitizer> post_splice_sanitizer_; |
| 54 | 87 |
| 55 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSplicer); | 88 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSplicer); |
| 56 }; | 89 }; |
| 57 | 90 |
| 58 } // namespace media | 91 } // namespace media |
| 59 | 92 |
| 60 #endif | 93 #endif |
| OLD | NEW |