OLD | NEW |
---|---|
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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 int NetEqImpl::GetAudio(AudioFrame* audio_frame, bool* muted) { | 192 int NetEqImpl::GetAudio(AudioFrame* audio_frame, bool* muted) { |
193 TRACE_EVENT0("webrtc", "NetEqImpl::GetAudio"); | 193 TRACE_EVENT0("webrtc", "NetEqImpl::GetAudio"); |
194 rtc::CritScope lock(&crit_sect_); | 194 rtc::CritScope lock(&crit_sect_); |
195 int error = GetAudioInternal(audio_frame, muted); | 195 int error = GetAudioInternal(audio_frame, muted); |
196 if (error != 0) { | 196 if (error != 0) { |
197 error_code_ = error; | 197 error_code_ = error; |
198 return kFail; | 198 return kFail; |
199 } | 199 } |
200 RTC_DCHECK_EQ( | 200 RTC_DCHECK_EQ( |
201 audio_frame->sample_rate_hz_, | 201 audio_frame->sample_rate_hz_, |
202 rtc::checked_cast<int>(audio_frame->samples_per_channel_ * 100)); | 202 rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100)); |
203 SetAudioFrameActivityAndType(vad_->enabled(), LastOutputType(), | 203 SetAudioFrameActivityAndType(vad_->enabled(), LastOutputType(), |
204 last_vad_activity_, audio_frame); | 204 last_vad_activity_, audio_frame); |
205 last_vad_activity_ = audio_frame->vad_activity_; | 205 last_vad_activity_ = audio_frame->vad_activity_; |
206 last_output_sample_rate_hz_ = audio_frame->sample_rate_hz_; | 206 last_output_sample_rate_hz_ = audio_frame->sample_rate_hz_; |
207 RTC_DCHECK(last_output_sample_rate_hz_ == 8000 || | 207 RTC_DCHECK(last_output_sample_rate_hz_ == 8000 || |
208 last_output_sample_rate_hz_ == 16000 || | 208 last_output_sample_rate_hz_ == 16000 || |
209 last_output_sample_rate_hz_ == 32000 || | 209 last_output_sample_rate_hz_ == 32000 || |
210 last_output_sample_rate_hz_ == 48000) | 210 last_output_sample_rate_hz_ == 48000) |
211 << "Unexpected sample rate " << last_output_sample_rate_hz_; | 211 << "Unexpected sample rate " << last_output_sample_rate_hz_; |
212 return kOK; | 212 return kOK; |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
819 const size_t buffer_length_after_insert = | 819 const size_t buffer_length_after_insert = |
820 packet_buffer_->NumPacketsInBuffer(); | 820 packet_buffer_->NumPacketsInBuffer(); |
821 | 821 |
822 if (buffer_length_after_insert > buffer_length_before_insert) { | 822 if (buffer_length_after_insert > buffer_length_before_insert) { |
823 const size_t packet_length_samples = | 823 const size_t packet_length_samples = |
824 (buffer_length_after_insert - buffer_length_before_insert) * | 824 (buffer_length_after_insert - buffer_length_before_insert) * |
825 decoder_frame_length_; | 825 decoder_frame_length_; |
826 if (packet_length_samples != decision_logic_->packet_length_samples()) { | 826 if (packet_length_samples != decision_logic_->packet_length_samples()) { |
827 decision_logic_->set_packet_length_samples(packet_length_samples); | 827 decision_logic_->set_packet_length_samples(packet_length_samples); |
828 delay_manager_->SetPacketAudioLength( | 828 delay_manager_->SetPacketAudioLength( |
829 rtc::checked_cast<int>((1000 * packet_length_samples) / fs_hz_)); | 829 rtc::dchecked_cast<int>((1000 * packet_length_samples) / fs_hz_)); |
830 } | 830 } |
831 } | 831 } |
832 | 832 |
833 // Update statistics. | 833 // Update statistics. |
834 if ((int32_t)(main_timestamp - timestamp_) >= 0 && !new_codec_) { | 834 if ((int32_t)(main_timestamp - timestamp_) >= 0 && !new_codec_) { |
835 // Only update statistics if incoming packet is not older than last played | 835 // Only update statistics if incoming packet is not older than last played |
836 // out packet, and if new codec flag is not set. | 836 // out packet, and if new codec flag is not set. |
837 delay_manager_->Update(main_sequence_number, main_timestamp, fs_hz_); | 837 delay_manager_->Update(main_sequence_number, main_timestamp, fs_hz_); |
838 } | 838 } |
839 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) { | 839 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) { |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1124 | 1124 |
1125 assert(expand_.get()); | 1125 assert(expand_.get()); |
1126 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - | 1126 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - |
1127 expand_->overlap_length()); | 1127 expand_->overlap_length()); |
1128 if (last_mode_ == kModeAccelerateSuccess || | 1128 if (last_mode_ == kModeAccelerateSuccess || |
1129 last_mode_ == kModeAccelerateLowEnergy || | 1129 last_mode_ == kModeAccelerateLowEnergy || |
1130 last_mode_ == kModePreemptiveExpandSuccess || | 1130 last_mode_ == kModePreemptiveExpandSuccess || |
1131 last_mode_ == kModePreemptiveExpandLowEnergy) { | 1131 last_mode_ == kModePreemptiveExpandLowEnergy) { |
1132 // Subtract (samples_left + output_size_samples_) from sampleMemory. | 1132 // Subtract (samples_left + output_size_samples_) from sampleMemory. |
1133 decision_logic_->AddSampleMemory( | 1133 decision_logic_->AddSampleMemory( |
1134 -(samples_left + rtc::checked_cast<int>(output_size_samples_))); | 1134 -(samples_left + rtc::dchecked_cast<int>(output_size_samples_))); |
1135 } | 1135 } |
1136 | 1136 |
1137 // Check if it is time to play a DTMF event. | 1137 // Check if it is time to play a DTMF event. |
1138 if (dtmf_buffer_->GetEvent( | 1138 if (dtmf_buffer_->GetEvent( |
1139 static_cast<uint32_t>( | 1139 static_cast<uint32_t>( |
1140 end_timestamp + generated_noise_samples), | 1140 end_timestamp + generated_noise_samples), |
1141 dtmf_event)) { | 1141 dtmf_event)) { |
1142 *play_dtmf = true; | 1142 *play_dtmf = true; |
1143 } | 1143 } |
1144 | 1144 |
1145 // Get instruction. | 1145 // Get instruction. |
1146 assert(sync_buffer_.get()); | 1146 assert(sync_buffer_.get()); |
1147 assert(expand_.get()); | 1147 assert(expand_.get()); |
1148 generated_noise_samples = | 1148 generated_noise_samples = |
1149 generated_noise_stopwatch_ | 1149 generated_noise_stopwatch_ |
1150 ? generated_noise_stopwatch_->ElapsedTicks() * output_size_samples_ + | 1150 ? generated_noise_stopwatch_->ElapsedTicks() * output_size_samples_ + |
1151 decision_logic_->noise_fast_forward() | 1151 decision_logic_->noise_fast_forward() |
1152 : 0; | 1152 : 0; |
1153 *operation = decision_logic_->GetDecision( | 1153 *operation = decision_logic_->GetDecision( |
1154 *sync_buffer_, *expand_, decoder_frame_length_, packet, last_mode_, | 1154 *sync_buffer_, *expand_, decoder_frame_length_, packet, last_mode_, |
1155 *play_dtmf, generated_noise_samples, &reset_decoder_); | 1155 *play_dtmf, generated_noise_samples, &reset_decoder_); |
1156 | 1156 |
1157 // Check if we already have enough samples in the |sync_buffer_|. If so, | 1157 // Check if we already have enough samples in the |sync_buffer_|. If so, |
1158 // change decision to normal, unless the decision was merge, accelerate, or | 1158 // change decision to normal, unless the decision was merge, accelerate, or |
1159 // preemptive expand. | 1159 // preemptive expand. |
1160 if (samples_left >= rtc::checked_cast<int>(output_size_samples_) && | 1160 if (samples_left >= rtc::dchecked_cast<int>(output_size_samples_) && |
1161 *operation != kMerge && | 1161 *operation != kMerge && *operation != kAccelerate && |
1162 *operation != kAccelerate && | 1162 *operation != kFastAccelerate && *operation != kPreemptiveExpand) { |
1163 *operation != kFastAccelerate && | |
1164 *operation != kPreemptiveExpand) { | |
1165 *operation = kNormal; | 1163 *operation = kNormal; |
1166 return 0; | 1164 return 0; |
1167 } | 1165 } |
1168 | 1166 |
1169 decision_logic_->ExpandDecision(*operation); | 1167 decision_logic_->ExpandDecision(*operation); |
1170 | 1168 |
1171 // Check conditions for reset. | 1169 // Check conditions for reset. |
1172 if (new_codec_ || *operation == kUndefined) { | 1170 if (new_codec_ || *operation == kUndefined) { |
1173 // The only valid reason to get kUndefined is that new_codec_ is set. | 1171 // The only valid reason to get kUndefined is that new_codec_ is set. |
1174 assert(new_codec_); | 1172 assert(new_codec_); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1447 } | 1445 } |
1448 | 1446 |
1449 int NetEqImpl::DecodeCng(AudioDecoder* decoder, int* decoded_length, | 1447 int NetEqImpl::DecodeCng(AudioDecoder* decoder, int* decoded_length, |
1450 AudioDecoder::SpeechType* speech_type) { | 1448 AudioDecoder::SpeechType* speech_type) { |
1451 if (!decoder) { | 1449 if (!decoder) { |
1452 // This happens when active decoder is not defined. | 1450 // This happens when active decoder is not defined. |
1453 *decoded_length = -1; | 1451 *decoded_length = -1; |
1454 return 0; | 1452 return 0; |
1455 } | 1453 } |
1456 | 1454 |
1457 while (*decoded_length < rtc::checked_cast<int>(output_size_samples_)) { | 1455 while (*decoded_length < rtc::dchecked_cast<int>(output_size_samples_)) { |
1458 const int length = decoder->Decode( | 1456 const int length = decoder->Decode( |
1459 nullptr, 0, fs_hz_, | 1457 nullptr, 0, fs_hz_, |
1460 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t), | 1458 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t), |
1461 &decoded_buffer_[*decoded_length], speech_type); | 1459 &decoded_buffer_[*decoded_length], speech_type); |
1462 if (length > 0) { | 1460 if (length > 0) { |
1463 *decoded_length += length; | 1461 *decoded_length += length; |
1464 } else { | 1462 } else { |
1465 // Error. | 1463 // Error. |
1466 LOG(LS_WARNING) << "Failed to decode CNG"; | 1464 LOG(LS_WARNING) << "Failed to decode CNG"; |
1467 *decoded_length = -1; | 1465 *decoded_length = -1; |
(...skipping 25 matching lines...) Expand all Loading... | |
1493 operation == kPreemptiveExpand); | 1491 operation == kPreemptiveExpand); |
1494 | 1492 |
1495 auto opt_result = packet_list->front().frame->Decode( | 1493 auto opt_result = packet_list->front().frame->Decode( |
1496 rtc::ArrayView<int16_t>(&decoded_buffer_[*decoded_length], | 1494 rtc::ArrayView<int16_t>(&decoded_buffer_[*decoded_length], |
1497 decoded_buffer_length_ - *decoded_length)); | 1495 decoded_buffer_length_ - *decoded_length)); |
1498 packet_list->pop_front(); | 1496 packet_list->pop_front(); |
1499 if (opt_result) { | 1497 if (opt_result) { |
1500 const auto& result = *opt_result; | 1498 const auto& result = *opt_result; |
1501 *speech_type = result.speech_type; | 1499 *speech_type = result.speech_type; |
1502 if (result.num_decoded_samples > 0) { | 1500 if (result.num_decoded_samples > 0) { |
1503 *decoded_length += rtc::checked_cast<int>(result.num_decoded_samples); | 1501 *decoded_length += rtc::dchecked_cast<int>(result.num_decoded_samples); |
ossu
2017/03/02 00:54:14
These decoded_length checks (above and below as we
kwiberg-webrtc
2017/03/02 02:13:37
Acknowledged.
| |
1504 // Update |decoder_frame_length_| with number of samples per channel. | 1502 // Update |decoder_frame_length_| with number of samples per channel. |
1505 decoder_frame_length_ = | 1503 decoder_frame_length_ = |
1506 result.num_decoded_samples / decoder->Channels(); | 1504 result.num_decoded_samples / decoder->Channels(); |
1507 } | 1505 } |
1508 } else { | 1506 } else { |
1509 // Error. | 1507 // Error. |
1510 // TODO(ossu): What to put here? | 1508 // TODO(ossu): What to put here? |
1511 LOG(LS_WARNING) << "Decode error"; | 1509 LOG(LS_WARNING) << "Decode error"; |
1512 *decoded_length = -1; | 1510 *decoded_length = -1; |
1513 packet_list->clear(); | 1511 packet_list->clear(); |
1514 break; | 1512 break; |
1515 } | 1513 } |
1516 if (*decoded_length > rtc::checked_cast<int>(decoded_buffer_length_)) { | 1514 if (*decoded_length > rtc::dchecked_cast<int>(decoded_buffer_length_)) { |
1517 // Guard against overflow. | 1515 // Guard against overflow. |
1518 LOG(LS_WARNING) << "Decoded too much."; | 1516 LOG(LS_WARNING) << "Decoded too much."; |
1519 packet_list->clear(); | 1517 packet_list->clear(); |
1520 return kDecodedTooMuch; | 1518 return kDecodedTooMuch; |
1521 } | 1519 } |
1522 } // End of decode loop. | 1520 } // End of decode loop. |
1523 | 1521 |
1524 // If the list is not empty at this point, either a decoding error terminated | 1522 // If the list is not empty at this point, either a decoding error terminated |
1525 // the while-loop, or list must hold exactly one CNG packet. | 1523 // the while-loop, or list must hold exactly one CNG packet. |
1526 assert( | 1524 assert( |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1979 } | 1977 } |
1980 | 1978 |
1981 const bool has_cng_packet = | 1979 const bool has_cng_packet = |
1982 decoder_database_->IsComfortNoise(packet->payload_type); | 1980 decoder_database_->IsComfortNoise(packet->payload_type); |
1983 // Store number of extracted samples. | 1981 // Store number of extracted samples. |
1984 size_t packet_duration = 0; | 1982 size_t packet_duration = 0; |
1985 if (packet->frame) { | 1983 if (packet->frame) { |
1986 packet_duration = packet->frame->Duration(); | 1984 packet_duration = packet->frame->Duration(); |
1987 // TODO(ossu): Is this the correct way to track Opus FEC packets? | 1985 // TODO(ossu): Is this the correct way to track Opus FEC packets? |
1988 if (packet->priority.codec_level > 0) { | 1986 if (packet->priority.codec_level > 0) { |
1989 stats_.SecondaryDecodedSamples(rtc::checked_cast<int>(packet_duration)); | 1987 stats_.SecondaryDecodedSamples( |
1988 rtc::dchecked_cast<int>(packet_duration)); | |
1990 } | 1989 } |
1991 } else if (!has_cng_packet) { | 1990 } else if (!has_cng_packet) { |
1992 LOG(LS_WARNING) << "Unknown payload type " | 1991 LOG(LS_WARNING) << "Unknown payload type " |
1993 << static_cast<int>(packet->payload_type); | 1992 << static_cast<int>(packet->payload_type); |
1994 RTC_NOTREACHED(); | 1993 RTC_NOTREACHED(); |
1995 } | 1994 } |
1996 | 1995 |
1997 if (packet_duration == 0) { | 1996 if (packet_duration == 0) { |
1998 // Decoder did not return a packet duration. Assume that the packet | 1997 // Decoder did not return a packet duration. Assume that the packet |
1999 // contains the same number of samples as the previous one. | 1998 // contains the same number of samples as the previous one. |
(...skipping 22 matching lines...) Expand all Loading... | |
2022 } while (extracted_samples < required_samples && next_packet_available); | 2021 } while (extracted_samples < required_samples && next_packet_available); |
2023 | 2022 |
2024 if (extracted_samples > 0) { | 2023 if (extracted_samples > 0) { |
2025 // Delete old packets only when we are going to decode something. Otherwise, | 2024 // Delete old packets only when we are going to decode something. Otherwise, |
2026 // we could end up in the situation where we never decode anything, since | 2025 // we could end up in the situation where we never decode anything, since |
2027 // all incoming packets are considered too old but the buffer will also | 2026 // all incoming packets are considered too old but the buffer will also |
2028 // never be flooded and flushed. | 2027 // never be flooded and flushed. |
2029 packet_buffer_->DiscardAllOldPackets(timestamp_); | 2028 packet_buffer_->DiscardAllOldPackets(timestamp_); |
2030 } | 2029 } |
2031 | 2030 |
2032 return rtc::checked_cast<int>(extracted_samples); | 2031 return rtc::dchecked_cast<int>(extracted_samples); |
2033 } | 2032 } |
2034 | 2033 |
2035 void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) { | 2034 void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) { |
2036 // Delete objects and create new ones. | 2035 // Delete objects and create new ones. |
2037 expand_.reset(expand_factory_->Create(background_noise_.get(), | 2036 expand_.reset(expand_factory_->Create(background_noise_.get(), |
2038 sync_buffer_.get(), &random_vector_, | 2037 sync_buffer_.get(), &random_vector_, |
2039 &stats_, fs_hz, channels)); | 2038 &stats_, fs_hz, channels)); |
2040 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get())); | 2039 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get())); |
2041 } | 2040 } |
2042 | 2041 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2129 } | 2128 } |
2130 } | 2129 } |
2131 | 2130 |
2132 void NetEqImpl::CreateDecisionLogic() { | 2131 void NetEqImpl::CreateDecisionLogic() { |
2133 decision_logic_.reset(DecisionLogic::Create( | 2132 decision_logic_.reset(DecisionLogic::Create( |
2134 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(), | 2133 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(), |
2135 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(), | 2134 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(), |
2136 tick_timer_.get())); | 2135 tick_timer_.get())); |
2137 } | 2136 } |
2138 } // namespace webrtc | 2137 } // namespace webrtc |
OLD | NEW |