| [900] | 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 | |
|---|
| [4233] | 34 | |
|---|
| [900] | 35 | |
|---|
| 36 | |
|---|
| 37 | #ifndef vic_session_h |
|---|
| 38 | #define vic_session_h |
|---|
| 39 | |
|---|
| 40 | #include "net.h" |
|---|
| 41 | #include "transmitter.h" |
|---|
| 42 | #include "timer.h" |
|---|
| 43 | #include "iohandler.h" |
|---|
| 44 | #include "source.h" |
|---|
| 45 | #include "mbus_handler.h" |
|---|
| 46 | |
|---|
| 47 | class Source; |
|---|
| 48 | class SessionManager; |
|---|
| [4353] | 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | bool is_sender_ = false; |
|---|
| [900] | 57 | |
|---|
| 58 | class DataHandler : public IOHandler { |
|---|
| 59 | public: |
|---|
| [956] | 60 | DataHandler* next; |
|---|
| 61 | inline DataHandler() : next(0), sm_(0), net_(0), addrp_(0) {} |
|---|
| 62 | |
|---|
| [900] | 63 | virtual void dispatch(int mask); |
|---|
| 64 | inline Network* net() const { return (net_); } |
|---|
| [956] | 65 | virtual void net(Network* net) { |
|---|
| [900] | 66 | unlink(); |
|---|
| 67 | link(net->rchannel(), TK_READABLE); |
|---|
| 68 | net_ = net; |
|---|
| 69 | if (addrp_) delete addrp_; |
|---|
| 70 | addrp_ = net->addr().copy(); |
|---|
| 71 | } |
|---|
| 72 | inline int recv(u_char* bp, int len, Address*& addrp) { |
|---|
| 73 | return (net_->recv(bp, len, *(addrp = addrp_))); |
|---|
| 74 | } |
|---|
| 75 | inline void send(u_char* bp, int len) { |
|---|
| 76 | net_->send(bp, len); |
|---|
| 77 | } |
|---|
| [956] | 78 | inline void manager(SessionManager* sm) { sm_ = sm; } |
|---|
| [900] | 79 | protected: |
|---|
| [956] | 80 | SessionManager *sm_; |
|---|
| [900] | 81 | Network* net_; |
|---|
| 82 | Address *addrp_; |
|---|
| 83 | }; |
|---|
| [956] | 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | #define CTRL_SESSION_BW_FRACTION (0.05) |
|---|
| 88 | #define CTRL_MIN_RPT_TIME (5.) |
|---|
| 89 | #define CTRL_SENDER_BW_FRACTION (0.25) |
|---|
| 90 | #define CTRL_RECEIVER_BW_FRACTION (1. - CTRL_SENDER_BW_FRACTION) |
|---|
| 91 | #define CTRL_SIZE_GAIN (1./8.) |
|---|
| [900] | 92 | |
|---|
| [956] | 93 | class CtrlHandler : public DataHandler, public Timer { |
|---|
| [900] | 94 | public: |
|---|
| [956] | 95 | CtrlHandler(); |
|---|
| 96 | |
|---|
| [900] | 97 | virtual void dispatch(int mask); |
|---|
| [956] | 98 | inline Network* net() const { return (net_); } |
|---|
| 99 | |
|---|
| 100 | virtual void timeout(); |
|---|
| 101 | virtual void net(Network* net); |
|---|
| 102 | void adapt(int nsrc, int nrr, int we_sent); |
|---|
| 103 | void sample_size(int cc); |
|---|
| 104 | inline double rint() const { return (rint_); } |
|---|
| [4259] | 105 | void send_aoa(); |
|---|
| 106 | void send_ts(); |
|---|
| [4665] | 107 | void send_ackv(); |
|---|
| [4587] | 108 | void send_p(); |
|---|
| [4290] | 109 | void send_ts_echo(); |
|---|
| [4254] | 110 | |
|---|
| [4354] | 111 | |
|---|
| [4353] | 112 | inline void i_am_sender() { is_sender_ = true; } |
|---|
| [4354] | 113 | inline void i_am_receiver() { is_sender_ = false; } |
|---|
| [4353] | 114 | |
|---|
| [956] | 115 | protected: |
|---|
| 116 | void schedule_timer(); |
|---|
| 117 | double ctrl_inv_bw_; |
|---|
| 118 | double ctrl_avg_size_; |
|---|
| 119 | double rint_; |
|---|
| [4831] | 120 | |
|---|
| 121 | |
|---|
| 122 | inline void dispatch_info(double now, int nbytes, int i) { |
|---|
| 123 | fprintf(stderr, " \tnow: %f\tdispatched[%d]: %d\n",now,i,nbytes); |
|---|
| 124 | } |
|---|
| [900] | 125 | }; |
|---|
| 126 | |
|---|
| 127 | class ReportTimer : public Timer { |
|---|
| 128 | public: |
|---|
| 129 | inline ReportTimer(SessionManager& sm) : sm_(sm) {} |
|---|
| 130 | void timeout(); |
|---|
| 131 | protected: |
|---|
| 132 | SessionManager& sm_; |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | class SessionManager : public Transmitter, public MtuAlloc { |
|---|
| [956] | 136 | public: |
|---|
| [900] | 137 | SessionManager(); |
|---|
| 138 | virtual ~SessionManager(); |
|---|
| 139 | virtual int command(int argc, const char*const* argv); |
|---|
| [4825] | 140 | virtual int recv(CtrlHandler*); |
|---|
| [900] | 141 | virtual void recv(DataHandler*); |
|---|
| [956] | 142 | virtual void announce(CtrlHandler*); |
|---|
| 143 | |
|---|
| 144 | virtual inline void send_bye() { send_report(&ch_[0], 1); } |
|---|
| 145 | |
|---|
| 146 | virtual void send_report(CtrlHandler*, int bye, int app = 0); |
|---|
| [4487] | 147 | virtual void build_xreport(CtrlHandler*, int bt); |
|---|
| [4667] | 148 | virtual void send_xreport(CtrlHandler* ch, |
|---|
| [4487] | 149 | u_int8_t bt, u_int8_t rsvd, u_int8_t thin, |
|---|
| 150 | u_int16_t begin_seq, u_int16_t end_seq, |
|---|
| 151 | u_int16_t *chunks, u_int16_t num_chunks, |
|---|
| 152 | u_int32_t xrssrc); |
|---|
| [4468] | 153 | |
|---|
| [4665] | 154 | |
|---|
| [4825] | 155 | int recv_xreport(CtrlHandler*, pktbuf*, bool); |
|---|
| [4665] | 156 | |
|---|
| [4255] | 157 | void build_aoa_pkt(CtrlHandler* ch); |
|---|
| 158 | void build_ts_pkt(CtrlHandler* ch); |
|---|
| [4290] | 159 | void build_ackv_pkt(CtrlHandler* ch); |
|---|
| [4587] | 160 | void build_p_pkt(CtrlHandler* ch); |
|---|
| [4290] | 161 | void build_ts_echo_pkt(CtrlHandler* ch); |
|---|
| [956] | 162 | |
|---|
| [4353] | 163 | |
|---|
| 164 | inline bool am_i_sender() { return is_sender_; } |
|---|
| [4831] | 165 | |
|---|
| 166 | virtual int check_xr_arrival(pktbuf*, bool); |
|---|
| [4353] | 167 | |
|---|
| [956] | 168 | protected: |
|---|
| 169 | |
|---|
| 170 | void demux(pktbuf* pb, Address & addr); |
|---|
| [900] | 171 | virtual int check_format(int fmt) const = 0; |
|---|
| [4831] | 172 | virtual void transmit(pktbuf* pb, bool ack_clock=0); |
|---|
| 173 | virtual void tx_data_only(pktbuf* pb, bool ack_clock); |
|---|
| [900] | 174 | void send_report(int bye); |
|---|
| [4487] | 175 | void send_ECNXreport(CtrlHandler* ch, u_int8_t tos, u_int16_t begin_seq); |
|---|
| [900] | 176 | int build_bye(rtcphdr* rh, Source& local); |
|---|
| [3995] | 177 | u_char* build_sdes_item(u_char* p, int code, Source&); |
|---|
| [900] | 178 | int build_sdes(rtcphdr* rh, Source& s); |
|---|
| [4184] | 179 | int build_app(rtcphdr* rh, Source& ls, const char *name, |
|---|
| 180 | void *data, int datalen); |
|---|
| [900] | 181 | |
|---|
| 182 | void parse_sr(rtcphdr* rh, int flags, u_char* ep, |
|---|
| [956] | 183 | Source* ps, Address & addr, int layer); |
|---|
| [900] | 184 | void parse_rr(rtcphdr* rh, int flags, u_char* ep, |
|---|
| [956] | 185 | Source* ps, Address & addr, int layer); |
|---|
| [4234] | 186 | void parse_xr(rtcphdr* rh, int flags, u_char* ep, |
|---|
| [4831] | 187 | Source* ps, Address & addr, int layer, bool ack_clock, pktbuf* pb=0); |
|---|
| [900] | 188 | void parse_rr_records(u_int32_t ssrc, rtcp_rr* r, int cnt, |
|---|
| 189 | const u_char* ep, Address & addr); |
|---|
| [4260] | 190 | void parse_xr_records(u_int32_t ssrc, rtcp_xr* xr, int cnt, |
|---|
| [4831] | 191 | const u_char* ep, Address & addr, bool ack_clock, pktbuf* pb); |
|---|
| [900] | 192 | int sdesbody(u_int32_t* p, u_char* ep, Source* ps, |
|---|
| [956] | 193 | Address & addr, u_int32_t ssrc, int layer); |
|---|
| [900] | 194 | void parse_sdes(rtcphdr* rh, int flags, u_char* ep, Source* ps, |
|---|
| [956] | 195 | Address & addr, u_int32_t ssrc, int layer); |
|---|
| [900] | 196 | void parse_bye(rtcphdr* rh, int flags, u_char* ep, Source* ps); |
|---|
| 197 | |
|---|
| 198 | int parseopts(const u_char* bp, int cc, Address & addr) const; |
|---|
| 199 | int ckid(const char*, int len); |
|---|
| 200 | |
|---|
| 201 | u_int32_t alloc_srcid(Address & addr) const; |
|---|
| 202 | |
|---|
| 203 | int lipSyncEnabled() { return (lipSyncEnabled_);} |
|---|
| 204 | void lipSyncEnabled(int v) { lipSyncEnabled_=v;} |
|---|
| 205 | |
|---|
| 206 | char* stats(char* cp) const; |
|---|
| 207 | |
|---|
| [956] | 208 | DataHandler dh_[NLAYER]; |
|---|
| 209 | CtrlHandler ch_[NLAYER]; |
|---|
| [900] | 210 | |
|---|
| 211 | |
|---|
| 212 | MBusHandler mb_; |
|---|
| 213 | |
|---|
| 214 | int lipSyncEnabled_; |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | u_int badversion_; |
|---|
| 218 | u_int badoptions_; |
|---|
| 219 | u_int badfmt_; |
|---|
| 220 | u_int badext_; |
|---|
| 221 | u_int nrunt_; |
|---|
| 222 | |
|---|
| 223 | u_int32_t last_np_; |
|---|
| 224 | u_int32_t sdes_seq_; |
|---|
| 225 | |
|---|
| 226 | double rtcp_inv_bw_; |
|---|
| 227 | double rtcp_avg_size_; |
|---|
| 228 | double rint_; |
|---|
| 229 | |
|---|
| 230 | int confid_; |
|---|
| 231 | |
|---|
| [956] | 232 | BufferPool* pool_; |
|---|
| [900] | 233 | u_char* pktbuf_; |
|---|
| [956] | 234 | |
|---|
| 235 | SourceManager *sm_; |
|---|
| [4233] | 236 | |
|---|
| 237 | |
|---|
| 238 | u_int16_t seqno_; |
|---|
| 239 | u_int16_t lastseq_; |
|---|
| [4246] | 240 | u_int16_t ackofack_; |
|---|
| [4233] | 241 | |
|---|
| 242 | |
|---|
| [4347] | 243 | u_int16_t ackvec_; |
|---|
| [4654] | 244 | |
|---|
| 245 | double recv_ts_; |
|---|
| [4730] | 246 | |
|---|
| 247 | private: |
|---|
| 248 | |
|---|
| 249 | inline void print_rtp_seqno(u_int16_t seqno) { |
|---|
| 250 | fprintf(stderr, "\n\tnow: %f\tseqno: %d\n\n",tx_get_now(),seqno); |
|---|
| 251 | } |
|---|
| 252 | inline void print_rtp_seqno(pktbuf* pb) { |
|---|
| 253 | rtphdr* rh = (rtphdr *) pb->data; |
|---|
| [4831] | 254 | fprintf(stderr, "\n\tnow: %f\tseqno: %d\n\n", |
|---|
| 255 | tx_get_now(),ntohs(rh->rh_seqno)); |
|---|
| [4730] | 256 | } |
|---|
| 257 | |
|---|
| [4804] | 258 | inline void sender_xr_info(const char* str, const int i, |
|---|
| [4831] | 259 | u_int16_t b, u_int16_t e, rtcp_xr_BT_1_hdr* xrh, u_int16_t l) { |
|---|
| [4804] | 260 | fprintf(stderr, " [%s +%d] beg:%d, end:%d, xr1len:%d (xrlen:%d)\n", |
|---|
| 261 | str, i, b, e, ntohs(xrh->xr_len),l); |
|---|
| [4730] | 262 | } |
|---|
| 263 | |
|---|
| 264 | inline void sender_xr_ts_info(double ts) { |
|---|
| 265 | fprintf(stderr, "*** recv_ts: %f so_rtime: %f diff: %f\n", |
|---|
| 266 | recv_ts_, ts, recv_ts_-ts); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| [4804] | 269 | inline void receiver_xr_info(const char* str, const int i, u_int16_t *c) { |
|---|
| 270 | fprintf(stderr, " [%s +%d] chunk[0]:%d\n",str, i, ntohs(c[0])); |
|---|
| [4730] | 271 | } |
|---|
| 272 | |
|---|
| 273 | inline void parse_xr_banner_top() { |
|---|
| 274 | fprintf(stderr, |
|---|
| 275 | "~~~~~~~~~~~~~~~~~~entering parse_xr_records()~~~~~~~~~~~~~~~~~~\n"); |
|---|
| 276 | } |
|---|
| 277 | inline void parse_xr_banner_bottom() { |
|---|
| 278 | fprintf(stderr, |
|---|
| 279 | "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n"); |
|---|
| 280 | } |
|---|
| [4831] | 281 | inline void xr_arrival_info(int nbytes, int i) { |
|---|
| 282 | fprintf(stderr, " \tnow: %f\tnbytes[%d]: %d\n",tx_get_now(),i,nbytes); |
|---|
| 283 | } |
|---|
| [900] | 284 | }; |
|---|
| 285 | |
|---|
| 286 | class AudioSessionManager : public SessionManager { |
|---|
| 287 | protected: |
|---|
| 288 | int check_format(int fmt) const; |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | class VideoSessionManager : public SessionManager { |
|---|
| 292 | protected: |
|---|
| 293 | int check_format(int fmt) const; |
|---|
| 294 | }; |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | #endif |
|---|