uvgrtp-base/examples
Joni Räsänen f66d78aa09 examples: Add more prints to zrtp example 2022-08-23 10:58:41 +03:00
..
.gitignore examples: Move examples folder to repo root 2022-02-28 09:52:18 +02:00
README.md doc: Remove mention of bug that has been fixed from tutorial 2022-03-01 09:04:54 +02:00
binding.cc examples: Make examples work with SCL 2022-03-31 15:15:55 +03:00
configuration.cc examples: Make examples work with SCL 2022-03-31 15:15:55 +03:00
custom_timestamps.cc examples: Make examples work with SCL 2022-03-31 15:15:55 +03:00
receiving_hook.cc examples: Move examples folder to repo root 2022-02-28 09:52:18 +02:00
receiving_poll.cc examples: Move examples folder to repo root 2022-02-28 09:52:18 +02:00
rtcp_hook.cc rtcp: Move SR and RR construction to rtcp packets module 2022-07-08 09:53:49 +03:00
sending.cc examples: Make examples work with SCL 2022-03-31 15:15:55 +03:00
sending_generic.cc examples: Move examples folder to repo root 2022-02-28 09:52:18 +02:00
srtp_user.cc examples: Move examples folder to repo root 2022-02-28 09:52:18 +02:00
srtp_zrtp.cc examples: Move examples folder to repo root 2022-02-28 09:52:18 +02:00
uvgRTP_examples.pro examples: Fix name of selection in project file 2022-03-04 07:44:32 +02:00
zrtp_multistream.cc examples: Add more prints to zrtp example 2022-08-23 10:58:41 +03:00

README.md

uvgRTP example codes

This directory contains a collection of commented and functioning examples that demonstrate the usage of uvgRTP. Currently, the API is very C-like and would benefit greatly from using more C++ features. Help with this would be greatly appreciated.

Tutorial

You can either include files individually from include folder or use lib.hh to include all necessary files with one line:

#include <uvgrtp/lib.hh>

Create context

When using uvgRTP, you must always first create the context object:

uvgrtp::context ctx;

Create session

Next, you will use the context object to create session objects. Session object contains all the different media streams you are sending/receiving to/from single IP address. Broadcast addresses should also work. There are two options fort creating this, Specify only remote address (currently this also binds to ANY with each media_stream):

uvgrtp::session *sess = ctx.create_session("10.10.10.1");

or specify both remote and local addresses:

uvgrtp::session *sess = ctx.create_session("10.10.10.1", "10.10.10.2");

Hopefully in the future also only binding to local address and only sending will be supported. This is discussed in issue #83 and PRs are welcome to this issue (be careful not to invalidate current API).

Create media_stream

To send/receive actual media, a media_stream object has to be created. The first parameter is the local port from which the sending happens and the second port is the port where the data is sent to (note that these are in the reverse order compared to creating the session). The third parameter specifies the RTP payload format which will be used for the outgoing and incoming data. The last parameter holds the flags that can be used to modify the behavior of uvgRTP in regards to this media_stream.

uvgrtp::media_stream *strm = sess->create_stream(8888, 8888, RTP_FORMAT_GENERIC, RTP_NO_FLAGS);

The encryption can be enabled here bug specifying RCE_SRTP| RCE_SRTP_KMNGMNT_ZRTP or RCE_SRTP | RCE_SRTP_KMNGMNT_USER in the last parameter. The RCE_SRTP_KMNGMNT_USER requires calling add_srtp_ctx(key, salt) for the created media_stream after creation. These flags start with prefix RCE_ and the explanations can be found in docs folder. Other useful flags include RCE_RTCP for enabling RTCP and RCE_H26X_PREPEND_SC for prepending start codes which are needed for decoding of an H26x stream.

Configure media_stream (optional)

Some of the media_stream functionality can be configured after the stream has been created:

strm->configure_ctx(RCC_MTU_SIZE, 2312);

The flags start with prefix RCC_ and the rest of the flags can be found in the docs folder. Also, see configuration example for more details.

Sending data

Sending can be done by simple calling push_frame()-function on created media_stream:

strm->push_frame((uint8_t *)message, msg_len, RTP_NO_FLAGS);

See sending example for more details.

Receiving data

There are two alternatives to receiving data. Using pull_frame()-function:

auto frame = strm->pull_frame();

or function callback based approach (I would recommend this to minimize latency):

strm->install_receive_hook(nullptr, rtp_receive_hook);

If you use classes, you can give a pointer to your class in the first parameter and call it in you callback function (an std::function API would be nice, but does not exist yet). In both versions, the user will be responsible for releasing the memory.

Cleanup

Cleanup can be dune with following functions:

sess->destroy_stream(strm);
ctx.destroy_session(sess);

Simple example (non-working)

#include <uvgrtp/lib.hh>

/* g++ main.cc -luvgrtp -lpthread && ./a.out */

int main(void)
{
    uvgrtp::context ctx;
    uvgrtp::session *sess = ctx.create_session("127.0.0.1");

    uvgrtp::media_stream *strm = sess->create_stream(8888, 8888, RTP_FORMAT_GENERIC, RTP_NO_FLAGS);

    strm->configure_ctx(RCC_MTU_SIZE, 2312);

    char *message  = (char *)"Hello, world!";
    size_t msg_len = strlen(message) + 1;

    for (;;) {
        strm->push_frame((uint8_t *)message, msg_len, RTP_NO_FLAGS);
        auto frame = strm->pull_frame();
        fprintf(stderr, "Message: '%s'\n", frame->payload);
        uvgrtp::frame::dealloc_frame(frame);
    }
}

Basic RTP examples

How to create a simple RTP sender (Pair with one of the receiver examples)

How to create a simple RTP receiver (hooking)

NOTE: The hook should not be used for extensive media processing. It is meant to be used as an interface between application and library where uvgRTP hands off the RTP frames to an application thread.

How to create a simple RTP receiver (polling)

Advanced RTP examples

How to modify uvgRTP behavior

How to fragment generic media types

How to enable UDP hole punching

How to use custom timestamps correctly

RTCP example

How to use RTCP instance (hooking)

Encryption examples

How to use SRTP with ZRTP

How to use multi-stream SRTP with ZRTP

How to use SRTP with user-managed keys