OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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_SHIFTER_H | 5 #ifndef MEDIA_BASE_AUDIO_SHIFTER_H |
6 #define MEDIA_BASE_AUDIO_SHIFTER_H | 6 #define MEDIA_BASE_AUDIO_SHIFTER_H |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <deque> | 10 #include <deque> |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // seconds. The larger the value, the smoother and less audible | 49 // seconds. The larger the value, the smoother and less audible |
50 // the transitions will be. (But it means that perfect audio | 50 // the transitions will be. (But it means that perfect audio |
51 // sync will take longer to achive.) | 51 // sync will take longer to achive.) |
52 // |rate| is audio frames per second, eg 48000. | 52 // |rate| is audio frames per second, eg 48000. |
53 // |channels| is number of channels in input and output audio. | 53 // |channels| is number of channels in input and output audio. |
54 // TODO(hubbe): Allow input rate and output rate to be different | 54 // TODO(hubbe): Allow input rate and output rate to be different |
55 // since we're going to be resampling anyways. | 55 // since we're going to be resampling anyways. |
56 AudioShifter(base::TimeDelta max_buffer_size, | 56 AudioShifter(base::TimeDelta max_buffer_size, |
57 base::TimeDelta clock_accuracy, | 57 base::TimeDelta clock_accuracy, |
58 base::TimeDelta adjustment_time, | 58 base::TimeDelta adjustment_time, |
59 size_t rate, | 59 int rate, |
60 int channels); | 60 int channels); |
61 ~AudioShifter(); | 61 ~AudioShifter(); |
62 | 62 |
| 63 int sample_rate() const { return rate_; } |
| 64 int channels() const { return channels_; } |
| 65 |
63 // Push Audio into the shifter. All inputs must have the same number of | 66 // Push Audio into the shifter. All inputs must have the same number of |
64 // channels, but bus size can vary. The playout time can be noisy and | 67 // channels, but bus size can vary. The playout time can be noisy and |
65 // does not have to line up perfectly with the number of samples pushed | 68 // does not have to line up perfectly with the number of samples pushed |
66 // so far. However, the playout_time in Push calls and Pull calls must | 69 // so far. However, the playout_time in Push calls and Pull calls must |
67 // not diverge over time. | 70 // not diverge over time. |
68 // Given audio from an a microphone, a reasonable way to calculate | 71 // Given audio from an a microphone, a reasonable way to calculate |
69 // playout_time would be now + 30ms. | 72 // playout_time would be now + 30ms. |
70 // Ideally playout_time is some time in the future, in which case | 73 // Ideally playout_time is some time in the future, in which case |
71 // the samples will be buffered until the approperiate time. If | 74 // the samples will be buffered until the approperiate time. If |
72 // playout_time is in the past, everything will still work, and we'll | 75 // playout_time is in the past, everything will still work, and we'll |
73 // try to keep the buffring to a minimum. | 76 // try to keep the buffring to a minimum. |
74 void Push(scoped_ptr<AudioBus> input, base::TimeTicks playout_time); | 77 void Push(scoped_ptr<AudioBus> input, base::TimeTicks playout_time); |
75 | 78 |
76 // Fills out |output| with samples. Tries to stretch/shrink the audio | 79 // Fills out |output| with samples. Tries to stretch/shrink the audio |
77 // to compensate for drift between input and output. | 80 // to compensate for drift between input and output. |
78 // If called from an output device data pull, a reasonable way to | 81 // If called from an output device data pull, a reasonable way to |
79 // calculate playout_time would be now + audio pipeline delay. | 82 // calculate playout_time would be now + audio pipeline delay. |
80 void Pull(AudioBus* output, base::TimeTicks playout_time); | 83 void Pull(AudioBus* output, base::TimeTicks playout_time); |
81 | 84 |
82 // Flush audio (but leave timing info) | |
83 void Flush(); | |
84 | |
85 private: | 85 private: |
86 void Zero(AudioBus* output); | 86 void Zero(AudioBus* output); |
87 void ResamplerCallback(int frame_delay, AudioBus* destination); | 87 void ResamplerCallback(int frame_delay, AudioBus* destination); |
88 | 88 |
89 struct AudioQueueEntry { | 89 struct AudioQueueEntry { |
90 AudioQueueEntry(base::TimeTicks target_playout_time_, | 90 AudioQueueEntry(base::TimeTicks target_playout_time_, |
91 scoped_ptr<AudioBus> audio_); | 91 scoped_ptr<AudioBus> audio_); |
92 ~AudioQueueEntry(); | 92 ~AudioQueueEntry(); |
93 base::TimeTicks target_playout_time; | 93 base::TimeTicks target_playout_time; |
94 linked_ptr<AudioBus> audio; | 94 linked_ptr<AudioBus> audio; |
95 }; | 95 }; |
96 | 96 |
97 typedef std::deque<AudioQueueEntry> AudioShifterQueue; | 97 typedef std::deque<AudioQueueEntry> AudioShifterQueue; |
98 | 98 |
99 // Set from constructor. | 99 // Set from constructor. |
100 const base::TimeDelta max_buffer_size_; | 100 const base::TimeDelta max_buffer_size_; |
101 const base::TimeDelta clock_accuracy_; | 101 const base::TimeDelta clock_accuracy_; |
102 const base::TimeDelta adjustment_time_; | 102 const base::TimeDelta adjustment_time_; |
103 const size_t rate_; | 103 const int rate_; |
| 104 const int channels_; |
104 | 105 |
105 // The clock smoothers are used to smooth out timestamps | 106 // The clock smoothers are used to smooth out timestamps |
106 // and adjust for drift and inaccurate clocks. | 107 // and adjust for drift and inaccurate clocks. |
107 scoped_ptr<ClockSmoother> input_clock_smoother_; | 108 scoped_ptr<ClockSmoother> input_clock_smoother_; |
108 scoped_ptr<ClockSmoother> output_clock_smoother_; | 109 scoped_ptr<ClockSmoother> output_clock_smoother_; |
109 | 110 |
110 // Are we currently outputting data? | 111 // Are we currently outputting data? |
111 bool running_; | 112 bool running_; |
112 | 113 |
113 // Number of frames already consumed from |queue_|. | 114 // Number of frames already consumed from |queue_|. |
(...skipping 18 matching lines...) Expand all Loading... |
132 // Resampler. | 133 // Resampler. |
133 MultiChannelResampler resampler_; | 134 MultiChannelResampler resampler_; |
134 | 135 |
135 // Current resampler ratio. | 136 // Current resampler ratio. |
136 double current_ratio_; | 137 double current_ratio_; |
137 }; | 138 }; |
138 | 139 |
139 } // namespace media | 140 } // namespace media |
140 | 141 |
141 #endif // MEDIA_BASE_AUDIO_SHIFTER_H | 142 #endif // MEDIA_BASE_AUDIO_SHIFTER_H |
OLD | NEW |