Chromium Code Reviews| 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 #include "media/base/audio_splicer.h" | 5 #include "media/base/audio_splicer.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <deque> | |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "media/base/audio_buffer.h" | 11 #include "media/base/audio_buffer.h" |
| 12 #include "media/base/audio_bus.h" | |
| 11 #include "media/base/audio_decoder_config.h" | 13 #include "media/base/audio_decoder_config.h" |
| 12 #include "media/base/audio_timestamp_helper.h" | 14 #include "media/base/audio_timestamp_helper.h" |
| 13 #include "media/base/buffers.h" | 15 #include "media/base/buffers.h" |
| 16 #include "media/base/vector_math.h" | |
| 14 | 17 |
| 15 namespace media { | 18 namespace media { |
| 16 | 19 |
| 17 // Largest gap or overlap allowed by this class. Anything | 20 // Largest gap or overlap allowed by this class. Anything |
| 18 // larger than this will trigger an error. | 21 // larger than this will trigger an error. |
| 19 // This is an arbitrary value, but the initial selection of 50ms | 22 // This is an arbitrary value, but the initial selection of 50ms |
| 20 // roughly represents the duration of 2 compressed AAC or MP3 frames. | 23 // roughly represents the duration of 2 compressed AAC or MP3 frames. |
| 21 static const int kMaxTimeDeltaInMilliseconds = 50; | 24 static const int kMaxTimeDeltaInMilliseconds = 50; |
| 22 | 25 |
| 23 AudioSplicer::AudioSplicer(int samples_per_second) | 26 // Minimum gap size needed before the splicer will take action to |
| 24 : output_timestamp_helper_(samples_per_second), | 27 // fill a gap. This avoids periodically inserting and then dropping samples |
| 25 min_gap_size_(2), | 28 // when the buffer timestamps are slightly off because of timestamp rounding |
| 26 received_end_of_stream_(false) { | 29 // in the source content. Unit is frames. |
| 30 static const int kMinGapSize = 2; | |
| 31 | |
| 32 // The number of milliseconds to crossfade before trimming when buffers overlap. | |
| 33 static const int kCrossfadeDurationInMilliseconds = 5; | |
| 34 | |
| 35 // AudioBuffer::TrimStart() is not as accurate as the timestamp helper, so | |
| 36 // manually adjust the duration and timestamp after trimming. | |
| 37 static void AccurateTrimStart(int frames_to_trim, | |
| 38 const scoped_refptr<AudioBuffer> buffer, | |
| 39 const AudioTimestampHelper& timestamp_helper) { | |
| 40 buffer->TrimStart(frames_to_trim); | |
| 41 buffer->set_timestamp(timestamp_helper.GetTimestamp()); | |
| 42 buffer->set_duration( | |
| 43 timestamp_helper.GetFrameDuration(buffer->frame_count())); | |
| 27 } | 44 } |
| 28 | 45 |
| 29 AudioSplicer::~AudioSplicer() { | 46 // AudioBuffer::TrimEnd() is not as accurate as the timestamp helper, so |
| 47 // manually adjust the duration after trimming. | |
| 48 static void AccurateTrimEnd(int frames_to_trim, | |
| 49 const scoped_refptr<AudioBuffer> buffer, | |
| 50 const AudioTimestampHelper& timestamp_helper) { | |
| 51 DCHECK(buffer->timestamp() == timestamp_helper.GetTimestamp()); | |
| 52 buffer->TrimEnd(frames_to_trim); | |
| 53 buffer->set_duration( | |
| 54 timestamp_helper.GetFrameDuration(buffer->frame_count())); | |
| 30 } | 55 } |
| 31 | 56 |
| 32 void AudioSplicer::Reset() { | 57 class AudioStreamSanitizer { |
| 33 output_timestamp_helper_.SetBaseTimestamp(kNoTimestamp()); | 58 public: |
| 59 explicit AudioStreamSanitizer(int samples_per_second); | |
| 60 ~AudioStreamSanitizer(); | |
| 61 | |
| 62 // Resets the sanitizer state by clearing the output buffers queue, and | |
| 63 // resetting the timestamp helper. | |
| 64 void Reset(); | |
| 65 | |
| 66 // Similar to Reset(), but initializes the timestamp helper with the given | |
| 67 // parameters. | |
| 68 void ResetTimestampState(int64 frame_count, base::TimeDelta base_timestamp); | |
| 69 | |
| 70 // Adds a new buffer full of samples or end of stream buffer to the splicer. | |
| 71 // Returns true if the buffer was accepted. False is returned if an error | |
| 72 // occurred. | |
| 73 bool AddInput(const scoped_refptr<AudioBuffer>& input); | |
| 74 | |
| 75 // Returns true if the sanitizer has a buffer to return. | |
| 76 bool HasNextBuffer() const; | |
| 77 | |
| 78 // Removes the next buffer from the output buffer queue and returns it; should | |
| 79 // only be called if HasNextBuffer() returns true. | |
| 80 scoped_refptr<AudioBuffer> GetNextBuffer(); | |
| 81 | |
| 82 // Returns the total frame count of all buffers available for output. | |
| 83 int GetFrameCount() const; | |
| 84 | |
| 85 // Returns the duration of all buffers added to the output queue thus far. | |
| 86 base::TimeDelta GetDuration() const; | |
| 87 | |
| 88 const AudioTimestampHelper& timestamp_helper() { | |
| 89 return output_timestamp_helper_; | |
| 90 } | |
| 91 | |
| 92 private: | |
| 93 void AddOutputBuffer(const scoped_refptr<AudioBuffer>& buffer); | |
| 94 | |
| 95 AudioTimestampHelper output_timestamp_helper_; | |
| 96 bool received_end_of_stream_; | |
| 97 | |
| 98 typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue; | |
| 99 BufferQueue output_buffers_; | |
| 100 | |
| 101 DISALLOW_ASSIGN(AudioStreamSanitizer); | |
| 102 }; | |
| 103 | |
| 104 AudioStreamSanitizer::AudioStreamSanitizer(int samples_per_second) | |
| 105 : output_timestamp_helper_(samples_per_second), | |
| 106 received_end_of_stream_(false) {} | |
| 107 | |
| 108 AudioStreamSanitizer::~AudioStreamSanitizer() {} | |
| 109 | |
| 110 void AudioStreamSanitizer::Reset() { | |
| 111 ResetTimestampState(0, kNoTimestamp()); | |
| 112 } | |
| 113 | |
| 114 void AudioStreamSanitizer::ResetTimestampState(int64 frame_count, | |
| 115 base::TimeDelta base_timestamp) { | |
| 34 output_buffers_.clear(); | 116 output_buffers_.clear(); |
| 35 received_end_of_stream_ = false; | 117 received_end_of_stream_ = false; |
| 118 output_timestamp_helper_.SetBaseTimestamp(base_timestamp); | |
| 119 if (frame_count > 0) | |
| 120 output_timestamp_helper_.AddFrames(frame_count); | |
| 36 } | 121 } |
| 37 | 122 |
| 38 bool AudioSplicer::AddInput(const scoped_refptr<AudioBuffer>& input) { | 123 bool AudioStreamSanitizer::AddInput(const scoped_refptr<AudioBuffer>& input) { |
| 39 DCHECK(!received_end_of_stream_ || input->end_of_stream()); | 124 DCHECK(!received_end_of_stream_ || input->end_of_stream()); |
| 40 | 125 |
| 41 if (input->end_of_stream()) { | 126 if (input->end_of_stream()) { |
| 42 output_buffers_.push_back(input); | 127 output_buffers_.push_back(input); |
| 43 received_end_of_stream_ = true; | 128 received_end_of_stream_ = true; |
| 44 return true; | 129 return true; |
| 45 } | 130 } |
| 46 | 131 |
| 47 DCHECK(input->timestamp() != kNoTimestamp()); | 132 DCHECK(input->timestamp() != kNoTimestamp()); |
| 48 DCHECK(input->duration() > base::TimeDelta()); | 133 DCHECK(input->duration() > base::TimeDelta()); |
| 49 DCHECK_GT(input->frame_count(), 0); | 134 DCHECK_GT(input->frame_count(), 0); |
| 50 | 135 |
| 51 if (output_timestamp_helper_.base_timestamp() == kNoTimestamp()) | 136 if (output_timestamp_helper_.base_timestamp() == kNoTimestamp()) |
| 52 output_timestamp_helper_.SetBaseTimestamp(input->timestamp()); | 137 output_timestamp_helper_.SetBaseTimestamp(input->timestamp()); |
| 53 | 138 |
| 54 if (output_timestamp_helper_.base_timestamp() > input->timestamp()) { | 139 if (output_timestamp_helper_.base_timestamp() > input->timestamp()) { |
| 55 DVLOG(1) << "Input timestamp is before the base timestamp."; | 140 DVLOG(1) << "Input timestamp is before the base timestamp."; |
| 56 return false; | 141 return false; |
| 57 } | 142 } |
| 58 | 143 |
| 59 base::TimeDelta timestamp = input->timestamp(); | 144 const base::TimeDelta timestamp = input->timestamp(); |
| 60 base::TimeDelta expected_timestamp = output_timestamp_helper_.GetTimestamp(); | 145 const base::TimeDelta expected_timestamp = |
| 61 base::TimeDelta delta = timestamp - expected_timestamp; | 146 output_timestamp_helper_.GetTimestamp(); |
| 147 const base::TimeDelta delta = timestamp - expected_timestamp; | |
| 62 | 148 |
| 63 if (std::abs(delta.InMilliseconds()) > kMaxTimeDeltaInMilliseconds) { | 149 if (std::abs(delta.InMilliseconds()) > kMaxTimeDeltaInMilliseconds) { |
| 64 DVLOG(1) << "Timestamp delta too large: " << delta.InMicroseconds() << "us"; | 150 DVLOG(1) << "Timestamp delta too large: " << delta.InMicroseconds() << "us"; |
| 65 return false; | 151 return false; |
| 66 } | 152 } |
| 67 | 153 |
| 68 int frames_to_fill = 0; | 154 int frames_to_fill = 0; |
| 69 if (delta != base::TimeDelta()) | 155 if (delta != base::TimeDelta()) |
| 70 frames_to_fill = output_timestamp_helper_.GetFramesToTarget(timestamp); | 156 frames_to_fill = output_timestamp_helper_.GetFramesToTarget(timestamp); |
| 71 | 157 |
| 72 if (frames_to_fill == 0 || std::abs(frames_to_fill) < min_gap_size_) { | 158 if (frames_to_fill == 0 || std::abs(frames_to_fill) < kMinGapSize) { |
| 73 AddOutputBuffer(input); | 159 AddOutputBuffer(input); |
| 74 return true; | 160 return true; |
| 75 } | 161 } |
| 76 | 162 |
| 77 if (frames_to_fill > 0) { | 163 if (frames_to_fill > 0) { |
| 78 DVLOG(1) << "Gap detected @ " << expected_timestamp.InMicroseconds() | 164 DVLOG(1) << "Gap detected @ " << expected_timestamp.InMicroseconds() |
| 79 << " us: " << delta.InMicroseconds() << " us"; | 165 << " us: " << delta.InMicroseconds() << " us"; |
| 80 | 166 |
| 81 // Create a buffer with enough silence samples to fill the gap and | 167 // Create a buffer with enough silence samples to fill the gap and |
| 82 // add it to the output buffer. | 168 // add it to the output buffer. |
| 83 scoped_refptr<AudioBuffer> gap = AudioBuffer::CreateEmptyBuffer( | 169 scoped_refptr<AudioBuffer> gap = AudioBuffer::CreateEmptyBuffer( |
| 84 input->channel_count(), | 170 input->channel_count(), |
| 85 frames_to_fill, | 171 frames_to_fill, |
| 86 expected_timestamp, | 172 expected_timestamp, |
| 87 output_timestamp_helper_.GetFrameDuration(frames_to_fill)); | 173 output_timestamp_helper_.GetFrameDuration(frames_to_fill)); |
| 88 AddOutputBuffer(gap); | 174 AddOutputBuffer(gap); |
| 89 | 175 |
| 90 // Add the input buffer now that the gap has been filled. | 176 // Add the input buffer now that the gap has been filled. |
| 91 AddOutputBuffer(input); | 177 AddOutputBuffer(input); |
| 92 return true; | 178 return true; |
| 93 } | 179 } |
| 94 | 180 |
| 95 int frames_to_skip = -frames_to_fill; | 181 // Overlapping buffers marked as splice frames are handled by AudioSplicer, |
| 182 // but decoder and demuxer quirks may sometimes produce overlapping samples | |
| 183 // which need to be sanitized. | |
| 184 // | |
| 185 // A crossfade can't be done here because only the current buffer is available | |
| 186 // at this point, not previous buffers. | |
| 187 DVLOG(1) << "Overlap detected @ " << expected_timestamp.InMicroseconds() | |
| 188 << " us: " << -delta.InMicroseconds() << " us"; | |
| 96 | 189 |
| 97 DVLOG(1) << "Overlap detected @ " << expected_timestamp.InMicroseconds() | 190 const int frames_to_skip = -frames_to_fill; |
| 98 << " us: " << -delta.InMicroseconds() << " us"; | |
| 99 | |
| 100 if (input->frame_count() <= frames_to_skip) { | 191 if (input->frame_count() <= frames_to_skip) { |
| 101 DVLOG(1) << "Dropping whole buffer"; | 192 DVLOG(1) << "Dropping whole buffer"; |
| 102 return true; | 193 return true; |
| 103 } | 194 } |
| 104 | 195 |
| 105 // Copy the trailing samples that do not overlap samples already output | 196 // Copy the trailing samples that do not overlap samples already output |
| 106 // into a new buffer. Add this new buffer to the output queue. | 197 // into a new buffer. Add this new buffer to the output queue. |
| 107 // | 198 AccurateTrimStart(frames_to_skip, input, output_timestamp_helper_); |
| 108 // TODO(acolwell): Implement a cross-fade here so the transition is less | |
|
acolwell GONE FROM CHROMIUM
2014/02/28 18:50:27
nit: I think this comment should stay. For the "ba
DaleCurtis
2014/02/28 21:14:26
I'll leave it, but it's kind of impossible to impl
| |
| 109 // jarring. | |
| 110 input->TrimStart(frames_to_skip); | |
| 111 AddOutputBuffer(input); | 199 AddOutputBuffer(input); |
| 112 return true; | 200 return true; |
| 113 } | 201 } |
| 114 | 202 |
| 115 bool AudioSplicer::HasNextBuffer() const { | 203 bool AudioStreamSanitizer::HasNextBuffer() const { |
| 116 return !output_buffers_.empty(); | 204 return !output_buffers_.empty(); |
| 117 } | 205 } |
| 118 | 206 |
| 119 scoped_refptr<AudioBuffer> AudioSplicer::GetNextBuffer() { | 207 scoped_refptr<AudioBuffer> AudioStreamSanitizer::GetNextBuffer() { |
| 120 scoped_refptr<AudioBuffer> ret = output_buffers_.front(); | 208 scoped_refptr<AudioBuffer> ret = output_buffers_.front(); |
| 121 output_buffers_.pop_front(); | 209 output_buffers_.pop_front(); |
| 122 return ret; | 210 return ret; |
| 123 } | 211 } |
| 124 | 212 |
| 125 void AudioSplicer::AddOutputBuffer(const scoped_refptr<AudioBuffer>& buffer) { | 213 void AudioStreamSanitizer::AddOutputBuffer( |
| 214 const scoped_refptr<AudioBuffer>& buffer) { | |
| 126 output_timestamp_helper_.AddFrames(buffer->frame_count()); | 215 output_timestamp_helper_.AddFrames(buffer->frame_count()); |
| 127 output_buffers_.push_back(buffer); | 216 output_buffers_.push_back(buffer); |
| 128 } | 217 } |
| 129 | 218 |
| 219 int AudioStreamSanitizer::GetFrameCount() const { | |
| 220 int frame_count = 0; | |
| 221 for (BufferQueue::const_iterator it = output_buffers_.begin(); | |
| 222 it != output_buffers_.end(); ++it) { | |
| 223 frame_count += (*it)->frame_count(); | |
| 224 } | |
| 225 return frame_count; | |
| 226 } | |
| 227 | |
| 228 base::TimeDelta AudioStreamSanitizer::GetDuration() const { | |
| 229 DCHECK(output_timestamp_helper_.base_timestamp() != kNoTimestamp()); | |
| 230 return output_timestamp_helper_.GetTimestamp() - | |
| 231 output_timestamp_helper_.base_timestamp(); | |
| 232 } | |
| 233 | |
| 234 AudioSplicer::AudioSplicer(int samples_per_second) | |
| 235 : max_crossfade_duration_( | |
| 236 base::TimeDelta::FromMilliseconds(kCrossfadeDurationInMilliseconds)), | |
| 237 splice_timestamp_(kNoTimestamp()), | |
| 238 output_sanitizer_(new AudioStreamSanitizer(samples_per_second)), | |
| 239 pre_splice_sanitizer_(new AudioStreamSanitizer(samples_per_second)), | |
| 240 post_splice_sanitizer_(new AudioStreamSanitizer(samples_per_second)) {} | |
| 241 | |
| 242 AudioSplicer::~AudioSplicer() {} | |
| 243 | |
| 244 void AudioSplicer::Reset() { | |
| 245 output_sanitizer_->Reset(); | |
| 246 pre_splice_sanitizer_->Reset(); | |
| 247 post_splice_sanitizer_->Reset(); | |
| 248 splice_timestamp_ = kNoTimestamp(); | |
| 249 } | |
| 250 | |
| 251 bool AudioSplicer::AddInput(const scoped_refptr<AudioBuffer>& input) { | |
| 252 // If we're not processing a splice, add the input to the output queue. | |
| 253 if (splice_timestamp_ == kNoTimestamp()) { | |
| 254 DCHECK(!pre_splice_sanitizer_->HasNextBuffer()); | |
| 255 DCHECK(!post_splice_sanitizer_->HasNextBuffer()); | |
| 256 return output_sanitizer_->AddInput(input); | |
| 257 } | |
| 258 | |
| 259 // If we're still receiving buffers before the splice point figure out which | |
| 260 // sanitizer (if any) to put them in. | |
| 261 if (!post_splice_sanitizer_->HasNextBuffer()) { | |
| 262 DCHECK(!input->end_of_stream()); | |
| 263 | |
| 264 // If the provided buffer is entirely before the splice point it can also be | |
| 265 // added to the output queue. | |
| 266 if (input->timestamp() + input->duration() < splice_timestamp_) { | |
| 267 DCHECK(!pre_splice_sanitizer_->HasNextBuffer()); | |
| 268 return output_sanitizer_->AddInput(input); | |
| 269 } | |
| 270 | |
| 271 // If we've encountered the first pre splice buffer, reset the pre splice | |
| 272 // sanitizer based on |output_sanitizer_|. This is done so that gaps and | |
| 273 // overlaps between buffers across the sanitizers are accounted for prior | |
| 274 // to calculating crossfade. | |
| 275 if (!pre_splice_sanitizer_->HasNextBuffer()) { | |
| 276 pre_splice_sanitizer_->ResetTimestampState( | |
| 277 output_sanitizer_->timestamp_helper().frame_count(), | |
| 278 output_sanitizer_->timestamp_helper().base_timestamp()); | |
| 279 } | |
| 280 | |
| 281 // If we're processing a splice and the input buffer does not overlap any of | |
| 282 // the existing buffers, append it to the splice queue for processing. | |
| 283 if (!pre_splice_sanitizer_->HasNextBuffer() || | |
| 284 input->timestamp() != splice_timestamp_) { | |
| 285 return pre_splice_sanitizer_->AddInput(input); | |
| 286 } | |
| 287 | |
| 288 // We've received the first overlapping buffer. | |
| 289 } | |
| 290 | |
| 291 // At this point we have all the fade out preroll buffers from the decoder. | |
| 292 // We now need to wait until we have enough data to perform the crossfade (or | |
| 293 // we receive an end of stream). | |
| 294 if (!post_splice_sanitizer_->AddInput(input)) | |
| 295 return false; | |
| 296 | |
| 297 if (!input->end_of_stream() && | |
| 298 post_splice_sanitizer_->GetDuration() < max_crossfade_duration_) { | |
| 299 return true; | |
| 300 } | |
| 301 | |
| 302 // Transfer out preroll buffers involved in the splice, drop those not. Since | |
| 303 // we don't want to care what format the AudioBuffers are in, we need to use | |
| 304 // an intermediary AudioBus to convert the data to float. | |
| 305 scoped_ptr<AudioBus> pre_splice_bus = ExtractCrossfadeFromPreSplice(); | |
| 306 | |
| 307 // Allocate output buffer for crossfade. | |
| 308 scoped_refptr<AudioBuffer> crossfade_buffer = | |
| 309 AudioBuffer::CreateBuffer(kSampleFormatPlanarF32, | |
| 310 pre_splice_bus->channels(), | |
| 311 pre_splice_bus->frames()); | |
| 312 | |
| 313 // Use the calculated timestamp and duration to ensure there's no extra gaps | |
| 314 // or overlaps to process when adding the buffer to |output_sanitizer_|. | |
| 315 const AudioTimestampHelper& output_ts_helper = | |
| 316 output_sanitizer_->timestamp_helper(); | |
| 317 crossfade_buffer->set_timestamp(output_ts_helper.GetTimestamp()); | |
| 318 crossfade_buffer->set_duration( | |
| 319 output_ts_helper.GetFrameDuration(pre_splice_bus->frames())); | |
| 320 | |
| 321 // AudioBuffer::ReadFrames() only allows output into an AudioBus, so wrap | |
| 322 // our AudioBuffer in one so we can avoid extra data copies. | |
| 323 scoped_ptr<AudioBus> crossfade_bus_wrapper = | |
| 324 AudioBus::CreateWrapper(crossfade_buffer->channel_count()); | |
|
acolwell GONE FROM CHROMIUM
2014/02/28 18:50:27
nit: Please move this and the following 5 lines in
DaleCurtis
2014/02/28 21:14:26
Done.
| |
| 325 crossfade_bus_wrapper->set_frames(crossfade_buffer->frame_count()); | |
| 326 for (int ch = 0; ch < crossfade_buffer->channel_count(); ++ch) { | |
| 327 crossfade_bus_wrapper->SetChannelData( | |
| 328 ch, reinterpret_cast<float*>(crossfade_buffer->channel_data()[ch])); | |
| 329 } | |
| 330 | |
| 331 // Insert the crossfade buffer into the output queue now so post splice | |
| 332 // buffers can be added in processing order. We will still modify the buffer | |
| 333 // during the crossfade step. | |
| 334 CHECK(output_sanitizer_->AddInput(crossfade_buffer)); | |
| 335 DCHECK_EQ(crossfade_buffer->frame_count(), crossfade_bus_wrapper->frames()); | |
| 336 | |
| 337 ExtractCrossfadeFromPostSplice(crossfade_bus_wrapper.get()); | |
| 338 | |
| 339 // Crossfade the audio into |crossfade_buffer|. | |
| 340 for (int ch = 0; ch < crossfade_bus_wrapper->channels(); ++ch) { | |
| 341 vector_math::Crossfade(pre_splice_bus->channel(ch), | |
| 342 pre_splice_bus->frames(), | |
| 343 crossfade_bus_wrapper->channel(ch)); | |
| 344 } | |
| 345 | |
| 346 // Clear the splice timestamp so new splices can be accepted. | |
| 347 splice_timestamp_ = kNoTimestamp(); | |
| 348 return true; | |
| 349 } | |
| 350 | |
| 351 bool AudioSplicer::HasNextBuffer() const { | |
| 352 return output_sanitizer_->HasNextBuffer(); | |
| 353 } | |
| 354 | |
| 355 scoped_refptr<AudioBuffer> AudioSplicer::GetNextBuffer() { | |
| 356 return output_sanitizer_->GetNextBuffer(); | |
| 357 } | |
| 358 | |
| 359 void AudioSplicer::SetSpliceTimestamp(base::TimeDelta splice_timestamp) { | |
| 360 DCHECK(splice_timestamp != kNoTimestamp()); | |
| 361 if (splice_timestamp_ == splice_timestamp) | |
| 362 return; | |
| 363 | |
| 364 // TODO(dalecurtis): We may need the concept of a future_splice_timestamp_ to | |
| 365 // handle cases where another splice comes in before we've received 5ms of | |
| 366 // data from the last one. Leave this as a CHECK for now to figure out if | |
| 367 // this case is possible. | |
| 368 CHECK(splice_timestamp_ == kNoTimestamp()); | |
| 369 splice_timestamp_ = splice_timestamp; | |
| 370 } | |
| 371 | |
| 372 scoped_ptr<AudioBus> AudioSplicer::ExtractCrossfadeFromPreSplice() { | |
| 373 const AudioTimestampHelper& output_ts_helper = | |
| 374 output_sanitizer_->timestamp_helper(); | |
| 375 | |
| 376 // Ensure |output_sanitizer_| has a valid base timestamp so we can use it for | |
| 377 // timestamp calculations. | |
| 378 if (output_ts_helper.base_timestamp() == kNoTimestamp()) { | |
| 379 output_sanitizer_->ResetTimestampState( | |
| 380 0, pre_splice_sanitizer_->timestamp_helper().base_timestamp()); | |
| 381 } | |
| 382 | |
| 383 int frames_before_splice = | |
| 384 output_ts_helper.GetFramesToTarget(splice_timestamp_); | |
| 385 | |
| 386 // Determine crossfade frame count based on available frames in each splicer | |
| 387 // and capping to the maximum crossfade duration. | |
| 388 const int max_crossfade_frame_count = | |
| 389 output_ts_helper.GetFramesToTarget(splice_timestamp_ + | |
| 390 max_crossfade_duration_) - | |
| 391 frames_before_splice; | |
| 392 const int frames_to_crossfade = std::min( | |
| 393 max_crossfade_frame_count, | |
| 394 std::min(pre_splice_sanitizer_->GetFrameCount() - frames_before_splice, | |
| 395 post_splice_sanitizer_->GetFrameCount())); | |
| 396 | |
| 397 int frames_read = 0; | |
| 398 scoped_ptr<AudioBus> output_bus; | |
| 399 while (pre_splice_sanitizer_->HasNextBuffer() && | |
| 400 frames_read < frames_to_crossfade) { | |
| 401 scoped_refptr<AudioBuffer> preroll = pre_splice_sanitizer_->GetNextBuffer(); | |
| 402 | |
| 403 // We don't know the channel count until we see the first buffer, so wait | |
| 404 // until the first buffer to allocate the output AudioBus. | |
| 405 if (!output_bus) { | |
| 406 output_bus = | |
| 407 AudioBus::Create(preroll->channel_count(), frames_to_crossfade); | |
| 408 } | |
| 409 | |
| 410 // There may be enough of a gap introduced during decoding such that an | |
| 411 // entire buffer exists before the splice point. | |
| 412 if (frames_before_splice >= preroll->frame_count()) { | |
| 413 frames_before_splice -= preroll->frame_count(); | |
| 414 CHECK(output_sanitizer_->AddInput(preroll)); | |
| 415 continue; | |
| 416 } | |
| 417 | |
| 418 const int frames_to_read = | |
| 419 std::min(preroll->frame_count() - frames_before_splice, | |
| 420 output_bus->frames() - frames_read); | |
| 421 preroll->ReadFrames( | |
| 422 frames_to_read, frames_before_splice, frames_read, output_bus.get()); | |
| 423 frames_read += frames_to_read; | |
| 424 | |
| 425 // If only part of the buffer was consumed, trim it appropriately and stick | |
| 426 // it into the output queue. | |
| 427 if (frames_before_splice) { | |
| 428 AccurateTrimEnd(preroll->frame_count() - frames_before_splice, | |
| 429 preroll, | |
| 430 output_ts_helper); | |
| 431 CHECK(output_sanitizer_->AddInput(preroll)); | |
| 432 frames_before_splice = 0; | |
| 433 } | |
| 434 } | |
| 435 | |
| 436 // All necessary buffers have been processed, it's safe to reset. | |
| 437 pre_splice_sanitizer_->Reset(); | |
| 438 DCHECK_EQ(output_bus->frames(), frames_read); | |
| 439 DCHECK_EQ(output_ts_helper.GetFramesToTarget(splice_timestamp_), 0); | |
| 440 return output_bus.Pass(); | |
| 441 } | |
| 442 | |
| 443 void AudioSplicer::ExtractCrossfadeFromPostSplice(AudioBus* output_bus) { | |
| 444 int frames_read = 0; | |
| 445 while (post_splice_sanitizer_->HasNextBuffer() && | |
| 446 frames_read < output_bus->frames()) { | |
| 447 scoped_refptr<AudioBuffer> postroll = | |
| 448 post_splice_sanitizer_->GetNextBuffer(); | |
| 449 const int frames_to_read = | |
| 450 std::min(postroll->frame_count(), output_bus->frames() - frames_read); | |
| 451 postroll->ReadFrames(frames_to_read, 0, frames_read, output_bus); | |
| 452 frames_read += frames_to_read; | |
| 453 | |
| 454 // If only part of the buffer was consumed, trim it appropriately and stick | |
| 455 // it into the output queue. | |
| 456 if (frames_to_read < postroll->frame_count()) { | |
| 457 AccurateTrimStart( | |
| 458 frames_to_read, postroll, output_sanitizer_->timestamp_helper()); | |
| 459 CHECK(output_sanitizer_->AddInput(postroll)); | |
| 460 } | |
| 461 } | |
| 462 | |
| 463 DCHECK_EQ(output_bus->frames(), frames_read); | |
| 464 | |
| 465 // Transfer all remaining buffers out and reset once empty. | |
| 466 while (post_splice_sanitizer_->HasNextBuffer()) | |
| 467 CHECK(output_sanitizer_->AddInput(post_splice_sanitizer_->GetNextBuffer())); | |
| 468 post_splice_sanitizer_->Reset(); | |
| 469 } | |
| 470 | |
| 130 } // namespace media | 471 } // namespace media |
| OLD | NEW |