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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 130 |
131 NetEqImpl::~NetEqImpl() = default; | 131 NetEqImpl::~NetEqImpl() = default; |
132 | 132 |
133 int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header, | 133 int NetEqImpl::InsertPacket(const WebRtcRTPHeader& rtp_header, |
134 rtc::ArrayView<const uint8_t> payload, | 134 rtc::ArrayView<const uint8_t> payload, |
135 uint32_t receive_timestamp) { | 135 uint32_t receive_timestamp) { |
136 rtc::MsanCheckInitialized(payload); | 136 rtc::MsanCheckInitialized(payload); |
137 TRACE_EVENT0("webrtc", "NetEqImpl::InsertPacket"); | 137 TRACE_EVENT0("webrtc", "NetEqImpl::InsertPacket"); |
138 rtc::CritScope lock(&crit_sect_); | 138 rtc::CritScope lock(&crit_sect_); |
139 int error = | 139 int error = |
140 InsertPacketInternal(rtp_header, payload, receive_timestamp, false); | 140 InsertPacketInternal(rtp_header, payload, receive_timestamp); |
141 if (error != 0) { | 141 if (error != 0) { |
142 error_code_ = error; | 142 error_code_ = error; |
143 return kFail; | 143 return kFail; |
144 } | |
145 return kOK; | |
146 } | |
147 | |
148 int NetEqImpl::InsertSyncPacket(const WebRtcRTPHeader& rtp_header, | |
149 uint32_t receive_timestamp) { | |
150 rtc::CritScope lock(&crit_sect_); | |
151 const uint8_t kSyncPayload[] = { 's', 'y', 'n', 'c' }; | |
152 int error = | |
153 InsertPacketInternal(rtp_header, kSyncPayload, receive_timestamp, true); | |
154 | |
155 if (error != 0) { | |
156 error_code_ = error; | |
157 return kFail; | |
158 } | 144 } |
159 return kOK; | 145 return kOK; |
160 } | 146 } |
161 | 147 |
162 namespace { | 148 namespace { |
163 void SetAudioFrameActivityAndType(bool vad_enabled, | 149 void SetAudioFrameActivityAndType(bool vad_enabled, |
164 NetEqImpl::OutputType type, | 150 NetEqImpl::OutputType type, |
165 AudioFrame::VADActivity last_vad_activity, | 151 AudioFrame::VADActivity last_vad_activity, |
166 AudioFrame* audio_frame) { | 152 AudioFrame* audio_frame) { |
167 switch (type) { | 153 switch (type) { |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 501 |
516 Operations NetEqImpl::last_operation_for_test() const { | 502 Operations NetEqImpl::last_operation_for_test() const { |
517 rtc::CritScope lock(&crit_sect_); | 503 rtc::CritScope lock(&crit_sect_); |
518 return last_operation_; | 504 return last_operation_; |
519 } | 505 } |
520 | 506 |
521 // Methods below this line are private. | 507 // Methods below this line are private. |
522 | 508 |
523 int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header, | 509 int NetEqImpl::InsertPacketInternal(const WebRtcRTPHeader& rtp_header, |
524 rtc::ArrayView<const uint8_t> payload, | 510 rtc::ArrayView<const uint8_t> payload, |
525 uint32_t receive_timestamp, | 511 uint32_t receive_timestamp) { |
526 bool is_sync_packet) { | |
527 if (payload.empty()) { | 512 if (payload.empty()) { |
528 LOG_F(LS_ERROR) << "payload is empty"; | 513 LOG_F(LS_ERROR) << "payload is empty"; |
529 return kInvalidPointer; | 514 return kInvalidPointer; |
530 } | 515 } |
531 // Sanity checks for sync-packets. | 516 |
532 if (is_sync_packet) { | |
533 if (decoder_database_->IsDtmf(rtp_header.header.payloadType) || | |
534 decoder_database_->IsRed(rtp_header.header.payloadType) || | |
535 decoder_database_->IsComfortNoise(rtp_header.header.payloadType)) { | |
536 LOG_F(LS_ERROR) << "Sync-packet with an unacceptable payload type " | |
537 << static_cast<int>(rtp_header.header.payloadType); | |
538 return kSyncPacketNotAccepted; | |
539 } | |
540 if (first_packet_ || !current_rtp_payload_type_ || | |
541 rtp_header.header.payloadType != *current_rtp_payload_type_ || | |
542 rtp_header.header.ssrc != ssrc_) { | |
543 // Even if |current_rtp_payload_type_| is empty, sync-packet isn't | |
544 // accepted. | |
545 LOG_F(LS_ERROR) | |
546 << "Changing codec, SSRC or first packet with sync-packet."; | |
547 return kSyncPacketNotAccepted; | |
548 } | |
549 } | |
550 PacketList packet_list; | 517 PacketList packet_list; |
551 RTPHeader main_header; | 518 RTPHeader main_header; |
552 { | 519 { |
553 // Convert to Packet. | 520 // Convert to Packet. |
554 // Create |packet| within this separate scope, since it should not be used | 521 // Create |packet| within this separate scope, since it should not be used |
555 // directly once it's been inserted in the packet list. This way, |packet| | 522 // directly once it's been inserted in the packet list. This way, |packet| |
556 // is not defined outside of this block. | 523 // is not defined outside of this block. |
557 Packet* packet = new Packet; | 524 Packet* packet = new Packet; |
558 packet->header.markerBit = false; | 525 packet->header.markerBit = false; |
559 packet->header.payloadType = rtp_header.header.payloadType; | 526 packet->header.payloadType = rtp_header.header.payloadType; |
560 packet->header.sequenceNumber = rtp_header.header.sequenceNumber; | 527 packet->header.sequenceNumber = rtp_header.header.sequenceNumber; |
561 packet->header.timestamp = rtp_header.header.timestamp; | 528 packet->header.timestamp = rtp_header.header.timestamp; |
562 packet->header.ssrc = rtp_header.header.ssrc; | 529 packet->header.ssrc = rtp_header.header.ssrc; |
563 packet->header.numCSRCs = 0; | 530 packet->header.numCSRCs = 0; |
564 packet->payload.SetData(payload.data(), payload.size()); | 531 packet->payload.SetData(payload.data(), payload.size()); |
565 packet->primary = true; | 532 packet->primary = true; |
566 // Waiting time will be set upon inserting the packet in the buffer. | 533 // Waiting time will be set upon inserting the packet in the buffer. |
567 RTC_DCHECK(!packet->waiting_time); | 534 RTC_DCHECK(!packet->waiting_time); |
568 packet->sync_packet = is_sync_packet; | |
569 // Insert packet in a packet list. | 535 // Insert packet in a packet list. |
570 packet_list.push_back(packet); | 536 packet_list.push_back(packet); |
571 // Save main payloads header for later. | 537 // Save main payloads header for later. |
572 memcpy(&main_header, &packet->header, sizeof(main_header)); | 538 memcpy(&main_header, &packet->header, sizeof(main_header)); |
573 } | 539 } |
574 | 540 |
575 bool update_sample_rate_and_channels = false; | 541 bool update_sample_rate_and_channels = false; |
576 // Reinitialize NetEq if it's needed (changed SSRC or first call). | 542 // Reinitialize NetEq if it's needed (changed SSRC or first call). |
577 if ((main_header.ssrc != ssrc_) || first_packet_) { | 543 if ((main_header.ssrc != ssrc_) || first_packet_) { |
578 // Note: |first_packet_| will be cleared further down in this method, once | 544 // Note: |first_packet_| will be cleared further down in this method, once |
(...skipping 15 matching lines...) Expand all Loading... |
594 timestamp_ = main_header.timestamp; | 560 timestamp_ = main_header.timestamp; |
595 | 561 |
596 // Reset timestamp scaling. | 562 // Reset timestamp scaling. |
597 timestamp_scaler_->Reset(); | 563 timestamp_scaler_->Reset(); |
598 | 564 |
599 // Trigger an update of sampling rate and the number of channels. | 565 // Trigger an update of sampling rate and the number of channels. |
600 update_sample_rate_and_channels = true; | 566 update_sample_rate_and_channels = true; |
601 } | 567 } |
602 | 568 |
603 // Update RTCP statistics, only for regular packets. | 569 // Update RTCP statistics, only for regular packets. |
604 if (!is_sync_packet) | 570 rtcp_.Update(main_header, receive_timestamp); |
605 rtcp_.Update(main_header, receive_timestamp); | |
606 | 571 |
607 // Check for RED payload type, and separate payloads into several packets. | 572 // Check for RED payload type, and separate payloads into several packets. |
608 if (decoder_database_->IsRed(main_header.payloadType)) { | 573 if (decoder_database_->IsRed(main_header.payloadType)) { |
609 assert(!is_sync_packet); // We had a sanity check for this. | |
610 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) { | 574 if (payload_splitter_->SplitRed(&packet_list) != PayloadSplitter::kOK) { |
611 PacketBuffer::DeleteAllPackets(&packet_list); | 575 PacketBuffer::DeleteAllPackets(&packet_list); |
612 return kRedundancySplitError; | 576 return kRedundancySplitError; |
613 } | 577 } |
614 // Only accept a few RED payloads of the same type as the main data, | 578 // Only accept a few RED payloads of the same type as the main data, |
615 // DTMF events and CNG. | 579 // DTMF events and CNG. |
616 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_); | 580 payload_splitter_->CheckRedPayloads(&packet_list, *decoder_database_); |
617 // Update the stored main payload header since the main payload has now | 581 // Update the stored main payload header since the main payload has now |
618 // changed. | 582 // changed. |
619 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header)); | 583 memcpy(&main_header, &packet_list.front()->header, sizeof(main_header)); |
(...skipping 10 matching lines...) Expand all Loading... |
630 timestamp_scaler_->ToInternal(&packet_list); | 594 timestamp_scaler_->ToInternal(&packet_list); |
631 | 595 |
632 // Process DTMF payloads. Cycle through the list of packets, and pick out any | 596 // Process DTMF payloads. Cycle through the list of packets, and pick out any |
633 // DTMF payloads found. | 597 // DTMF payloads found. |
634 PacketList::iterator it = packet_list.begin(); | 598 PacketList::iterator it = packet_list.begin(); |
635 while (it != packet_list.end()) { | 599 while (it != packet_list.end()) { |
636 Packet* current_packet = (*it); | 600 Packet* current_packet = (*it); |
637 assert(current_packet); | 601 assert(current_packet); |
638 assert(!current_packet->payload.empty()); | 602 assert(!current_packet->payload.empty()); |
639 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) { | 603 if (decoder_database_->IsDtmf(current_packet->header.payloadType)) { |
640 assert(!current_packet->sync_packet); // We had a sanity check for this. | |
641 DtmfEvent event; | 604 DtmfEvent event; |
642 int ret = DtmfBuffer::ParseEvent(current_packet->header.timestamp, | 605 int ret = DtmfBuffer::ParseEvent(current_packet->header.timestamp, |
643 current_packet->payload.data(), | 606 current_packet->payload.data(), |
644 current_packet->payload.size(), &event); | 607 current_packet->payload.size(), &event); |
645 if (ret != DtmfBuffer::kOK) { | 608 if (ret != DtmfBuffer::kOK) { |
646 PacketBuffer::DeleteAllPackets(&packet_list); | 609 PacketBuffer::DeleteAllPackets(&packet_list); |
647 return kDtmfParsingError; | 610 return kDtmfParsingError; |
648 } | 611 } |
649 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) { | 612 if (dtmf_buffer_->InsertEvent(event) != DtmfBuffer::kOK) { |
650 PacketBuffer::DeleteAllPackets(&packet_list); | 613 PacketBuffer::DeleteAllPackets(&packet_list); |
(...skipping 12 matching lines...) Expand all Loading... |
663 PacketBuffer::DeleteAllPackets(&packet_list); | 626 PacketBuffer::DeleteAllPackets(&packet_list); |
664 switch (ret) { | 627 switch (ret) { |
665 case PayloadSplitter::kUnknownPayloadType: | 628 case PayloadSplitter::kUnknownPayloadType: |
666 return kUnknownRtpPayloadType; | 629 return kUnknownRtpPayloadType; |
667 default: | 630 default: |
668 return kOtherError; | 631 return kOtherError; |
669 } | 632 } |
670 } | 633 } |
671 | 634 |
672 // Split payloads into smaller chunks. This also verifies that all payloads | 635 // Split payloads into smaller chunks. This also verifies that all payloads |
673 // are of a known payload type. SplitAudio() method is protected against | 636 // are of a known payload type. |
674 // sync-packets. | |
675 ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_); | 637 ret = payload_splitter_->SplitAudio(&packet_list, *decoder_database_); |
676 if (ret != PayloadSplitter::kOK) { | 638 if (ret != PayloadSplitter::kOK) { |
677 PacketBuffer::DeleteAllPackets(&packet_list); | 639 PacketBuffer::DeleteAllPackets(&packet_list); |
678 switch (ret) { | 640 switch (ret) { |
679 case PayloadSplitter::kUnknownPayloadType: | 641 case PayloadSplitter::kUnknownPayloadType: |
680 return kUnknownRtpPayloadType; | 642 return kUnknownRtpPayloadType; |
681 case PayloadSplitter::kFrameSplitError: | 643 case PayloadSplitter::kFrameSplitError: |
682 return kFrameSplitError; | 644 return kFrameSplitError; |
683 default: | 645 default: |
684 return kOtherError; | 646 return kOtherError; |
685 } | 647 } |
686 } | 648 } |
687 | 649 |
688 // Update bandwidth estimate, if the packet is not sync-packet nor comfort | 650 // Update bandwidth estimate, if the packet is not comfort noise. |
689 // noise. | 651 if (!packet_list.empty() && |
690 if (!packet_list.empty() && !packet_list.front()->sync_packet && | |
691 !decoder_database_->IsComfortNoise(main_header.payloadType)) { | 652 !decoder_database_->IsComfortNoise(main_header.payloadType)) { |
692 // The list can be empty here if we got nothing but DTMF payloads. | 653 // The list can be empty here if we got nothing but DTMF payloads. |
693 AudioDecoder* decoder = | 654 AudioDecoder* decoder = |
694 decoder_database_->GetDecoder(main_header.payloadType); | 655 decoder_database_->GetDecoder(main_header.payloadType); |
695 assert(decoder); // Should always get a valid object, since we have | 656 assert(decoder); // Should always get a valid object, since we have |
696 // already checked that the payload types are known. | 657 // already checked that the payload types are known. |
697 decoder->IncomingPacket(packet_list.front()->payload.data(), | 658 decoder->IncomingPacket(packet_list.front()->payload.data(), |
698 packet_list.front()->payload.size(), | 659 packet_list.front()->payload.size(), |
699 packet_list.front()->header.sequenceNumber, | 660 packet_list.front()->header.sequenceNumber, |
700 packet_list.front()->header.timestamp, | 661 packet_list.front()->header.timestamp, |
(...skipping 754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1455 // The number of channels in the |sync_buffer_| should be the same as the | 1416 // The number of channels in the |sync_buffer_| should be the same as the |
1456 // number decoder channels. | 1417 // number decoder channels. |
1457 assert(sync_buffer_->Channels() == decoder->Channels()); | 1418 assert(sync_buffer_->Channels() == decoder->Channels()); |
1458 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels()); | 1419 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels()); |
1459 assert(operation == kNormal || operation == kAccelerate || | 1420 assert(operation == kNormal || operation == kAccelerate || |
1460 operation == kFastAccelerate || operation == kMerge || | 1421 operation == kFastAccelerate || operation == kMerge || |
1461 operation == kPreemptiveExpand); | 1422 operation == kPreemptiveExpand); |
1462 packet_list->pop_front(); | 1423 packet_list->pop_front(); |
1463 const size_t payload_length = packet->payload.size(); | 1424 const size_t payload_length = packet->payload.size(); |
1464 int decode_length; | 1425 int decode_length; |
1465 if (packet->sync_packet) { | 1426 if (!packet->primary) { |
1466 // Decode to silence with the same frame size as the last decode. | |
1467 memset(&decoded_buffer_[*decoded_length], 0, | |
1468 decoder_frame_length_ * decoder->Channels() * | |
1469 sizeof(decoded_buffer_[0])); | |
1470 decode_length = rtc::checked_cast<int>(decoder_frame_length_); | |
1471 } else if (!packet->primary) { | |
1472 // This is a redundant payload; call the special decoder method. | 1427 // This is a redundant payload; call the special decoder method. |
1473 decode_length = decoder->DecodeRedundant( | 1428 decode_length = decoder->DecodeRedundant( |
1474 packet->payload.data(), packet->payload.size(), fs_hz_, | 1429 packet->payload.data(), packet->payload.size(), fs_hz_, |
1475 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t), | 1430 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t), |
1476 &decoded_buffer_[*decoded_length], speech_type); | 1431 &decoded_buffer_[*decoded_length], speech_type); |
1477 } else { | 1432 } else { |
1478 decode_length = decoder->Decode( | 1433 decode_length = decoder->Decode( |
1479 packet->payload.data(), packet->payload.size(), fs_hz_, | 1434 packet->payload.data(), packet->payload.size(), fs_hz_, |
1480 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t), | 1435 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t), |
1481 &decoded_buffer_[*decoded_length], speech_type); | 1436 &decoded_buffer_[*decoded_length], speech_type); |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1967 prev_sequence_number = packet->header.sequenceNumber; | 1922 prev_sequence_number = packet->header.sequenceNumber; |
1968 prev_timestamp = packet->header.timestamp; | 1923 prev_timestamp = packet->header.timestamp; |
1969 prev_payload_type = packet->header.payloadType; | 1924 prev_payload_type = packet->header.payloadType; |
1970 } | 1925 } |
1971 | 1926 |
1972 // Store number of extracted samples. | 1927 // Store number of extracted samples. |
1973 int packet_duration = 0; | 1928 int packet_duration = 0; |
1974 AudioDecoder* decoder = decoder_database_->GetDecoder( | 1929 AudioDecoder* decoder = decoder_database_->GetDecoder( |
1975 packet->header.payloadType); | 1930 packet->header.payloadType); |
1976 if (decoder) { | 1931 if (decoder) { |
1977 if (packet->sync_packet) { | 1932 if (packet->primary) { |
1978 packet_duration = rtc::checked_cast<int>(decoder_frame_length_); | 1933 packet_duration = decoder->PacketDuration(packet->payload.data(), |
| 1934 packet->payload.size()); |
1979 } else { | 1935 } else { |
1980 if (packet->primary) { | 1936 packet_duration = decoder->PacketDurationRedundant( |
1981 packet_duration = decoder->PacketDuration(packet->payload.data(), | 1937 packet->payload.data(), packet->payload.size()); |
1982 packet->payload.size()); | 1938 stats_.SecondaryDecodedSamples(packet_duration); |
1983 } else { | |
1984 packet_duration = decoder->PacketDurationRedundant( | |
1985 packet->payload.data(), packet->payload.size()); | |
1986 stats_.SecondaryDecodedSamples(packet_duration); | |
1987 } | |
1988 } | 1939 } |
1989 } else if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) { | 1940 } else if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) { |
1990 LOG(LS_WARNING) << "Unknown payload type " | 1941 LOG(LS_WARNING) << "Unknown payload type " |
1991 << static_cast<int>(packet->header.payloadType); | 1942 << static_cast<int>(packet->header.payloadType); |
1992 assert(false); | 1943 assert(false); |
1993 } | 1944 } |
1994 if (packet_duration <= 0) { | 1945 if (packet_duration <= 0) { |
1995 // Decoder did not return a packet duration. Assume that the packet | 1946 // Decoder did not return a packet duration. Assume that the packet |
1996 // contains the same number of samples as the previous one. | 1947 // contains the same number of samples as the previous one. |
1997 packet_duration = rtc::checked_cast<int>(decoder_frame_length_); | 1948 packet_duration = rtc::checked_cast<int>(decoder_frame_length_); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2124 } | 2075 } |
2125 } | 2076 } |
2126 | 2077 |
2127 void NetEqImpl::CreateDecisionLogic() { | 2078 void NetEqImpl::CreateDecisionLogic() { |
2128 decision_logic_.reset(DecisionLogic::Create( | 2079 decision_logic_.reset(DecisionLogic::Create( |
2129 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(), | 2080 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(), |
2130 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(), | 2081 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(), |
2131 tick_timer_.get())); | 2082 tick_timer_.get())); |
2132 } | 2083 } |
2133 } // namespace webrtc | 2084 } // namespace webrtc |
OLD | NEW |