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

Side by Side Diff: webrtc/voice_engine/channel.cc

Issue 1263223002: Adding locking to webrtc::voe::Channel to fix race conditions (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixing typo that was somehow introduced.. Created 5 years, 4 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
« no previous file with comments | « webrtc/voice_engine/channel.h ('k') | webrtc/voice_engine/voe_video_sync_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 _mute(false), 773 _mute(false),
774 _panLeft(1.0f), 774 _panLeft(1.0f),
775 _panRight(1.0f), 775 _panRight(1.0f),
776 _outputGain(1.0f), 776 _outputGain(1.0f),
777 _playOutbandDtmfEvent(false), 777 _playOutbandDtmfEvent(false),
778 _playInbandDtmfEvent(false), 778 _playInbandDtmfEvent(false),
779 _lastLocalTimeStamp(0), 779 _lastLocalTimeStamp(0),
780 _lastPayloadType(0), 780 _lastPayloadType(0),
781 _includeAudioLevelIndication(false), 781 _includeAudioLevelIndication(false),
782 _outputSpeechType(AudioFrame::kNormalSpeech), 782 _outputSpeechType(AudioFrame::kNormalSpeech),
783 video_sync_lock_(CriticalSectionWrapper::CreateCriticalSection()),
783 _average_jitter_buffer_delay_us(0), 784 _average_jitter_buffer_delay_us(0),
784 least_required_delay_ms_(0),
785 _previousTimestamp(0), 785 _previousTimestamp(0),
786 _recPacketDelayMs(20), 786 _recPacketDelayMs(20),
787 _RxVadDetection(false), 787 _RxVadDetection(false),
788 _rxAgcIsEnabled(false), 788 _rxAgcIsEnabled(false),
789 _rxNsIsEnabled(false), 789 _rxNsIsEnabled(false),
790 restored_packet_in_use_(false), 790 restored_packet_in_use_(false),
791 rtcp_observer_(new VoERtcpObserver(this)), 791 rtcp_observer_(new VoERtcpObserver(this)),
792 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())), 792 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
793 assoc_send_channel_lock_(CriticalSectionWrapper::CreateCriticalSection()), 793 assoc_send_channel_lock_(CriticalSectionWrapper::CreateCriticalSection()),
794 associate_send_channel_(ChannelOwner(nullptr)) 794 associate_send_channel_(ChannelOwner(nullptr))
(...skipping 2759 matching lines...) Expand 10 before | Expand all | Expand 10 after
3554 "Channel::GetNetworkStatistics()"); 3554 "Channel::GetNetworkStatistics()");
3555 return audio_coding_->GetNetworkStatistics(&stats); 3555 return audio_coding_->GetNetworkStatistics(&stats);
3556 } 3556 }
3557 3557
3558 void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const { 3558 void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3559 audio_coding_->GetDecodingCallStatistics(stats); 3559 audio_coding_->GetDecodingCallStatistics(stats);
3560 } 3560 }
3561 3561
3562 bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms, 3562 bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3563 int* playout_buffer_delay_ms) const { 3563 int* playout_buffer_delay_ms) const {
3564 CriticalSectionScoped cs(video_sync_lock_.get());
3564 if (_average_jitter_buffer_delay_us == 0) { 3565 if (_average_jitter_buffer_delay_us == 0) {
3565 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId), 3566 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3566 "Channel::GetDelayEstimate() no valid estimate."); 3567 "Channel::GetDelayEstimate() no valid estimate.");
3567 return false; 3568 return false;
3568 } 3569 }
3569 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 + 3570 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3570 _recPacketDelayMs; 3571 _recPacketDelayMs;
3571 *playout_buffer_delay_ms = playout_delay_ms_; 3572 *playout_buffer_delay_ms = playout_delay_ms_;
3572 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId), 3573 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3573 "Channel::GetDelayEstimate()"); 3574 "Channel::GetDelayEstimate()");
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
3613 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) 3614 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
3614 { 3615 {
3615 _engineStatisticsPtr->SetLastError( 3616 _engineStatisticsPtr->SetLastError(
3616 VE_AUDIO_CODING_MODULE_ERROR, kTraceError, 3617 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3617 "SetMinimumPlayoutDelay() failed to set min playout delay"); 3618 "SetMinimumPlayoutDelay() failed to set min playout delay");
3618 return -1; 3619 return -1;
3619 } 3620 }
3620 return 0; 3621 return 0;
3621 } 3622 }
3622 3623
3623 void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3624 uint32_t playout_timestamp = 0;
3625
3626 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
3627 // This can happen if this channel has not been received any RTP packet. In
3628 // this case, NetEq is not capable of computing playout timestamp.
3629 return;
3630 }
3631
3632 uint16_t delay_ms = 0;
3633 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3634 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3635 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3636 " delay from the ADM");
3637 _engineStatisticsPtr->SetLastError(
3638 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3639 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3640 return;
3641 }
3642
3643 jitter_buffer_playout_timestamp_ = playout_timestamp;
3644
3645 // Remove the playout delay.
3646 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
3647
3648 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3649 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3650 playout_timestamp);
3651
3652 if (rtcp) {
3653 playout_timestamp_rtcp_ = playout_timestamp;
3654 } else {
3655 playout_timestamp_rtp_ = playout_timestamp;
3656 }
3657 playout_delay_ms_ = delay_ms;
3658 }
3659
3660 int Channel::GetPlayoutTimestamp(unsigned int& timestamp) { 3624 int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3661 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId), 3625 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3662 "Channel::GetPlayoutTimestamp()"); 3626 "Channel::GetPlayoutTimestamp()");
3663 if (playout_timestamp_rtp_ == 0) { 3627 unsigned int playout_timestamp_rtp;
the sun 2015/08/05 08:49:12 Nits: use uint32_t (type of the member we're assig
3628 {
3629 CriticalSectionScoped cs(video_sync_lock_.get());
3630 playout_timestamp_rtp = playout_timestamp_rtp_;
3631 }
3632 if (playout_timestamp_rtp == 0) {
3664 _engineStatisticsPtr->SetLastError( 3633 _engineStatisticsPtr->SetLastError(
3665 VE_CANNOT_RETRIEVE_VALUE, kTraceError, 3634 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3666 "GetPlayoutTimestamp() failed to retrieve timestamp"); 3635 "GetPlayoutTimestamp() failed to retrieve timestamp");
3667 return -1; 3636 return -1;
3668 } 3637 }
3669 timestamp = playout_timestamp_rtp_; 3638 timestamp = playout_timestamp_rtp;
3670 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, 3639 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3671 VoEId(_instanceId,_channelId), 3640 VoEId(_instanceId,_channelId),
3672 "GetPlayoutTimestamp() => timestamp=%u", timestamp); 3641 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3673 return 0; 3642 return 0;
3674 } 3643 }
3675 3644
3676 int Channel::SetInitTimestamp(unsigned int timestamp) { 3645 int Channel::SetInitTimestamp(unsigned int timestamp) {
3677 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), 3646 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3678 "Channel::SetInitTimestamp()"); 3647 "Channel::SetInitTimestamp()");
3679 if (channel_state_.Get().sending) { 3648 if (channel_state_.Get().sending) {
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
3915 if (!RTCP) 3884 if (!RTCP)
3916 { 3885 {
3917 return _transportPtr->SendPacket(_channelId, data, len); 3886 return _transportPtr->SendPacket(_channelId, data, len);
3918 } 3887 }
3919 else 3888 else
3920 { 3889 {
3921 return _transportPtr->SendRTCPPacket(_channelId, data, len); 3890 return _transportPtr->SendRTCPPacket(_channelId, data, len);
3922 } 3891 }
3923 } 3892 }
3924 3893
3894 void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3895 uint32_t playout_timestamp = 0;
3896
3897 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
3898 // This can happen if this channel has not been received any RTP packet. In
3899 // this case, NetEq is not capable of computing playout timestamp.
3900 return;
3901 }
3902
3903 uint16_t delay_ms = 0;
3904 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3905 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3906 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3907 " delay from the ADM");
3908 _engineStatisticsPtr->SetLastError(
3909 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3910 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3911 return;
3912 }
3913
3914 jitter_buffer_playout_timestamp_ = playout_timestamp;
3915
3916 // Remove the playout delay.
3917 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
3918
3919 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3920 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3921 playout_timestamp);
3922
3923 {
3924 CriticalSectionScoped cs(video_sync_lock_.get());
3925 if (rtcp) {
3926 playout_timestamp_rtcp_ = playout_timestamp;
3927 } else {
3928 playout_timestamp_rtp_ = playout_timestamp;
3929 }
3930 playout_delay_ms_ = delay_ms;
3931 }
3932 }
3933
3925 // Called for incoming RTP packets after successful RTP header parsing. 3934 // Called for incoming RTP packets after successful RTP header parsing.
3926 void Channel::UpdatePacketDelay(uint32_t rtp_timestamp, 3935 void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
3927 uint16_t sequence_number) { 3936 uint16_t sequence_number) {
3928 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId), 3937 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3929 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)", 3938 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
3930 rtp_timestamp, sequence_number); 3939 rtp_timestamp, sequence_number);
3931 3940
3932 // Get frequency of last received payload 3941 // Get frequency of last received payload
3933 int rtp_receive_frequency = GetPlayoutFrequency(); 3942 int rtp_receive_frequency = GetPlayoutFrequency();
3934 3943
3935 // Update the least required delay.
3936 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
3937
3938 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for 3944 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
3939 // every incoming packet. 3945 // every incoming packet.
3940 uint32_t timestamp_diff_ms = (rtp_timestamp - 3946 uint32_t timestamp_diff_ms = (rtp_timestamp -
3941 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000); 3947 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
3942 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) || 3948 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
3943 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) { 3949 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
3944 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP 3950 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
3945 // timestamp, the resulting difference is negative, but is set to zero. 3951 // timestamp, the resulting difference is negative, but is set to zero.
3946 // This can happen when a network glitch causes a packet to arrive late, 3952 // This can happen when a network glitch causes a packet to arrive late,
3947 // and during long comfort noise periods with clock drift. 3953 // and during long comfort noise periods with clock drift.
3948 timestamp_diff_ms = 0; 3954 timestamp_diff_ms = 0;
3949 } 3955 }
3950 3956
3951 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) / 3957 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
3952 (rtp_receive_frequency / 1000); 3958 (rtp_receive_frequency / 1000);
3953 3959
3954 _previousTimestamp = rtp_timestamp; 3960 _previousTimestamp = rtp_timestamp;
3955 3961
3956 if (timestamp_diff_ms == 0) return; 3962 if (timestamp_diff_ms == 0) return;
3957 3963
3958 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) { 3964 {
3959 _recPacketDelayMs = packet_delay_ms; 3965 CriticalSectionScoped cs(video_sync_lock_.get());
3966
3967 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
3968 _recPacketDelayMs = packet_delay_ms;
3969 }
3970
3971 if (_average_jitter_buffer_delay_us == 0) {
3972 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
3973 return;
3974 }
3975
3976 // Filter average delay value using exponential filter (alpha is
3977 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
3978 // risk of rounding error) and compensate for it in GetDelayEstimate()
3979 // later.
3980 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
3981 1000 * timestamp_diff_ms + 500) / 8;
3960 } 3982 }
3961
3962 if (_average_jitter_buffer_delay_us == 0) {
3963 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
3964 return;
3965 }
3966
3967 // Filter average delay value using exponential filter (alpha is
3968 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
3969 // risk of rounding error) and compensate for it in GetDelayEstimate()
3970 // later.
3971 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
3972 1000 * timestamp_diff_ms + 500) / 8;
3973 } 3983 }
3974 3984
3975 void 3985 void
3976 Channel::RegisterReceiveCodecsToRTPModule() 3986 Channel::RegisterReceiveCodecsToRTPModule()
3977 { 3987 {
3978 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId), 3988 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3979 "Channel::RegisterReceiveCodecsToRTPModule()"); 3989 "Channel::RegisterReceiveCodecsToRTPModule()");
3980 3990
3981 3991
3982 CodecInst codec; 3992 CodecInst codec;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
4131 int64_t min_rtt = 0; 4141 int64_t min_rtt = 0;
4132 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) 4142 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4133 != 0) { 4143 != 0) {
4134 return 0; 4144 return 0;
4135 } 4145 }
4136 return rtt; 4146 return rtt;
4137 } 4147 }
4138 4148
4139 } // namespace voe 4149 } // namespace voe
4140 } // namespace webrtc 4150 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/voice_engine/channel.h ('k') | webrtc/voice_engine/voe_video_sync_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698