uvgrtp-base/docs/examples/receiving_hook.cc

61 lines
2.4 KiB
C++
Raw Normal View History

2020-04-27 11:07:24 +00:00
#include <uvgrtp/lib.hh>
#include <thread>
void receive_hook(void *arg, uvgrtp::frame::rtp_frame *frame)
{
if (!frame) {
fprintf(stderr, "invalid frame received!\n");
return;
}
/* Now we own the frame. Here you could give the frame to the application
2020-10-06 03:45:04 +00:00
* if f.ex "arg" was some application-specific pointer
*
2020-10-06 03:45:04 +00:00
* arg->copy_frame(frame) or whatever
*
2020-10-06 03:45:04 +00:00
* When we're done with the frame, it must be deallocated manually */
(void)uvgrtp::frame::dealloc_frame(frame);
}
int main(void)
{
/* To use the library, one must create a global RTP context object */
uvgrtp::context ctx;
/* Each new IP address requires a separate RTP session.
2020-10-06 03:45:04 +00:00
* This session object contains all media streams and an RTCP object (if enabled) */
uvgrtp::session *sess = ctx.create_session("127.0.0.1");
/* 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
* specify the media format for the stream and any configuration flags if needed
*
2020-10-06 03:45:04 +00:00
* If ZRTP is enabled, the first media stream instance does a Diffie-Hellman key exchange
* with remote and rest of the media streams use Multistream mode. ZRTP requires that both
* source and destination ports are known so it can perform the key exchange
*
* First port is source port aka the port that we listen to and second port is the port
* that remote listens to
*
* This same object is used for both sending and receiving media
*
2020-10-06 03:45:04 +00:00
* In this example, we have one media stream with remote participant: HEVC */
uvgrtp::media_stream *hevc = sess->create_stream(8888, 8889, RTP_FORMAT_H265, 0);
/* Receive hook can be installed and the receiver will call this hook when an RTP frame is received
*
* This is a non-blocking operation
*
* If necessary, receive hook can be given an argument and this argument is supplied to
* receive hook every time the hook is called. This argument could a pointer to application-
* specfic object if the application needs to be called inside the hook
*
* If it's not needed, it should be set to nullptr */
hevc->install_receive_hook(nullptr, receive_hook);
/* Session must be destroyed manually */
ctx.destroy_session(sess);
return 0;
}