-
Notifications
You must be signed in to change notification settings - Fork 94
/
receiving_hook.cc
93 lines (76 loc) · 3.29 KB
/
receiving_hook.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <uvgrtp/lib.hh>
#include <thread>
#include <iostream>
/* There are two main ways of getting received RTP frames from uvgRTP.
* This example demonstrates the usage of hook function to receive RTP frames.
*
* The advantage of using a hook function is minimal CPU usage and delay between
* uvgRTP receiving the frame and application processing the frame. When using
* the hook method, the application must take care that it is not using the hook
* function for heavy processing since this may block RTP frame reception.
*
* Hook based frame reception is generally recommended for most serious applications,
* but there can be situations where polling method is better, especially if performance
* is not a huge concern or if there needs to be tight control when the frame is
* received by the application.
*
* This example only implements the receiving, but it can be used together with the
* sending example to test the functionality.
*/
// parameters for this test. You can change these to suit your network environment
constexpr uint16_t LOCAL_PORT = 8890;
constexpr char LOCAL_ADDRESS[] = "127.0.0.1";
// This example runs for 5 seconds
constexpr auto RECEIVE_TIME_S = std::chrono::seconds(10);
void rtp_receive_hook(void *arg, uvgrtp::frame::rtp_frame *frame);
void cleanup(uvgrtp::context& ctx, uvgrtp::session *sess, uvgrtp::media_stream *receiver);
int main(void)
{
std::cout << "Starting uvgRTP RTP receive hook example" << std::endl;
uvgrtp::context ctx;
uvgrtp::session *sess = ctx.create_session(LOCAL_ADDRESS);
int flags = RCE_RECEIVE_ONLY;
uvgrtp::media_stream *receiver = sess->create_stream(LOCAL_PORT, RTP_FORMAT_H265, flags);
/* Receive hook can be installed and uvgRTP 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
* the 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 */
if (!receiver || receiver->install_receive_hook(nullptr, rtp_receive_hook) != RTP_OK)
{
std::cerr << "Failed to install RTP reception hook";
cleanup(ctx, sess, receiver);
return EXIT_FAILURE;
}
std::cout << "Waiting incoming packets for " << RECEIVE_TIME_S.count() << " s" << std::endl;
std::this_thread::sleep_for(RECEIVE_TIME_S); // lets this example run for some time
cleanup(ctx, sess, receiver);
return EXIT_SUCCESS;
}
void rtp_receive_hook(void *arg, uvgrtp::frame::rtp_frame *frame)
{
std::cout << "Received RTP frame" << std::endl;
/* Now we own the frame. Here you could give the frame to the application
* if f.ex "arg" was some application-specific pointer
*
* arg->copy_frame(frame) or whatever
*
* When we're done with the frame, it must be deallocated manually */
(void)uvgrtp::frame::dealloc_frame(frame);
}
void cleanup(uvgrtp::context& ctx, uvgrtp::session *sess, uvgrtp::media_stream *receiver)
{
if (receiver)
{
sess->destroy_stream(receiver);
}
if (sess)
{
/* Session must be destroyed manually */
ctx.destroy_session(sess);
}
}