2020-04-27 11:07:24 +00:00
|
|
|
#include <uvgrtp/lib.hh>
|
2021-07-16 13:22:58 +00:00
|
|
|
|
|
|
|
|
#include <iostream>
|
2019-10-24 05:13:03 +00:00
|
|
|
#include <thread>
|
|
|
|
|
|
2021-07-27 07:36:54 +00:00
|
|
|
/* 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
|
2021-07-16 13:22:58 +00:00
|
|
|
constexpr uint16_t LOCAL_PORT = 8890;
|
|
|
|
|
|
|
|
|
|
constexpr char REMOTE_ADDRESS[] = "127.0.0.1";
|
|
|
|
|
constexpr uint16_t REMOTE_PORT = 8888;
|
|
|
|
|
|
2021-07-27 07:36:54 +00:00
|
|
|
// How long this example will run
|
2021-07-27 13:51:14 +00:00
|
|
|
constexpr auto RECEIVE_TIME_MS = std::chrono::milliseconds(3000);
|
2021-07-16 13:22:58 +00:00
|
|
|
constexpr int RECEIVER_WAIT_TIME_MS = 100;
|
|
|
|
|
|
2021-07-27 07:36:54 +00:00
|
|
|
void process_frame(uvgrtp::frame::rtp_frame *frame);
|
2021-07-16 13:22:58 +00:00
|
|
|
|
2020-02-14 06:12:50 +00:00
|
|
|
int main(void)
|
2019-10-24 05:13:03 +00:00
|
|
|
{
|
2021-07-16 13:22:58 +00:00
|
|
|
std::cout << "Starting uvgRTP RTP receive hook example" << std::endl;
|
|
|
|
|
|
2021-02-19 01:13:43 +00:00
|
|
|
uvgrtp::context ctx;
|
2021-07-16 13:22:58 +00:00
|
|
|
uvgrtp::session *sess = ctx.create_session(REMOTE_ADDRESS);
|
2021-07-27 07:36:54 +00:00
|
|
|
int flags = RCE_NO_FLAGS;
|
|
|
|
|
|
|
|
|
|
uvgrtp::media_stream *receiver = sess->create_stream(LOCAL_PORT, REMOTE_PORT,
|
|
|
|
|
RTP_FORMAT_H265, flags);
|
2019-10-24 05:13:03 +00:00
|
|
|
|
2021-07-27 07:36:54 +00:00
|
|
|
// TODO: Explain how to stop poll in middle of the wait
|
2019-10-24 05:13:03 +00:00
|
|
|
|
2021-07-27 07:36:54 +00:00
|
|
|
if (receiver)
|
2021-07-20 13:18:29 +00:00
|
|
|
{
|
|
|
|
|
uvgrtp::frame::rtp_frame *frame = nullptr;
|
2019-10-24 05:13:03 +00:00
|
|
|
|
2021-07-20 13:18:29 +00:00
|
|
|
std::cout << "Start receiving frames for " << RECEIVE_TIME_MS.count() << " ms" << std::endl;
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
2021-07-16 13:22:58 +00:00
|
|
|
|
2021-07-20 13:18:29 +00:00
|
|
|
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 */
|
2021-07-27 07:36:54 +00:00
|
|
|
frame = receiver->pull_frame(RECEIVER_WAIT_TIME_MS);
|
2021-07-16 13:22:58 +00:00
|
|
|
|
2021-07-20 13:18:29 +00:00
|
|
|
if (frame)
|
|
|
|
|
process_frame(frame);
|
|
|
|
|
}
|
2019-10-24 05:13:03 +00:00
|
|
|
|
2021-07-27 07:36:54 +00:00
|
|
|
sess->destroy_stream(receiver);
|
2021-07-16 13:22:58 +00:00
|
|
|
}
|
2020-02-14 06:12:50 +00:00
|
|
|
|
2021-07-16 13:22:58 +00:00
|
|
|
if (sess)
|
|
|
|
|
{
|
|
|
|
|
/* Session must be destroyed manually */
|
|
|
|
|
ctx.destroy_session(sess);
|
|
|
|
|
}
|
2021-04-23 05:55:15 +00:00
|
|
|
|
2021-07-20 13:18:29 +00:00
|
|
|
return EXIT_SUCCESS;
|
2019-10-24 05:13:03 +00:00
|
|
|
}
|
2021-07-27 07:36:54 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|