-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathreceiving_poll.cc
78 lines (61 loc) · 2.68 KB
/
receiving_poll.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
#include <uvgrtp/lib.hh>
#include <iostream>
#include <thread>
/* This example demostrates using polling to receive RTP frames. Polling in
* uvgRTP can be done with function pull_frame in media_streamer. This pull_frame
* function can be used with or without a timeout argument. If used without a timeout
* argument, the function will return when a frame is received or the media stream
* is destroyed. At this point I would recommend using it with timeout and not
* destroying the media stream since this functionality has not been verified.
*
* Compared to hook function, polling offers more control on frame reception,
* but I would recommend using a hook function where possible due to reduced
* CPU usage and latency.
*
* This example implements only the reception of the stream, but it can be paired
* with the sending example to complete the demonstration.
*/
// parameters of this example. You may change these to reflect you network environment
constexpr uint16_t LOCAL_PORT = 8890;
constexpr char LOCAL_ADDRESS[] = "127.0.0.1";
// How long this example will run
constexpr auto RECEIVE_TIME_MS = std::chrono::milliseconds(10000);
constexpr int RECEIVER_WAIT_TIME_MS = 100;
void process_frame(uvgrtp::frame::rtp_frame *frame);
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);
if (receiver)
{
std::cout << "Start receiving frames for " << RECEIVE_TIME_MS.count() << " ms" << std::endl;
auto start = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - start < RECEIVE_TIME_MS)
{
/* You can specify a timeout for the operation and if the a frame is not received
* within that time limit, pull_frame() returns a nullptr
*
* The parameter tells how long time a frame is waited in milliseconds */
uvgrtp::frame::rtp_frame* frame = receiver->pull_frame(RECEIVER_WAIT_TIME_MS);
if (frame)
process_frame(frame);
}
sess->destroy_stream(receiver);
}
if (sess)
{
/* Session must be destroyed manually */
ctx.destroy_session(sess);
}
return EXIT_SUCCESS;
}
void process_frame(uvgrtp::frame::rtp_frame *frame)
{
std::cout << "Received an RTP frame" << std::endl;
/* When we receive a frame, the ownership of the frame belongs to us and
* when we're done with it, we need to deallocate the frame */
(void)uvgrtp::frame::dealloc_frame(frame);
}