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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_coding/neteq/timestamp_scaler.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 13 | #include "modules/audio_coding/neteq/decoder_database.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 17 | void TimestampScaler::Reset() { |
| 18 | first_packet_received_ = false; |
| 19 | } |
| 20 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 21 | void TimestampScaler::ToInternal(Packet* packet) { |
| 22 | if (!packet) { |
| 23 | return; |
| 24 | } |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 25 | packet->timestamp = ToInternal(packet->timestamp, packet->payload_type); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void TimestampScaler::ToInternal(PacketList* packet_list) { |
| 29 | PacketList::iterator it; |
| 30 | for (it = packet_list->begin(); it != packet_list->end(); ++it) { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 31 | ToInternal(&(*it)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | uint32_t TimestampScaler::ToInternal(uint32_t external_timestamp, |
| 36 | uint8_t rtp_payload_type) { |
| 37 | const DecoderDatabase::DecoderInfo* info = |
| 38 | decoder_database_.GetDecoderInfo(rtp_payload_type); |
| 39 | if (!info) { |
| 40 | // Payload type is unknown. Do not scale. |
| 41 | return external_timestamp; |
| 42 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 43 | if (!(info->IsComfortNoise() || info->IsDtmf())) { |
| 44 | // Do not change the timestamp scaling settings for DTMF or CNG. |
| 45 | numerator_ = info->SampleRateHz(); |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 46 | if (info->GetFormat().clockrate_hz == 0) { |
| 47 | // If the clockrate is invalid (i.e. with an old-style external codec) |
| 48 | // we cannot do any timestamp scaling. |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 49 | denominator_ = numerator_; |
| 50 | } else { |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 51 | denominator_ = info->GetFormat().clockrate_hz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 54 | if (numerator_ != denominator_) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 55 | // We have a scale factor != 1. |
| 56 | if (!first_packet_received_) { |
| 57 | external_ref_ = external_timestamp; |
| 58 | internal_ref_ = external_timestamp; |
| 59 | first_packet_received_ = true; |
| 60 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 61 | 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] | 62 | assert(denominator_ > 0); // Should not be possible. |
| 63 | external_ref_ = external_timestamp; |
| 64 | internal_ref_ += (external_diff * numerator_) / denominator_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 65 | return internal_ref_; |
| 66 | } else { |
| 67 | // No scaling. |
| 68 | return external_timestamp; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const { |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 74 | if (!first_packet_received_ || (numerator_ == denominator_)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | // Not initialized, or scale factor is 1. |
| 76 | return internal_timestamp; |
| 77 | } else { |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 78 | 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] | 79 | assert(numerator_ > 0); // Should not be possible. |
| 80 | // Do not update references in this method. |
| 81 | // Switch |denominator_| and |numerator_| to convert the other way. |
| 82 | return external_ref_ + (internal_diff * denominator_) / numerator_; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace webrtc |