blob: 3b67a380b32517d09c5f530deb6d8908423077a9 [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"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014
15namespace webrtc {
16
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020017void TimestampScaler::Reset() {
18 first_packet_received_ = false;
19}
20
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021void TimestampScaler::ToInternal(Packet* packet) {
22 if (!packet) {
23 return;
24 }
ossu7a377612016-10-18 04:06:13 -070025 packet->timestamp = ToInternal(packet->timestamp, packet->payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026}
27
28void TimestampScaler::ToInternal(PacketList* packet_list) {
29 PacketList::iterator it;
30 for (it = packet_list->begin(); it != packet_list->end(); ++it) {
ossua73f6c92016-10-24 08:25:28 -070031 ToInternal(&(*it));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 }
33}
34
35uint32_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 }
ossu09f15602016-08-29 03:59:05 -070043 if (!(info->IsComfortNoise() || info->IsDtmf())) {
44 // Do not change the timestamp scaling settings for DTMF or CNG.
45 numerator_ = info->SampleRateHz();
ossuf1b08da2016-09-23 02:19:43 -070046 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.
ossu09f15602016-08-29 03:59:05 -070049 denominator_ = numerator_;
50 } else {
ossuf1b08da2016-09-23 02:19:43 -070051 denominator_ = info->GetFormat().clockrate_hz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052 }
53 }
ossu09f15602016-08-29 03:59:05 -070054 if (numerator_ != denominator_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055 // 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 }
ossu09f15602016-08-29 03:59:05 -070061 const int64_t external_diff = int64_t{external_timestamp} - external_ref_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062 assert(denominator_ > 0); // Should not be possible.
63 external_ref_ = external_timestamp;
64 internal_ref_ += (external_diff * numerator_) / denominator_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 return internal_ref_;
66 } else {
67 // No scaling.
68 return external_timestamp;
69 }
70}
71
72
73uint32_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_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079 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