blob: b228e017b2a30ffbcd70364d9d48d97e2b8d29f6 [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 Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/logging.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
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) {
kwibergee1879c2015-10-29 06:20:28 -070047 case NetEqDecoder::kDecoderG722:
48 case NetEqDecoder::kDecoderG722_2ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 // Use timestamp scaling with factor 2 (two output samples per RTP
50 // timestamp).
51 numerator_ = 2;
52 denominator_ = 1;
53 break;
54 }
kwibergee1879c2015-10-29 06:20:28 -070055 case NetEqDecoder::kDecoderAVT:
56 case NetEqDecoder::kDecoderCNGnb:
57 case NetEqDecoder::kDecoderCNGwb:
Tina le Grand325b3452015-12-08 10:13:00 +010058 case NetEqDecoder::kDecoderCNGswb32kHz:
59 case NetEqDecoder::kDecoderCNGswb48kHz: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060 // Do not change the timestamp scaling settings for DTMF or CNG.
61 break;
62 }
63 default: {
64 // Do not use timestamp scaling for any other codec.
65 numerator_ = 1;
66 denominator_ = 1;
67 break;
68 }
69 }
70
71 if (!(numerator_ == 1 && denominator_ == 1)) {
72 // We have a scale factor != 1.
73 if (!first_packet_received_) {
74 external_ref_ = external_timestamp;
75 internal_ref_ = external_timestamp;
76 first_packet_received_ = true;
77 }
henrik.lundin07c51e32016-02-11 03:35:43 -080078 int64_t external_diff = external_timestamp - external_ref_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079 assert(denominator_ > 0); // Should not be possible.
80 external_ref_ = external_timestamp;
81 internal_ref_ += (external_diff * numerator_) / denominator_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 return internal_ref_;
83 } else {
84 // No scaling.
85 return external_timestamp;
86 }
87}
88
89
90uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const {
91 if (!first_packet_received_ || (numerator_ == 1 && denominator_ == 1)) {
92 // Not initialized, or scale factor is 1.
93 return internal_timestamp;
94 } else {
henrik.lundin07c51e32016-02-11 03:35:43 -080095 int64_t internal_diff = internal_timestamp - internal_ref_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000096 assert(numerator_ > 0); // Should not be possible.
97 // Do not update references in this method.
98 // Switch |denominator_| and |numerator_| to convert the other way.
99 return external_ref_ + (internal_diff * denominator_) / numerator_;
100 }
101}
102
103} // namespace webrtc