Skip to content

Commit

Permalink
Add four CTS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Nov 27, 2023
1 parent 5a8c945 commit aaecdad
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 42 deletions.
101 changes: 76 additions & 25 deletions src/tests/roc_audio/test_depacketizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "roc_core/time.h"
#include "roc_packet/packet_factory.h"
#include "roc_packet/queue.h"
#include "roc_packet/units.h"
#include "roc_rtp/composer.h"
#include "roc_status/status_code.h"

Expand Down Expand Up @@ -679,6 +680,81 @@ TEST(depacketizer, timestamp) {
}
}

TEST(depacketizer, timestamp_fract_frame_per_packet) {
enum {
StartTimestamp = 1000,
NumPackets = 3,
SamplesPerFrame = SamplesPerPacket + 50
};

PcmEncoder encoder(PcmFmt, SampleSpecs);
PcmDecoder decoder(PcmFmt, SampleSpecs);

packet::Queue queue;
Depacketizer dp(queue, decoder, SampleSpecs, false);
CHECK(dp.is_valid());

core::nanoseconds_t capt_ts =
Now + SampleSpecs.samples_overall_2_ns(SamplesPerPacket);
// 1st packet in the frame has 0 capture ts, and the next
CHECK_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, StartTimestamp, 0.1f, 0)));
CHECK_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, StartTimestamp + SamplesPerPacket / NumCh,
0.1f, capt_ts)));
expect_output(dp, SamplesPerFrame, 0.1f, Now);
}

TEST(depacketizer, timestamp_small_non_zero_cts) {
enum {
StartTimestamp = 1000,
StartCts = 5, // very close to unix epoch
PacketsPerFrame = 10
};

PcmEncoder encoder(PcmFmt, SampleSpecs);
PcmDecoder decoder(PcmFmt, SampleSpecs);

packet::Queue queue;
Depacketizer dp(queue, decoder, SampleSpecs, false);
CHECK(dp.is_valid());

// 1st packet in frame has 0 capture ts
packet::stream_timestamp_t stream_ts = StartTimestamp;
LONGS_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, StartTimestamp, 0.1f, 0)));
stream_ts += SamplesPerPacket;

// starting from 2nd packet, there is CTS, but it starts from very
// small value (close to unix epoch)
core::nanoseconds_t capt_ts = StartCts;
for (size_t n = 1; n < PacketsPerFrame; n++) {
LONGS_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, stream_ts, 0.1f, capt_ts)));
stream_ts += SamplesPerPacket;
capt_ts += SampleSpecs.samples_overall_2_ns(SamplesPerPacket);
}

// remember cts that should be used for second frame
const core::nanoseconds_t second_frame_capt_ts = capt_ts;

// second frame
for (size_t n = 0; n < PacketsPerFrame; n++) {
LONGS_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, stream_ts, 0.2f, capt_ts)));
stream_ts += SamplesPerPacket;
capt_ts += SampleSpecs.samples_overall_2_ns(SamplesPerPacket);
}

// first frame has zero cts
// if depacketizer couldn't handle small cts properly, it would
// produce negative cts instead
expect_output(dp, SamplesPerPacket * PacketsPerFrame, 0.1f, 0);

// second frame has non-zero cts
expect_output(dp, SamplesPerPacket * PacketsPerFrame, 0.2f, second_frame_capt_ts);
}

TEST(depacketizer, read_after_error) {
const status::StatusCode codes[] = {
status::StatusUnknown,
Expand Down Expand Up @@ -711,30 +787,5 @@ TEST(depacketizer, read_after_error) {
}
}

TEST(depacketizer, timestamp_fract_frame_per_packet) {
enum {
StartTimestamp = 1000,
NumPackets = 3,
SamplesPerFrame = SamplesPerPacket + 50
};

PcmEncoder encoder(PcmFmt, SampleSpecs);
PcmDecoder decoder(PcmFmt, SampleSpecs);

packet::Queue queue;
Depacketizer dp(queue, decoder, SampleSpecs, false);
CHECK(dp.is_valid());

core::nanoseconds_t capt_ts =
Now + SampleSpecs.samples_overall_2_ns(SamplesPerPacket);
// 1st packet in the frame has 0 capture ts, and the next
CHECK_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, StartTimestamp, 0.1f, 0)));
CHECK_EQUAL(status::StatusOK,
queue.write(new_packet(encoder, StartTimestamp + SamplesPerPacket / NumCh,
0.1f, capt_ts)));
expect_output(dp, SamplesPerFrame, 0.1f, Now);
}

} // namespace audio
} // namespace roc
40 changes: 38 additions & 2 deletions src/tests/roc_audio/test_packetizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "roc_audio/pcm_encoder.h"
#include "roc_core/buffer_factory.h"
#include "roc_core/heap_arena.h"
#include "roc_core/time.h"
#include "roc_packet/packet_factory.h"
#include "roc_packet/queue.h"
#include "roc_rtp/composer.h"
Expand Down Expand Up @@ -88,7 +89,9 @@ class PacketChecker {
}
CHECK(core::ns_equal_delta(pp->rtp()->capture_timestamp, capture_ts_,
core::Microsecond));
capture_ts_ += SampleSpecs.samples_per_chan_2_ns(n_samples);
if (capture_ts_) {
capture_ts_ += SampleSpecs.samples_per_chan_2_ns(n_samples);
}
UNSIGNED_LONGS_EQUAL(n_samples, pp->rtp()->duration);
UNSIGNED_LONGS_EQUAL(PayloadType, pp->rtp()->payload_type);

Expand Down Expand Up @@ -154,7 +157,9 @@ class FrameMaker {

Frame frame(buf.data(), buf.size());
frame.set_capture_timestamp(capture_ts_);
capture_ts_ += SampleSpecs.samples_per_chan_2_ns(num_samples);
if (capture_ts_) {
capture_ts_ += SampleSpecs.samples_per_chan_2_ns(num_samples);
}
writer.write(frame);
}

Expand Down Expand Up @@ -305,5 +310,36 @@ TEST(packetizer, flush) {
}
}

TEST(packetizer, timestamp_zero_cts) {
enum {
NumFrames = 10,
NumSamples = (SamplesPerPacket - 1),
NumPackets = (NumSamples * NumFrames / SamplesPerPacket)
};

PcmEncoder encoder(PcmFmt, SampleSpecs);
PcmDecoder decoder(PcmFmt, SampleSpecs);

packet::Queue packet_queue;

Packetizer packetizer(packet_queue, rtp_composer, encoder, packet_factory,
byte_buffer_factory, PacketDuration, SampleSpecs, PayloadType);

const core::nanoseconds_t zero_cts = 0;

FrameMaker frame_maker(zero_cts);
PacketChecker packet_checker(decoder, zero_cts);

for (size_t fn = 0; fn < NumFrames; fn++) {
frame_maker.write(packetizer, NumSamples);
}

for (size_t pn = 0; pn < NumPackets; pn++) {
packet_checker.read(packet_queue, SamplesPerPacket);
}

UNSIGNED_LONGS_EQUAL(0, packet_queue.size());
}

} // namespace audio
} // namespace roc
Loading

0 comments on commit aaecdad

Please sign in to comment.