Line data Source code
1 : /*
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/neteq/timestamp_scaler.h"
12 :
13 : #include "webrtc/modules/audio_coding/neteq/decoder_database.h"
14 : #include "webrtc/system_wrappers/include/logging.h"
15 :
16 : namespace webrtc {
17 :
18 0 : void TimestampScaler::Reset() {
19 0 : first_packet_received_ = false;
20 0 : }
21 :
22 0 : void TimestampScaler::ToInternal(Packet* packet) {
23 0 : if (!packet) {
24 0 : return;
25 : }
26 0 : packet->timestamp = ToInternal(packet->timestamp, packet->payload_type);
27 : }
28 :
29 0 : void TimestampScaler::ToInternal(PacketList* packet_list) {
30 0 : PacketList::iterator it;
31 0 : for (it = packet_list->begin(); it != packet_list->end(); ++it) {
32 0 : ToInternal(&(*it));
33 : }
34 0 : }
35 :
36 0 : uint32_t TimestampScaler::ToInternal(uint32_t external_timestamp,
37 : uint8_t rtp_payload_type) {
38 : const DecoderDatabase::DecoderInfo* info =
39 0 : decoder_database_.GetDecoderInfo(rtp_payload_type);
40 0 : if (!info) {
41 : // Payload type is unknown. Do not scale.
42 0 : return external_timestamp;
43 : }
44 0 : if (!(info->IsComfortNoise() || info->IsDtmf())) {
45 : // Do not change the timestamp scaling settings for DTMF or CNG.
46 0 : numerator_ = info->SampleRateHz();
47 0 : if (info->GetFormat().clockrate_hz == 0) {
48 : // If the clockrate is invalid (i.e. with an old-style external codec)
49 : // we cannot do any timestamp scaling.
50 0 : denominator_ = numerator_;
51 : } else {
52 0 : denominator_ = info->GetFormat().clockrate_hz;
53 : }
54 : }
55 0 : if (numerator_ != denominator_) {
56 : // We have a scale factor != 1.
57 0 : if (!first_packet_received_) {
58 0 : external_ref_ = external_timestamp;
59 0 : internal_ref_ = external_timestamp;
60 0 : first_packet_received_ = true;
61 : }
62 0 : const int64_t external_diff = int64_t{external_timestamp} - external_ref_;
63 0 : assert(denominator_ > 0); // Should not be possible.
64 0 : external_ref_ = external_timestamp;
65 0 : internal_ref_ += (external_diff * numerator_) / denominator_;
66 0 : return internal_ref_;
67 : } else {
68 : // No scaling.
69 0 : return external_timestamp;
70 : }
71 : }
72 :
73 :
74 0 : uint32_t TimestampScaler::ToExternal(uint32_t internal_timestamp) const {
75 0 : if (!first_packet_received_ || (numerator_ == denominator_)) {
76 : // Not initialized, or scale factor is 1.
77 0 : return internal_timestamp;
78 : } else {
79 0 : const int64_t internal_diff = int64_t{internal_timestamp} - internal_ref_;
80 0 : assert(numerator_ > 0); // Should not be possible.
81 : // Do not update references in this method.
82 : // Switch |denominator_| and |numerator_| to convert the other way.
83 0 : return external_ref_ + (internal_diff * denominator_) / numerator_;
84 : }
85 : }
86 :
87 : } // namespace webrtc
|