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