examples: Improved sending example

This commit is contained in:
Joni Räsänen 2021-07-20 16:25:52 +03:00
parent 1453f1c447
commit 9af136f121
1 changed files with 42 additions and 11 deletions

View File

@ -1,14 +1,26 @@
#include <uvgrtp/lib.hh>
#define PAYLOAD_MAXLEN 100
#include <iostream>
constexpr uint16_t LOCAL_PORT = 8888;
constexpr char REMOTE_ADDRESS[] = "127.0.0.1";
constexpr uint16_t REMOTE_PORT = 8890;
constexpr size_t PAYLOAD_LEN = 100;
constexpr int SEND_TEST_PACKETS = 100;
constexpr auto END_WAIT = std::chrono::seconds(5);
int main(void)
{
std::cout << "Starting uvgRTP RTP sending example" << std::endl;
/* To use the library, one must create a global RTP context object */
uvgrtp::context ctx;
/* Each new IP address requires a separate RTP session */
uvgrtp::session *sess = ctx.create_session("127.0.0.1");
uvgrtp::session *sess = ctx.create_session(REMOTE_ADDRESS);
/* Each RTP session has one or more media streams. These media streams are bidirectional
* and they require both source and destination ports for the connection. One must also
@ -22,18 +34,37 @@ int main(void)
* This same object is used for both sending and receiving media
*
* In this example, we have one media stream with the remote participant: H265 */
uvgrtp::media_stream *hevc = sess->create_stream(8888, 8889, RTP_FORMAT_H265, RTP_NO_FLAGS);
uint8_t *buffer = new uint8_t[PAYLOAD_MAXLEN];
int flags = RTP_NO_FLAGS;
uvgrtp::media_stream *hevc = sess->create_stream(LOCAL_PORT, REMOTE_PORT,
RTP_FORMAT_H265, flags);
for (int i = 0; i < 10; ++i) {
if (hevc->push_frame(buffer, PAYLOAD_MAXLEN, RTP_NO_FLAGS) != RTP_OK)
fprintf(stderr, "Failed to send RTP frame!");
if (hevc)
{
for (int i = 0; i < SEND_TEST_PACKETS; ++i)
{
std::unique_ptr<uint8_t[]> dummy_frame = std::unique_ptr<uint8_t[]>(new uint8_t[PAYLOAD_LEN]);
std::cout << "Sending frame " << i + 1 << '/' << SEND_TEST_PACKETS << std::endl;
if (hevc->push_frame(std::move(dummy_frame), PAYLOAD_LEN, RTP_NO_FLAGS) != RTP_OK)
{
std::cout << "Failed to send RTP frame!" << std::endl;
}
}
std::cout << "Sending finished. Waiting "<< END_WAIT.count()
<< " seconds before exiting." << std::endl;
std::this_thread::sleep_for(END_WAIT);
sess->destroy_stream(hevc);
}
/* Session must be destroyed manually */
delete[] buffer;
ctx.destroy_session(sess);
if (sess)
{
/* Session must be destroyed manually */
ctx.destroy_session(sess);
}
return 0;
return EXIT_SUCCESS;
}