Create Runner class
Several classes have common active_ and runner_ variables and stop/start/active routines (such as reader and dispatch). Create one common class for these to make the interface cleaner
This commit is contained in:
parent
bde19e3f7f
commit
0f0a052e54
|
|
@ -50,7 +50,7 @@ kvz_rtp::connection::connection(rtp_format_t fmt, bool reader):
|
|||
kvz_rtp::connection::~connection()
|
||||
{
|
||||
if (rtcp_) {
|
||||
rtcp_->terminate();
|
||||
rtcp_->stop();
|
||||
delete rtcp_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
#include "frame.hh"
|
||||
#include "rtcp.hh"
|
||||
#include "runner.hh"
|
||||
#include "socket.hh"
|
||||
#include "util.hh"
|
||||
|
||||
|
|
@ -22,13 +23,11 @@ namespace kvz_rtp {
|
|||
class dispatcher;
|
||||
class frame_queue;
|
||||
|
||||
class connection {
|
||||
class connection : public runner {
|
||||
public:
|
||||
connection(rtp_format_t fmt, bool reader);
|
||||
virtual ~connection();
|
||||
|
||||
virtual rtp_error_t start() = 0;
|
||||
|
||||
uint16_t get_sequence() const;
|
||||
uint32_t get_ssrc() const;
|
||||
uint8_t get_payload() const;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ kvz_rtp::dispatcher::dispatcher(kvz_rtp::socket *socket):
|
|||
kvz_rtp::dispatcher::~dispatcher()
|
||||
{
|
||||
delete runner_;
|
||||
runner_ = nullptr;
|
||||
}
|
||||
|
||||
rtp_error_t kvz_rtp::dispatcher::start()
|
||||
|
|
@ -30,11 +31,6 @@ rtp_error_t kvz_rtp::dispatcher::stop()
|
|||
return RTP_OK;
|
||||
}
|
||||
|
||||
bool kvz_rtp::dispatcher::active()
|
||||
{
|
||||
return active_;
|
||||
}
|
||||
|
||||
std::condition_variable& kvz_rtp::dispatcher::get_cvar()
|
||||
{
|
||||
return cv_;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "queue.hh"
|
||||
#include "socket.hh"
|
||||
#include "runner.hh"
|
||||
#include "util.hh"
|
||||
|
||||
namespace kvz_rtp {
|
||||
|
|
@ -30,15 +31,11 @@ namespace kvz_rtp {
|
|||
* experiences to very small (<50 us even for large frames [>170 kB]) */
|
||||
typedef struct transaction transaction_t;
|
||||
|
||||
class dispatcher {
|
||||
class dispatcher : public runner {
|
||||
public:
|
||||
dispatcher(kvz_rtp::socket *socket);
|
||||
~dispatcher();
|
||||
|
||||
rtp_error_t start();
|
||||
rtp_error_t stop();
|
||||
bool active();
|
||||
|
||||
/* Add new transaction to dispatcher's task queue
|
||||
* The task queue is emptied in FIFO style */
|
||||
rtp_error_t trigger_send(kvz_rtp::transaction_t *transaction);
|
||||
|
|
@ -62,9 +59,6 @@ namespace kvz_rtp {
|
|||
std::mutex q_mtx_;
|
||||
std::queue<kvz_rtp::transaction_t *> tasks_;
|
||||
|
||||
bool active_;
|
||||
std::thread *runner_;
|
||||
|
||||
kvz_rtp::socket *socket_;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
kvz_rtp::reader::reader(rtp_format_t fmt, std::string src_addr, int src_port):
|
||||
connection(fmt, true),
|
||||
active_(false),
|
||||
src_addr_(src_addr),
|
||||
src_port_(src_port),
|
||||
recv_hook_arg_(nullptr),
|
||||
|
|
@ -106,11 +105,6 @@ kvz_rtp::frame::rtp_frame *kvz_rtp::reader::pull_frame()
|
|||
return nextFrame;
|
||||
}
|
||||
|
||||
bool kvz_rtp::reader::active()
|
||||
{
|
||||
return active_;
|
||||
}
|
||||
|
||||
uint8_t *kvz_rtp::reader::get_recv_buffer() const
|
||||
{
|
||||
return recv_buffer_;
|
||||
|
|
|
|||
|
|
@ -16,11 +16,13 @@ namespace kvz_rtp {
|
|||
/* NOTE: this operation is blocking */
|
||||
kvz_rtp::frame::rtp_frame *pull_frame();
|
||||
|
||||
// open socket and start runner_
|
||||
/* Open socket, start frame receiver and RTCP
|
||||
*
|
||||
* Return RTP_OK on success
|
||||
* Return RTP_MEMORY_ERROR if memory deallocation failed
|
||||
* Return RTP_GENERIC_ERROR for any other error */
|
||||
rtp_error_t start();
|
||||
|
||||
bool active();
|
||||
|
||||
bool recv_hook_installed();
|
||||
void recv_hook(kvz_rtp::frame::rtp_frame *frame);
|
||||
void install_recv_hook(void *arg, void (*hook)(void *arg, kvz_rtp::frame::rtp_frame *));
|
||||
|
|
@ -44,15 +46,11 @@ namespace kvz_rtp {
|
|||
void return_frame(kvz_rtp::frame::rtp_frame *frame);
|
||||
|
||||
private:
|
||||
// TODO implement ring buffer
|
||||
bool active_;
|
||||
|
||||
// connection-related stuff
|
||||
std::string src_addr_;
|
||||
int src_port_;
|
||||
|
||||
// receiver thread related stuff
|
||||
std::thread *runner_;
|
||||
/* receiver thread related stuff */
|
||||
uint8_t *recv_buffer_; /* buffer for incoming packet (MAX_PACKET) */
|
||||
uint32_t recv_buffer_len_; /* buffer length */
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ kvz_rtp::rtcp::rtcp(uint32_t ssrc, bool receiver):
|
|||
tp_(0), tc_(0), tn_(0), pmembers_(0),
|
||||
members_(0), senders_(0), rtcp_bandwidth_(0),
|
||||
we_sent_(0), avg_rtcp_pkt_pize_(0), rtcp_pkt_count_(0),
|
||||
initial_(true), active_(false), num_receivers_(0)
|
||||
initial_(true), num_receivers_(0)
|
||||
{
|
||||
ssrc_ = ssrc;
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ rtp_error_t kvz_rtp::rtcp::start()
|
|||
return RTP_OK;
|
||||
}
|
||||
|
||||
rtp_error_t kvz_rtp::rtcp::terminate()
|
||||
rtp_error_t kvz_rtp::rtcp::stop()
|
||||
{
|
||||
if (runner_ == nullptr)
|
||||
goto free_mem;
|
||||
|
|
@ -170,11 +170,6 @@ free_mem:
|
|||
return RTP_OK;
|
||||
}
|
||||
|
||||
bool kvz_rtp::rtcp::active() const
|
||||
{
|
||||
return active_;
|
||||
}
|
||||
|
||||
bool kvz_rtp::rtcp::receiver() const
|
||||
{
|
||||
return receiver_;
|
||||
|
|
|
|||
15
src/rtcp.hh
15
src/rtcp.hh
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "clock.hh"
|
||||
#include "frame.hh"
|
||||
#include "runner.hh"
|
||||
#include "socket.hh"
|
||||
#include "util.hh"
|
||||
|
||||
|
|
@ -21,7 +22,7 @@ namespace kvz_rtp {
|
|||
const int MAX_MISORDER = 100;
|
||||
const int MIN_TIMEOUT = 5000;
|
||||
|
||||
class rtcp {
|
||||
class rtcp : public runner {
|
||||
public:
|
||||
rtcp(uint32_t ssrc, bool receiver);
|
||||
~rtcp();
|
||||
|
|
@ -34,11 +35,7 @@ namespace kvz_rtp {
|
|||
/* End the RTCP session and send RTCP BYE to all participants
|
||||
*
|
||||
* return RTP_OK on success */
|
||||
rtp_error_t terminate();
|
||||
|
||||
/* return true if the connection is still considered active
|
||||
* and RTCP transmissions should continue */
|
||||
bool active() const;
|
||||
rtp_error_t stop();
|
||||
|
||||
/* return true if this RTCP instance belongs to an RTP receiver
|
||||
* and a receiver report should be generated, otherwise sender report is generated */
|
||||
|
|
@ -193,7 +190,6 @@ namespace kvz_rtp {
|
|||
rtp_error_t generate_sender_report();
|
||||
rtp_error_t generate_receiver_report();
|
||||
|
||||
std::thread *runner_;
|
||||
bool receiver_;
|
||||
|
||||
/* TODO: time_t?? */
|
||||
|
|
@ -227,11 +223,6 @@ namespace kvz_rtp {
|
|||
/* Flag that is true if the application has not yet sent an RTCP packet. */
|
||||
bool initial_;
|
||||
|
||||
/* Flag that is true if the connection is still considered open.
|
||||
* When clients decided to leave, he calls rtcp->terminate()
|
||||
* which stops the rtcp runner and RTCP BYE message to all participants */
|
||||
bool active_;
|
||||
|
||||
/* Copy of our own current SSRC */
|
||||
uint32_t ssrc_;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
#include "runner.hh"
|
||||
|
||||
kvz_rtp::runner::runner():
|
||||
active_(false), runner_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
kvz_rtp::runner::~runner()
|
||||
{
|
||||
active_ = false;
|
||||
|
||||
if (runner_)
|
||||
delete runner_;
|
||||
}
|
||||
|
||||
rtp_error_t kvz_rtp::runner::start()
|
||||
{
|
||||
active_ = true;
|
||||
}
|
||||
|
||||
rtp_error_t kvz_rtp::runner::stop()
|
||||
{
|
||||
active_ = false;
|
||||
active_ = false;
|
||||
}
|
||||
|
||||
bool kvz_rtp::runner::active()
|
||||
{
|
||||
return active_;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "util.hh"
|
||||
|
||||
namespace kvz_rtp {
|
||||
class runner {
|
||||
public:
|
||||
runner();
|
||||
virtual ~runner();
|
||||
|
||||
virtual rtp_error_t start();
|
||||
virtual rtp_error_t stop();
|
||||
|
||||
virtual bool active();
|
||||
|
||||
protected:
|
||||
bool active_;
|
||||
std::thread *runner_;
|
||||
};
|
||||
};
|
||||
Loading…
Reference in New Issue