blob: 8bc0f2d0fcf227da22f59e1798dadde472688490 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000013#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
14#include "webrtc/modules/audio_coding/neteq/defines.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015#include "webrtc/system_wrappers/interface/logging.h"
16
17namespace webrtc {
18
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020019void TimestampScaler::Reset() {
20 first_packet_received_ = false;
21}
22
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023void TimestampScaler::ToInternal(Packet* packet) {
24 if (!packet) {
25 return;
26 }
27 packet->header.timestamp = ToInternal(packet->header.timestamp,
28 packet->header.payloadType);
29}
30
31void TimestampScaler::ToInternal(PacketList* packet_list) {
32 PacketList::iterator it;
33 for (it = packet_list->begin(); it != packet_list->end(); ++it) {
34 ToInternal(*it);
35 }
36}
37
38uint32_t TimestampScaler::ToInternal(uint32_t external_timestamp,
39 uint8_t rtp_payload_type) {
40 const DecoderDatabase::DecoderInfo* info =
41 decoder_database_.GetDecoderInfo(rtp_payload_type);
42 if (!info) {
43 // Payload type is unknown. Do not scale.
44 return external_timestamp;
45 }
46 switch (info->codec_type) {
47 case kDecoderG722:
48 case kDecoderG722_2ch: {
49 // Use timestamp scaling with factor 2 (two output samples per RTP
50 // timestamp).
51 numerator_ = 2;
52 denominator_ = 1;
53 break;
54 }
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +000055 case kDecoderISACfb:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 case kDecoderCNGswb48kHz: {
57 // Use timestamp scaling with factor 2/3 (32 kHz sample rate, but RTP
58 // timestamps run on 48 kHz).
59 // TODO(tlegrand): Remove scaling for kDecoderCNGswb48kHz once ACM has
minyue@webrtc.org7549ff42014-04-02 15:03:01 +000060 // full 48 kHz support.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 numerator_ = 2;
62 denominator_ = 3;
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +000063 break;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064 }
65 case kDecoderAVT:
66 case kDecoderCNGnb:
67 case kDecoderCNGwb:
68 case kDecoderCNGswb32kHz: {
69 // Do not change the timestamp scaling settings for DTMF or CNG.
70 break;
71 }
72 default: {
73 // Do not use timestamp scaling for any other codec.
74 numerator_ = 1;
75 denominator_ = 1;
76 break;
77 }
78 }
79
80 if (!(numerator_ == 1 && denominator_ == 1)) {
81 // We have a scale factor != 1.
82 if (!first_packet_received_) {
83 external_ref_ = external_timestamp;
84 internal_ref_ = external_timestamp;
85 first_packet_received_ = true;
86 }
87 int32_t external_diff = external_timestamp - external_ref_;
88 assert(denominator_ > 0); // Should not be possible.
89 external_ref_ = external_timestamp;
90 internal_ref_ += (external_diff * numerator_) / denominator_;
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +000091 LOG(LS_VERBOSE) << "Converting timestamp: " << external_timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092 " -> " << internal_ref_;
93 return internal_ref_;
94 } else {
95 // No scaling.
96 return external_timestamp;
97 }
98}
99
100
101uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const {
102 if (!first_packet_received_ || (numerator_ == 1 && denominator_ == 1)) {
103 // Not initialized, or scale factor is 1.
104 return internal_timestamp;
105 } else {
106 int32_t internal_diff = internal_timestamp - internal_ref_;
107 assert(numerator_ > 0); // Should not be possible.
108 // Do not update references in this method.
109 // Switch |denominator_| and |numerator_| to convert the other way.
110 return external_ref_ + (internal_diff * denominator_) / numerator_;
111 }
112}
113
114} // namespace webrtc