| 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 | static const char rcsid[] = |
|---|
| 37 | "@(#) $Header$ (LBL)"; |
|---|
| 38 | |
|---|
| 39 | #include "config.h" |
|---|
| 40 | #include <math.h> |
|---|
| 41 | #include <errno.h> |
|---|
| 42 | #include <string.h> |
|---|
| 43 | #ifdef WIN32 |
|---|
| 44 | extern "C" int getpid(); |
|---|
| 45 | #endif |
|---|
| 46 | #include "source.h" |
|---|
| 47 | #include "vic_tcl.h" |
|---|
| 48 | #include "media-timer.h" |
|---|
| 49 | #include "crypt.h" |
|---|
| 50 | #include "timer.h" |
|---|
| 51 | #include "ntp-time.h" |
|---|
| 52 | #include "session.h" |
|---|
| 53 | #include "cc/tfwc_sndr.h" |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | static class SessionMatcher : public Matcher { |
|---|
| 60 | public: |
|---|
| 61 | SessionMatcher() : Matcher("session") {} |
|---|
| 62 | TclObject* match(const char* id) { |
|---|
| 63 | if (strcmp(id, "audio/rtp") == 0) |
|---|
| 64 | return (new AudioSessionManager); |
|---|
| 65 | else if (strcmp(id, "video/rtp") == 0) |
|---|
| 66 | return (new VideoSessionManager); |
|---|
| 67 | return (0); |
|---|
| 68 | } |
|---|
| 69 | } session_matcher; |
|---|
| 70 | |
|---|
| 71 | int VideoSessionManager::check_format(int fmt) const |
|---|
| 72 | { |
|---|
| 73 | switch(fmt) { |
|---|
| 74 | case RTP_PT_RAW: |
|---|
| 75 | case RTP_PT_CELLB: |
|---|
| 76 | case RTP_PT_JPEG: |
|---|
| 77 | case RTP_PT_CUSEEME: |
|---|
| 78 | case RTP_PT_NV: |
|---|
| 79 | case RTP_PT_CPV: |
|---|
| 80 | case RTP_PT_H261: |
|---|
| 81 | case RTP_PT_BVC: |
|---|
| 82 | case RTP_PT_H261_COMPAT: |
|---|
| 83 | case RTP_PT_H263: |
|---|
| 84 | case RTP_PT_MPEG4: |
|---|
| 85 | case RTP_PT_H264: |
|---|
| 86 | case RTP_PT_H263P: |
|---|
| 87 | case RTP_PT_LDCT: |
|---|
| 88 | case RTP_PT_PVH: |
|---|
| 89 | case RTP_PT_DV: |
|---|
| 90 | #ifdef USE_H261AS |
|---|
| 91 | case RTP_PT_H261AS: |
|---|
| 92 | #endif |
|---|
| 93 | return (1); |
|---|
| 94 | } |
|---|
| 95 | return (0); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | int AudioSessionManager::check_format(int fmt) const |
|---|
| 99 | { |
|---|
| 100 | switch (fmt) { |
|---|
| 101 | case RTP_PT_PCMU: |
|---|
| 102 | case RTP_PT_CELP: |
|---|
| 103 | case RTP_PT_GSM: |
|---|
| 104 | case RTP_PT_DVI: |
|---|
| 105 | case RTP_PT_LPC: |
|---|
| 106 | return (1); |
|---|
| 107 | } |
|---|
| 108 | return (0); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | static SessionManager* manager; |
|---|
| 113 | |
|---|
| 114 | void |
|---|
| 115 | adios() |
|---|
| 116 | { |
|---|
| 117 | if (SourceManager::instance().localsrc() != 0) |
|---|
| 118 | manager->send_bye(); |
|---|
| 119 | exit(0); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | void DataHandler::dispatch(int) |
|---|
| 128 | { |
|---|
| 129 | sm_->recv(this); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void CtrlHandler::dispatch(int) |
|---|
| 133 | { |
|---|
| 134 | sm_->recv(this); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | CtrlHandler::CtrlHandler() |
|---|
| 138 | : ctrl_inv_bw_(0.), |
|---|
| 139 | ctrl_avg_size_(128.), |
|---|
| 140 | rint_(0.0) |
|---|
| 141 | { |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | inline void CtrlHandler::schedule_timer() |
|---|
| 145 | { |
|---|
| 146 | msched(int(fmod(double(random()), rint_) + rint_ * .5 + .5)); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void CtrlHandler::net(Network* n) |
|---|
| 150 | { |
|---|
| 151 | DataHandler::net(n); |
|---|
| 152 | cancel(); |
|---|
| 153 | if (n != 0) { |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | double rint = ctrl_avg_size_ * ctrl_inv_bw_; |
|---|
| 164 | if (rint < CTRL_MIN_RPT_TIME / 2. * 1000.) |
|---|
| 165 | rint = CTRL_MIN_RPT_TIME / 2. * 1000.; |
|---|
| 166 | rint_ = rint; |
|---|
| 167 | schedule_timer(); |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void CtrlHandler::sample_size(int cc) |
|---|
| 172 | { |
|---|
| 173 | ctrl_avg_size_ += CTRL_SIZE_GAIN * (double(cc + 28) - ctrl_avg_size_); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | void CtrlHandler::adapt(int nsrc, int nrr, int we_sent) |
|---|
| 177 | { |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | double ibw = ctrl_inv_bw_; |
|---|
| 187 | if (nrr) { |
|---|
| 188 | |
|---|
| 189 | if (we_sent) { |
|---|
| 190 | ibw *= 1./CTRL_SENDER_BW_FRACTION; |
|---|
| 191 | nsrc = nrr; |
|---|
| 192 | } else { |
|---|
| 193 | ibw *= 1./CTRL_RECEIVER_BW_FRACTION; |
|---|
| 194 | nsrc -= nrr; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | double rint = ctrl_avg_size_ * double(nsrc) * ibw; |
|---|
| 198 | if (rint < CTRL_MIN_RPT_TIME * 1000.) |
|---|
| 199 | rint = CTRL_MIN_RPT_TIME * 1000.; |
|---|
| 200 | rint_ = rint; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void CtrlHandler::timeout() |
|---|
| 204 | { |
|---|
| 205 | sm_->announce(this); |
|---|
| 206 | schedule_timer(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | SessionManager::SessionManager() |
|---|
| 211 | |
|---|
| 212 | : mb_(mbus_handler_engine, NULL), |
|---|
| 213 | lipSyncEnabled_(0), |
|---|
| 214 | badversion_(0), |
|---|
| 215 | badoptions_(0), |
|---|
| 216 | badfmt_(0), |
|---|
| 217 | badext_(0), |
|---|
| 218 | nrunt_(0), |
|---|
| 219 | last_np_(0), |
|---|
| 220 | sdes_seq_(0), |
|---|
| 221 | rtcp_inv_bw_(0.), |
|---|
| 222 | rtcp_avg_size_(128.), |
|---|
| 223 | confid_(-1), |
|---|
| 224 | seqno_(0), |
|---|
| 225 | lastseq_(0), |
|---|
| 226 | ackvec_(0) |
|---|
| 227 | { |
|---|
| 228 | |
|---|
| 229 | manager = this; |
|---|
| 230 | |
|---|
| 231 | for (int i = 0; i < NLAYER; ++i) { |
|---|
| 232 | dh_[i].manager(this); |
|---|
| 233 | ch_[i].manager(this); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | pktbuf_ = new u_char[2 * RTP_MTU]; |
|---|
| 238 | pool_ = new BufferPool; |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | double rint = rtcp_avg_size_ * rtcp_inv_bw_; |
|---|
| 250 | if (rint < RTCP_MIN_RPT_TIME / 2. * 1000.) |
|---|
| 251 | rint = RTCP_MIN_RPT_TIME / 2. * 1000.; |
|---|
| 252 | rint_ = rint; |
|---|
| 253 | |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | SessionManager::~SessionManager() |
|---|
| 257 | { |
|---|
| 258 | if (pktbuf_) |
|---|
| 259 | delete[] pktbuf_; |
|---|
| 260 | |
|---|
| 261 | delete pool_; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | u_int32_t SessionManager::alloc_srcid(Address & addr) const |
|---|
| 265 | { |
|---|
| 266 | timeval tv; |
|---|
| 267 | ::gettimeofday(&tv, 0); |
|---|
| 268 | u_int32_t srcid = u_int32_t(tv.tv_sec + tv.tv_usec); |
|---|
| 269 | srcid += (u_int32_t)getuid(); |
|---|
| 270 | srcid += (u_int32_t)getpid(); |
|---|
| 271 | |
|---|
| 272 | for(unsigned int i = 0; i < (addr.length() % sizeof(u_int32_t)); i++) { |
|---|
| 273 | srcid += ((u_int32_t*)((const void*)addr))[i]; |
|---|
| 274 | } |
|---|
| 275 | return (srcid); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | extern char* onestat(char* cp, const char* name, u_long v); |
|---|
| 279 | |
|---|
| 280 | char* SessionManager::stats(char* cp) const |
|---|
| 281 | { |
|---|
| 282 | cp = onestat(cp, "Bad-RTP-version", badversion_); |
|---|
| 283 | cp = onestat(cp, "Bad-RTPv1-options", badoptions_); |
|---|
| 284 | cp = onestat(cp, "Bad-Payload-Format", badfmt_); |
|---|
| 285 | cp = onestat(cp, "Bad-RTP-Extension", badext_); |
|---|
| 286 | cp = onestat(cp, "Runts", nrunt_); |
|---|
| 287 | Crypt* p = dh_[0].net()->crypt(); |
|---|
| 288 | if (p != 0) { |
|---|
| 289 | cp = onestat(cp, "Crypt-Bad-Length", p->badpktlen()); |
|---|
| 290 | cp = onestat(cp, "Crypt-Bad-P-Bit", p->badpbit()); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | if (ch_[0].net() != 0) { |
|---|
| 294 | Crypt* p = ch_[0].net()->crypt(); |
|---|
| 295 | if (p != 0) { |
|---|
| 296 | cp = onestat(cp, "Crypt-Ctrl-Bad-Length", p->badpktlen()); |
|---|
| 297 | cp = onestat(cp, "Crypt-Ctrl-Bad-P-Bit", p->badpbit()); |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | *--cp = 0; |
|---|
| 301 | return (cp); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | int SessionManager::command(int argc, const char*const* argv) |
|---|
| 305 | { |
|---|
| 306 | Tcl& tcl = Tcl::instance(); |
|---|
| 307 | char* cp = tcl.buffer(); |
|---|
| 308 | if (argc == 2) { |
|---|
| 309 | if (strcmp(argv[1], "active") == 0) { |
|---|
| 310 | SourceManager::instance().sortactive(cp); |
|---|
| 311 | tcl.result(cp); |
|---|
| 312 | return (TCL_OK); |
|---|
| 313 | } |
|---|
| 314 | if (strcmp(argv[1], "local-addr-heuristic") == 0) { |
|---|
| 315 | strcpy(cp, intoa(LookupLocalAddr())); |
|---|
| 316 | tcl.result(cp); |
|---|
| 317 | return (TCL_OK); |
|---|
| 318 | } |
|---|
| 319 | if (strcmp(argv[1], "stats") == 0) { |
|---|
| 320 | stats(cp); |
|---|
| 321 | tcl.result(cp); |
|---|
| 322 | return (TCL_OK); |
|---|
| 323 | } |
|---|
| 324 | if (strcmp(argv[1], "nb") == 0) { |
|---|
| 325 | sprintf(cp, "%u", 8 * nb_); |
|---|
| 326 | tcl.result(cp); |
|---|
| 327 | return (TCL_OK); |
|---|
| 328 | } |
|---|
| 329 | if (strcmp(argv[1], "nf") == 0) { |
|---|
| 330 | sprintf(cp, "%u", nf_); |
|---|
| 331 | tcl.result(cp); |
|---|
| 332 | return (TCL_OK); |
|---|
| 333 | } |
|---|
| 334 | if (strcmp(argv[1], "np") == 0 || |
|---|
| 335 | strcmp(argv[1], "ns") == 0) { |
|---|
| 336 | sprintf(cp, "%u", np_); |
|---|
| 337 | tcl.result(cp); |
|---|
| 338 | return (TCL_OK); |
|---|
| 339 | } |
|---|
| 340 | if (strcmp(argv[1], "lip-sync") == 0) { |
|---|
| 341 | sprintf(cp, "%u", lipSyncEnabled_); |
|---|
| 342 | tcl.result(cp); |
|---|
| 343 | return (TCL_OK); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | } else if (argc == 3) { |
|---|
| 347 | if (strcmp(argv[1], "sm") == 0) { |
|---|
| 348 | sm_ = (SourceManager*)TclObject::lookup(argv[2]); |
|---|
| 349 | return (TCL_OK); |
|---|
| 350 | } |
|---|
| 351 | if (strcmp(argv[1], "name") == 0) { |
|---|
| 352 | Source* s = SourceManager::instance().localsrc(); |
|---|
| 353 | s->sdes(RTCP_SDES_NAME, argv[2]); |
|---|
| 354 | return (TCL_OK); |
|---|
| 355 | } |
|---|
| 356 | if (strcmp(argv[1], "email") == 0) { |
|---|
| 357 | Source* s = SourceManager::instance().localsrc(); |
|---|
| 358 | s->sdes(RTCP_SDES_EMAIL, argv[2]); |
|---|
| 359 | return (TCL_OK); |
|---|
| 360 | } |
|---|
| 361 | if (strcmp(argv[1], "random-srcid") == 0) { |
|---|
| 362 | Address * addrp; |
|---|
| 363 | if ((addrp = (dh_[0].net())->alloc(argv[2])) !=0 ) { |
|---|
| 364 | sprintf(cp, "%u", alloc_srcid(*addrp)); |
|---|
| 365 | delete addrp; |
|---|
| 366 | } |
|---|
| 367 | tcl.result(cp); |
|---|
| 368 | return (TCL_OK); |
|---|
| 369 | } |
|---|
| 370 | if (strcmp(argv[1], "data-net") == 0) { |
|---|
| 371 | dh_[0].net((Network*)TclObject::lookup(argv[2])); |
|---|
| 372 | return (TCL_OK); |
|---|
| 373 | } |
|---|
| 374 | if (strcmp(argv[1], "ctrl-net") == 0) { |
|---|
| 375 | ch_[0].net((Network*)TclObject::lookup(argv[2])); |
|---|
| 376 | return (TCL_OK); |
|---|
| 377 | } |
|---|
| 378 | if (strcmp(argv[1], "max-bandwidth") == 0) { |
|---|
| 379 | double bw = atof(argv[2]) / 8.; |
|---|
| 380 | rtcp_inv_bw_ = 1. / (bw * RTCP_SESSION_BW_FRACTION); |
|---|
| 381 | return (TCL_OK); |
|---|
| 382 | } |
|---|
| 383 | if (strcmp(argv[1], "confid") == 0) { |
|---|
| 384 | confid_ = atoi(argv[2]); |
|---|
| 385 | return (TCL_OK); |
|---|
| 386 | } |
|---|
| 387 | if (strcmp(argv[1], "data-bandwidth") == 0) { |
|---|
| 388 | |
|---|
| 389 | bps(atoi(argv[2])); |
|---|
| 390 | return (TCL_OK); |
|---|
| 391 | } |
|---|
| 392 | if (strcmp(argv[1], "mtu") == 0) { |
|---|
| 393 | mtu_ = atoi(argv[2]); |
|---|
| 394 | return (TCL_OK); |
|---|
| 395 | } |
|---|
| 396 | if (strcmp(argv[1], "loopback") == 0) { |
|---|
| 397 | loopback_ = atoi(argv[2]); |
|---|
| 398 | return (TCL_OK); |
|---|
| 399 | } |
|---|
| 400 | if (strcmp(argv[1], "lip-sync") == 0) { |
|---|
| 401 | lipSyncEnabled_ = atoi(argv[2]); |
|---|
| 402 | return (TCL_OK); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | } else if (argc == 4) { |
|---|
| 406 | if (strcmp(argv[1], "data-net") == 0) { |
|---|
| 407 | u_int layer = atoi(argv[3]); |
|---|
| 408 | if (layer >= NLAYER) |
|---|
| 409 | abort(); |
|---|
| 410 | if (*argv[2] == 0) { |
|---|
| 411 | dh_[layer].net(0); |
|---|
| 412 | return (TCL_OK); |
|---|
| 413 | } |
|---|
| 414 | Network* net = (Network*)TclObject::lookup(argv[2]); |
|---|
| 415 | if (net == 0) { |
|---|
| 416 | tcl.resultf("no network %s", argv[2]); |
|---|
| 417 | return (TCL_ERROR); |
|---|
| 418 | } |
|---|
| 419 | if (net->rchannel() < 0) { |
|---|
| 420 | tcl.resultf("network not open"); |
|---|
| 421 | return (TCL_ERROR); |
|---|
| 422 | } |
|---|
| 423 | dh_[layer].net(net); |
|---|
| 424 | return (TCL_OK); |
|---|
| 425 | } |
|---|
| 426 | if (strcmp(argv[1], "ctrl-net") == 0) { |
|---|
| 427 | u_int layer = atoi(argv[3]); |
|---|
| 428 | if (layer >= NLAYER) |
|---|
| 429 | abort(); |
|---|
| 430 | if (*argv[2] == 0) { |
|---|
| 431 | ch_[layer].net(0); |
|---|
| 432 | return (TCL_OK); |
|---|
| 433 | } |
|---|
| 434 | Network* net = (Network*)TclObject::lookup(argv[2]); |
|---|
| 435 | if (net == 0) { |
|---|
| 436 | tcl.resultf("no network %s", argv[2]); |
|---|
| 437 | return (TCL_ERROR); |
|---|
| 438 | } |
|---|
| 439 | if (net->rchannel() < 0) { |
|---|
| 440 | tcl.resultf("network not open"); |
|---|
| 441 | return (TCL_ERROR); |
|---|
| 442 | } |
|---|
| 443 | ch_[layer].net(net); |
|---|
| 444 | return (TCL_OK); |
|---|
| 445 | } |
|---|
| 446 | return (Transmitter::command(argc, argv)); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | return (TCL_ERROR); |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | void SessionManager::transmit(pktbuf* pb) |
|---|
| 453 | { |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | if (pb->layer < loop_layer_) { |
|---|
| 459 | |
|---|
| 460 | Network* n = dh_[pb->layer].net(); |
|---|
| 461 | if (n != 0) |
|---|
| 462 | n->send(pb); |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | u_char* SessionManager::build_sdes_item(u_char* p, int code, Source& s) |
|---|
| 467 | { |
|---|
| 468 | const char* value = s.sdes(code); |
|---|
| 469 | if (value != 0) { |
|---|
| 470 | int len = strlen(value); |
|---|
| 471 | *p++ = code; |
|---|
| 472 | *p++ = len; |
|---|
| 473 | memcpy(p, value, len); |
|---|
| 474 | p += len; |
|---|
| 475 | } |
|---|
| 476 | return (p); |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | int SessionManager::build_sdes(rtcphdr* rh, Source& ls) |
|---|
| 480 | { |
|---|
| 481 | int flags = RTP_VERSION << 14 | 1 << 8 | RTCP_PT_SDES; |
|---|
| 482 | rh->rh_flags = htons(flags); |
|---|
| 483 | rh->rh_ssrc = ls.srcid(); |
|---|
| 484 | u_char* p = (u_char*)(rh + 1); |
|---|
| 485 | p = build_sdes_item(p, RTCP_SDES_CNAME, ls); |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | int nameslot, noteslot; |
|---|
| 498 | const char* note = ls.sdes(RTCP_SDES_NOTE); |
|---|
| 499 | if (note) { |
|---|
| 500 | if (*note) { |
|---|
| 501 | nameslot = RTCP_SDES_NOTE; |
|---|
| 502 | noteslot = RTCP_SDES_NAME; |
|---|
| 503 | } else { |
|---|
| 504 | nameslot = RTCP_SDES_NAME; |
|---|
| 505 | noteslot = RTCP_SDES_NOTE; |
|---|
| 506 | } |
|---|
| 507 | } else { |
|---|
| 508 | nameslot = RTCP_SDES_NAME; |
|---|
| 509 | noteslot = RTCP_SDES_NAME; |
|---|
| 510 | } |
|---|
| 511 | u_int seq = (++sdes_seq_) & 0x7; |
|---|
| 512 | switch (seq) { |
|---|
| 513 | |
|---|
| 514 | case 0: case 4: |
|---|
| 515 | p = build_sdes_item(p, RTCP_SDES_EMAIL, ls); |
|---|
| 516 | break; |
|---|
| 517 | |
|---|
| 518 | case 2: |
|---|
| 519 | p = build_sdes_item(p, noteslot, ls); |
|---|
| 520 | break; |
|---|
| 521 | case 6: |
|---|
| 522 | p = build_sdes_item(p, RTCP_SDES_TOOL, ls); |
|---|
| 523 | break; |
|---|
| 524 | default: |
|---|
| 525 | p = build_sdes_item(p, nameslot, ls); |
|---|
| 526 | } |
|---|
| 527 | int len = p - (u_char*)rh; |
|---|
| 528 | int pad = 4 - (len & 3); |
|---|
| 529 | len += pad; |
|---|
| 530 | rh->rh_len = htons((len >> 2) - 1); |
|---|
| 531 | while (--pad >= 0) |
|---|
| 532 | *p++ = 0; |
|---|
| 533 | |
|---|
| 534 | return (len); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | int SessionManager::build_app(rtcphdr* rh, Source& ls, const char *name, |
|---|
| 538 | void *data, int datalen) |
|---|
| 539 | { |
|---|
| 540 | int flags = RTP_VERSION << 14 | 1 << 8 | RTCP_PT_APP; |
|---|
| 541 | rh->rh_flags = htons(flags); |
|---|
| 542 | rh->rh_ssrc = ls.srcid(); |
|---|
| 543 | u_char* p = (u_char*)(rh + 1); |
|---|
| 544 | int len; |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | len = strlen(name); |
|---|
| 548 | if( len < 4 ) { |
|---|
| 549 | memcpy(p,name,len); |
|---|
| 550 | p += len; |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | while( p - (u_char*)(rh+1) < 4 ) |
|---|
| 554 | *p++ = 0; |
|---|
| 555 | } |
|---|
| 556 | else { |
|---|
| 557 | |
|---|
| 558 | memcpy(p,name,4); |
|---|
| 559 | p += 4; |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | memcpy(p,data,datalen); |
|---|
| 564 | p += datalen; |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | len = p - (u_char*)rh; |
|---|
| 568 | int pad = 4 - (len & 3); |
|---|
| 569 | while( --pad >= 0 ) |
|---|
| 570 | *p++ = 0; |
|---|
| 571 | len = p - (u_char*)rh; |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | rh->rh_len = htons((len >> 2) - 1); |
|---|
| 575 | |
|---|
| 576 | return (len); |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | void SessionManager::announce(CtrlHandler* ch) |
|---|
| 592 | { |
|---|
| 593 | send_report(ch, 0); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | void SessionManager::send_report(CtrlHandler* ch, int bye, int app) |
|---|
| 603 | { |
|---|
| 604 | UNUSED(app); |
|---|
| 605 | |
|---|
| 606 | SourceManager& sm = SourceManager::instance(); |
|---|
| 607 | Source& s = *sm.localsrc(); |
|---|
| 608 | rtcphdr* rh = (rtcphdr*)pktbuf_; |
|---|
| 609 | rh->rh_ssrc = s.srcid(); |
|---|
| 610 | int flags = RTP_VERSION << 14; |
|---|
| 611 | int layer = ch - ch_; |
|---|
| 612 | Source::Layer& sl = s.layer(layer); |
|---|
| 613 | timeval now = unixtime(); |
|---|
| 614 | sl.lts_ctrl(now); |
|---|
| 615 | int we_sent = 0; |
|---|
| 616 | rtcp_rr* rr; |
|---|
| 617 | rtcp_xr* xr; |
|---|
| 618 | Tcl& tcl = Tcl::instance(); |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | MediaTimer* mt = MediaTimer::instance(); |
|---|
| 627 | if (sl.np() != last_np_ && mt) { |
|---|
| 628 | last_np_ = sl.np(); |
|---|
| 629 | we_sent = 1; |
|---|
| 630 | flags |= RTCP_PT_SR; |
|---|
| 631 | rtcp_sr* sr = (rtcp_sr*)(rh + 1); |
|---|
| 632 | sr->sr_ntp = ntp64time(now); |
|---|
| 633 | HTONL(sr->sr_ntp.upper); |
|---|
| 634 | HTONL(sr->sr_ntp.lower); |
|---|
| 635 | sr->sr_ts = htonl(mt->ref_ts()); |
|---|
| 636 | sr->sr_np = htonl(sl.np()); |
|---|
| 637 | sr->sr_nb = htonl(sl.nb()); |
|---|
| 638 | rr = (rtcp_rr*)(sr + 1); |
|---|
| 639 | xr = (rtcp_xr*)(rr + 1); |
|---|
| 640 | } else { |
|---|
| 641 | flags |= RTCP_PT_RR; |
|---|
| 642 | rr = (rtcp_rr*)(rh + 1); |
|---|
| 643 | xr = (rtcp_xr*)(rr + 1); |
|---|
| 644 | } |
|---|
| 645 | int nrr = 0; |
|---|
| 646 | int nsrc = 0; |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | u_int inactive = u_int(ch->rint() * (32./1000.)); |
|---|
| 655 | if (inactive < 2) |
|---|
| 656 | inactive = 2; |
|---|
| 657 | for (Source* sp = sm.sources(); sp != 0; sp = sp->next_) { |
|---|
| 658 | ++nsrc; |
|---|
| 659 | Source::Layer& sl = sp->layer(layer); |
|---|
| 660 | |
|---|
| 661 | int received = sl.np() - sl.snp(); |
|---|
| 662 | if (received == 0) { |
|---|
| 663 | |
|---|
| 664 | if (u_int(now.tv_sec - sl.lts_ctrl().tv_sec) > inactive) |
|---|
| 665 | --nsrc; |
|---|
| 666 | continue; |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | sl.snp(sl.np()); |
|---|
| 670 | rr->rr_srcid = sp->srcid(); |
|---|
| 671 | |
|---|
| 672 | int expected = sl.ns() - sl.sns(); |
|---|
| 673 | |
|---|
| 674 | sl.sns(sl.ns()); |
|---|
| 675 | u_int32_t v; |
|---|
| 676 | int lost = expected - received; |
|---|
| 677 | if (lost <= 0) |
|---|
| 678 | v = 0; |
|---|
| 679 | else |
|---|
| 680 | |
|---|
| 681 | v = ((lost << 8) / expected) << 24; |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | v |= (sl.ns() - sl.np()) & 0xffffff; |
|---|
| 685 | rr->rr_loss = htonl(v); |
|---|
| 686 | |
|---|
| 687 | rr->rr_ehsr = htonl(sl.ehs()); |
|---|
| 688 | rr->rr_dv = (sp->handler() != 0) ? sp->handler()->delvar() : 0; |
|---|
| 689 | |
|---|
| 690 | rr->rr_lsr = htonl(sl.sts_ctrl()); |
|---|
| 691 | |
|---|
| 692 | if (sl.lts_ctrl().tv_sec == 0) |
|---|
| 693 | rr->rr_dlsr = 0; |
|---|
| 694 | else { |
|---|
| 695 | u_int32_t ntp_now = ntptime(now); |
|---|
| 696 | |
|---|
| 697 | u_int32_t ntp_then = ntptime(sl.lts_ctrl()); |
|---|
| 698 | rr->rr_dlsr = htonl(ntp_now - ntp_then); |
|---|
| 699 | } |
|---|
| 700 | ++rr; |
|---|
| 701 | if (++nrr >= 31) |
|---|
| 702 | break; |
|---|
| 703 | } |
|---|
| 704 | flags |= nrr << 8; |
|---|
| 705 | rh->rh_flags = htons(flags); |
|---|
| 706 | int len = (u_char*)rr - pktbuf_; |
|---|
| 707 | rh->rh_len = htons((len >> 2) - 1); |
|---|
| 708 | |
|---|
| 709 | if (bye) |
|---|
| 710 | len += build_bye((rtcphdr*)rr, s); |
|---|
| 711 | else |
|---|
| 712 | len += build_sdes((rtcphdr*)rr, s); |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | const char *data = tcl.attr("site"); |
|---|
| 716 | if(data) |
|---|
| 717 | { |
|---|
| 718 | rr = (rtcp_rr*)(((u_char*)rh) + len); |
|---|
| 719 | len += build_app((rtcphdr*)rr, s, "site", (void *)data, strlen(data)); |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | ch->send(pktbuf_, len); |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | |
|---|
| 727 | |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | ch->adapt(nsrc, nrr, we_sent); |
|---|
| 754 | ch->sample_size(len); |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | if (layer == 0) |
|---|
| 758 | sm.CheckActiveSources(ch->rint()); |
|---|
| 759 | |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | int SessionManager::build_bye(rtcphdr* rh, Source& ls) |
|---|
| 763 | { |
|---|
| 764 | int flags = RTP_VERSION << 14 | 1 << 8 | RTCP_PT_BYE; |
|---|
| 765 | rh->rh_flags = ntohs(flags); |
|---|
| 766 | rh->rh_len = htons(1); |
|---|
| 767 | rh->rh_ssrc = ls.srcid(); |
|---|
| 768 | return (8); |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | void SessionManager::recv(DataHandler* dh) |
|---|
| 775 | { |
|---|
| 776 | int layer = dh - dh_; |
|---|
| 777 | pktbuf* pb = pool_->alloc(layer); |
|---|
| 778 | Address * addrp; |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | int cc = dh->recv(pb->data, sizeof(pb->data), addrp); |
|---|
| 785 | |
|---|
| 786 | if (cc <= 0) { |
|---|
| 787 | pb->release(); |
|---|
| 788 | return; |
|---|
| 789 | } |
|---|
| 790 | |
|---|
| 791 | rtphdr* rh = (rtphdr*)pb->data; |
|---|
| 792 | seqno_ = ntohs(rh->rh_seqno); |
|---|
| 793 | debug_msg("received seqno: %d\n", seqno_); |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | if (!loopback_) { |
|---|
| 797 | |
|---|
| 798 | SourceManager& sm = SourceManager::instance(); |
|---|
| 799 | if (rh->rh_ssrc == (*sm.localsrc()).srcid()) { |
|---|
| 800 | debug_msg("(loopback) seqno: %d\n", seqno_); |
|---|
| 801 | pb->release(); |
|---|
| 802 | return; |
|---|
| 803 | } |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | for (int i = lastseq_+1; i <= seqno_; i++) { |
|---|
| 808 | SET_BIT_VEC (ackvec_, 1); |
|---|
| 809 | } |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | bool isThere; |
|---|
| 813 | debug_msg("XXX received ackvec:"); |
|---|
| 814 | for (int i = lastseq_+1; i <= seqno_; i++) { |
|---|
| 815 | isThere = SEE_BIT_VEC (ackvec_, i, seqno_); |
|---|
| 816 | printf(" %d... %s ", seqno_, isThere ? "Ok" : "NOk"); |
|---|
| 817 | } |
|---|
| 818 | printf("\n"); |
|---|
| 819 | lastseq_ = seqno_; |
|---|
| 820 | |
|---|
| 821 | int version = pb->data[0] >> 6; |
|---|
| 822 | |
|---|
| 823 | if (version != 2) { |
|---|
| 824 | ++badversion_; |
|---|
| 825 | pb->release(); |
|---|
| 826 | return; |
|---|
| 827 | } |
|---|
| 828 | if (cc < (int)sizeof(rtphdr)) { |
|---|
| 829 | ++nrunt_; |
|---|
| 830 | pb->release(); |
|---|
| 831 | return; |
|---|
| 832 | } |
|---|
| 833 | pb->len = cc; |
|---|
| 834 | |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | demux(pb, *addrp); |
|---|
| 838 | } |
|---|
| 839 | |
|---|
| 840 | void SessionManager::demux(pktbuf* pb, Address & addr) |
|---|
| 841 | { |
|---|
| 842 | rtphdr* rh = (rtphdr*)pb->data; |
|---|
| 843 | u_int32_t srcid = rh->rh_ssrc; |
|---|
| 844 | int flags = ntohs(rh->rh_flags); |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | |
|---|
| 848 | if ((flags & RTP_X) != 0) { |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | ++badext_; |
|---|
| 854 | pb->release(); |
|---|
| 855 | return; |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | |
|---|
| 860 | |
|---|
| 861 | |
|---|
| 862 | int fmt = flags & 0x7f; |
|---|
| 863 | if (!check_format(fmt)) { |
|---|
| 864 | ++badfmt_; |
|---|
| 865 | pb->release(); |
|---|
| 866 | return; |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | SourceManager& sm = SourceManager::instance(); |
|---|
| 870 | u_int16_t seqno = ntohs(rh->rh_seqno); |
|---|
| 871 | Source* s = sm.demux(srcid, addr, seqno, pb->layer); |
|---|
| 872 | if (s == 0) { |
|---|
| 873 | |
|---|
| 874 | |
|---|
| 875 | |
|---|
| 876 | |
|---|
| 877 | |
|---|
| 878 | |
|---|
| 879 | pb->release(); |
|---|
| 880 | return; |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | s->mbus(&mb_); |
|---|
| 884 | |
|---|
| 885 | Source::Layer& sl = s->layer(pb->layer); |
|---|
| 886 | timeval now = unixtime(); |
|---|
| 887 | |
|---|
| 888 | sl.lts_data(now); |
|---|
| 889 | |
|---|
| 890 | sl.sts_data(rh->rh_ts); |
|---|
| 891 | |
|---|
| 892 | |
|---|
| 893 | int cnt = (flags >> 8) & 0xf; |
|---|
| 894 | if (cnt > 0) { |
|---|
| 895 | |
|---|
| 896 | rtphdr hdr = *rh; |
|---|
| 897 | pb->dp += (cnt << 2); |
|---|
| 898 | pb->len -= (cnt << 2); |
|---|
| 899 | u_int32_t* bp = (u_int32_t*)(rh + 1); |
|---|
| 900 | while (--cnt >= 0) { |
|---|
| 901 | u_int32_t csrc = *(u_int32_t*)bp; |
|---|
| 902 | bp += 4; |
|---|
| 903 | Source* cs = sm.lookup(csrc, srcid, addr); |
|---|
| 904 | |
|---|
| 905 | cs->layer(pb->layer).lts_data(now); |
|---|
| 906 | cs->action(); |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | |
|---|
| 910 | |
|---|
| 911 | rh = (rtphdr*)pb->dp; |
|---|
| 912 | *rh = hdr; |
|---|
| 913 | } else |
|---|
| 914 | s->action(); |
|---|
| 915 | |
|---|
| 916 | if (s->sync() && lipSyncEnabled()) { |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | |
|---|
| 927 | |
|---|
| 928 | |
|---|
| 929 | |
|---|
| 930 | |
|---|
| 931 | int dup = sl.cs(seqno, s); |
|---|
| 932 | sl.np(1); |
|---|
| 933 | sl.nb(pb->len); |
|---|
| 934 | if (dup) { |
|---|
| 935 | pb->release(); |
|---|
| 936 | return; |
|---|
| 937 | } |
|---|
| 938 | if (flags & RTP_M) |
|---|
| 939 | |
|---|
| 940 | sl.nf(1); |
|---|
| 941 | |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | |
|---|
| 945 | pktbuf_ = (u_char*)new_blk(); |
|---|
| 946 | } |
|---|
| 947 | |
|---|
| 948 | else { |
|---|
| 949 | |
|---|
| 950 | |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | |
|---|
| 957 | PacketHandler* h = s->handler(); |
|---|
| 958 | if (h == 0) |
|---|
| 959 | h = s->activate(fmt); |
|---|
| 960 | else if (s->format() != fmt) |
|---|
| 961 | h = s->change_format(fmt); |
|---|
| 962 | |
|---|
| 963 | |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | int dup = sl.cs(seqno, s); |
|---|
| 971 | sl.np(1); |
|---|
| 972 | sl.nb(pb->len); |
|---|
| 973 | if (dup){ |
|---|
| 974 | pb->release(); |
|---|
| 975 | return; |
|---|
| 976 | } |
|---|
| 977 | if (flags & RTP_M) |
|---|
| 978 | |
|---|
| 979 | sl.nf(1); |
|---|
| 980 | #ifdef notdef |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | int hlen = h->hdrlen(); |
|---|
| 985 | cc -= hlen; |
|---|
| 986 | if (cc < 0) { |
|---|
| 987 | |
|---|
| 988 | sl.runt(1); |
|---|
| 989 | pb->release(); |
|---|
| 990 | return; |
|---|
| 991 | } |
|---|
| 992 | #endif |
|---|
| 993 | if (s->mute()) { |
|---|
| 994 | pb->release(); |
|---|
| 995 | return; |
|---|
| 996 | } |
|---|
| 997 | |
|---|
| 998 | h->recv(pb); |
|---|
| 999 | } |
|---|
| 1000 | |
|---|
| 1001 | } |
|---|
| 1002 | |
|---|
| 1003 | void SessionManager::parse_rr_records(u_int32_t, rtcp_rr*, int, |
|---|
| 1004 | const u_char*, Address &) |
|---|
| 1005 | { |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | |
|---|
| 1009 | void SessionManager::parse_sr(rtcphdr* rh, int flags, u_char*ep, |
|---|
| 1010 | Source* ps, Address & addr, int layer) |
|---|
| 1011 | { |
|---|
| 1012 | rtcp_sr* sr = (rtcp_sr*)(rh + 1); |
|---|
| 1013 | Source* s; |
|---|
| 1014 | u_int32_t ssrc = rh->rh_ssrc; |
|---|
| 1015 | if (ps->srcid() != ssrc) |
|---|
| 1016 | s = SourceManager::instance().lookup(ssrc, ssrc, addr); |
|---|
| 1017 | else |
|---|
| 1018 | s = ps; |
|---|
| 1019 | |
|---|
| 1020 | Source::Layer& sl = s->layer(layer); |
|---|
| 1021 | |
|---|
| 1022 | timeval now = unixtime(); |
|---|
| 1023 | |
|---|
| 1024 | sl.lts_ctrl(now); |
|---|
| 1025 | sl.sts_ctrl(ntohl(sr->sr_ntp.upper) << 16 | |
|---|
| 1026 | ntohl(sr->sr_ntp.lower) >> 16); |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | |
|---|
| 1030 | |
|---|
| 1031 | |
|---|
| 1032 | |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | s->rtp_ctrl(ntohl(sr->sr_ts)); |
|---|
| 1036 | u_int32_t t = ntptime(now); |
|---|
| 1037 | s->map_ntp_time(t); |
|---|
| 1038 | s->map_rtp_time(s->convert_time(t)); |
|---|
| 1039 | s->rtp2ntp(1); |
|---|
| 1040 | |
|---|
| 1041 | |
|---|
| 1042 | int cnt = flags >> 8 & 0x1f; |
|---|
| 1043 | parse_rr_records(ssrc, (rtcp_rr*)(sr + 1), cnt, ep, addr); |
|---|
| 1044 | } |
|---|
| 1045 | |
|---|
| 1046 | void SessionManager::parse_rr(rtcphdr* rh, int flags, u_char* ep, |
|---|
| 1047 | Source* ps, Address & addr, int layer) |
|---|
| 1048 | { |
|---|
| 1049 | Source* s; |
|---|
| 1050 | u_int32_t ssrc = rh->rh_ssrc; |
|---|
| 1051 | if (ps->srcid() != ssrc) |
|---|
| 1052 | s = SourceManager::instance().lookup(ssrc, ssrc, addr); |
|---|
| 1053 | else |
|---|
| 1054 | s = ps; |
|---|
| 1055 | |
|---|
| 1056 | s->layer(layer).lts_ctrl(unixtime()); |
|---|
| 1057 | int cnt = flags >> 8 & 0x1f; |
|---|
| 1058 | parse_rr_records(ssrc, (rtcp_rr*)(rh + 1), cnt, ep, addr); |
|---|
| 1059 | } |
|---|
| 1060 | |
|---|
| 1061 | void SessionManager::parse_xr(rtcphdr* rh, int flags, u_char* ep, |
|---|
| 1062 | Source* ps, Address & addr, int layer) |
|---|
| 1063 | { |
|---|
| 1064 | |
|---|
| 1065 | Source* s; |
|---|
| 1066 | u_int32_t ssrc = rh->rh_ssrc; |
|---|
| 1067 | if (ps->srcid() != ssrc) |
|---|
| 1068 | s = SourceManager::instance().lookup(ssrc, ssrc, addr); |
|---|
| 1069 | else |
|---|
| 1070 | s = ps; |
|---|
| 1071 | |
|---|
| 1072 | s->layer(layer).lts_ctrl(unixtime()); |
|---|
| 1073 | int cnt = flags >> 8 & 0x1f; |
|---|
| 1074 | parse_xr_records(ssrc, (rtcp_xr*)(rh + 1), cnt, ep, addr); |
|---|
| 1075 | } |
|---|
| 1076 | |
|---|
| 1077 | void SessionManager::parse_xr_records(u_int32_t ssrc, rtcp_xr* r, int cnt, |
|---|
| 1078 | const u_char* ep, Address & addr) |
|---|
| 1079 | { |
|---|
| 1080 | debug_msg("XXX parse_xr_records\n"); |
|---|
| 1081 | UNUSED(cnt); |
|---|
| 1082 | UNUSED(ep); |
|---|
| 1083 | UNUSED(addr); |
|---|
| 1084 | |
|---|
| 1085 | ackvec_ = r->xr_ackvec; |
|---|
| 1086 | |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | } |
|---|
| 1091 | |
|---|
| 1092 | int SessionManager::sdesbody(u_int32_t* p, u_char* ep, Source* ps, |
|---|
| 1093 | Address & addr, u_int32_t ssrc, int layer) |
|---|
| 1094 | { |
|---|
| 1095 | Source* s; |
|---|
| 1096 | u_int32_t srcid = *p; |
|---|
| 1097 | if (ps->srcid() != srcid) |
|---|
| 1098 | s = SourceManager::instance().lookup(srcid, ssrc, addr); |
|---|
| 1099 | else |
|---|
| 1100 | s = ps; |
|---|
| 1101 | if (s == 0) |
|---|
| 1102 | return (0); |
|---|
| 1103 | |
|---|
| 1104 | |
|---|
| 1105 | |
|---|
| 1106 | |
|---|
| 1107 | |
|---|
| 1108 | s->layer(layer).lts_ctrl(unixtime()); |
|---|
| 1109 | |
|---|
| 1110 | u_char* cp = (u_char*)(p + 1); |
|---|
| 1111 | while (cp < ep) { |
|---|
| 1112 | char buf[256]; |
|---|
| 1113 | |
|---|
| 1114 | u_int type = cp[0]; |
|---|
| 1115 | if (type == 0) { |
|---|
| 1116 | |
|---|
| 1117 | return (((cp - (u_char*)p) >> 2) + 1); |
|---|
| 1118 | } |
|---|
| 1119 | u_int len = cp[1]; |
|---|
| 1120 | u_char* eopt = cp + len + 2; |
|---|
| 1121 | if (eopt > ep) |
|---|
| 1122 | return (0); |
|---|
| 1123 | |
|---|
| 1124 | if (type >= RTCP_SDES_MIN && type <= RTCP_SDES_MAX) { |
|---|
| 1125 | memcpy(buf, (char*)&cp[2], len); |
|---|
| 1126 | buf[len] = 0; |
|---|
| 1127 | s->sdes(type, buf); |
|---|
| 1128 | } |
|---|
| 1129 | ; |
|---|
| 1130 | |
|---|
| 1131 | cp = eopt; |
|---|
| 1132 | } |
|---|
| 1133 | return (0); |
|---|
| 1134 | } |
|---|
| 1135 | |
|---|
| 1136 | void SessionManager::parse_sdes(rtcphdr* rh, int flags, u_char* ep, Source* ps, |
|---|
| 1137 | Address & addr, u_int32_t ssrc, int layer) |
|---|
| 1138 | { |
|---|
| 1139 | int cnt = flags >> 8 & 0x1f; |
|---|
| 1140 | u_int32_t* p = (u_int32_t*)&rh->rh_ssrc; |
|---|
| 1141 | while (--cnt >= 0 && (u_char*)p < ep) { |
|---|
| 1142 | int n = sdesbody(p, ep, ps, addr, ssrc, layer); |
|---|
| 1143 | if (n == 0) |
|---|
| 1144 | break; |
|---|
| 1145 | p += n; |
|---|
| 1146 | } |
|---|
| 1147 | if (cnt >= 0) |
|---|
| 1148 | ps->badsdes(1); |
|---|
| 1149 | } |
|---|
| 1150 | |
|---|
| 1151 | void SessionManager::parse_bye(rtcphdr* rh, int flags, u_char* ep, Source* ps) |
|---|
| 1152 | { |
|---|
| 1153 | int cnt = flags >> 8 & 0x1f; |
|---|
| 1154 | u_int32_t* p = (u_int32_t*)&rh->rh_ssrc; |
|---|
| 1155 | |
|---|
| 1156 | while (--cnt >= 0) { |
|---|
| 1157 | if (p >= (u_int32_t*)ep) { |
|---|
| 1158 | ps->badbye(1); |
|---|
| 1159 | return; |
|---|
| 1160 | } |
|---|
| 1161 | Source* s; |
|---|
| 1162 | if (ps->srcid() != rh->rh_ssrc) |
|---|
| 1163 | s = SourceManager::instance().consult(*p); |
|---|
| 1164 | else |
|---|
| 1165 | s = ps; |
|---|
| 1166 | if (s != 0) |
|---|
| 1167 | s->lts_done(unixtime()); |
|---|
| 1168 | ++p; |
|---|
| 1169 | } |
|---|
| 1170 | } |
|---|
| 1171 | |
|---|
| 1172 | |
|---|
| 1173 | |
|---|
| 1174 | |
|---|
| 1175 | void SessionManager::recv(CtrlHandler* ch) |
|---|
| 1176 | { |
|---|
| 1177 | Address * srcp; |
|---|
| 1178 | int cc = ch->recv(pktbuf_, 2 * RTP_MTU, srcp); |
|---|
| 1179 | if (cc <= 0) |
|---|
| 1180 | return; |
|---|
| 1181 | |
|---|
| 1182 | rtcphdr* rh = (rtcphdr*)pktbuf_; |
|---|
| 1183 | |
|---|
| 1184 | |
|---|
| 1185 | if (!loopback_) { |
|---|
| 1186 | SourceManager& sm = SourceManager::instance(); |
|---|
| 1187 | if (rh->rh_ssrc == (*sm.localsrc()).srcid()) |
|---|
| 1188 | return; |
|---|
| 1189 | } |
|---|
| 1190 | |
|---|
| 1191 | if (cc < int(sizeof(*rh))) { |
|---|
| 1192 | ++nrunt_; |
|---|
| 1193 | return; |
|---|
| 1194 | } |
|---|
| 1195 | |
|---|
| 1196 | |
|---|
| 1197 | |
|---|
| 1198 | |
|---|
| 1199 | switch(ntohs(rh->rh_flags) & 0xc0ff) { |
|---|
| 1200 | case RTP_VERSION << 14 | RTCP_PT_SR: |
|---|
| 1201 | case RTP_VERSION << 14 | RTCP_PT_RR: |
|---|
| 1202 | case RTP_VERSION << 14 | RTCP_PT_XR: |
|---|
| 1203 | case RTP_VERSION << 14 | RTCP_PT_BYE: |
|---|
| 1204 | break; |
|---|
| 1205 | default: |
|---|
| 1206 | |
|---|
| 1207 | |
|---|
| 1208 | |
|---|
| 1209 | |
|---|
| 1210 | |
|---|
| 1211 | ++badversion_; |
|---|
| 1212 | return; |
|---|
| 1213 | } |
|---|
| 1214 | |
|---|
| 1215 | |
|---|
| 1216 | |
|---|
| 1217 | |
|---|
| 1218 | rtcp_avg_size_ += RTCP_SIZE_GAIN * (double(cc + 28) - rtcp_avg_size_); |
|---|
| 1219 | Address & addr = *srcp; |
|---|
| 1220 | |
|---|
| 1221 | |
|---|
| 1222 | |
|---|
| 1223 | |
|---|
| 1224 | |
|---|
| 1225 | |
|---|
| 1226 | |
|---|
| 1227 | u_int32_t ssrc = rh->rh_ssrc; |
|---|
| 1228 | Source* ps = SourceManager::instance().lookup(ssrc, ssrc, addr); |
|---|
| 1229 | if (ps == 0) |
|---|
| 1230 | return; |
|---|
| 1231 | |
|---|
| 1232 | int layer = ch - ch_; |
|---|
| 1233 | |
|---|
| 1234 | |
|---|
| 1235 | |
|---|
| 1236 | |
|---|
| 1237 | |
|---|
| 1238 | u_char* epack = (u_char*)rh + cc; |
|---|
| 1239 | while ((u_char*)rh < epack) { |
|---|
| 1240 | u_int len = (ntohs(rh->rh_len) << 2) + 4; |
|---|
| 1241 | u_char* ep = (u_char*)rh + len; |
|---|
| 1242 | if (ep > epack) { |
|---|
| 1243 | ps->badsesslen(1); |
|---|
| 1244 | return; |
|---|
| 1245 | } |
|---|
| 1246 | u_int flags = ntohs(rh->rh_flags); |
|---|
| 1247 | if (flags >> 14 != RTP_VERSION) { |
|---|
| 1248 | ps->badsessver(1); |
|---|
| 1249 | return; |
|---|
| 1250 | } |
|---|
| 1251 | switch (flags & 0xff) { |
|---|
| 1252 | |
|---|
| 1253 | case RTCP_PT_SR: |
|---|
| 1254 | parse_sr(rh, flags, ep, ps, addr, layer); |
|---|
| 1255 | break; |
|---|
| 1256 | |
|---|
| 1257 | case RTCP_PT_RR: |
|---|
| 1258 | parse_rr(rh, flags, ep, ps, addr, layer); |
|---|
| 1259 | break; |
|---|
| 1260 | |
|---|
| 1261 | case RTCP_PT_XR: |
|---|
| 1262 | parse_xr(rh, flags, ep, ps, addr, layer); |
|---|
| 1263 | break; |
|---|
| 1264 | |
|---|
| 1265 | case RTCP_PT_SDES: |
|---|
| 1266 | parse_sdes(rh, flags, ep, ps, addr, ssrc, layer); |
|---|
| 1267 | break; |
|---|
| 1268 | |
|---|
| 1269 | case RTCP_PT_BYE: |
|---|
| 1270 | parse_bye(rh, flags, ep, ps); |
|---|
| 1271 | break; |
|---|
| 1272 | |
|---|
| 1273 | default: |
|---|
| 1274 | ps->badsessopt(1); |
|---|
| 1275 | break; |
|---|
| 1276 | } |
|---|
| 1277 | rh = (rtcphdr*)ep; |
|---|
| 1278 | } |
|---|
| 1279 | return; |
|---|
| 1280 | } |
|---|