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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 decoded_packet_timestamp_(0) { | 97 decoded_packet_timestamp_(0) { |
98 LOG(LS_INFO) << "NetEq config: " << config.ToString(); | 98 LOG(LS_INFO) << "NetEq config: " << config.ToString(); |
99 int fs = config.sample_rate_hz; | 99 int fs = config.sample_rate_hz; |
100 if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) { | 100 if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) { |
101 LOG(LS_ERROR) << "Sample rate " << fs << " Hz not supported. " << | 101 LOG(LS_ERROR) << "Sample rate " << fs << " Hz not supported. " << |
102 "Changing to 8000 Hz."; | 102 "Changing to 8000 Hz."; |
103 fs = 8000; | 103 fs = 8000; |
104 } | 104 } |
105 LOG(LS_VERBOSE) << "Create NetEqImpl object with fs = " << fs << "."; | 105 LOG(LS_VERBOSE) << "Create NetEqImpl object with fs = " << fs << "."; |
106 fs_hz_ = fs; | 106 fs_hz_ = fs; |
107 fs_mult_ = fs / 8000; | 107 fs_mult_ = static_cast<size_t>(fs / 8000); |
108 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_; | 108 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_; |
109 decoder_frame_length_ = 3 * output_size_samples_; | 109 decoder_frame_length_ = 3 * output_size_samples_; |
110 WebRtcSpl_Init(); | 110 WebRtcSpl_Init(); |
111 if (create_components) { | 111 if (create_components) { |
112 SetSampleRateAndChannels(fs, 1); // Default is 1 channel. | 112 SetSampleRateAndChannels(fs, 1); // Default is 1 channel. |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
116 NetEqImpl::~NetEqImpl() { | 116 NetEqImpl::~NetEqImpl() { |
117 LOG(LS_INFO) << "Deleting NetEqImpl object."; | 117 LOG(LS_INFO) << "Deleting NetEqImpl object."; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 | 152 |
153 if (error != 0) { | 153 if (error != 0) { |
154 LOG_FERR1(LS_WARNING, InsertPacketInternal, error); | 154 LOG_FERR1(LS_WARNING, InsertPacketInternal, error); |
155 error_code_ = error; | 155 error_code_ = error; |
156 return kFail; | 156 return kFail; |
157 } | 157 } |
158 return kOK; | 158 return kOK; |
159 } | 159 } |
160 | 160 |
161 int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio, | 161 int NetEqImpl::GetAudio(size_t max_length, int16_t* output_audio, |
162 int* samples_per_channel, int* num_channels, | 162 size_t* samples_per_channel, int* num_channels, |
163 NetEqOutputType* type) { | 163 NetEqOutputType* type) { |
164 CriticalSectionScoped lock(crit_sect_.get()); | 164 CriticalSectionScoped lock(crit_sect_.get()); |
165 LOG(LS_VERBOSE) << "GetAudio"; | 165 LOG(LS_VERBOSE) << "GetAudio"; |
166 int error = GetAudioInternal(max_length, output_audio, samples_per_channel, | 166 int error = GetAudioInternal(max_length, output_audio, samples_per_channel, |
167 num_channels); | 167 num_channels); |
168 LOG(LS_VERBOSE) << "Produced " << *samples_per_channel << | 168 LOG(LS_VERBOSE) << "Produced " << *samples_per_channel << |
169 " samples/channel for " << *num_channels << " channel(s)"; | 169 " samples/channel for " << *num_channels << " channel(s)"; |
170 if (error != 0) { | 170 if (error != 0) { |
171 LOG_FERR1(LS_WARNING, GetAudioInternal, error); | 171 LOG_FERR1(LS_WARNING, GetAudioInternal, error); |
172 error_code_ = error; | 172 error_code_ = error; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 // Deprecated. | 308 // Deprecated. |
309 // TODO(henrik.lundin) Delete. | 309 // TODO(henrik.lundin) Delete. |
310 NetEqPlayoutMode NetEqImpl::PlayoutMode() const { | 310 NetEqPlayoutMode NetEqImpl::PlayoutMode() const { |
311 CriticalSectionScoped lock(crit_sect_.get()); | 311 CriticalSectionScoped lock(crit_sect_.get()); |
312 return playout_mode_; | 312 return playout_mode_; |
313 } | 313 } |
314 | 314 |
315 int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) { | 315 int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) { |
316 CriticalSectionScoped lock(crit_sect_.get()); | 316 CriticalSectionScoped lock(crit_sect_.get()); |
317 assert(decoder_database_.get()); | 317 assert(decoder_database_.get()); |
318 const int total_samples_in_buffers = | 318 const size_t total_samples_in_buffers = |
319 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(), | 319 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(), |
320 decoder_frame_length_) + | 320 decoder_frame_length_) + |
321 static_cast<int>(sync_buffer_->FutureLength()); | 321 sync_buffer_->FutureLength(); |
322 assert(delay_manager_.get()); | 322 assert(delay_manager_.get()); |
323 assert(decision_logic_.get()); | 323 assert(decision_logic_.get()); |
324 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers, | 324 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers, |
325 decoder_frame_length_, *delay_manager_.get(), | 325 decoder_frame_length_, *delay_manager_.get(), |
326 *decision_logic_.get(), stats); | 326 *decision_logic_.get(), stats); |
327 return 0; | 327 return 0; |
328 } | 328 } |
329 | 329 |
330 void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) { | 330 void NetEqImpl::WaitingTimes(std::vector<int>* waiting_times) { |
331 CriticalSectionScoped lock(crit_sect_.get()); | 331 CriticalSectionScoped lock(crit_sect_.get()); |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 assert(decoder); // Should always get a valid object, since we have | 613 assert(decoder); // Should always get a valid object, since we have |
614 // already checked that the payload types are known. | 614 // already checked that the payload types are known. |
615 decoder->IncomingPacket(packet_list.front()->payload, | 615 decoder->IncomingPacket(packet_list.front()->payload, |
616 packet_list.front()->payload_length, | 616 packet_list.front()->payload_length, |
617 packet_list.front()->header.sequenceNumber, | 617 packet_list.front()->header.sequenceNumber, |
618 packet_list.front()->header.timestamp, | 618 packet_list.front()->header.timestamp, |
619 receive_timestamp); | 619 receive_timestamp); |
620 } | 620 } |
621 | 621 |
622 // Insert packets in buffer. | 622 // Insert packets in buffer. |
623 int temp_bufsize = packet_buffer_->NumPacketsInBuffer(); | 623 size_t temp_bufsize = packet_buffer_->NumPacketsInBuffer(); |
624 ret = packet_buffer_->InsertPacketList( | 624 ret = packet_buffer_->InsertPacketList( |
625 &packet_list, | 625 &packet_list, |
626 *decoder_database_, | 626 *decoder_database_, |
627 ¤t_rtp_payload_type_, | 627 ¤t_rtp_payload_type_, |
628 ¤t_cng_rtp_payload_type_); | 628 ¤t_cng_rtp_payload_type_); |
629 if (ret == PacketBuffer::kFlushed) { | 629 if (ret == PacketBuffer::kFlushed) { |
630 // Reset DSP timestamp etc. if packet buffer flushed. | 630 // Reset DSP timestamp etc. if packet buffer flushed. |
631 new_codec_ = true; | 631 new_codec_ = true; |
632 update_sample_rate_and_channels = true; | 632 update_sample_rate_and_channels = true; |
633 LOG_F(LS_WARNING) << "Packet buffer flushed"; | 633 LOG_F(LS_WARNING) << "Packet buffer flushed"; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
677 assert(dec_info); // Already checked that the payload type is known. | 677 assert(dec_info); // Already checked that the payload type is known. |
678 delay_manager_->LastDecoderType(dec_info->codec_type); | 678 delay_manager_->LastDecoderType(dec_info->codec_type); |
679 if (delay_manager_->last_pack_cng_or_dtmf() == 0) { | 679 if (delay_manager_->last_pack_cng_or_dtmf() == 0) { |
680 // Calculate the total speech length carried in each packet. | 680 // Calculate the total speech length carried in each packet. |
681 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize; | 681 temp_bufsize = packet_buffer_->NumPacketsInBuffer() - temp_bufsize; |
682 temp_bufsize *= decoder_frame_length_; | 682 temp_bufsize *= decoder_frame_length_; |
683 | 683 |
684 if ((temp_bufsize > 0) && | 684 if ((temp_bufsize > 0) && |
685 (temp_bufsize != decision_logic_->packet_length_samples())) { | 685 (temp_bufsize != decision_logic_->packet_length_samples())) { |
686 decision_logic_->set_packet_length_samples(temp_bufsize); | 686 decision_logic_->set_packet_length_samples(temp_bufsize); |
687 delay_manager_->SetPacketAudioLength((1000 * temp_bufsize) / fs_hz_); | 687 delay_manager_->SetPacketAudioLength( |
688 static_cast<int>((1000 * temp_bufsize) / fs_hz_)); | |
688 } | 689 } |
689 | 690 |
690 // Update statistics. | 691 // Update statistics. |
691 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 && | 692 if ((int32_t) (main_header.timestamp - timestamp_) >= 0 && |
692 !new_codec_) { | 693 !new_codec_) { |
693 // Only update statistics if incoming packet is not older than last played | 694 // Only update statistics if incoming packet is not older than last played |
694 // out packet, and if new codec flag is not set. | 695 // out packet, and if new codec flag is not set. |
695 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp, | 696 delay_manager_->Update(main_header.sequenceNumber, main_header.timestamp, |
696 fs_hz_); | 697 fs_hz_); |
697 } | 698 } |
698 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) { | 699 } else if (delay_manager_->last_pack_cng_or_dtmf() == -1) { |
699 // This is first "normal" packet after CNG or DTMF. | 700 // This is first "normal" packet after CNG or DTMF. |
700 // Reset packet time counter and measure time until next packet, | 701 // Reset packet time counter and measure time until next packet, |
701 // but don't update statistics. | 702 // but don't update statistics. |
702 delay_manager_->set_last_pack_cng_or_dtmf(0); | 703 delay_manager_->set_last_pack_cng_or_dtmf(0); |
703 delay_manager_->ResetPacketIatCount(); | 704 delay_manager_->ResetPacketIatCount(); |
704 } | 705 } |
705 return 0; | 706 return 0; |
706 } | 707 } |
707 | 708 |
708 int NetEqImpl::GetAudioInternal(size_t max_length, | 709 int NetEqImpl::GetAudioInternal(size_t max_length, |
709 int16_t* output, | 710 int16_t* output, |
710 int* samples_per_channel, | 711 size_t* samples_per_channel, |
711 int* num_channels) { | 712 int* num_channels) { |
712 PacketList packet_list; | 713 PacketList packet_list; |
713 DtmfEvent dtmf_event; | 714 DtmfEvent dtmf_event; |
714 Operations operation; | 715 Operations operation; |
715 bool play_dtmf; | 716 bool play_dtmf; |
716 int return_value = GetDecision(&operation, &packet_list, &dtmf_event, | 717 int return_value = GetDecision(&operation, &packet_list, &dtmf_event, |
717 &play_dtmf); | 718 &play_dtmf); |
718 if (return_value != 0) { | 719 if (return_value != 0) { |
719 LOG_FERR1(LS_WARNING, GetDecision, return_value); | 720 LOG_FERR1(LS_WARNING, GetDecision, return_value); |
720 assert(false); | 721 assert(false); |
721 last_mode_ = kModeError; | 722 last_mode_ = kModeError; |
722 return return_value; | 723 return return_value; |
723 } | 724 } |
724 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation << | 725 LOG(LS_VERBOSE) << "GetDecision returned operation=" << operation << |
725 " and " << packet_list.size() << " packet(s)"; | 726 " and " << packet_list.size() << " packet(s)"; |
726 | 727 |
727 AudioDecoder::SpeechType speech_type; | 728 AudioDecoder::SpeechType speech_type; |
728 int length = 0; | 729 int length = 0; |
729 int decode_return_value = Decode(&packet_list, &operation, | 730 int decode_return_value = Decode(&packet_list, &operation, |
730 &length, &speech_type); | 731 &length, &speech_type); |
731 | 732 |
732 assert(vad_.get()); | 733 assert(vad_.get()); |
733 bool sid_frame_available = | 734 bool sid_frame_available = |
734 (operation == kRfc3389Cng && !packet_list.empty()); | 735 (operation == kRfc3389Cng && !packet_list.empty()); |
735 vad_->Update(decoded_buffer_.get(), length, speech_type, | 736 vad_->Update(decoded_buffer_.get(), static_cast<size_t>(length), speech_type, |
736 sid_frame_available, fs_hz_); | 737 sid_frame_available, fs_hz_); |
737 | 738 |
738 algorithm_buffer_->Clear(); | 739 algorithm_buffer_->Clear(); |
739 switch (operation) { | 740 switch (operation) { |
740 case kNormal: { | 741 case kNormal: { |
741 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf); | 742 DoNormal(decoded_buffer_.get(), length, speech_type, play_dtmf); |
742 break; | 743 break; |
743 } | 744 } |
744 case kMerge: { | 745 case kMerge: { |
745 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf); | 746 DoMerge(decoded_buffer_.get(), length, speech_type, play_dtmf); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
824 // Copy from |algorithm_buffer| to |sync_buffer_|. | 825 // Copy from |algorithm_buffer| to |sync_buffer_|. |
825 sync_buffer_->PushBack(*algorithm_buffer_); | 826 sync_buffer_->PushBack(*algorithm_buffer_); |
826 | 827 |
827 // Extract data from |sync_buffer_| to |output|. | 828 // Extract data from |sync_buffer_| to |output|. |
828 size_t num_output_samples_per_channel = output_size_samples_; | 829 size_t num_output_samples_per_channel = output_size_samples_; |
829 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels(); | 830 size_t num_output_samples = output_size_samples_ * sync_buffer_->Channels(); |
830 if (num_output_samples > max_length) { | 831 if (num_output_samples > max_length) { |
831 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " << | 832 LOG(LS_WARNING) << "Output array is too short. " << max_length << " < " << |
832 output_size_samples_ << " * " << sync_buffer_->Channels(); | 833 output_size_samples_ << " * " << sync_buffer_->Channels(); |
833 num_output_samples = max_length; | 834 num_output_samples = max_length; |
834 num_output_samples_per_channel = static_cast<int>( | 835 num_output_samples_per_channel = max_length / sync_buffer_->Channels(); |
835 max_length / sync_buffer_->Channels()); | |
836 } | 836 } |
837 int samples_from_sync = static_cast<int>( | 837 size_t samples_from_sync = |
838 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel, | 838 sync_buffer_->GetNextAudioInterleaved(num_output_samples_per_channel, |
839 output)); | 839 output); |
840 *num_channels = static_cast<int>(sync_buffer_->Channels()); | 840 *num_channels = static_cast<int>(sync_buffer_->Channels()); |
841 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" << | 841 LOG(LS_VERBOSE) << "Sync buffer (" << *num_channels << " channel(s)):" << |
842 " insert " << algorithm_buffer_->Size() << " samples, extract " << | 842 " insert " << algorithm_buffer_->Size() << " samples, extract " << |
843 samples_from_sync << " samples"; | 843 samples_from_sync << " samples"; |
844 if (samples_from_sync != output_size_samples_) { | 844 if (samples_from_sync != output_size_samples_) { |
845 LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_"; | 845 LOG_F(LS_ERROR) << "samples_from_sync != output_size_samples_"; |
846 // TODO(minyue): treatment of under-run, filling zeros | 846 // TODO(minyue): treatment of under-run, filling zeros |
847 memset(output, 0, num_output_samples * sizeof(int16_t)); | 847 memset(output, 0, num_output_samples * sizeof(int16_t)); |
848 *samples_per_channel = output_size_samples_; | 848 *samples_per_channel = output_size_samples_; |
849 return kSampleUnderrun; | 849 return kSampleUnderrun; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
933 } | 933 } |
934 | 934 |
935 assert(expand_.get()); | 935 assert(expand_.get()); |
936 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - | 936 const int samples_left = static_cast<int>(sync_buffer_->FutureLength() - |
937 expand_->overlap_length()); | 937 expand_->overlap_length()); |
938 if (last_mode_ == kModeAccelerateSuccess || | 938 if (last_mode_ == kModeAccelerateSuccess || |
939 last_mode_ == kModeAccelerateLowEnergy || | 939 last_mode_ == kModeAccelerateLowEnergy || |
940 last_mode_ == kModePreemptiveExpandSuccess || | 940 last_mode_ == kModePreemptiveExpandSuccess || |
941 last_mode_ == kModePreemptiveExpandLowEnergy) { | 941 last_mode_ == kModePreemptiveExpandLowEnergy) { |
942 // Subtract (samples_left + output_size_samples_) from sampleMemory. | 942 // Subtract (samples_left + output_size_samples_) from sampleMemory. |
943 decision_logic_->AddSampleMemory(-(samples_left + output_size_samples_)); | 943 decision_logic_->AddSampleMemory( |
944 -(samples_left + static_cast<int>(output_size_samples_))); | |
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:47
Done.
| |
944 } | 945 } |
945 | 946 |
946 // Check if it is time to play a DTMF event. | 947 // Check if it is time to play a DTMF event. |
947 if (dtmf_buffer_->GetEvent( | 948 if (dtmf_buffer_->GetEvent( |
948 static_cast<uint32_t>( | 949 static_cast<uint32_t>( |
949 end_timestamp + decision_logic_->generated_noise_samples()), | 950 end_timestamp + decision_logic_->generated_noise_samples()), |
950 dtmf_event)) { | 951 dtmf_event)) { |
951 *play_dtmf = true; | 952 *play_dtmf = true; |
952 } | 953 } |
953 | 954 |
954 // Get instruction. | 955 // Get instruction. |
955 assert(sync_buffer_.get()); | 956 assert(sync_buffer_.get()); |
956 assert(expand_.get()); | 957 assert(expand_.get()); |
957 *operation = decision_logic_->GetDecision(*sync_buffer_, | 958 *operation = decision_logic_->GetDecision(*sync_buffer_, |
958 *expand_, | 959 *expand_, |
959 decoder_frame_length_, | 960 decoder_frame_length_, |
960 header, | 961 header, |
961 last_mode_, | 962 last_mode_, |
962 *play_dtmf, | 963 *play_dtmf, |
963 &reset_decoder_); | 964 &reset_decoder_); |
964 | 965 |
965 // Check if we already have enough samples in the |sync_buffer_|. If so, | 966 // Check if we already have enough samples in the |sync_buffer_|. If so, |
966 // change decision to normal, unless the decision was merge, accelerate, or | 967 // change decision to normal, unless the decision was merge, accelerate, or |
967 // preemptive expand. | 968 // preemptive expand. |
968 if (samples_left >= output_size_samples_ && *operation != kMerge && | 969 if (samples_left >= static_cast<int>(output_size_samples_) && |
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
Done.
| |
969 *operation != kAccelerate && *operation != kFastAccelerate && | 970 *operation != kMerge && |
971 *operation != kAccelerate && | |
972 *operation != kFastAccelerate && | |
970 *operation != kPreemptiveExpand) { | 973 *operation != kPreemptiveExpand) { |
971 *operation = kNormal; | 974 *operation = kNormal; |
972 return 0; | 975 return 0; |
973 } | 976 } |
974 | 977 |
975 decision_logic_->ExpandDecision(*operation); | 978 decision_logic_->ExpandDecision(*operation); |
976 | 979 |
977 // Check conditions for reset. | 980 // Check conditions for reset. |
978 if (new_codec_ || *operation == kUndefined) { | 981 if (new_codec_ || *operation == kUndefined) { |
979 // The only valid reason to get kUndefined is that new_codec_ is set. | 982 // The only valid reason to get kUndefined is that new_codec_ is set. |
(...skipping 27 matching lines...) Expand all Loading... | |
1007 // new value. | 1010 // new value. |
1008 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp); | 1011 sync_buffer_->IncreaseEndTimestamp(timestamp_ - end_timestamp); |
1009 end_timestamp = timestamp_; | 1012 end_timestamp = timestamp_; |
1010 new_codec_ = false; | 1013 new_codec_ = false; |
1011 decision_logic_->SoftReset(); | 1014 decision_logic_->SoftReset(); |
1012 buffer_level_filter_->Reset(); | 1015 buffer_level_filter_->Reset(); |
1013 delay_manager_->Reset(); | 1016 delay_manager_->Reset(); |
1014 stats_.ResetMcu(); | 1017 stats_.ResetMcu(); |
1015 } | 1018 } |
1016 | 1019 |
1017 int required_samples = output_size_samples_; | 1020 size_t required_samples = output_size_samples_; |
1018 const int samples_10_ms = 80 * fs_mult_; | 1021 const size_t samples_10_ms = 80 * fs_mult_; |
1019 const int samples_20_ms = 2 * samples_10_ms; | 1022 const size_t samples_20_ms = 2 * samples_10_ms; |
1020 const int samples_30_ms = 3 * samples_10_ms; | 1023 const size_t samples_30_ms = 3 * samples_10_ms; |
1021 | 1024 |
1022 switch (*operation) { | 1025 switch (*operation) { |
1023 case kExpand: { | 1026 case kExpand: { |
1024 timestamp_ = end_timestamp; | 1027 timestamp_ = end_timestamp; |
1025 return 0; | 1028 return 0; |
1026 } | 1029 } |
1027 case kRfc3389CngNoPacket: | 1030 case kRfc3389CngNoPacket: |
1028 case kCodecInternalCng: { | 1031 case kCodecInternalCng: { |
1029 return 0; | 1032 return 0; |
1030 } | 1033 } |
1031 case kDtmf: { | 1034 case kDtmf: { |
1032 // TODO(hlundin): Write test for this. | 1035 // TODO(hlundin): Write test for this. |
1033 // Update timestamp. | 1036 // Update timestamp. |
1034 timestamp_ = end_timestamp; | 1037 timestamp_ = end_timestamp; |
1035 if (decision_logic_->generated_noise_samples() > 0 && | 1038 if (decision_logic_->generated_noise_samples() > 0 && |
1036 last_mode_ != kModeDtmf) { | 1039 last_mode_ != kModeDtmf) { |
1037 // Make a jump in timestamp due to the recently played comfort noise. | 1040 // Make a jump in timestamp due to the recently played comfort noise. |
1038 uint32_t timestamp_jump = | 1041 uint32_t timestamp_jump = |
1039 static_cast<uint32_t>(decision_logic_->generated_noise_samples()); | 1042 static_cast<uint32_t>(decision_logic_->generated_noise_samples()); |
1040 sync_buffer_->IncreaseEndTimestamp(timestamp_jump); | 1043 sync_buffer_->IncreaseEndTimestamp(timestamp_jump); |
1041 timestamp_ += timestamp_jump; | 1044 timestamp_ += timestamp_jump; |
1042 } | 1045 } |
1043 decision_logic_->set_generated_noise_samples(0); | 1046 decision_logic_->set_generated_noise_samples(0); |
1044 return 0; | 1047 return 0; |
1045 } | 1048 } |
1046 case kAccelerate: | 1049 case kAccelerate: |
1047 case kFastAccelerate: { | 1050 case kFastAccelerate: { |
1048 // In order to do an accelerate we need at least 30 ms of audio data. | 1051 // In order to do an accelerate we need at least 30 ms of audio data. |
1049 if (samples_left >= samples_30_ms) { | 1052 if (samples_left >= static_cast<int>(samples_30_ms)) { |
1050 // Already have enough data, so we do not need to extract any more. | 1053 // Already have enough data, so we do not need to extract any more. |
1051 decision_logic_->set_sample_memory(samples_left); | 1054 decision_logic_->set_sample_memory(samples_left); |
1052 decision_logic_->set_prev_time_scale(true); | 1055 decision_logic_->set_prev_time_scale(true); |
1053 return 0; | 1056 return 0; |
1054 } else if (samples_left >= samples_10_ms && | 1057 } else if (samples_left >= static_cast<int>(samples_10_ms) && |
1055 decoder_frame_length_ >= samples_30_ms) { | 1058 decoder_frame_length_ >= samples_30_ms) { |
1056 // Avoid decoding more data as it might overflow the playout buffer. | 1059 // Avoid decoding more data as it might overflow the playout buffer. |
1057 *operation = kNormal; | 1060 *operation = kNormal; |
1058 return 0; | 1061 return 0; |
1059 } else if (samples_left < samples_20_ms && | 1062 } else if (samples_left < static_cast<int>(samples_20_ms) && |
1060 decoder_frame_length_ < samples_30_ms) { | 1063 decoder_frame_length_ < samples_30_ms) { |
1061 // Build up decoded data by decoding at least 20 ms of audio data. Do | 1064 // Build up decoded data by decoding at least 20 ms of audio data. Do |
1062 // not perform accelerate yet, but wait until we only need to do one | 1065 // not perform accelerate yet, but wait until we only need to do one |
1063 // decoding. | 1066 // decoding. |
1064 required_samples = 2 * output_size_samples_; | 1067 required_samples = 2 * output_size_samples_; |
1065 *operation = kNormal; | 1068 *operation = kNormal; |
1066 } | 1069 } |
1067 // If none of the above is true, we have one of two possible situations: | 1070 // If none of the above is true, we have one of two possible situations: |
1068 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or | 1071 // (1) 20 ms <= samples_left < 30 ms and decoder_frame_length_ < 30 ms; or |
1069 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms. | 1072 // (2) samples_left < 10 ms and decoder_frame_length_ >= 30 ms. |
1070 // In either case, we move on with the accelerate decision, and decode one | 1073 // In either case, we move on with the accelerate decision, and decode one |
1071 // frame now. | 1074 // frame now. |
1072 break; | 1075 break; |
1073 } | 1076 } |
1074 case kPreemptiveExpand: { | 1077 case kPreemptiveExpand: { |
1075 // In order to do a preemptive expand we need at least 30 ms of decoded | 1078 // In order to do a preemptive expand we need at least 30 ms of decoded |
1076 // audio data. | 1079 // audio data. |
1077 if ((samples_left >= samples_30_ms) || | 1080 if ((samples_left >= static_cast<int>(samples_30_ms)) || |
1078 (samples_left >= samples_10_ms && | 1081 (samples_left >= static_cast<int>(samples_10_ms) && |
1079 decoder_frame_length_ >= samples_30_ms)) { | 1082 decoder_frame_length_ >= samples_30_ms)) { |
1080 // Already have enough data, so we do not need to extract any more. | 1083 // Already have enough data, so we do not need to extract any more. |
1081 // Or, avoid decoding more data as it might overflow the playout buffer. | 1084 // Or, avoid decoding more data as it might overflow the playout buffer. |
1082 // Still try preemptive expand, though. | 1085 // Still try preemptive expand, though. |
1083 decision_logic_->set_sample_memory(samples_left); | 1086 decision_logic_->set_sample_memory(samples_left); |
1084 decision_logic_->set_prev_time_scale(true); | 1087 decision_logic_->set_prev_time_scale(true); |
1085 return 0; | 1088 return 0; |
1086 } | 1089 } |
1087 if (samples_left < samples_20_ms && | 1090 if (samples_left < static_cast<int>(samples_20_ms) && |
1088 decoder_frame_length_ < samples_30_ms) { | 1091 decoder_frame_length_ < samples_30_ms) { |
1089 // Build up decoded data by decoding at least 20 ms of audio data. | 1092 // Build up decoded data by decoding at least 20 ms of audio data. |
1090 // Still try to perform preemptive expand. | 1093 // Still try to perform preemptive expand. |
1091 required_samples = 2 * output_size_samples_; | 1094 required_samples = 2 * output_size_samples_; |
1092 } | 1095 } |
1093 // Move on with the preemptive expand decision. | 1096 // Move on with the preemptive expand decision. |
1094 break; | 1097 break; |
1095 } | 1098 } |
1096 case kMerge: { | 1099 case kMerge: { |
1097 required_samples = | 1100 required_samples = |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1135 } | 1138 } |
1136 | 1139 |
1137 if (*operation == kAccelerate || *operation == kFastAccelerate || | 1140 if (*operation == kAccelerate || *operation == kFastAccelerate || |
1138 *operation == kPreemptiveExpand) { | 1141 *operation == kPreemptiveExpand) { |
1139 decision_logic_->set_sample_memory(samples_left + extracted_samples); | 1142 decision_logic_->set_sample_memory(samples_left + extracted_samples); |
1140 decision_logic_->set_prev_time_scale(true); | 1143 decision_logic_->set_prev_time_scale(true); |
1141 } | 1144 } |
1142 | 1145 |
1143 if (*operation == kAccelerate || *operation == kFastAccelerate) { | 1146 if (*operation == kAccelerate || *operation == kFastAccelerate) { |
1144 // Check that we have enough data (30ms) to do accelerate. | 1147 // Check that we have enough data (30ms) to do accelerate. |
1145 if (extracted_samples + samples_left < samples_30_ms) { | 1148 if (extracted_samples + samples_left < static_cast<int>(samples_30_ms)) { |
1146 // TODO(hlundin): Write test for this. | 1149 // TODO(hlundin): Write test for this. |
1147 // Not enough, do normal operation instead. | 1150 // Not enough, do normal operation instead. |
1148 *operation = kNormal; | 1151 *operation = kNormal; |
1149 } | 1152 } |
1150 } | 1153 } |
1151 | 1154 |
1152 timestamp_ = end_timestamp; | 1155 timestamp_ = end_timestamp; |
1153 return 0; | 1156 return 0; |
1154 } | 1157 } |
1155 | 1158 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1283 // Decode to silence with the same frame size as the last decode. | 1286 // Decode to silence with the same frame size as the last decode. |
1284 LOG(LS_VERBOSE) << "Decoding sync-packet: " << | 1287 LOG(LS_VERBOSE) << "Decoding sync-packet: " << |
1285 " ts=" << packet->header.timestamp << | 1288 " ts=" << packet->header.timestamp << |
1286 ", sn=" << packet->header.sequenceNumber << | 1289 ", sn=" << packet->header.sequenceNumber << |
1287 ", pt=" << static_cast<int>(packet->header.payloadType) << | 1290 ", pt=" << static_cast<int>(packet->header.payloadType) << |
1288 ", ssrc=" << packet->header.ssrc << | 1291 ", ssrc=" << packet->header.ssrc << |
1289 ", len=" << packet->payload_length; | 1292 ", len=" << packet->payload_length; |
1290 memset(&decoded_buffer_[*decoded_length], 0, | 1293 memset(&decoded_buffer_[*decoded_length], 0, |
1291 decoder_frame_length_ * decoder->Channels() * | 1294 decoder_frame_length_ * decoder->Channels() * |
1292 sizeof(decoded_buffer_[0])); | 1295 sizeof(decoded_buffer_[0])); |
1293 decode_length = decoder_frame_length_; | 1296 decode_length = static_cast<int>(decoder_frame_length_); |
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
Done.
| |
1294 } else if (!packet->primary) { | 1297 } else if (!packet->primary) { |
1295 // This is a redundant payload; call the special decoder method. | 1298 // This is a redundant payload; call the special decoder method. |
1296 LOG(LS_VERBOSE) << "Decoding packet (redundant):" << | 1299 LOG(LS_VERBOSE) << "Decoding packet (redundant):" << |
1297 " ts=" << packet->header.timestamp << | 1300 " ts=" << packet->header.timestamp << |
1298 ", sn=" << packet->header.sequenceNumber << | 1301 ", sn=" << packet->header.sequenceNumber << |
1299 ", pt=" << static_cast<int>(packet->header.payloadType) << | 1302 ", pt=" << static_cast<int>(packet->header.payloadType) << |
1300 ", ssrc=" << packet->header.ssrc << | 1303 ", ssrc=" << packet->header.ssrc << |
1301 ", len=" << packet->payload_length; | 1304 ", len=" << packet->payload_length; |
1302 decode_length = decoder->DecodeRedundant( | 1305 decode_length = decoder->DecodeRedundant( |
1303 packet->payload, packet->payload_length, fs_hz_, | 1306 packet->payload, packet->payload_length, fs_hz_, |
(...skipping 12 matching lines...) Expand all Loading... | |
1316 &decoded_buffer_[*decoded_length], speech_type); | 1319 &decoded_buffer_[*decoded_length], speech_type); |
1317 } | 1320 } |
1318 | 1321 |
1319 delete[] packet->payload; | 1322 delete[] packet->payload; |
1320 delete packet; | 1323 delete packet; |
1321 packet = NULL; | 1324 packet = NULL; |
1322 if (decode_length > 0) { | 1325 if (decode_length > 0) { |
1323 *decoded_length += decode_length; | 1326 *decoded_length += decode_length; |
1324 // Update |decoder_frame_length_| with number of samples per channel. | 1327 // Update |decoder_frame_length_| with number of samples per channel. |
1325 decoder_frame_length_ = | 1328 decoder_frame_length_ = |
1326 decode_length / static_cast<int>(decoder->Channels()); | 1329 static_cast<size_t>(decode_length) / decoder->Channels(); |
1327 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples (" | 1330 LOG(LS_VERBOSE) << "Decoded " << decode_length << " samples (" |
1328 << decoder->Channels() << " channel(s) -> " | 1331 << decoder->Channels() << " channel(s) -> " |
1329 << decoder_frame_length_ << " samples per channel)"; | 1332 << decoder_frame_length_ << " samples per channel)"; |
1330 } else if (decode_length < 0) { | 1333 } else if (decode_length < 0) { |
1331 // Error. | 1334 // Error. |
1332 LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length); | 1335 LOG_FERR2(LS_WARNING, Decode, decode_length, payload_length); |
1333 *decoded_length = -1; | 1336 *decoded_length = -1; |
1334 PacketBuffer::DeleteAllPackets(packet_list); | 1337 PacketBuffer::DeleteAllPackets(packet_list); |
1335 break; | 1338 break; |
1336 } | 1339 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1375 | 1378 |
1376 if (!play_dtmf) { | 1379 if (!play_dtmf) { |
1377 dtmf_tone_generator_->Reset(); | 1380 dtmf_tone_generator_->Reset(); |
1378 } | 1381 } |
1379 } | 1382 } |
1380 | 1383 |
1381 void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length, | 1384 void NetEqImpl::DoMerge(int16_t* decoded_buffer, size_t decoded_length, |
1382 AudioDecoder::SpeechType speech_type, bool play_dtmf) { | 1385 AudioDecoder::SpeechType speech_type, bool play_dtmf) { |
1383 assert(mute_factor_array_.get()); | 1386 assert(mute_factor_array_.get()); |
1384 assert(merge_.get()); | 1387 assert(merge_.get()); |
1385 int new_length = merge_->Process(decoded_buffer, decoded_length, | 1388 size_t new_length = merge_->Process(decoded_buffer, decoded_length, |
1386 mute_factor_array_.get(), | 1389 mute_factor_array_.get(), |
1387 algorithm_buffer_.get()); | 1390 algorithm_buffer_.get()); |
1388 int expand_length_correction = new_length - | 1391 size_t expand_length_correction = new_length - |
1389 static_cast<int>(decoded_length / algorithm_buffer_->Channels()); | 1392 decoded_length / algorithm_buffer_->Channels(); |
1390 | 1393 |
1391 // Update in-call and post-call statistics. | 1394 // Update in-call and post-call statistics. |
1392 if (expand_->MuteFactor(0) == 0) { | 1395 if (expand_->MuteFactor(0) == 0) { |
1393 // Expand generates only noise. | 1396 // Expand generates only noise. |
1394 stats_.ExpandedNoiseSamples(expand_length_correction); | 1397 stats_.ExpandedNoiseSamples(expand_length_correction); |
1395 } else { | 1398 } else { |
1396 // Expansion generates more than only noise. | 1399 // Expansion generates more than only noise. |
1397 stats_.ExpandedVoiceSamples(expand_length_correction); | 1400 stats_.ExpandedVoiceSamples(expand_length_correction); |
1398 } | 1401 } |
1399 | 1402 |
1400 last_mode_ = kModeMerge; | 1403 last_mode_ = kModeMerge; |
1401 // If last packet was decoded as an inband CNG, set mode to CNG instead. | 1404 // If last packet was decoded as an inband CNG, set mode to CNG instead. |
1402 if (speech_type == AudioDecoder::kComfortNoise) { | 1405 if (speech_type == AudioDecoder::kComfortNoise) { |
1403 last_mode_ = kModeCodecInternalCng; | 1406 last_mode_ = kModeCodecInternalCng; |
1404 } | 1407 } |
1405 expand_->Reset(); | 1408 expand_->Reset(); |
1406 if (!play_dtmf) { | 1409 if (!play_dtmf) { |
1407 dtmf_tone_generator_->Reset(); | 1410 dtmf_tone_generator_->Reset(); |
1408 } | 1411 } |
1409 } | 1412 } |
1410 | 1413 |
1411 int NetEqImpl::DoExpand(bool play_dtmf) { | 1414 int NetEqImpl::DoExpand(bool play_dtmf) { |
1412 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) < | 1415 while ((sync_buffer_->FutureLength() - expand_->overlap_length()) < |
1413 static_cast<size_t>(output_size_samples_)) { | 1416 output_size_samples_) { |
1414 algorithm_buffer_->Clear(); | 1417 algorithm_buffer_->Clear(); |
1415 int return_value = expand_->Process(algorithm_buffer_.get()); | 1418 int return_value = expand_->Process(algorithm_buffer_.get()); |
1416 int length = static_cast<int>(algorithm_buffer_->Size()); | 1419 size_t length = algorithm_buffer_->Size(); |
1417 | 1420 |
1418 // Update in-call and post-call statistics. | 1421 // Update in-call and post-call statistics. |
1419 if (expand_->MuteFactor(0) == 0) { | 1422 if (expand_->MuteFactor(0) == 0) { |
1420 // Expand operation generates only noise. | 1423 // Expand operation generates only noise. |
1421 stats_.ExpandedNoiseSamples(length); | 1424 stats_.ExpandedNoiseSamples(length); |
1422 } else { | 1425 } else { |
1423 // Expand operation generates more than only noise. | 1426 // Expand operation generates more than only noise. |
1424 stats_.ExpandedVoiceSamples(length); | 1427 stats_.ExpandedVoiceSamples(length); |
1425 } | 1428 } |
1426 | 1429 |
(...skipping 26 matching lines...) Expand all Loading... | |
1453 borrowed_samples_per_channel = static_cast<int>(required_samples - | 1456 borrowed_samples_per_channel = static_cast<int>(required_samples - |
1454 decoded_length_per_channel); | 1457 decoded_length_per_channel); |
1455 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels], | 1458 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels], |
1456 decoded_buffer, | 1459 decoded_buffer, |
1457 sizeof(int16_t) * decoded_length); | 1460 sizeof(int16_t) * decoded_length); |
1458 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel, | 1461 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel, |
1459 decoded_buffer); | 1462 decoded_buffer); |
1460 decoded_length = required_samples * num_channels; | 1463 decoded_length = required_samples * num_channels; |
1461 } | 1464 } |
1462 | 1465 |
1463 int16_t samples_removed; | 1466 size_t samples_removed; |
1464 Accelerate::ReturnCodes return_code = | 1467 Accelerate::ReturnCodes return_code = |
1465 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate, | 1468 accelerate_->Process(decoded_buffer, decoded_length, fast_accelerate, |
1466 algorithm_buffer_.get(), &samples_removed); | 1469 algorithm_buffer_.get(), &samples_removed); |
1467 stats_.AcceleratedSamples(samples_removed); | 1470 stats_.AcceleratedSamples(samples_removed); |
1468 switch (return_code) { | 1471 switch (return_code) { |
1469 case Accelerate::kSuccess: | 1472 case Accelerate::kSuccess: |
1470 last_mode_ = kModeAccelerateSuccess; | 1473 last_mode_ = kModeAccelerateSuccess; |
1471 break; | 1474 break; |
1472 case Accelerate::kSuccessLowEnergy: | 1475 case Accelerate::kSuccessLowEnergy: |
1473 last_mode_ = kModeAccelerateLowEnergy; | 1476 last_mode_ = kModeAccelerateLowEnergy; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1512 expand_->Reset(); | 1515 expand_->Reset(); |
1513 return 0; | 1516 return 0; |
1514 } | 1517 } |
1515 | 1518 |
1516 int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer, | 1519 int NetEqImpl::DoPreemptiveExpand(int16_t* decoded_buffer, |
1517 size_t decoded_length, | 1520 size_t decoded_length, |
1518 AudioDecoder::SpeechType speech_type, | 1521 AudioDecoder::SpeechType speech_type, |
1519 bool play_dtmf) { | 1522 bool play_dtmf) { |
1520 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms. | 1523 const size_t required_samples = 240 * fs_mult_; // Must have 30 ms. |
1521 size_t num_channels = algorithm_buffer_->Channels(); | 1524 size_t num_channels = algorithm_buffer_->Channels(); |
1522 int borrowed_samples_per_channel = 0; | 1525 size_t borrowed_samples_per_channel = 0; |
1523 int old_borrowed_samples_per_channel = 0; | 1526 size_t old_borrowed_samples_per_channel = 0; |
1524 size_t decoded_length_per_channel = decoded_length / num_channels; | 1527 size_t decoded_length_per_channel = decoded_length / num_channels; |
1525 if (decoded_length_per_channel < required_samples) { | 1528 if (decoded_length_per_channel < required_samples) { |
1526 // Must move data from the |sync_buffer_| in order to get 30 ms. | 1529 // Must move data from the |sync_buffer_| in order to get 30 ms. |
1527 borrowed_samples_per_channel = static_cast<int>(required_samples - | 1530 borrowed_samples_per_channel = |
1528 decoded_length_per_channel); | 1531 required_samples - decoded_length_per_channel; |
1529 // Calculate how many of these were already played out. | 1532 // Calculate how many of these were already played out. |
1530 const int future_length = static_cast<int>(sync_buffer_->FutureLength()); | |
1531 old_borrowed_samples_per_channel = | 1533 old_borrowed_samples_per_channel = |
1532 (borrowed_samples_per_channel > future_length) ? | 1534 (borrowed_samples_per_channel > sync_buffer_->FutureLength()) ? |
1533 (borrowed_samples_per_channel - future_length) : 0; | 1535 (borrowed_samples_per_channel - sync_buffer_->FutureLength()) : 0; |
1534 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels], | 1536 memmove(&decoded_buffer[borrowed_samples_per_channel * num_channels], |
1535 decoded_buffer, | 1537 decoded_buffer, |
1536 sizeof(int16_t) * decoded_length); | 1538 sizeof(int16_t) * decoded_length); |
1537 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel, | 1539 sync_buffer_->ReadInterleavedFromEnd(borrowed_samples_per_channel, |
1538 decoded_buffer); | 1540 decoded_buffer); |
1539 decoded_length = required_samples * num_channels; | 1541 decoded_length = required_samples * num_channels; |
1540 } | 1542 } |
1541 | 1543 |
1542 int16_t samples_added; | 1544 size_t samples_added; |
1543 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process( | 1545 PreemptiveExpand::ReturnCodes return_code = preemptive_expand_->Process( |
1544 decoded_buffer, static_cast<int>(decoded_length), | 1546 decoded_buffer, decoded_length, |
1545 old_borrowed_samples_per_channel, | 1547 old_borrowed_samples_per_channel, |
1546 algorithm_buffer_.get(), &samples_added); | 1548 algorithm_buffer_.get(), &samples_added); |
1547 stats_.PreemptiveExpandedSamples(samples_added); | 1549 stats_.PreemptiveExpandedSamples(samples_added); |
1548 switch (return_code) { | 1550 switch (return_code) { |
1549 case PreemptiveExpand::kSuccess: | 1551 case PreemptiveExpand::kSuccess: |
1550 last_mode_ = kModePreemptiveExpandSuccess; | 1552 last_mode_ = kModePreemptiveExpandSuccess; |
1551 break; | 1553 break; |
1552 case PreemptiveExpand::kSuccessLowEnergy: | 1554 case PreemptiveExpand::kSuccessLowEnergy: |
1553 last_mode_ = kModePreemptiveExpandLowEnergy; | 1555 last_mode_ = kModePreemptiveExpandLowEnergy; |
1554 break; | 1556 break; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1731 expand_->Reset(); | 1733 expand_->Reset(); |
1732 last_mode_ = kModeDtmf; | 1734 last_mode_ = kModeDtmf; |
1733 | 1735 |
1734 // Set to false because the DTMF is already in the algorithm buffer. | 1736 // Set to false because the DTMF is already in the algorithm buffer. |
1735 *play_dtmf = false; | 1737 *play_dtmf = false; |
1736 return 0; | 1738 return 0; |
1737 } | 1739 } |
1738 | 1740 |
1739 void NetEqImpl::DoAlternativePlc(bool increase_timestamp) { | 1741 void NetEqImpl::DoAlternativePlc(bool increase_timestamp) { |
1740 AudioDecoder* decoder = decoder_database_->GetActiveDecoder(); | 1742 AudioDecoder* decoder = decoder_database_->GetActiveDecoder(); |
1741 int length; | 1743 size_t length; |
1742 if (decoder && decoder->HasDecodePlc()) { | 1744 if (decoder && decoder->HasDecodePlc()) { |
1743 // Use the decoder's packet-loss concealment. | 1745 // Use the decoder's packet-loss concealment. |
1744 // TODO(hlundin): Will probably need a longer buffer for multi-channel. | 1746 // TODO(hlundin): Will probably need a longer buffer for multi-channel. |
1745 int16_t decoded_buffer[kMaxFrameSize]; | 1747 int16_t decoded_buffer[kMaxFrameSize]; |
1746 length = decoder->DecodePlc(1, decoded_buffer); | 1748 length = decoder->DecodePlc(1, decoded_buffer); |
1747 if (length > 0) { | 1749 if (length > 0) |
1748 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length); | 1750 algorithm_buffer_->PushBackInterleaved(decoded_buffer, length); |
1749 } else { | |
1750 length = 0; | |
1751 } | |
1752 } else { | 1751 } else { |
1753 // Do simple zero-stuffing. | 1752 // Do simple zero-stuffing. |
1754 length = output_size_samples_; | 1753 length = output_size_samples_; |
1755 algorithm_buffer_->Zeros(length); | 1754 algorithm_buffer_->Zeros(length); |
1756 // By not advancing the timestamp, NetEq inserts samples. | 1755 // By not advancing the timestamp, NetEq inserts samples. |
1757 stats_.AddZeros(length); | 1756 stats_.AddZeros(length); |
1758 } | 1757 } |
1759 if (increase_timestamp) { | 1758 if (increase_timestamp) { |
1760 sync_buffer_->IncreaseEndTimestamp(static_cast<uint32_t>(length)); | 1759 sync_buffer_->IncreaseEndTimestamp(static_cast<uint32_t>(length)); |
1761 } | 1760 } |
1762 expand_->Reset(); | 1761 expand_->Reset(); |
1763 } | 1762 } |
1764 | 1763 |
1765 int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels, | 1764 int NetEqImpl::DtmfOverdub(const DtmfEvent& dtmf_event, size_t num_channels, |
1766 int16_t* output) const { | 1765 int16_t* output) const { |
1767 size_t out_index = 0; | 1766 size_t out_index = 0; |
1768 int overdub_length = output_size_samples_; // Default value. | 1767 size_t overdub_length = output_size_samples_; // Default value. |
1769 | 1768 |
1770 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) { | 1769 if (sync_buffer_->dtmf_index() > sync_buffer_->next_index()) { |
1771 // Special operation for transition from "DTMF only" to "DTMF overdub". | 1770 // Special operation for transition from "DTMF only" to "DTMF overdub". |
1772 out_index = std::min( | 1771 out_index = std::min( |
1773 sync_buffer_->dtmf_index() - sync_buffer_->next_index(), | 1772 sync_buffer_->dtmf_index() - sync_buffer_->next_index(), |
1774 static_cast<size_t>(output_size_samples_)); | 1773 output_size_samples_); |
1775 overdub_length = output_size_samples_ - static_cast<int>(out_index); | 1774 overdub_length = output_size_samples_ - out_index; |
1776 } | 1775 } |
1777 | 1776 |
1778 AudioMultiVector dtmf_output(num_channels); | 1777 AudioMultiVector dtmf_output(num_channels); |
1779 int dtmf_return_value = 0; | 1778 int dtmf_return_value = 0; |
1780 if (!dtmf_tone_generator_->initialized()) { | 1779 if (!dtmf_tone_generator_->initialized()) { |
1781 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no, | 1780 dtmf_return_value = dtmf_tone_generator_->Init(fs_hz_, dtmf_event.event_no, |
1782 dtmf_event.volume); | 1781 dtmf_event.volume); |
1783 } | 1782 } |
1784 if (dtmf_return_value == 0) { | 1783 if (dtmf_return_value == 0) { |
1785 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length, | 1784 dtmf_return_value = dtmf_tone_generator_->Generate(overdub_length, |
1786 &dtmf_output); | 1785 &dtmf_output); |
1787 assert((size_t) overdub_length == dtmf_output.Size()); | 1786 assert(overdub_length == dtmf_output.Size()); |
1788 } | 1787 } |
1789 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]); | 1788 dtmf_output.ReadInterleaved(overdub_length, &output[out_index]); |
1790 return dtmf_return_value < 0 ? dtmf_return_value : 0; | 1789 return dtmf_return_value < 0 ? dtmf_return_value : 0; |
1791 } | 1790 } |
1792 | 1791 |
1793 int NetEqImpl::ExtractPackets(int required_samples, PacketList* packet_list) { | 1792 int NetEqImpl::ExtractPackets(size_t required_samples, |
1793 PacketList* packet_list) { | |
1794 bool first_packet = true; | 1794 bool first_packet = true; |
1795 uint8_t prev_payload_type = 0; | 1795 uint8_t prev_payload_type = 0; |
1796 uint32_t prev_timestamp = 0; | 1796 uint32_t prev_timestamp = 0; |
1797 uint16_t prev_sequence_number = 0; | 1797 uint16_t prev_sequence_number = 0; |
1798 bool next_packet_available = false; | 1798 bool next_packet_available = false; |
1799 | 1799 |
1800 const RTPHeader* header = packet_buffer_->NextRtpHeader(); | 1800 const RTPHeader* header = packet_buffer_->NextRtpHeader(); |
1801 assert(header); | 1801 assert(header); |
1802 if (!header) { | 1802 if (!header) { |
1803 return -1; | 1803 return -1; |
1804 } | 1804 } |
1805 uint32_t first_timestamp = header->timestamp; | 1805 uint32_t first_timestamp = header->timestamp; |
1806 int extracted_samples = 0; | 1806 int extracted_samples = 0; |
1807 | 1807 |
1808 // Packet extraction loop. | 1808 // Packet extraction loop. |
1809 do { | 1809 do { |
1810 timestamp_ = header->timestamp; | 1810 timestamp_ = header->timestamp; |
1811 int discard_count = 0; | 1811 size_t discard_count = 0; |
1812 Packet* packet = packet_buffer_->GetNextPacket(&discard_count); | 1812 Packet* packet = packet_buffer_->GetNextPacket(&discard_count); |
1813 // |header| may be invalid after the |packet_buffer_| operation. | 1813 // |header| may be invalid after the |packet_buffer_| operation. |
1814 header = NULL; | 1814 header = NULL; |
1815 if (!packet) { | 1815 if (!packet) { |
1816 LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) << | 1816 LOG_FERR1(LS_ERROR, GetNextPacket, discard_count) << |
1817 "Should always be able to extract a packet here"; | 1817 "Should always be able to extract a packet here"; |
1818 assert(false); // Should always be able to extract a packet here. | 1818 assert(false); // Should always be able to extract a packet here. |
1819 return -1; | 1819 return -1; |
1820 } | 1820 } |
1821 stats_.PacketsDiscarded(discard_count); | 1821 stats_.PacketsDiscarded(discard_count); |
1822 // Store waiting time in ms; packets->waiting_time is in "output blocks". | 1822 // Store waiting time in ms; packets->waiting_time is in "output blocks". |
1823 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs); | 1823 stats_.StoreWaitingTime(packet->waiting_time * kOutputSizeMs); |
1824 assert(packet->payload_length > 0); | 1824 assert(packet->payload_length > 0); |
1825 packet_list->push_back(packet); // Store packet in list. | 1825 packet_list->push_back(packet); // Store packet in list. |
1826 | 1826 |
1827 if (first_packet) { | 1827 if (first_packet) { |
1828 first_packet = false; | 1828 first_packet = false; |
1829 decoded_packet_sequence_number_ = prev_sequence_number = | 1829 decoded_packet_sequence_number_ = prev_sequence_number = |
1830 packet->header.sequenceNumber; | 1830 packet->header.sequenceNumber; |
1831 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp; | 1831 decoded_packet_timestamp_ = prev_timestamp = packet->header.timestamp; |
1832 prev_payload_type = packet->header.payloadType; | 1832 prev_payload_type = packet->header.payloadType; |
1833 } | 1833 } |
1834 | 1834 |
1835 // Store number of extracted samples. | 1835 // Store number of extracted samples. |
1836 int packet_duration = 0; | 1836 int packet_duration = 0; |
1837 AudioDecoder* decoder = decoder_database_->GetDecoder( | 1837 AudioDecoder* decoder = decoder_database_->GetDecoder( |
1838 packet->header.payloadType); | 1838 packet->header.payloadType); |
1839 if (decoder) { | 1839 if (decoder) { |
1840 if (packet->sync_packet) { | 1840 if (packet->sync_packet) { |
1841 packet_duration = decoder_frame_length_; | 1841 packet_duration = static_cast<int>(decoder_frame_length_); |
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:47
Done.
| |
1842 } else { | 1842 } else { |
1843 if (packet->primary) { | 1843 if (packet->primary) { |
1844 packet_duration = decoder->PacketDuration(packet->payload, | 1844 packet_duration = decoder->PacketDuration(packet->payload, |
1845 packet->payload_length); | 1845 packet->payload_length); |
1846 } else { | 1846 } else { |
1847 packet_duration = decoder-> | 1847 packet_duration = decoder-> |
1848 PacketDurationRedundant(packet->payload, packet->payload_length); | 1848 PacketDurationRedundant(packet->payload, packet->payload_length); |
1849 stats_.SecondaryDecodedSamples(packet_duration); | 1849 stats_.SecondaryDecodedSamples(packet_duration); |
1850 } | 1850 } |
1851 } | 1851 } |
1852 } else { | 1852 } else { |
1853 LOG_FERR1(LS_WARNING, GetDecoder, | 1853 LOG_FERR1(LS_WARNING, GetDecoder, |
1854 static_cast<int>(packet->header.payloadType)) | 1854 static_cast<int>(packet->header.payloadType)) |
1855 << "Could not find a decoder for a packet about to be extracted."; | 1855 << "Could not find a decoder for a packet about to be extracted."; |
1856 assert(false); | 1856 assert(false); |
1857 } | 1857 } |
1858 if (packet_duration <= 0) { | 1858 if (packet_duration <= 0) { |
1859 // Decoder did not return a packet duration. Assume that the packet | 1859 // Decoder did not return a packet duration. Assume that the packet |
1860 // contains the same number of samples as the previous one. | 1860 // contains the same number of samples as the previous one. |
1861 packet_duration = decoder_frame_length_; | 1861 packet_duration = static_cast<int>(decoder_frame_length_); |
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
Done.
| |
1862 } | 1862 } |
1863 extracted_samples = packet->header.timestamp - first_timestamp + | 1863 extracted_samples = packet->header.timestamp - first_timestamp + |
1864 packet_duration; | 1864 packet_duration; |
1865 | 1865 |
1866 // Check what packet is available next. | 1866 // Check what packet is available next. |
1867 header = packet_buffer_->NextRtpHeader(); | 1867 header = packet_buffer_->NextRtpHeader(); |
1868 next_packet_available = false; | 1868 next_packet_available = false; |
1869 if (header && prev_payload_type == header->payloadType) { | 1869 if (header && prev_payload_type == header->payloadType) { |
1870 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number; | 1870 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number; |
1871 int32_t ts_diff = header->timestamp - prev_timestamp; | 1871 size_t ts_diff = header->timestamp - prev_timestamp; |
1872 if (seq_no_diff == 1 || | 1872 if (seq_no_diff == 1 || |
1873 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) { | 1873 (seq_no_diff == 0 && ts_diff == decoder_frame_length_)) { |
1874 // The next sequence number is available, or the next part of a packet | 1874 // The next sequence number is available, or the next part of a packet |
1875 // that was split into pieces upon insertion. | 1875 // that was split into pieces upon insertion. |
1876 next_packet_available = true; | 1876 next_packet_available = true; |
1877 } | 1877 } |
1878 prev_sequence_number = header->sequenceNumber; | 1878 prev_sequence_number = header->sequenceNumber; |
1879 } | 1879 } |
1880 } while (extracted_samples < required_samples && next_packet_available); | 1880 } while (extracted_samples < static_cast<int>(required_samples) && |
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
Done.
| |
1881 next_packet_available); | |
1881 | 1882 |
1882 if (extracted_samples > 0) { | 1883 if (extracted_samples > 0) { |
1883 // Delete old packets only when we are going to decode something. Otherwise, | 1884 // Delete old packets only when we are going to decode something. Otherwise, |
1884 // we could end up in the situation where we never decode anything, since | 1885 // we could end up in the situation where we never decode anything, since |
1885 // all incoming packets are considered too old but the buffer will also | 1886 // all incoming packets are considered too old but the buffer will also |
1886 // never be flooded and flushed. | 1887 // never be flooded and flushed. |
1887 packet_buffer_->DiscardAllOldPackets(timestamp_); | 1888 packet_buffer_->DiscardAllOldPackets(timestamp_); |
1888 } | 1889 } |
1889 | 1890 |
1890 return extracted_samples; | 1891 return extracted_samples; |
1891 } | 1892 } |
1892 | 1893 |
1893 void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) { | 1894 void NetEqImpl::UpdatePlcComponents(int fs_hz, size_t channels) { |
1894 // Delete objects and create new ones. | 1895 // Delete objects and create new ones. |
1895 expand_.reset(expand_factory_->Create(background_noise_.get(), | 1896 expand_.reset(expand_factory_->Create(background_noise_.get(), |
1896 sync_buffer_.get(), &random_vector_, | 1897 sync_buffer_.get(), &random_vector_, |
1897 fs_hz, channels)); | 1898 fs_hz, channels)); |
1898 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get())); | 1899 merge_.reset(new Merge(fs_hz, channels, expand_.get(), sync_buffer_.get())); |
1899 } | 1900 } |
1900 | 1901 |
1901 void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) { | 1902 void NetEqImpl::SetSampleRateAndChannels(int fs_hz, size_t channels) { |
1902 LOG_API2(fs_hz, channels); | 1903 LOG_API2(fs_hz, channels); |
1903 // TODO(hlundin): Change to an enumerator and skip assert. | 1904 // TODO(hlundin): Change to an enumerator and skip assert. |
1904 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000); | 1905 assert(fs_hz == 8000 || fs_hz == 16000 || fs_hz == 32000 || fs_hz == 48000); |
1905 assert(channels > 0); | 1906 assert(channels > 0); |
1906 | 1907 |
1907 fs_hz_ = fs_hz; | 1908 fs_hz_ = fs_hz; |
1908 fs_mult_ = fs_hz / 8000; | 1909 fs_mult_ = static_cast<size_t>(fs_hz / 8000); |
1909 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_; | 1910 output_size_samples_ = kOutputSizeMs * 8 * fs_mult_; |
1910 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms. | 1911 decoder_frame_length_ = 3 * output_size_samples_; // Initialize to 30ms. |
1911 | 1912 |
1912 last_mode_ = kModeNormal; | 1913 last_mode_ = kModeNormal; |
1913 | 1914 |
1914 // Create a new array of mute factors and set all to 1. | 1915 // Create a new array of mute factors and set all to 1. |
1915 mute_factor_array_.reset(new int16_t[channels]); | 1916 mute_factor_array_.reset(new int16_t[channels]); |
1916 for (size_t i = 0; i < channels; ++i) { | 1917 for (size_t i = 0; i < channels; ++i) { |
1917 mute_factor_array_[i] = 16384; // 1.0 in Q14. | 1918 mute_factor_array_[i] = 16384; // 1.0 in Q14. |
1918 } | 1919 } |
(...skipping 25 matching lines...) Expand all Loading... | |
1944 | 1945 |
1945 // Move index so that we create a small set of future samples (all 0). | 1946 // Move index so that we create a small set of future samples (all 0). |
1946 sync_buffer_->set_next_index(sync_buffer_->next_index() - | 1947 sync_buffer_->set_next_index(sync_buffer_->next_index() - |
1947 expand_->overlap_length()); | 1948 expand_->overlap_length()); |
1948 | 1949 |
1949 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_, | 1950 normal_.reset(new Normal(fs_hz, decoder_database_.get(), *background_noise_, |
1950 expand_.get())); | 1951 expand_.get())); |
1951 accelerate_.reset( | 1952 accelerate_.reset( |
1952 accelerate_factory_->Create(fs_hz, channels, *background_noise_)); | 1953 accelerate_factory_->Create(fs_hz, channels, *background_noise_)); |
1953 preemptive_expand_.reset(preemptive_expand_factory_->Create( | 1954 preemptive_expand_.reset(preemptive_expand_factory_->Create( |
1954 fs_hz, channels, | 1955 fs_hz, channels, *background_noise_, expand_->overlap_length())); |
1955 *background_noise_, | |
1956 static_cast<int>(expand_->overlap_length()))); | |
1957 | 1956 |
1958 // Delete ComfortNoise object and create a new one. | 1957 // Delete ComfortNoise object and create a new one. |
1959 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(), | 1958 comfort_noise_.reset(new ComfortNoise(fs_hz, decoder_database_.get(), |
1960 sync_buffer_.get())); | 1959 sync_buffer_.get())); |
1961 | 1960 |
1962 // Verify that |decoded_buffer_| is long enough. | 1961 // Verify that |decoded_buffer_| is long enough. |
1963 if (decoded_buffer_length_ < kMaxFrameSize * channels) { | 1962 if (decoded_buffer_length_ < kMaxFrameSize * channels) { |
1964 // Reallocate to larger size. | 1963 // Reallocate to larger size. |
1965 decoded_buffer_length_ = kMaxFrameSize * channels; | 1964 decoded_buffer_length_ = kMaxFrameSize * channels; |
1966 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]); | 1965 decoded_buffer_.reset(new int16_t[decoded_buffer_length_]); |
(...skipping 26 matching lines...) Expand all Loading... | |
1993 | 1992 |
1994 void NetEqImpl::CreateDecisionLogic() { | 1993 void NetEqImpl::CreateDecisionLogic() { |
1995 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, | 1994 decision_logic_.reset(DecisionLogic::Create(fs_hz_, output_size_samples_, |
1996 playout_mode_, | 1995 playout_mode_, |
1997 decoder_database_.get(), | 1996 decoder_database_.get(), |
1998 *packet_buffer_.get(), | 1997 *packet_buffer_.get(), |
1999 delay_manager_.get(), | 1998 delay_manager_.get(), |
2000 buffer_level_filter_.get())); | 1999 buffer_level_filter_.get())); |
2001 } | 2000 } |
2002 } // namespace webrtc | 2001 } // namespace webrtc |
OLD | NEW |