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

Side by Side Diff: webrtc/modules/audio_coding/neteq/neteq_impl.cc

Issue 2326953003: Added a ParsePayload method to AudioDecoder. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 return kNotImplemented; 323 return kNotImplemented;
324 } 324 }
325 325
326 int NetEqImpl::CurrentDelayMs() const { 326 int NetEqImpl::CurrentDelayMs() const {
327 rtc::CritScope lock(&crit_sect_); 327 rtc::CritScope lock(&crit_sect_);
328 if (fs_hz_ == 0) 328 if (fs_hz_ == 0)
329 return 0; 329 return 0;
330 // Sum up the samples in the packet buffer with the future length of the sync 330 // Sum up the samples in the packet buffer with the future length of the sync
331 // buffer, and divide the sum by the sample rate. 331 // buffer, and divide the sum by the sample rate.
332 const size_t delay_samples = 332 const size_t delay_samples =
333 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(), 333 packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
334 decoder_frame_length_) +
335 sync_buffer_->FutureLength(); 334 sync_buffer_->FutureLength();
336 // The division below will truncate. 335 // The division below will truncate.
337 const int delay_ms = 336 const int delay_ms =
338 static_cast<int>(delay_samples) / rtc::CheckedDivExact(fs_hz_, 1000); 337 static_cast<int>(delay_samples) / rtc::CheckedDivExact(fs_hz_, 1000);
339 return delay_ms; 338 return delay_ms;
340 } 339 }
341 340
342 int NetEqImpl::FilteredCurrentDelayMs() const { 341 int NetEqImpl::FilteredCurrentDelayMs() const {
343 rtc::CritScope lock(&crit_sect_); 342 rtc::CritScope lock(&crit_sect_);
344 // Calculate the filtered packet buffer level in samples. The value from 343 // Calculate the filtered packet buffer level in samples. The value from
(...skipping 24 matching lines...) Expand all
369 // TODO(henrik.lundin) Delete. 368 // TODO(henrik.lundin) Delete.
370 NetEqPlayoutMode NetEqImpl::PlayoutMode() const { 369 NetEqPlayoutMode NetEqImpl::PlayoutMode() const {
371 rtc::CritScope lock(&crit_sect_); 370 rtc::CritScope lock(&crit_sect_);
372 return playout_mode_; 371 return playout_mode_;
373 } 372 }
374 373
375 int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) { 374 int NetEqImpl::NetworkStatistics(NetEqNetworkStatistics* stats) {
376 rtc::CritScope lock(&crit_sect_); 375 rtc::CritScope lock(&crit_sect_);
377 assert(decoder_database_.get()); 376 assert(decoder_database_.get());
378 const size_t total_samples_in_buffers = 377 const size_t total_samples_in_buffers =
379 packet_buffer_->NumSamplesInBuffer(decoder_database_.get(), 378 packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
380 decoder_frame_length_) +
381 sync_buffer_->FutureLength(); 379 sync_buffer_->FutureLength();
382 assert(delay_manager_.get()); 380 assert(delay_manager_.get());
383 assert(decision_logic_.get()); 381 assert(decision_logic_.get());
384 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers, 382 stats_.GetNetworkStatistics(fs_hz_, total_samples_in_buffers,
385 decoder_frame_length_, *delay_manager_.get(), 383 decoder_frame_length_, *delay_manager_.get(),
386 *decision_logic_.get(), stats); 384 *decision_logic_.get(), stats);
387 return 0; 385 return 0;
388 } 386 }
389 387
390 void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) { 388 void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 decoder_database_->GetDecoder(main_header.payloadType); 653 decoder_database_->GetDecoder(main_header.payloadType);
656 assert(decoder); // Should always get a valid object, since we have 654 assert(decoder); // Should always get a valid object, since we have
657 // already checked that the payload types are known. 655 // already checked that the payload types are known.
658 decoder->IncomingPacket(packet_list.front()->payload.data(), 656 decoder->IncomingPacket(packet_list.front()->payload.data(),
659 packet_list.front()->payload.size(), 657 packet_list.front()->payload.size(),
660 packet_list.front()->header.sequenceNumber, 658 packet_list.front()->header.sequenceNumber,
661 packet_list.front()->header.timestamp, 659 packet_list.front()->header.timestamp,
662 receive_timestamp); 660 receive_timestamp);
663 } 661 }
664 662
663 PacketList parsed_packet_list;
664 while (!packet_list.empty()) {
665 Packet* packet = packet_list.front();
kwiberg-webrtc 2016/09/10 07:34:59 unique_ptr?
ossu 2016/09/12 10:31:37 Acknowledged.
666 packet_list.pop_front();
667 const DecoderDatabase::DecoderInfo* info =
668 decoder_database_->GetDecoderInfo(packet->header.payloadType);
669 if (!info) {
670 LOG(LS_WARNING) << "SplitAudio unknown payload type";
671 return kUnknownRtpPayloadType;
672 }
673
674 if (info->IsComfortNoise()) {
675 // Carry comfort noise packets along.
676 parsed_packet_list.push_back(packet);
677 } else {
678 std::vector<AudioDecoder::ParseResult> results =
679 info->GetDecoder()->ParsePayload(&packet->payload,
680 packet->header.timestamp,
681 packet->primary);
682
683 // Reuse the packet if possible
684 if (results.size() == 1) {
685 auto& result = results[0];
686 RTC_DCHECK(result.frame);
687 packet->header.timestamp = result.timestamp;
688 packet->primary = result.primary;
689 packet->frame = std::move(result.frame);
690 parsed_packet_list.push_back(packet);
691 } else {
692 for (auto& result : results) {
693 // If you can't parse, return an empty list instead!
694 RTC_DCHECK(result.frame);
695 Packet* parsed_packet = new Packet;
kwiberg-webrtc 2016/09/10 07:34:59 unique_ptr?
ossu 2016/09/12 10:31:37 Acknowledged.
696 parsed_packet->header = packet->header;
697 parsed_packet->header.timestamp = result.timestamp;
698 // TODO(ossu): Move from primary to some sort of priority level.
699 parsed_packet->primary = result.primary;
700 parsed_packet->frame = std::move(result.frame);
701 parsed_packet_list.push_back(parsed_packet);
702 }
703
704 delete packet;
kwiberg-webrtc 2016/09/10 07:34:59 unique_ptr!
hlundin-webrtc 2016/09/15 08:12:16 FTW!
705 }
706 }
707 }
708
665 if (nack_enabled_) { 709 if (nack_enabled_) {
666 RTC_DCHECK(nack_); 710 RTC_DCHECK(nack_);
667 if (update_sample_rate_and_channels) { 711 if (update_sample_rate_and_channels) {
668 nack_->Reset(); 712 nack_->Reset();
669 } 713 }
670 nack_->UpdateLastReceivedPacket(packet_list.front()->header.sequenceNumber, 714 nack_->UpdateLastReceivedPacket(
671 packet_list.front()->header.timestamp); 715 parsed_packet_list.front()->header.sequenceNumber,
716 parsed_packet_list.front()->header.timestamp);
672 } 717 }
673 718
674 // Insert packets in buffer. 719 // Insert packets in buffer.
675 const size_t buffer_length_before_insert = 720 const size_t buffer_length_before_insert =
676 packet_buffer_->NumPacketsInBuffer(); 721 packet_buffer_->NumPacketsInBuffer();
677 ret = packet_buffer_->InsertPacketList( 722 ret = packet_buffer_->InsertPacketList(
678 &packet_list, 723 &parsed_packet_list, *decoder_database_, &current_rtp_payload_type_,
679 *decoder_database_,
680 &current_rtp_payload_type_,
681 &current_cng_rtp_payload_type_); 724 &current_cng_rtp_payload_type_);
682 if (ret == PacketBuffer::kFlushed) { 725 if (ret == PacketBuffer::kFlushed) {
683 // Reset DSP timestamp etc. if packet buffer flushed. 726 // Reset DSP timestamp etc. if packet buffer flushed.
684 new_codec_ = true; 727 new_codec_ = true;
685 update_sample_rate_and_channels = true; 728 update_sample_rate_and_channels = true;
686 } else if (ret != PacketBuffer::kOK) { 729 } else if (ret != PacketBuffer::kOK) {
687 PacketBuffer::DeleteAllPackets(&packet_list); 730 PacketBuffer::DeleteAllPackets(&parsed_packet_list);
688 return kOtherError; 731 return kOtherError;
689 } 732 }
690 733
691 if (first_packet_) { 734 if (first_packet_) {
692 first_packet_ = false; 735 first_packet_ = false;
693 // Update the codec on the next GetAudio call. 736 // Update the codec on the next GetAudio call.
694 new_codec_ = true; 737 new_codec_ = true;
695 } 738 }
696 739
697 if (current_rtp_payload_type_) { 740 if (current_rtp_payload_type_) {
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
1414 !decoder_database_->IsComfortNoise(packet->header.payloadType)) { 1457 !decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1415 assert(decoder); // At this point, we must have a decoder object. 1458 assert(decoder); // At this point, we must have a decoder object.
1416 // The number of channels in the |sync_buffer_| should be the same as the 1459 // The number of channels in the |sync_buffer_| should be the same as the
1417 // number decoder channels. 1460 // number decoder channels.
1418 assert(sync_buffer_->Channels() == decoder->Channels()); 1461 assert(sync_buffer_->Channels() == decoder->Channels());
1419 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels()); 1462 assert(decoded_buffer_length_ >= kMaxFrameSize * decoder->Channels());
1420 assert(operation == kNormal || operation == kAccelerate || 1463 assert(operation == kNormal || operation == kAccelerate ||
1421 operation == kFastAccelerate || operation == kMerge || 1464 operation == kFastAccelerate || operation == kMerge ||
1422 operation == kPreemptiveExpand); 1465 operation == kPreemptiveExpand);
1423 packet_list->pop_front(); 1466 packet_list->pop_front();
1424 const size_t payload_length = packet->payload.size(); 1467 auto opt_result = packet->frame->Decode(
1425 int decode_length; 1468 rtc::ArrayView<int16_t>(&decoded_buffer_[*decoded_length],
1426 if (!packet->primary) { 1469 decoded_buffer_length_ - *decoded_length));
1427 // This is a redundant payload; call the special decoder method.
1428 decode_length = decoder->DecodeRedundant(
1429 packet->payload.data(), packet->payload.size(), fs_hz_,
1430 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1431 &decoded_buffer_[*decoded_length], speech_type);
1432 } else {
1433 decode_length = decoder->Decode(
1434 packet->payload.data(), packet->payload.size(), fs_hz_,
1435 (decoded_buffer_length_ - *decoded_length) * sizeof(int16_t),
1436 &decoded_buffer_[*decoded_length], speech_type);
1437 }
1438
1439 delete packet; 1470 delete packet;
1440 packet = NULL; 1471 packet = NULL;
1441 if (decode_length > 0) { 1472 if (opt_result) {
1442 *decoded_length += decode_length; 1473 const AudioDecoder::Frame::DecodeResult& result = *opt_result;
1443 // Update |decoder_frame_length_| with number of samples per channel. 1474 *speech_type = result.speech_type;
1444 decoder_frame_length_ = 1475 if (result.num_decoded_samples > 0) {
1445 static_cast<size_t>(decode_length) / decoder->Channels(); 1476 *decoded_length += result.num_decoded_samples;
1446 } else if (decode_length < 0) { 1477 // Update |decoder_frame_length_| with number of samples per channel.
1478 decoder_frame_length_ =
1479 result.num_decoded_samples / decoder->Channels();
1480 }
1481 } else {
1447 // Error. 1482 // Error.
1448 LOG(LS_WARNING) << "Decode " << decode_length << " " << payload_length; 1483 // TODO(ossu): What to put here?
1484 LOG(LS_WARNING) << "Decode error";
1449 *decoded_length = -1; 1485 *decoded_length = -1;
1450 PacketBuffer::DeleteAllPackets(packet_list); 1486 PacketBuffer::DeleteAllPackets(packet_list);
1451 break; 1487 break;
1452 } 1488 }
1453 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) { 1489 if (*decoded_length > static_cast<int>(decoded_buffer_length_)) {
1454 // Guard against overflow. 1490 // Guard against overflow.
1455 LOG(LS_WARNING) << "Decoded too much."; 1491 LOG(LS_WARNING) << "Decoded too much.";
1456 PacketBuffer::DeleteAllPackets(packet_list); 1492 PacketBuffer::DeleteAllPackets(packet_list);
1457 return kDecodedTooMuch; 1493 return kDecodedTooMuch;
1458 } 1494 }
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
1901 Packet* packet = packet_buffer_->GetNextPacket(&discard_count); 1937 Packet* packet = packet_buffer_->GetNextPacket(&discard_count);
1902 // |header| may be invalid after the |packet_buffer_| operation. 1938 // |header| may be invalid after the |packet_buffer_| operation.
1903 header = NULL; 1939 header = NULL;
1904 if (!packet) { 1940 if (!packet) {
1905 LOG(LS_ERROR) << "Should always be able to extract a packet here"; 1941 LOG(LS_ERROR) << "Should always be able to extract a packet here";
1906 assert(false); // Should always be able to extract a packet here. 1942 assert(false); // Should always be able to extract a packet here.
1907 return -1; 1943 return -1;
1908 } 1944 }
1909 stats_.PacketsDiscarded(discard_count); 1945 stats_.PacketsDiscarded(discard_count);
1910 stats_.StoreWaitingTime(packet->waiting_time->ElapsedMs()); 1946 stats_.StoreWaitingTime(packet->waiting_time->ElapsedMs());
1911 assert(!packet->payload.empty()); 1947 assert(packet->frame || !packet->payload.empty());
hlundin-webrtc 2016/09/09 12:11:50 Switch to DCHECK while you're at it.
ossu 2016/09/12 10:31:37 Acknowledged.
1912 packet_list->push_back(packet); // Store packet in list. 1948 packet_list->push_back(packet); // Store packet in list.
1913 1949
1914 if (first_packet) { 1950 if (first_packet) {
1915 first_packet = false; 1951 first_packet = false;
1916 if (nack_enabled_) { 1952 if (nack_enabled_) {
1917 RTC_DCHECK(nack_); 1953 RTC_DCHECK(nack_);
1918 // TODO(henrik.lundin): Should we update this for all decoded packets? 1954 // TODO(henrik.lundin): Should we update this for all decoded packets?
1919 nack_->UpdateLastDecodedPacket(packet->header.sequenceNumber, 1955 nack_->UpdateLastDecodedPacket(packet->header.sequenceNumber,
1920 packet->header.timestamp); 1956 packet->header.timestamp);
1921 } 1957 }
1922 prev_sequence_number = packet->header.sequenceNumber; 1958 prev_sequence_number = packet->header.sequenceNumber;
1923 prev_timestamp = packet->header.timestamp; 1959 prev_timestamp = packet->header.timestamp;
1924 prev_payload_type = packet->header.payloadType; 1960 prev_payload_type = packet->header.payloadType;
1925 } 1961 }
1926 1962
1927 // Store number of extracted samples. 1963 // Store number of extracted samples.
1928 int packet_duration = 0; 1964 size_t packet_duration = 0;
1929 AudioDecoder* decoder = decoder_database_->GetDecoder( 1965 if (packet->frame) {
1930 packet->header.payloadType); 1966 packet_duration = packet->frame->Duration();
1931 if (decoder) { 1967 // TODO(ossu): Is this the correct way to track samples decoded from a
hlundin-webrtc 2016/09/09 12:11:50 Good question.
ossu 2016/09/12 10:31:37 What does the SecondaryDecodedSamples stat mean? I
hlundin-webrtc 2016/09/15 08:12:16 It was added to track how often Opus FEC saved the
1932 if (packet->primary) { 1968 // redundant packet?
1933 packet_duration = decoder->PacketDuration(packet->payload.data(), 1969 if (packet_duration > 0 && !packet->primary) {
1934 packet->payload.size());
1935 } else {
1936 packet_duration = decoder->PacketDurationRedundant(
1937 packet->payload.data(), packet->payload.size());
1938 stats_.SecondaryDecodedSamples(packet_duration); 1970 stats_.SecondaryDecodedSamples(packet_duration);
1939 } 1971 }
1940 } else if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) { 1972 } else if (!decoder_database_->IsComfortNoise(packet->header.payloadType)) {
1941 LOG(LS_WARNING) << "Unknown payload type " 1973 LOG(LS_WARNING) << "Unknown payload type "
1942 << static_cast<int>(packet->header.payloadType); 1974 << static_cast<int>(packet->header.payloadType);
1943 assert(false); 1975 RTC_DCHECK(false);
kwiberg-webrtc 2016/09/10 07:34:59 RTC_NOTREACHED();
ossu 2016/09/12 10:31:37 Ah, yes, of course!
1944 } 1976 }
1945 if (packet_duration <= 0) { 1977
1978 if (packet_duration == 0) {
1946 // Decoder did not return a packet duration. Assume that the packet 1979 // Decoder did not return a packet duration. Assume that the packet
1947 // contains the same number of samples as the previous one. 1980 // contains the same number of samples as the previous one.
1948 packet_duration = rtc::checked_cast<int>(decoder_frame_length_); 1981 packet_duration = decoder_frame_length_;
1949 } 1982 }
1950 extracted_samples = packet->header.timestamp - first_timestamp + 1983 extracted_samples = packet->header.timestamp - first_timestamp +
1951 packet_duration; 1984 packet_duration;
1952 1985
1953 // Check what packet is available next. 1986 // Check what packet is available next.
1954 header = packet_buffer_->NextRtpHeader(); 1987 header = packet_buffer_->NextRtpHeader();
1955 next_packet_available = false; 1988 next_packet_available = false;
1956 if (header && prev_payload_type == header->payloadType) { 1989 if (header && prev_payload_type == header->payloadType) {
1957 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number; 1990 int16_t seq_no_diff = header->sequenceNumber - prev_sequence_number;
1958 size_t ts_diff = header->timestamp - prev_timestamp; 1991 size_t ts_diff = header->timestamp - prev_timestamp;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2075 } 2108 }
2076 } 2109 }
2077 2110
2078 void NetEqImpl::CreateDecisionLogic() { 2111 void NetEqImpl::CreateDecisionLogic() {
2079 decision_logic_.reset(DecisionLogic::Create( 2112 decision_logic_.reset(DecisionLogic::Create(
2080 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(), 2113 fs_hz_, output_size_samples_, playout_mode_, decoder_database_.get(),
2081 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(), 2114 *packet_buffer_.get(), delay_manager_.get(), buffer_level_filter_.get(),
2082 tick_timer_.get())); 2115 tick_timer_.get()));
2083 } 2116 }
2084 } // namespace webrtc 2117 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698