2020-07-31 14:02:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-08-03 10:35:48 +00:00
|
|
|
#include <mutex>
|
2020-07-31 14:02:58 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
2020-08-03 10:35:48 +00:00
|
|
|
#include "frame.hh"
|
2020-07-31 14:02:58 +00:00
|
|
|
#include "runner.hh"
|
|
|
|
|
#include "socket.hh"
|
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
|
|
namespace uvg_rtp {
|
|
|
|
|
|
2020-08-04 10:08:35 +00:00
|
|
|
typedef rtp_error_t (*packet_handler)(ssize_t, void *, int, uvg_rtp::frame::rtp_frame **);
|
2020-07-31 14:02:58 +00:00
|
|
|
|
|
|
|
|
class pkt_dispatcher : public runner {
|
|
|
|
|
public:
|
|
|
|
|
pkt_dispatcher(uvg_rtp::socket socket);
|
|
|
|
|
~pkt_dispatcher();
|
|
|
|
|
|
|
|
|
|
/* Install a generic handler for an incoming packet
|
|
|
|
|
*
|
|
|
|
|
* Return RTP_OK on successfully
|
|
|
|
|
* Return RTP_INVALID_VALUE if "handler" is nullptr */
|
|
|
|
|
rtp_error_t install_handler(packet_handler handler);
|
|
|
|
|
|
2020-08-03 07:58:50 +00:00
|
|
|
/* Install receive hook for the RTP packet dispatcher
|
|
|
|
|
*
|
|
|
|
|
* Return RTP_OK on success
|
|
|
|
|
* Return RTP_INVALID_VALUE if "hook" is nullptr */
|
|
|
|
|
rtp_error_t install_receive_hook(void *arg, void (*hook)(void *, uvg_rtp::frame::rtp_frame *));
|
|
|
|
|
|
2020-08-05 06:10:16 +00:00
|
|
|
/* Start the RTP packet dispatcher
|
|
|
|
|
*
|
|
|
|
|
* Return RTP_OK on success
|
|
|
|
|
* Return RTP_MEMORY_ERROR if allocation of a thread object fails */
|
|
|
|
|
rtp_error_t start(uvg_rtp::socket *socket, int flags);
|
|
|
|
|
|
2020-08-03 10:35:48 +00:00
|
|
|
/* Fetchj frame from the frame queue that contains all received frame.
|
|
|
|
|
* pull_frame() will block until there is a frame that can be returned.
|
|
|
|
|
* If "timeout" is given, pull_frame() will block only for however long
|
|
|
|
|
* that value tells it to.
|
|
|
|
|
* If no frame is received within that time period, pull_frame() returns nullptr
|
|
|
|
|
*
|
|
|
|
|
* Return pointer to RTP frame on success
|
|
|
|
|
* Return nullptr if operation timed out or an error occurred */
|
|
|
|
|
uvg_rtp::frame::rtp_frame *pull_frame();
|
|
|
|
|
uvg_rtp::frame::rtp_frame *pull_frame(size_t ms);
|
|
|
|
|
|
2020-07-31 14:02:58 +00:00
|
|
|
/* Return reference to the vector that holds all installed handlers */
|
|
|
|
|
std::vector<uvg_rtp::packet_handler>& get_handlers();
|
|
|
|
|
|
2020-08-05 05:16:19 +00:00
|
|
|
/* RTP packet dispatcher thread */
|
|
|
|
|
static void runner(uvg_rtp::pkt_dispatcher *dispatcher, uvg_rtp::socket *socket, int flags);
|
|
|
|
|
|
2020-07-31 14:02:58 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
uvg_rtp::socket socket_;
|
|
|
|
|
std::vector<packet_handler> packet_handlers_;
|
2020-08-03 07:58:50 +00:00
|
|
|
|
2020-08-03 10:35:48 +00:00
|
|
|
/* If receive hook has not been installed, frames are pushed to "frames_"
|
|
|
|
|
* and they can be retrieved using pull_frame() */
|
|
|
|
|
std::vector<uvg_rtp::frame::rtp_frame *> frames_;
|
|
|
|
|
std::mutex frames_mtx_;
|
|
|
|
|
|
2020-08-03 07:58:50 +00:00
|
|
|
void *recv_hook_arg_;
|
|
|
|
|
void (*recv_hook_)(void *arg, uvg_rtp::frame::rtp_frame *frame);
|
2020-07-31 14:02:58 +00:00
|
|
|
};
|
|
|
|
|
}
|