2022-03-04 08:18:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "uvgrtp/lib.hh"
|
|
|
|
|
2022-03-23 12:28:09 +00:00
|
|
|
class Test_receiver;
|
|
|
|
|
2022-03-04 08:18:14 +00:00
|
|
|
void wait_until_next_frame(std::chrono::steady_clock::time_point& start,
|
|
|
|
int frame_index, int packet_interval_ms);
|
|
|
|
|
2022-07-13 10:32:27 +00:00
|
|
|
inline std::unique_ptr<uint8_t[]> create_test_packet(rtp_format_t format, uint8_t nal_type,
|
|
|
|
bool add_start_code, size_t size, int rtp_flags);
|
2022-03-04 08:18:14 +00:00
|
|
|
|
2022-07-13 10:32:27 +00:00
|
|
|
inline void test_packet_size(std::unique_ptr<uint8_t[]> test_packet, int packets, size_t size,
|
|
|
|
uvgrtp::session* sess, uvgrtp::media_stream* sender, uvgrtp::media_stream* receiver, int rtp_flags);
|
|
|
|
|
|
|
|
inline void send_packets(std::unique_ptr<uint8_t[]> test_packet, size_t size,
|
|
|
|
uvgrtp::session* sess, uvgrtp::media_stream* sender,
|
|
|
|
int packets, int packet_interval_ms, bool print_progress, int rtp_flags);
|
|
|
|
|
|
|
|
inline void add_hook(Test_receiver* tester, uvgrtp::media_stream* receiver,
|
|
|
|
void (*hook)(void*, uvgrtp::frame::rtp_frame*));
|
2022-03-25 08:31:48 +00:00
|
|
|
|
2022-03-04 08:18:14 +00:00
|
|
|
inline void cleanup_sess(uvgrtp::context& ctx, uvgrtp::session* sess);
|
|
|
|
inline void cleanup_ms(uvgrtp::session* sess, uvgrtp::media_stream* ms);
|
|
|
|
|
2022-03-25 08:31:48 +00:00
|
|
|
inline void process_rtp_frame(uvgrtp::frame::rtp_frame* frame);
|
|
|
|
inline void rtp_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
|
|
|
|
|
2022-03-04 08:18:14 +00:00
|
|
|
class Test_receiver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Test_receiver(int expectedPackets) :
|
|
|
|
receivedPackets_(0),
|
|
|
|
expectedPackets_(expectedPackets)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void receive()
|
|
|
|
{
|
|
|
|
++receivedPackets_;
|
|
|
|
}
|
|
|
|
|
2022-03-23 12:28:09 +00:00
|
|
|
void gotAll()
|
2022-03-04 08:18:14 +00:00
|
|
|
{
|
2022-03-23 12:28:09 +00:00
|
|
|
EXPECT_EQ(receivedPackets_, expectedPackets_);
|
2022-03-04 08:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
int receivedPackets_;
|
|
|
|
int expectedPackets_;
|
|
|
|
};
|
|
|
|
|
2022-07-13 10:32:27 +00:00
|
|
|
inline std::unique_ptr<uint8_t[]> create_test_packet(rtp_format_t format, uint8_t nal_type,
|
|
|
|
bool add_start_code, size_t size, int rtp_flags)
|
|
|
|
{
|
|
|
|
std::unique_ptr<uint8_t[]> test_frame = std::unique_ptr<uint8_t[]>(new uint8_t[size]);
|
|
|
|
memset(test_frame.get(), 'b', size);
|
|
|
|
|
|
|
|
if (add_start_code && size > 8)
|
|
|
|
{
|
|
|
|
int pos = 0;
|
|
|
|
if (format == RTP_FORMAT_H264)
|
|
|
|
{
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc6184#section-1.3
|
|
|
|
if (!(rtp_flags & RTP_NO_H26X_SCL))
|
|
|
|
{
|
|
|
|
memset(test_frame.get() + pos, 0, 2);
|
|
|
|
pos += 2;
|
|
|
|
memset(test_frame.get() + pos, 1, 1);
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
memset(test_frame.get() + pos, 5, 1); // Intra frame
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
else if (format == RTP_FORMAT_H265)
|
|
|
|
{
|
|
|
|
// see https://datatracker.ietf.org/doc/html/rfc7798#section-1.1.4
|
|
|
|
if (!(rtp_flags & RTP_NO_H26X_SCL))
|
|
|
|
{
|
|
|
|
memset(test_frame.get() + pos, 0, 3);
|
|
|
|
pos += 3;
|
|
|
|
memset(test_frame.get() + pos, 1, 1);
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
memset(test_frame.get() + pos, (19 << 1), 1); // Intra frame
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
else if (format == RTP_FORMAT_H266)
|
|
|
|
{
|
|
|
|
// see https://datatracker.ietf.org/doc/html/draft-ietf-avtcore-rtp-vvc#section-1.1.4
|
|
|
|
if (!(rtp_flags & RTP_NO_H26X_SCL))
|
|
|
|
{
|
|
|
|
memset(test_frame.get() + pos, 0, 3);
|
|
|
|
pos += 3;
|
|
|
|
memset(test_frame.get() + pos, 1, 1);
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
// |F|Z| LayerID | Type | TID |
|
|
|
|
memset(test_frame.get() + pos, 0, 1);
|
|
|
|
pos += 1;
|
|
|
|
memset(test_frame.get() + pos, (7 << 3), 1); // Intra frame (type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return test_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void send_packets(std::unique_ptr<uint8_t[]> test_packet, size_t size,
|
|
|
|
uvgrtp::session* sess, uvgrtp::media_stream* sender,
|
|
|
|
int packets, int packet_interval_ms, bool print_progress, int rtp_flags)
|
2022-03-04 08:18:14 +00:00
|
|
|
{
|
|
|
|
EXPECT_NE(nullptr, sess);
|
|
|
|
EXPECT_NE(nullptr, sender);
|
|
|
|
if (sess && sender)
|
|
|
|
{
|
|
|
|
std::cout << "Sending " << packets << " test packets with size " << size << std::endl;
|
|
|
|
|
|
|
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
|
|
|
for (unsigned int i = 0; i < packets; ++i)
|
|
|
|
{
|
2022-07-13 10:32:27 +00:00
|
|
|
std::unique_ptr<uint8_t[]> test_frame = std::unique_ptr<uint8_t[]>(new uint8_t[size]);
|
|
|
|
memcpy(test_frame.get(), test_packet.get(), size);
|
2022-03-04 08:18:14 +00:00
|
|
|
|
2022-03-31 07:05:41 +00:00
|
|
|
rtp_error_t ret = RTP_OK;
|
2022-07-13 10:32:27 +00:00
|
|
|
if ((ret = sender->push_frame(std::move(test_frame), size, rtp_flags)) != RTP_OK)
|
2022-03-04 08:18:14 +00:00
|
|
|
{
|
2022-03-31 07:05:41 +00:00
|
|
|
std::cout << "Failed to send test packet! Return value: " << ret << std::endl;
|
|
|
|
return;
|
2022-03-04 08:18:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 12:28:09 +00:00
|
|
|
if (i % (packets / 10) == packets / 10 - 1 && print_progress)
|
2022-03-04 08:18:14 +00:00
|
|
|
{
|
|
|
|
std::cout << "Sent " << (i + 1) * 100 / packets << " % of data" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet_interval_ms > 0)
|
|
|
|
{
|
|
|
|
wait_until_next_frame(start, i, packet_interval_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void wait_until_next_frame(std::chrono::steady_clock::time_point& start, int frame_index, int packet_interval_ms)
|
|
|
|
{
|
|
|
|
// wait until it is time to send the next frame. Simulates a steady sending pace
|
|
|
|
// and included only for demostration purposes since you can use uvgRTP to send
|
|
|
|
// packets as fast as desired
|
|
|
|
auto time_since_start = std::chrono::steady_clock::now() - start;
|
|
|
|
auto next_frame_time = (frame_index + 1) * std::chrono::milliseconds(packet_interval_ms);
|
|
|
|
if (next_frame_time > time_since_start)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(next_frame_time - time_since_start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void cleanup_sess(uvgrtp::context& ctx, uvgrtp::session* sess)
|
|
|
|
{
|
|
|
|
EXPECT_NE(nullptr, sess);
|
|
|
|
if (sess)
|
|
|
|
{
|
|
|
|
// Session must be destroyed manually
|
|
|
|
ctx.destroy_session(sess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void cleanup_ms(uvgrtp::session* sess, uvgrtp::media_stream* ms)
|
|
|
|
{
|
|
|
|
EXPECT_NE(nullptr, ms);
|
|
|
|
EXPECT_NE(nullptr, sess);
|
|
|
|
if (sess && ms)
|
|
|
|
{
|
|
|
|
sess->destroy_stream(ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 10:32:27 +00:00
|
|
|
inline void test_packet_size(std::unique_ptr<uint8_t[]> test_packet, int packets, size_t size,
|
|
|
|
uvgrtp::session* sess, uvgrtp::media_stream* sender, uvgrtp::media_stream* receiver, int rtp_flags)
|
2022-03-25 08:31:48 +00:00
|
|
|
{
|
|
|
|
EXPECT_NE(nullptr, sess);
|
|
|
|
EXPECT_NE(nullptr, sender);
|
|
|
|
EXPECT_NE(nullptr, receiver);
|
|
|
|
|
|
|
|
if (sess && sender && receiver)
|
|
|
|
{
|
|
|
|
Test_receiver* tester = new Test_receiver(packets);
|
|
|
|
|
2022-05-13 06:44:08 +00:00
|
|
|
int interval_ms = 10;
|
|
|
|
|
2022-03-25 08:31:48 +00:00
|
|
|
add_hook(tester, receiver, rtp_receive_hook);
|
2022-07-13 10:32:27 +00:00
|
|
|
send_packets(std::move(test_packet), size, sess, sender, packets, interval_ms, false, rtp_flags);
|
2022-03-25 08:31:48 +00:00
|
|
|
|
2022-06-18 16:17:30 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50 + size/1000));
|
2022-03-25 08:31:48 +00:00
|
|
|
|
|
|
|
tester->gotAll();
|
|
|
|
delete tester;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 12:28:09 +00:00
|
|
|
inline void add_hook(Test_receiver* tester, uvgrtp::media_stream* receiver,
|
2022-03-04 08:18:14 +00:00
|
|
|
void (*hook)(void*, uvgrtp::frame::rtp_frame*))
|
|
|
|
{
|
|
|
|
EXPECT_NE(nullptr, receiver);
|
|
|
|
if (receiver)
|
|
|
|
{
|
2022-03-23 12:28:09 +00:00
|
|
|
EXPECT_EQ(RTP_OK, receiver->install_receive_hook(tester, hook));
|
2022-03-04 08:18:14 +00:00
|
|
|
}
|
2022-03-25 08:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void rtp_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
|
|
|
|
{
|
|
|
|
if (arg != nullptr)
|
|
|
|
{
|
|
|
|
Test_receiver* tester = (Test_receiver*)arg;
|
|
|
|
tester->receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
process_rtp_frame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void process_rtp_frame(uvgrtp::frame::rtp_frame* frame)
|
|
|
|
{
|
|
|
|
EXPECT_NE(0, frame->payload_len);
|
|
|
|
EXPECT_EQ(2, frame->header.version);
|
|
|
|
(void)uvgrtp::frame::dealloc_frame(frame);
|
2022-03-04 08:18:14 +00:00
|
|
|
}
|