OLD | NEW |
| (Empty) |
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/main/acm2/acm_send_test.h" | |
12 | |
13 #include <assert.h> | |
14 #include <stdio.h> | |
15 #include <string.h> | |
16 | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "webrtc/base/checks.h" | |
19 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h" | |
20 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h" | |
21 #include "webrtc/modules/audio_coding/neteq/tools/packet.h" | |
22 | |
23 namespace webrtc { | |
24 namespace test { | |
25 | |
26 AcmSendTest::AcmSendTest(InputAudioFile* audio_source, | |
27 int source_rate_hz, | |
28 int test_duration_ms) | |
29 : clock_(0), | |
30 audio_source_(audio_source), | |
31 source_rate_hz_(source_rate_hz), | |
32 input_block_size_samples_( | |
33 static_cast<size_t>(source_rate_hz_ * kBlockSizeMs / 1000)), | |
34 codec_registered_(false), | |
35 test_duration_ms_(test_duration_ms), | |
36 frame_type_(kAudioFrameSpeech), | |
37 payload_type_(0), | |
38 timestamp_(0), | |
39 sequence_number_(0) { | |
40 webrtc::AudioCoding::Config config; | |
41 config.clock = &clock_; | |
42 config.transport = this; | |
43 acm_.reset(webrtc::AudioCoding::Create(config)); | |
44 input_frame_.sample_rate_hz_ = source_rate_hz_; | |
45 input_frame_.num_channels_ = 1; | |
46 input_frame_.samples_per_channel_ = input_block_size_samples_; | |
47 assert(input_block_size_samples_ * input_frame_.num_channels_ <= | |
48 AudioFrame::kMaxDataSizeSamples); | |
49 } | |
50 | |
51 bool AcmSendTest::RegisterCodec(int codec_type, | |
52 int channels, | |
53 int payload_type, | |
54 int frame_size_samples) { | |
55 codec_registered_ = | |
56 acm_->RegisterSendCodec(codec_type, payload_type, frame_size_samples); | |
57 input_frame_.num_channels_ = channels; | |
58 assert(input_block_size_samples_ * input_frame_.num_channels_ <= | |
59 AudioFrame::kMaxDataSizeSamples); | |
60 return codec_registered_; | |
61 } | |
62 | |
63 Packet* AcmSendTest::NextPacket() { | |
64 assert(codec_registered_); | |
65 if (filter_.test(static_cast<size_t>(payload_type_))) { | |
66 // This payload type should be filtered out. Since the payload type is the | |
67 // same throughout the whole test run, no packet at all will be delivered. | |
68 // We can just as well signal that the test is over by returning NULL. | |
69 return NULL; | |
70 } | |
71 // Insert audio and process until one packet is produced. | |
72 while (clock_.TimeInMilliseconds() < test_duration_ms_) { | |
73 clock_.AdvanceTimeMilliseconds(kBlockSizeMs); | |
74 RTC_CHECK( | |
75 audio_source_->Read(input_block_size_samples_, input_frame_.data_)); | |
76 if (input_frame_.num_channels_ > 1) { | |
77 InputAudioFile::DuplicateInterleaved(input_frame_.data_, | |
78 input_block_size_samples_, | |
79 input_frame_.num_channels_, | |
80 input_frame_.data_); | |
81 } | |
82 int32_t encoded_bytes = acm_->Add10MsAudio(input_frame_); | |
83 EXPECT_GE(encoded_bytes, 0); | |
84 input_frame_.timestamp_ += static_cast<uint32_t>(input_block_size_samples_); | |
85 if (encoded_bytes > 0) { | |
86 // Encoded packet received. | |
87 return CreatePacket(); | |
88 } | |
89 } | |
90 // Test ended. | |
91 return NULL; | |
92 } | |
93 | |
94 // This method receives the callback from ACM when a new packet is produced. | |
95 int32_t AcmSendTest::SendData(FrameType frame_type, | |
96 uint8_t payload_type, | |
97 uint32_t timestamp, | |
98 const uint8_t* payload_data, | |
99 size_t payload_len_bytes, | |
100 const RTPFragmentationHeader* fragmentation) { | |
101 // Store the packet locally. | |
102 frame_type_ = frame_type; | |
103 payload_type_ = payload_type; | |
104 timestamp_ = timestamp; | |
105 last_payload_vec_.assign(payload_data, payload_data + payload_len_bytes); | |
106 assert(last_payload_vec_.size() == payload_len_bytes); | |
107 return 0; | |
108 } | |
109 | |
110 Packet* AcmSendTest::CreatePacket() { | |
111 const size_t kRtpHeaderSize = 12; | |
112 size_t allocated_bytes = last_payload_vec_.size() + kRtpHeaderSize; | |
113 uint8_t* packet_memory = new uint8_t[allocated_bytes]; | |
114 // Populate the header bytes. | |
115 packet_memory[0] = 0x80; | |
116 packet_memory[1] = static_cast<uint8_t>(payload_type_); | |
117 packet_memory[2] = (sequence_number_ >> 8) & 0xFF; | |
118 packet_memory[3] = (sequence_number_) & 0xFF; | |
119 packet_memory[4] = (timestamp_ >> 24) & 0xFF; | |
120 packet_memory[5] = (timestamp_ >> 16) & 0xFF; | |
121 packet_memory[6] = (timestamp_ >> 8) & 0xFF; | |
122 packet_memory[7] = timestamp_ & 0xFF; | |
123 // Set SSRC to 0x12345678. | |
124 packet_memory[8] = 0x12; | |
125 packet_memory[9] = 0x34; | |
126 packet_memory[10] = 0x56; | |
127 packet_memory[11] = 0x78; | |
128 | |
129 ++sequence_number_; | |
130 | |
131 // Copy the payload data. | |
132 memcpy(packet_memory + kRtpHeaderSize, | |
133 &last_payload_vec_[0], | |
134 last_payload_vec_.size()); | |
135 Packet* packet = | |
136 new Packet(packet_memory, allocated_bytes, clock_.TimeInMilliseconds()); | |
137 assert(packet); | |
138 assert(packet->valid_header()); | |
139 return packet; | |
140 } | |
141 | |
142 } // namespace test | |
143 } // namespace webrtc | |
OLD | NEW |