Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/AudioBufferSourceNode.cpp

Issue 2929283002: Avoid AudioBufferSourceHandler data race. (Closed)
Patch Set: minimize lock taking Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 // get any bad rate values. 583 // get any bad rate values.
584 final_playback_rate = clampTo(final_playback_rate, 0.0, kMaxRate); 584 final_playback_rate = clampTo(final_playback_rate, 0.0, kMaxRate);
585 585
586 bool is_playback_rate_valid = 586 bool is_playback_rate_valid =
587 !std::isnan(final_playback_rate) && !std::isinf(final_playback_rate); 587 !std::isnan(final_playback_rate) && !std::isinf(final_playback_rate);
588 DCHECK(is_playback_rate_valid); 588 DCHECK(is_playback_rate_valid);
589 589
590 if (!is_playback_rate_valid) 590 if (!is_playback_rate_valid)
591 final_playback_rate = 1.0; 591 final_playback_rate = 1.0;
592 592
593 // Record the minimum playback rate for use by handleStoppableSourceNode. 593 // Record the minimum playback rate for use by HandleStoppableSourceNode.
594 min_playback_rate_ = std::min(final_playback_rate, min_playback_rate_); 594 if (final_playback_rate < min_playback_rate_) {
595 MutexLocker locker(min_playback_rate_mutex_);
596 min_playback_rate_ = final_playback_rate;
597 }
595 598
596 return final_playback_rate; 599 return final_playback_rate;
597 } 600 }
598 601
602 double AudioBufferSourceHandler::MinPlaybackRate() {
hongchan 2017/06/12 14:09:17 I think we prefer Get...() as a getter name.
sof 2017/06/12 14:51:57 Done (a naming outlier for this class.)
603 MutexLocker locker(min_playback_rate_mutex_);
hongchan 2017/06/12 14:09:17 Can we have DCHECK(isMainThread()) here?
sof 2017/06/12 14:51:57 Done.
604 return min_playback_rate_;
605 }
606
599 bool AudioBufferSourceHandler::PropagatesSilence() const { 607 bool AudioBufferSourceHandler::PropagatesSilence() const {
600 return !IsPlayingOrScheduled() || HasFinished() || !buffer_; 608 return !IsPlayingOrScheduled() || HasFinished() || !buffer_;
601 } 609 }
602 610
603 void AudioBufferSourceHandler::HandleStoppableSourceNode() { 611 void AudioBufferSourceHandler::HandleStoppableSourceNode() {
604 // If the source node is not looping, and we have a buffer, we can determine 612 // If the source node is not looping, and we have a buffer, we can determine
605 // when the source would stop playing. This is intended to handle the 613 // when the source would stop playing. This is intended to handle the
606 // (uncommon) scenario where start() has been called but is never connected to 614 // (uncommon) scenario where start() has been called but is never connected to
607 // the destination (directly or indirectly). By stopping the node, the node 615 // the destination (directly or indirectly). By stopping the node, the node
608 // can be collected. Otherwise, the node will never get collected, leaking 616 // can be collected. Otherwise, the node will never get collected, leaking
609 // memory. 617 // memory.
610 // 618 //
611 // If looping was ever done (m_didSetLooping = true), give up. We can't 619 // If looping was ever done (m_didSetLooping = true), give up. We can't
612 // easily determine how long we looped so we don't know the actual duration 620 // easily determine how long we looped so we don't know the actual duration
613 // thus far, so don't try to do anything fancy. 621 // thus far, so don't try to do anything fancy.
622 double min_playback_rate = MinPlaybackRate();
614 if (!DidSetLooping() && Buffer() && IsPlayingOrScheduled() && 623 if (!DidSetLooping() && Buffer() && IsPlayingOrScheduled() &&
615 min_playback_rate_ > 0) { 624 min_playback_rate > 0) {
616 // Adjust the duration to include the playback rate. Only need to account 625 // Adjust the duration to include the playback rate. Only need to account
617 // for rate < 1 which makes the sound last longer. For rate >= 1, the 626 // for rate < 1 which makes the sound last longer. For rate >= 1, the
618 // source stops sooner, but that's ok. 627 // source stops sooner, but that's ok.
619 double actual_duration = Buffer()->duration() / min_playback_rate_; 628 double actual_duration = Buffer()->duration() / min_playback_rate;
620 629
621 double stop_time = start_time_ + actual_duration; 630 double stop_time = start_time_ + actual_duration;
622 631
623 // See crbug.com/478301. If a source node is started via start(), the source 632 // See crbug.com/478301. If a source node is started via start(), the source
624 // may not start at that time but one quantum (128 frames) later. But we 633 // may not start at that time but one quantum (128 frames) later. But we
625 // compute the stop time based on the start time and the duration, so we end 634 // compute the stop time based on the start time and the duration, so we end
626 // up stopping one quantum early. Thus, add a little extra time; we just 635 // up stopping one quantum early. Thus, add a little extra time; we just
627 // need to stop the source sometime after it should have stopped if it 636 // need to stop the source sometime after it should have stopped if it
628 // hadn't already. We don't need to be super precise on when to stop. 637 // hadn't already. We don't need to be super precise on when to stop.
629 double extra_stop_time = 638 double extra_stop_time =
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 765
757 void AudioBufferSourceNode::start(double when, 766 void AudioBufferSourceNode::start(double when,
758 double grain_offset, 767 double grain_offset,
759 double grain_duration, 768 double grain_duration,
760 ExceptionState& exception_state) { 769 ExceptionState& exception_state) {
761 GetAudioBufferSourceHandler().Start(when, grain_offset, grain_duration, 770 GetAudioBufferSourceHandler().Start(when, grain_offset, grain_duration,
762 exception_state); 771 exception_state);
763 } 772 }
764 773
765 } // namespace blink 774 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698