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 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 int length = 0; | 820 int length = 0; |
821 int decode_return_value = Decode(&packet_list, &operation, | 821 int decode_return_value = Decode(&packet_list, &operation, |
822 &length, &speech_type); | 822 &length, &speech_type); |
823 | 823 |
824 assert(vad_.get()); | 824 assert(vad_.get()); |
825 bool sid_frame_available = | 825 bool sid_frame_available = |
826 (operation == kRfc3389Cng && !packet_list.empty()); | 826 (operation == kRfc3389Cng && !packet_list.empty()); |
827 vad_->Update(decoded_buffer_.get(), static_cast<size_t>(length), speech_type, | 827 vad_->Update(decoded_buffer_.get(), static_cast<size_t>(length), speech_type, |
828 sid_frame_available, fs_hz_); | 828 sid_frame_available, fs_hz_); |
829 | 829 |
| 830 if (sid_frame_available || speech_type == AudioDecoder::kComfortNoise) { |
| 831 // Start a new stopwatch since we are decoding a new CNG packet. |
| 832 generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch(); |
| 833 } |
| 834 |
830 algorithm_buffer_->Clear(); | 835 algorithm_buffer_->Clear(); |
831 switch (operation) { | 836 switch (operation) { |
832 case kNormal: { | 837 case kNormal: { |
833 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf); | 838 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf); |
834 break; | 839 break; |
835 } | 840 } |
836 case kMerge: { | 841 case kMerge: { |
837 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf); | 842 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf); |
838 break; | 843 break; |
839 } | 844 } |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 // Set the timestamp in the audio frame to zero before the first packet has | 997 // Set the timestamp in the audio frame to zero before the first packet has |
993 // been inserted. Otherwise, subtract the frame size in samples to get the | 998 // been inserted. Otherwise, subtract the frame size in samples to get the |
994 // timestamp of the first sample in the frame (playout_timestamp_ is the | 999 // timestamp of the first sample in the frame (playout_timestamp_ is the |
995 // last + 1). | 1000 // last + 1). |
996 audio_frame->timestamp_ = | 1001 audio_frame->timestamp_ = |
997 first_packet_ | 1002 first_packet_ |
998 ? 0 | 1003 ? 0 |
999 : timestamp_scaler_->ToExternal(playout_timestamp_) - | 1004 : timestamp_scaler_->ToExternal(playout_timestamp_) - |
1000 static_cast<uint32_t>(audio_frame->samples_per_channel_); | 1005 static_cast<uint32_t>(audio_frame->samples_per_channel_); |
1001 | 1006 |
| 1007 if (!(last_mode_ == kModeRfc3389Cng || |
| 1008 last_mode_ == kModeCodecInternalCng || |
| 1009 last_mode_ == kModeExpand)) { |
| 1010 generated_noise_stopwatch_.reset(); |
| 1011 } |
| 1012 |
1002 if (decode_return_value) return decode_return_value; | 1013 if (decode_return_value) return decode_return_value; |
1003 return return_value; | 1014 return return_value; |
1004 } | 1015 } |
1005 | 1016 |
1006 int NetEqImpl::GetDecision(Operations* operation, | 1017 int NetEqImpl::GetDecision(Operations* operation, |
1007 PacketList* packet_list, | 1018 PacketList* packet_list, |
1008 DtmfEvent* dtmf_event, | 1019 DtmfEvent* dtmf_event, |
1009 bool* play_dtmf) { | 1020 bool* play_dtmf) { |
1010 // Initialize output variables. | 1021 // Initialize output variables. |
1011 *play_dtmf = false; | 1022 *play_dtmf = false; |
1012 *operation = kUndefined; | 1023 *operation = kUndefined; |
1013 | 1024 |
1014 // Increment time counters. | 1025 // Increment time counters. |
1015 stats_.IncreaseCounter(output_size_samples_, fs_hz_); | 1026 stats_.IncreaseCounter(output_size_samples_, fs_hz_); |
1016 | 1027 |
1017 assert(sync_buffer_.get()); | 1028 assert(sync_buffer_.get()); |
1018 uint32_t end_timestamp = sync_buffer_->end_timestamp(); | 1029 uint32_t end_timestamp = sync_buffer_->end_timestamp(); |
1019 if (!new_codec_) { | 1030 if (!new_codec_) { |
1020 const uint32_t five_seconds_samples = 5 * fs_hz_; | 1031 const uint32_t five_seconds_samples = 5 * fs_hz_; |
1021 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples); | 1032 packet_buffer_->DiscardOldPackets(end_timestamp, five_seconds_samples); |
1022 } | 1033 } |
1023 const RTPHeader* header = packet_buffer_->NextRtpHeader(); | 1034 const RTPHeader* header = packet_buffer_->NextRtpHeader(); |
1024 | 1035 |
| 1036 RTC_DCHECK(!generated_noise_stopwatch_ || |
| 1037 generated_noise_stopwatch_->ElapsedTicks() >= 1); |
| 1038 uint64_t generated_noise_samples = |
| 1039 generated_noise_stopwatch_ |
| 1040 ? (generated_noise_stopwatch_->ElapsedTicks() - 1) * |
| 1041 output_size_samples_ + |
| 1042 decision_logic_->noise_fast_forward() |
| 1043 : 0; |
| 1044 |
1025 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) { | 1045 if (decision_logic_->CngRfc3389On() || last_mode_ == kModeRfc3389Cng) { |
1026 // Because of timestamp peculiarities, we have to "manually" disallow using | 1046 // Because of timestamp peculiarities, we have to "manually" disallow using |
1027 // a CNG packet with the same timestamp as the one that was last played. | 1047 // a CNG packet with the same timestamp as the one that was last played. |
1028 // This can happen when using redundancy and will cause the timing to shift. | 1048 // This can happen when using redundancy and will cause the timing to shift. |
1029 while (header && decoder_database_->IsComfortNoise(header->payloadType) && | 1049 while (header && decoder_database_->IsComfortNoise(header->payloadType) && |
1030 (end_timestamp >= header->timestamp || | 1050 (end_timestamp >= header->timestamp || |
1031 end_timestamp + decision_logic_->generated_noise_samples() > | 1051 end_timestamp + generated_noise_samples > header->timestamp)) { |
1032 header->timestamp)) { | |
1033 // Don't use this packet, discard it. | 1052 // Don't use this packet, discard it. |
1034 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) { | 1053 if (packet_buffer_->DiscardNextPacket() != PacketBuffer::kOK) { |
1035 assert(false); // Must be ok by design. | 1054 assert(false); // Must be ok by design. |
1036 } | 1055 } |
1037 // Check buffer again. | 1056 // Check buffer again. |
1038 if (!new_codec_) { | 1057 if (!new_codec_) { |
1039 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_); | 1058 packet_buffer_->DiscardOldPackets(end_timestamp, 5 * fs_hz_); |
1040 } | 1059 } |
1041 header = packet_buffer_->NextRtpHeader(); | 1060 header = packet_buffer_->NextRtpHeader(); |
1042 } | 1061 } |
1043 } | 1062 } |
1044 | 1063 |
1045 assert(expand_.get()); | 1064 assert(expand_.get()); |
1046 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - | 1065 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - |
1047 expand_->overlap_length()); | 1066 expand_->overlap_length()); |
1048 if (last_mode_ == kModeAccelerateSuccess || | 1067 if (last_mode_ == kModeAccelerateSuccess || |
1049 last_mode_ == kModeAccelerateLowEnergy || | 1068 last_mode_ == kModeAccelerateLowEnergy || |
1050 last_mode_ == kModePreemptiveExpandSuccess || | 1069 last_mode_ == kModePreemptiveExpandSuccess || |
1051 last_mode_ == kModePreemptiveExpandLowEnergy) { | 1070 last_mode_ == kModePreemptiveExpandLowEnergy) { |
1052 // Subtract (samples_left + output_size_samples_) from sampleMemory. | 1071 // Subtract (samples_left + output_size_samples_) from sampleMemory. |
1053 decision_logic_->AddSampleMemory( | 1072 decision_logic_->AddSampleMemory( |
1054 -(samples_left + rtc::checked_cast<int>(output_size_samples_))); | 1073 -(samples_left + rtc::checked_cast<int>(output_size_samples_))); |
1055 } | 1074 } |
1056 | 1075 |
1057 // Check if it is time to play a DTMF event. | 1076 // Check if it is time to play a DTMF event. |
1058 if (dtmf_buffer_->GetEvent( | 1077 if (dtmf_buffer_->GetEvent( |
1059 static_cast<uint32_t>( | 1078 static_cast<uint32_t>( |
1060 end_timestamp + decision_logic_->generated_noise_samples()), | 1079 end_timestamp + generated_noise_samples), |
1061 dtmf_event)) { | 1080 dtmf_event)) { |
1062 *play_dtmf = true; | 1081 *play_dtmf = true; |
1063 } | 1082 } |
1064 | 1083 |
1065 // Get instruction. | 1084 // Get instruction. |
1066 assert(sync_buffer_.get()); | 1085 assert(sync_buffer_.get()); |
1067 assert(expand_.get()); | 1086 assert(expand_.get()); |
1068 *operation = decision_logic_->GetDecision(*sync_buffer_, | 1087 generated_noise_samples = |
1069 *expand_, | 1088 generated_noise_stopwatch_ |
1070 decoder_frame_length_, | 1089 ? generated_noise_stopwatch_->ElapsedTicks() * output_size_samples_ + |
1071 header, | 1090 decision_logic_->noise_fast_forward() |
1072 last_mode_, | 1091 : 0; |
1073 *play_dtmf, | 1092 *operation = decision_logic_->GetDecision( |
1074 &reset_decoder_); | 1093 *sync_buffer_, *expand_, decoder_frame_length_, header, last_mode_, |
| 1094 *play_dtmf, generated_noise_samples, &reset_decoder_); |
1075 | 1095 |
1076 // Check if we already have enough samples in the |sync_buffer_|. If so, | 1096 // Check if we already have enough samples in the |sync_buffer_|. If so, |
1077 // change decision to normal, unless the decision was merge, accelerate, or | 1097 // change decision to normal, unless the decision was merge, accelerate, or |
1078 // preemptive expand. | 1098 // preemptive expand. |
1079 if (samples_left >= rtc::checked_cast<int>(output_size_samples_) && | 1099 if (samples_left >= rtc::checked_cast<int>(output_size_samples_) && |
1080 *operation != kMerge && | 1100 *operation != kMerge && |
1081 *operation != kAccelerate && | 1101 *operation != kAccelerate && |
1082 *operation != kFastAccelerate && | 1102 *operation != kFastAccelerate && |
1083 *operation != kPreemptiveExpand) { | 1103 *operation != kPreemptiveExpand) { |
1084 *operation = kNormal; | 1104 *operation = kNormal; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1137 return 0; | 1157 return 0; |
1138 } | 1158 } |
1139 case kRfc3389CngNoPacket: | 1159 case kRfc3389CngNoPacket: |
1140 case kCodecInternalCng: { | 1160 case kCodecInternalCng: { |
1141 return 0; | 1161 return 0; |
1142 } | 1162 } |
1143 case kDtmf: { | 1163 case kDtmf: { |
1144 // TODO(hlundin): Write test for this. | 1164 // TODO(hlundin): Write test for this. |
1145 // Update timestamp. | 1165 // Update timestamp. |
1146 timestamp_ = end_timestamp; | 1166 timestamp_ = end_timestamp; |
1147 if (decision_logic_->generated_noise_samples() > 0 && | 1167 const uint64_t generated_noise_samples = |
1148 last_mode_ != kModeDtmf) { | 1168 generated_noise_stopwatch_ |
| 1169 ? generated_noise_stopwatch_->ElapsedTicks() * |
| 1170 output_size_samples_ + |
| 1171 decision_logic_->noise_fast_forward() |
| 1172 : 0; |
| 1173 if (generated_noise_samples > 0 && last_mode_ != kModeDtmf) { |
1149 // Make a jump in timestamp due to the recently played comfort noise. | 1174 // Make a jump in timestamp due to the recently played comfort noise. |
1150 uint32_t timestamp_jump = | 1175 uint32_t timestamp_jump = |
1151 static_cast<uint32_t>(decision_logic_->generated_noise_samples()); | 1176 static_cast<uint32_t>(generated_noise_samples); |
1152 sync_buffer_->IncreaseEndTimestamp(timestamp_jump); | 1177 sync_buffer_->IncreaseEndTimestamp(timestamp_jump); |
1153 timestamp_ += timestamp_jump; | 1178 timestamp_ += timestamp_jump; |
1154 } | 1179 } |
1155 decision_logic_->set_generated_noise_samples(0); | |
1156 return 0; | 1180 return 0; |
1157 } | 1181 } |
1158 case kAccelerate: | 1182 case kAccelerate: |
1159 case kFastAccelerate: { | 1183 case kFastAccelerate: { |
1160 // In order to do an accelerate we need at least 30 ms of audio data. | 1184 // In order to do an accelerate we need at least 30 ms of audio data. |
1161 if (samples_left >= static_cast<int>(samples_30_ms)) { | 1185 if (samples_left >= static_cast<int>(samples_30_ms)) { |
1162 // Already have enough data, so we do not need to extract any more. | 1186 // Already have enough data, so we do not need to extract any more. |
1163 decision_logic_->set_sample_memory(samples_left); | 1187 decision_logic_->set_sample_memory(samples_left); |
1164 decision_logic_->set_prev_time_scale(true); | 1188 decision_logic_->set_prev_time_scale(true); |
1165 return 0; | 1189 return 0; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1228 // if comfort noise is not played. If comfort noise was just played, | 1252 // if comfort noise is not played. If comfort noise was just played, |
1229 // this adjustment of timestamp is only done to get back in sync with the | 1253 // this adjustment of timestamp is only done to get back in sync with the |
1230 // stream timestamp; no loss to report. | 1254 // stream timestamp; no loss to report. |
1231 stats_.LostSamples(header->timestamp - end_timestamp); | 1255 stats_.LostSamples(header->timestamp - end_timestamp); |
1232 } | 1256 } |
1233 | 1257 |
1234 if (*operation != kRfc3389Cng) { | 1258 if (*operation != kRfc3389Cng) { |
1235 // We are about to decode and use a non-CNG packet. | 1259 // We are about to decode and use a non-CNG packet. |
1236 decision_logic_->SetCngOff(); | 1260 decision_logic_->SetCngOff(); |
1237 } | 1261 } |
1238 // Reset CNG timestamp as a new packet will be delivered. | |
1239 // (Also if this is a CNG packet, since playedOutTS is updated.) | |
1240 decision_logic_->set_generated_noise_samples(0); | |
1241 | 1262 |
1242 extracted_samples = ExtractPackets(required_samples, packet_list); | 1263 extracted_samples = ExtractPackets(required_samples, packet_list); |
1243 if (extracted_samples < 0) { | 1264 if (extracted_samples < 0) { |
1244 return kPacketBufferCorruption; | 1265 return kPacketBufferCorruption; |
1245 } | 1266 } |
1246 } | 1267 } |
1247 | 1268 |
1248 if (*operation == kAccelerate || *operation == kFastAccelerate || | 1269 if (*operation == kAccelerate || *operation == kFastAccelerate || |
1249 *operation == kPreemptiveExpand) { | 1270 *operation == kPreemptiveExpand) { |
1250 decision_logic_->set_sample_memory(samples_left + extracted_samples); | 1271 decision_logic_->set_sample_memory(samples_left + extracted_samples); |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1563 if (return_value < 0) { | 1584 if (return_value < 0) { |
1564 return return_value; | 1585 return return_value; |
1565 } | 1586 } |
1566 | 1587 |
1567 sync_buffer_->PushBack(*algorithm_buffer_); | 1588 sync_buffer_->PushBack(*algorithm_buffer_); |
1568 algorithm_buffer_->Clear(); | 1589 algorithm_buffer_->Clear(); |
1569 } | 1590 } |
1570 if (!play_dtmf) { | 1591 if (!play_dtmf) { |
1571 dtmf_tone_generator_->Reset(); | 1592 dtmf_tone_generator_->Reset(); |
1572 } | 1593 } |
| 1594 |
| 1595 if (!generated_noise_stopwatch_) { |
| 1596 // Start a new stopwatch since we may be covering for a lost CNG packet. |
| 1597 generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch(); |
| 1598 } |
| 1599 |
1573 return 0; | 1600 return 0; |
1574 } | 1601 } |
1575 | 1602 |
1576 int NetEqImpl::DoAccelerate(int16_t* decoded_buffer, | 1603 int NetEqImpl::DoAccelerate(int16_t* decoded_buffer, |
1577 size_t decoded_length, | 1604 size_t decoded_length, |
1578 AudioDecoder::SpeechType speech_type, | 1605 AudioDecoder::SpeechType speech_type, |
1579 bool play_dtmf, | 1606 bool play_dtmf, |
1580 bool fast_accelerate) { | 1607 bool fast_accelerate) { |
1581 const size_t required_samples = | 1608 const size_t required_samples = |
1582 static_cast<size_t>(240 * fs_mult_); // Must have 30 ms. | 1609 static_cast<size_t>(240 * fs_mult_); // Must have 30 ms. |
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2114 | 2141 |
2115 void NetEqImpl::CreateDecisionLogic() { | 2142 void NetEqImpl::CreateDecisionLogic() { |
2116 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, | 2143 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, |
2117 playout_mode_, | 2144 playout_mode_, |
2118 decoder_database_.get(), | 2145 decoder_database_.get(), |
2119 *packet_buffer_.get(), | 2146 *packet_buffer_.get(), |
2120 delay_manager_.get(), | 2147 delay_manager_.get(), |
2121 buffer_level_filter_.get())); | 2148 buffer_level_filter_.get())); |
2122 } | 2149 } |
2123 } // namespace webrtc | 2150 } // namespace webrtc |
OLD | NEW |