henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/include/logging.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 18 | void TimestampScaler::Reset() { |
| 19 | first_packet_received_ = false; |
| 20 | } |
| 21 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | void TimestampScaler::ToInternal(Packet* packet) { |
| 23 | if (!packet) { |
| 24 | return; |
| 25 | } |
| 26 | packet->header.timestamp = ToInternal(packet->header.timestamp, |
| 27 | packet->header.payloadType); |
| 28 | } |
| 29 | |
| 30 | void TimestampScaler::ToInternal(PacketList* packet_list) { |
| 31 | PacketList::iterator it; |
| 32 | for (it = packet_list->begin(); it != packet_list->end(); ++it) { |
| 33 | ToInternal(*it); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | uint32_t TimestampScaler::ToInternal(uint32_t external_timestamp, |
| 38 | uint8_t rtp_payload_type) { |
| 39 | const DecoderDatabase::DecoderInfo* info = |
| 40 | decoder_database_.GetDecoderInfo(rtp_payload_type); |
| 41 | if (!info) { |
| 42 | // Payload type is unknown. Do not scale. |
| 43 | return external_timestamp; |
| 44 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame^] | 45 | if (!(info->IsComfortNoise() || info->IsDtmf())) { |
| 46 | // Do not change the timestamp scaling settings for DTMF or CNG. |
| 47 | numerator_ = info->SampleRateHz(); |
| 48 | if (info->codec_type == NetEqDecoder::kDecoderArbitrary) { |
| 49 | // We have no format mapping for "arbitrary" external codecs, so we cannot |
| 50 | // support timestamp scaling of them. |
| 51 | denominator_ = numerator_; |
| 52 | } else { |
| 53 | denominator_ = info->GetFormat().clockrate_hz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame^] | 56 | if (numerator_ != denominator_) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 57 | // We have a scale factor != 1. |
| 58 | if (!first_packet_received_) { |
| 59 | external_ref_ = external_timestamp; |
| 60 | internal_ref_ = external_timestamp; |
| 61 | first_packet_received_ = true; |
| 62 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame^] | 63 | const int64_t external_diff = int64_t{external_timestamp} - external_ref_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | assert(denominator_ > 0); // Should not be possible. |
| 65 | external_ref_ = external_timestamp; |
| 66 | internal_ref_ += (external_diff * numerator_) / denominator_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 67 | return internal_ref_; |
| 68 | } else { |
| 69 | // No scaling. |
| 70 | return external_timestamp; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const { |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame^] | 76 | if (!first_packet_received_ || (numerator_ == denominator_)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | // Not initialized, or scale factor is 1. |
| 78 | return internal_timestamp; |
| 79 | } else { |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame^] | 80 | const int64_t internal_diff = int64_t{internal_timestamp} - internal_ref_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 81 | assert(numerator_ > 0); // Should not be possible. |
| 82 | // Do not update references in this method. |
| 83 | // Switch |denominator_| and |numerator_| to convert the other way. |
| 84 | return external_ref_ + (internal_diff * denominator_) / numerator_; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | } // namespace webrtc |