blob: 950c09cdd9917e5b68a41f64861b1d6e2ce7cb98 [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.orgd94659d2013-01-29 12:09:21 +000055 case kDecoderCNGswb48kHz: {
56 // Use timestamp scaling with factor 2/3 (32 kHz sample rate, but RTP
57 // timestamps run on 48 kHz).
58 // TODO(tlegrand): Remove scaling for kDecoderCNGswb48kHz once ACM has
minyue@webrtc.org7549ff42014-04-02 15:03:01 +000059 // full 48 kHz support.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060 numerator_ = 2;
61 denominator_ = 3;
kjellander@webrtc.org7d2b6a92015-01-28 18:37:58 +000062 break;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063 }
64 case kDecoderAVT:
65 case kDecoderCNGnb:
66 case kDecoderCNGwb:
67 case kDecoderCNGswb32kHz: {
68 // Do not change the timestamp scaling settings for DTMF or CNG.
69 break;
70 }
71 default: {
72 // Do not use timestamp scaling for any other codec.
73 numerator_ = 1;
74 denominator_ = 1;
75 break;
76 }
77 }
78
79 if (!(numerator_ == 1 && denominator_ == 1)) {
80 // We have a scale factor != 1.
81 if (!first_packet_received_) {
82 external_ref_ = external_timestamp;
83 internal_ref_ = external_timestamp;
84 first_packet_received_ = true;
85 }
86 int32_t external_diff = external_timestamp - external_ref_;
87 assert(denominator_ > 0); // Should not be possible.
88 external_ref_ = external_timestamp;
89 internal_ref_ += (external_diff * numerator_) / denominator_;
turaj@webrtc.org0c0fae82013-09-25 17:42:17 +000090 LOG(LS_VERBOSE) << "Converting timestamp: " << external_timestamp <<
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 " -> " << internal_ref_;
92 return internal_ref_;
93 } else {
94 // No scaling.
95 return external_timestamp;
96 }
97}
98
99
100uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const {
101 if (!first_packet_received_ || (numerator_ == 1 && denominator_ == 1)) {
102 // Not initialized, or scale factor is 1.
103 return internal_timestamp;
104 } else {
105 int32_t internal_diff = internal_timestamp - internal_ref_;
106 assert(numerator_ > 0); // Should not be possible.
107 // Do not update references in this method.
108 // Switch |denominator_| and |numerator_| to convert the other way.
109 return external_ref_ + (internal_diff * denominator_) / numerator_;
110 }
111}
112
113} // namespace webrtc