-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
packet_writer.h
207 lines (157 loc) · 5.7 KB
/
packet_writer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2015 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef ROC_PIPELINE_TEST_HELPERS_PACKET_WRITER_H_
#define ROC_PIPELINE_TEST_HELPERS_PACKET_WRITER_H_
#include <CppUTest/TestHarness.h>
#include "roc_packet/units.h"
#include "test_helpers/utils.h"
#include "roc_audio/iframe_encoder.h"
#include "roc_core/buffer_factory.h"
#include "roc_core/noncopyable.h"
#include "roc_core/scoped_ptr.h"
#include "roc_packet/icomposer.h"
#include "roc_packet/iwriter.h"
#include "roc_packet/packet_factory.h"
#include "roc_rtp/format_map.h"
namespace roc {
namespace pipeline {
namespace test {
class PacketWriter : public core::NonCopyable<> {
public:
PacketWriter(core::IArena& arena,
packet::IWriter& writer,
packet::IComposer& composer,
rtp::FormatMap& format_map,
packet::PacketFactory& packet_factory,
core::BufferFactory<uint8_t>& buffer_factory,
rtp::PayloadType pt,
const address::SocketAddr& src_addr,
const address::SocketAddr& dst_addr)
: writer_(writer)
, composer_(composer)
, payload_encoder_(new_encoder_(arena, format_map, pt), arena)
, packet_factory_(packet_factory)
, buffer_factory_(buffer_factory)
, src_addr_(src_addr)
, dst_addr_(dst_addr)
, source_(0)
, seqnum_(0)
, timestamp_(0)
, pt_(pt)
, offset_(0)
, corrupt_(false) {
}
void write_packets(size_t num_packets,
size_t samples_per_packet,
const audio::SampleSpec& sample_spec) {
CHECK(num_packets > 0);
for (size_t np = 0; np < num_packets; np++) {
writer_.write(new_packet_(samples_per_packet, sample_spec));
}
}
void shift_to(size_t num_packets, size_t samples_per_packet) {
seqnum_ = packet::seqnum_t(num_packets);
timestamp_ = packet::stream_timestamp_t(num_packets * samples_per_packet);
offset_ = uint8_t(num_packets * samples_per_packet);
}
uint8_t offset() const {
return offset_;
}
void set_offset(size_t offset) {
offset_ = uint8_t(offset);
}
packet::stream_source_t source() const {
return source_;
}
void set_source(packet::stream_source_t source) {
source_ = source;
}
packet::seqnum_t seqnum() const {
return seqnum_;
}
void set_seqnum(packet::seqnum_t seqnum) {
seqnum_ = seqnum;
}
packet::stream_timestamp_t timestamp() const {
return timestamp_;
}
void set_timestamp(packet::stream_timestamp_t timestamp) {
timestamp_ = timestamp;
}
void set_corrupt(bool corrupt) {
corrupt_ = corrupt;
}
private:
enum { MaxSamples = 4096 };
static audio::IFrameEncoder*
new_encoder_(core::IArena& arena, rtp::FormatMap& format_map, rtp::PayloadType pt) {
const rtp::Format* fmt = format_map.find_by_pt(pt);
CHECK(fmt);
return fmt->new_encoder(arena, fmt->pcm_format, fmt->sample_spec);
}
packet::PacketPtr new_packet_(size_t samples_per_packet,
const audio::SampleSpec& sample_spec) {
packet::PacketPtr pp = packet_factory_.new_packet();
CHECK(pp);
pp->add_flags(packet::Packet::FlagUDP);
pp->udp()->src_addr = src_addr_;
pp->udp()->dst_addr = dst_addr_;
pp->set_data(new_buffer_(samples_per_packet, sample_spec));
if (corrupt_) {
pp->data().data()[0] = 0;
}
return pp;
}
core::Slice<uint8_t> new_buffer_(size_t samples_per_packet,
const audio::SampleSpec& sample_spec) {
CHECK(samples_per_packet * sample_spec.num_channels() < MaxSamples);
packet::PacketPtr pp = packet_factory_.new_packet();
CHECK(pp);
core::Slice<uint8_t> bp = buffer_factory_.new_buffer();
CHECK(bp);
CHECK(composer_.prepare(
*pp, bp, payload_encoder_->encoded_byte_count(samples_per_packet)));
pp->set_data(bp);
pp->rtp()->source = source_;
pp->rtp()->seqnum = seqnum_;
pp->rtp()->stream_timestamp = timestamp_;
pp->rtp()->payload_type = pt_;
seqnum_++;
timestamp_ += samples_per_packet;
audio::sample_t samples[MaxSamples];
for (size_t ns = 0; ns < samples_per_packet; ns++) {
for (size_t nc = 0; nc < sample_spec.num_channels(); nc++) {
samples[ns * sample_spec.num_channels() + nc] = nth_sample(offset_);
}
offset_++;
}
payload_encoder_->begin(pp->rtp()->payload.data(), pp->rtp()->payload.size());
UNSIGNED_LONGS_EQUAL(samples_per_packet,
payload_encoder_->write(samples, samples_per_packet));
payload_encoder_->end();
CHECK(composer_.compose(*pp));
return pp->data();
}
packet::IWriter& writer_;
packet::IComposer& composer_;
core::ScopedPtr<audio::IFrameEncoder> payload_encoder_;
packet::PacketFactory& packet_factory_;
core::BufferFactory<uint8_t>& buffer_factory_;
address::SocketAddr src_addr_;
address::SocketAddr dst_addr_;
packet::stream_source_t source_;
packet::seqnum_t seqnum_;
packet::stream_timestamp_t timestamp_;
rtp::PayloadType pt_;
uint8_t offset_;
bool corrupt_;
};
} // namespace test
} // namespace pipeline
} // namespace roc
#endif // ROC_PIPELINE_TEST_HELPERS_PACKET_WRITER_H_