henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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/tools/constant_pcm_packet_source.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | #include "webrtc/base/checks.h" |
kjellander@webrtc.org | 3c652b6 | 2015-11-18 22:07:57 | [diff] [blame] | 16 | #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 17 | #include "webrtc/modules/audio_coding/neteq/tools/packet.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | ConstantPcmPacketSource::ConstantPcmPacketSource(size_t payload_len_samples, |
| 23 | int16_t sample_value, |
| 24 | int sample_rate_hz, |
| 25 | int payload_type) |
| 26 | : payload_len_samples_(payload_len_samples), |
| 27 | packet_len_bytes_(2 * payload_len_samples_ + kHeaderLenBytes), |
| 28 | samples_per_ms_(sample_rate_hz / 1000), |
| 29 | next_arrival_time_ms_(0.0), |
| 30 | payload_type_(payload_type), |
| 31 | seq_number_(0), |
| 32 | timestamp_(0), |
| 33 | payload_ssrc_(0xABCD1234) { |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 34 | size_t encoded_len = WebRtcPcm16b_Encode(&sample_value, 1, encoded_sample_); |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 35 | RTC_CHECK_EQ(2U, encoded_len); |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 36 | } |
| 37 | |
henrik.lundin | 46ba49c | 2016-05-25 05:50:47 | [diff] [blame] | 38 | std::unique_ptr<Packet> ConstantPcmPacketSource::NextPacket() { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 39 | RTC_CHECK_GT(packet_len_bytes_, kHeaderLenBytes); |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 40 | uint8_t* packet_memory = new uint8_t[packet_len_bytes_]; |
| 41 | // Fill the payload part of the packet memory with the pre-encoded value. |
kwiberg@webrtc.org | 648f5d6 | 2015-02-10 09:18:28 | [diff] [blame] | 42 | for (unsigned i = 0; i < 2 * payload_len_samples_; ++i) |
| 43 | packet_memory[kHeaderLenBytes + i] = encoded_sample_[i % 2]; |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 44 | WriteHeader(packet_memory); |
| 45 | // |packet| assumes ownership of |packet_memory|. |
henrik.lundin | 46ba49c | 2016-05-25 05:50:47 | [diff] [blame] | 46 | std::unique_ptr<Packet> packet( |
| 47 | new Packet(packet_memory, packet_len_bytes_, next_arrival_time_ms_)); |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 48 | next_arrival_time_ms_ += payload_len_samples_ / samples_per_ms_; |
| 49 | return packet; |
| 50 | } |
| 51 | |
| 52 | void ConstantPcmPacketSource::WriteHeader(uint8_t* packet_memory) { |
| 53 | packet_memory[0] = 0x80; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 | [diff] [blame] | 54 | packet_memory[1] = static_cast<uint8_t>(payload_type_); |
| 55 | packet_memory[2] = seq_number_ >> 8; |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 56 | packet_memory[3] = seq_number_ & 0xFF; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 | [diff] [blame] | 57 | packet_memory[4] = timestamp_ >> 24; |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 58 | packet_memory[5] = (timestamp_ >> 16) & 0xFF; |
| 59 | packet_memory[6] = (timestamp_ >> 8) & 0xFF; |
| 60 | packet_memory[7] = timestamp_ & 0xFF; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 | [diff] [blame] | 61 | packet_memory[8] = payload_ssrc_ >> 24; |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 | [diff] [blame] | 62 | packet_memory[9] = (payload_ssrc_ >> 16) & 0xFF; |
| 63 | packet_memory[10] = (payload_ssrc_ >> 8) & 0xFF; |
| 64 | packet_memory[11] = payload_ssrc_ & 0xFF; |
| 65 | ++seq_number_; |
| 66 | timestamp_ += static_cast<uint32_t>(payload_len_samples_); |
| 67 | } |
| 68 | |
| 69 | } // namespace test |
| 70 | } // namespace webrtc |