blob: 07d945edce0712d7f51e4ece41c9f9bad8e2bcba [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/timestamp_scaler.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/decoder_database.h"
Karl Wiberg31fbb542017-10-16 12:42:38 +020014#include "rtc_base/checks.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015
16namespace webrtc {
17
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020018void TimestampScaler::Reset() {
19 first_packet_received_ = false;
20}
21
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022void TimestampScaler::ToInternal(Packet* packet) {
23 if (!packet) {
24 return;
25 }
ossu7a377612016-10-18 04:06:13 -070026 packet->timestamp = ToInternal(packet->timestamp, packet->payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027}
28
29void TimestampScaler::ToInternal(PacketList* packet_list) {
30 PacketList::iterator it;
31 for (it = packet_list->begin(); it != packet_list->end(); ++it) {
ossua73f6c92016-10-24 08:25:28 -070032 ToInternal(&(*it));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033 }
34}
35
36uint32_t TimestampScaler::ToInternal(uint32_t external_timestamp,
37 uint8_t rtp_payload_type) {
38 const DecoderDatabase::DecoderInfo* info =
39 decoder_database_.GetDecoderInfo(rtp_payload_type);
40 if (!info) {
41 // Payload type is unknown. Do not scale.
42 return external_timestamp;
43 }
ossu09f15602016-08-29 03:59:05 -070044 if (!(info->IsComfortNoise() || info->IsDtmf())) {
45 // Do not change the timestamp scaling settings for DTMF or CNG.
46 numerator_ = info->SampleRateHz();
ossuf1b08da2016-09-23 02:19:43 -070047 if (info->GetFormat().clockrate_hz == 0) {
48 // If the clockrate is invalid (i.e. with an old-style external codec)
49 // we cannot do any timestamp scaling.
ossu09f15602016-08-29 03:59:05 -070050 denominator_ = numerator_;
51 } else {
ossuf1b08da2016-09-23 02:19:43 -070052 denominator_ = info->GetFormat().clockrate_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053 }
54 }
ossu09f15602016-08-29 03:59:05 -070055 if (numerator_ != denominator_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 // We have a scale factor != 1.
57 if (!first_packet_received_) {
58 external_ref_ = external_timestamp;
59 internal_ref_ = external_timestamp;
60 first_packet_received_ = true;
61 }
ossu09f15602016-08-29 03:59:05 -070062 const int64_t external_diff = int64_t{external_timestamp} - external_ref_;
Karl Wiberg31fbb542017-10-16 12:42:38 +020063 RTC_DCHECK_GT(denominator_, 0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064 external_ref_ = external_timestamp;
65 internal_ref_ += (external_diff * numerator_) / denominator_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066 return internal_ref_;
67 } else {
68 // No scaling.
69 return external_timestamp;
70 }
71}
72
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const {
ossu09f15602016-08-29 03:59:05 -070074 if (!first_packet_received_ || (numerator_ == denominator_)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 // Not initialized, or scale factor is 1.
76 return internal_timestamp;
77 } else {
ossu09f15602016-08-29 03:59:05 -070078 const int64_t internal_diff = int64_t{internal_timestamp} - internal_ref_;
Karl Wiberg31fbb542017-10-16 12:42:38 +020079 RTC_DCHECK_GT(numerator_, 0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 // 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