| 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 | static const char rcsid[] = |
|---|
| 36 | "@(#) $Header$ (LBL)"; |
|---|
| 37 | |
|---|
| 38 | #include <stdio.h> |
|---|
| 39 | #include <stdlib.h> |
|---|
| 40 | #ifndef WIN32 |
|---|
| 41 | #include <unistd.h> |
|---|
| 42 | #endif |
|---|
| 43 | #include <errno.h> |
|---|
| 44 | #include <string.h> |
|---|
| 45 | #ifdef WIN32 |
|---|
| 46 | |
|---|
| 47 | #include <io.h> |
|---|
| 48 | #include <sys/stat.h> |
|---|
| 49 | #else |
|---|
| 50 | #include <sys/param.h> |
|---|
| 51 | #include <sys/uio.h> |
|---|
| 52 | #include <netinet/in.h> |
|---|
| 53 | #include <sys/file.h> |
|---|
| 54 | #include <sys/stat.h> |
|---|
| 55 | #endif |
|---|
| 56 | #include "ntp-time.h" |
|---|
| 57 | #include "pktbuf-rtp.h" |
|---|
| 58 | #include "transmitter.h" |
|---|
| 59 | #include "net.h" |
|---|
| 60 | #include "source.h" |
|---|
| 61 | #include "decoder.h" |
|---|
| 62 | #include "vic_tcl.h" |
|---|
| 63 | |
|---|
| 64 | #if defined(sun) && !defined(__svr4__) || (defined(_AIX) && !defined(_AIX41)) |
|---|
| 65 | extern "C" writev(int, iovec*, int); |
|---|
| 66 | #endif |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | int Transmitter::nbufs_; |
|---|
| 71 | int Transmitter::nhdrs_; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | u_int16_t Transmitter::seqno_ = 1; |
|---|
| 81 | |
|---|
| 82 | Transmitter::Transmitter() : |
|---|
| 83 | mtu_(1024), |
|---|
| 84 | nf_(0), |
|---|
| 85 | nb_(0), |
|---|
| 86 | np_(0), |
|---|
| 87 | kbps_(128), |
|---|
| 88 | nextpkttime_(0.), |
|---|
| 89 | busy_(0), |
|---|
| 90 | head_(0), |
|---|
| 91 | tail_(0), |
|---|
| 92 | loop_layer_(1000), |
|---|
| 93 | loopback_(0), |
|---|
| 94 | is_cc_active_(1), |
|---|
| 95 | is_buf_empty_(1), |
|---|
| 96 | cc_type_(WBCC) |
|---|
| 97 | { |
|---|
| 98 | memset((char*)&mh_, 0, sizeof(mh_)); |
|---|
| 99 | mh_.msg_iovlen = 2; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | epc_ = 0; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | inline double Transmitter::gettimeofday_secs() const |
|---|
| 107 | { |
|---|
| 108 | timeval tv; |
|---|
| 109 | ::gettimeofday(&tv, 0); |
|---|
| 110 | return (tv.tv_sec + 1e-6 * tv.tv_usec); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void Transmitter::loopback(pktbuf* pb) |
|---|
| 114 | { |
|---|
| 115 | int layer = pb->layer; |
|---|
| 116 | rtphdr* rh = (rtphdr*)pb->data; |
|---|
| 117 | int cc = pb->len; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | if (layer >= loop_layer_) { |
|---|
| 122 | |
|---|
| 123 | pb->release(); |
|---|
| 124 | return; |
|---|
| 125 | } |
|---|
| 126 | nb_ += cc; |
|---|
| 127 | ++np_; |
|---|
| 128 | |
|---|
| 129 | SourceManager& sm = SourceManager::instance(); |
|---|
| 130 | Source* s = sm.localsrc(); |
|---|
| 131 | timeval now = unixtime(); |
|---|
| 132 | Source::Layer& sl = s->layer(pb->layer); |
|---|
| 133 | |
|---|
| 134 | sl.lts_data(now); |
|---|
| 135 | s->action(); |
|---|
| 136 | sl.sts_data(rh->rh_ts); |
|---|
| 137 | sl.np(1); |
|---|
| 138 | sl.nb(cc); |
|---|
| 139 | sl.cs((u_int16_t)ntohs(rh->rh_seqno),s); |
|---|
| 140 | |
|---|
| 141 | int flags = ntohs(rh->rh_flags); |
|---|
| 142 | if (flags & RTP_M) { |
|---|
| 143 | ++nf_; |
|---|
| 144 | sl.nf(1); |
|---|
| 145 | } |
|---|
| 146 | int fmt = flags & 0x7f; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | PacketHandler* h = s->handler(); |
|---|
| 152 | if (h == 0) |
|---|
| 153 | h = s->activate(fmt); |
|---|
| 154 | else if (s->format() != fmt) { |
|---|
| 155 | h = s->change_format(fmt); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | if (s->mute()) { |
|---|
| 159 | pb->release(); |
|---|
| 160 | return; |
|---|
| 161 | } |
|---|
| 162 | h->recv(pb); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | int Transmitter::dumpfd_ = -1; |
|---|
| 166 | void Transmitter::dump(int fd) |
|---|
| 167 | { |
|---|
| 168 | dumpfd_ = fd; |
|---|
| 169 | #define MAGIC "RTPCLIP 1.0" |
|---|
| 170 | (void)write(fd, MAGIC, sizeof(MAGIC)); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | #ifdef WIN32 |
|---|
| 175 | int writev(int fd, iovec* iov, int iovlen) |
|---|
| 176 | { |
|---|
| 177 | int len = 0, n; |
|---|
| 178 | for (int i = 0; i < iovlen; i++) { |
|---|
| 179 | if ((n = write(fd, iov[i].iov_base, iov[i].iov_len)) == -1) { |
|---|
| 180 | perror("writev"); |
|---|
| 181 | exit(1); |
|---|
| 182 | } |
|---|
| 183 | len += n; |
|---|
| 184 | } |
|---|
| 185 | return(len); |
|---|
| 186 | } |
|---|
| 187 | #endif |
|---|
| 188 | |
|---|
| 189 | void Transmitter::dump(int fd, iovec* iov, int iovlen) const |
|---|
| 190 | { |
|---|
| 191 | register int length = 0; |
|---|
| 192 | for (int i = iovlen; --i >= 0; ) |
|---|
| 193 | length += iov[i].iov_len; |
|---|
| 194 | |
|---|
| 195 | char cliphdr[4]; |
|---|
| 196 | *(short*)cliphdr = htons(length); |
|---|
| 197 | cliphdr[2] = 0; |
|---|
| 198 | cliphdr[3] = 0; |
|---|
| 199 | |
|---|
| 200 | (void)write(fd, cliphdr, 4); |
|---|
| 201 | if (writev(fd, iov, iovlen) < 0) { |
|---|
| 202 | perror("write"); |
|---|
| 203 | exit(1); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | double Transmitter::txtime(pktbuf* pb) |
|---|
| 212 | { |
|---|
| 213 | |
|---|
| 214 | int cc = pb->len; |
|---|
| 215 | return (8 * cc / (1000. * kbps_)); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | int Transmitter::tx_buf_size() { |
|---|
| 222 | int size = 0; |
|---|
| 223 | pktbuf* pb = head_; |
|---|
| 224 | while (pb) { |
|---|
| 225 | size++; |
|---|
| 226 | pb = pb->next; |
|---|
| 227 | } |
|---|
| 228 | return size; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | void Transmitter::send(pktbuf* pb) |
|---|
| 232 | { |
|---|
| 233 | switch (cc_type_) { |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | case WBCC: |
|---|
| 238 | |
|---|
| 239 | if(is_buf_empty_) { |
|---|
| 240 | if (head_ != 0) { |
|---|
| 241 | tail_->next = pb; |
|---|
| 242 | tail_ = pb; |
|---|
| 243 | } else |
|---|
| 244 | tail_ = head_ = pb; |
|---|
| 245 | pb->next = 0; |
|---|
| 246 | cc_tfwc_output(); |
|---|
| 247 | is_buf_empty_ = false; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | else { |
|---|
| 251 | if (head_ != 0) { |
|---|
| 252 | tail_->next = pb; |
|---|
| 253 | tail_ = pb; |
|---|
| 254 | } else |
|---|
| 255 | tail_ = head_ = pb; |
|---|
| 256 | pb->next = 0; |
|---|
| 257 | cc_tfwc_output(pb); |
|---|
| 258 | } |
|---|
| 259 | break; |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | case RBCC: |
|---|
| 265 | |
|---|
| 266 | if(is_buf_empty_) { |
|---|
| 267 | if (head_ != 0) { |
|---|
| 268 | tail_->next = pb; |
|---|
| 269 | tail_ = pb; |
|---|
| 270 | } else |
|---|
| 271 | tail_ = head_ = pb; |
|---|
| 272 | pb->next = 0; |
|---|
| 273 | cc_tfrc_output(); |
|---|
| 274 | is_buf_empty_ = false; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | else { |
|---|
| 278 | if (head_ != 0) { |
|---|
| 279 | tail_->next = pb; |
|---|
| 280 | tail_ = pb; |
|---|
| 281 | } else |
|---|
| 282 | tail_ = head_ = pb; |
|---|
| 283 | pb->next = 0; |
|---|
| 284 | } |
|---|
| 285 | break; |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | case NOCC: |
|---|
| 291 | default: |
|---|
| 292 | |
|---|
| 293 | if (!busy_) { |
|---|
| 294 | double delay = txtime(pb); |
|---|
| 295 | nextpkttime_ = gettimeofday_secs() + delay; |
|---|
| 296 | output(pb); |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | msched(int(delay * 1e-3)); |
|---|
| 302 | busy_ = 1; |
|---|
| 303 | } else { |
|---|
| 304 | if (head_ != 0) { |
|---|
| 305 | tail_->next = pb; |
|---|
| 306 | tail_ = pb; |
|---|
| 307 | } else |
|---|
| 308 | tail_ = head_ = pb; |
|---|
| 309 | pb->next = 0; |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | void Transmitter::cc_tfwc_output(pktbuf* pb) |
|---|
| 315 | { |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | rtphdr* rh = (rtphdr *) pb->data; |
|---|
| 322 | |
|---|
| 323 | int magic = (int) tfwc_magic(); |
|---|
| 324 | |
|---|
| 325 | int jack = (int) tfwc_sndr_jacked(); |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | if (ntohs(rh->rh_seqno) <= magic + jack) { |
|---|
| 329 | |
|---|
| 330 | tfwc_sndr_send(ntohs(rh->rh_seqno), tx_now()-tx_now_offset_); |
|---|
| 331 | |
|---|
| 332 | head_ = pb->next; |
|---|
| 333 | |
|---|
| 334 | output_data_only(pb); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | void Transmitter::cc_tfwc_output() |
|---|
| 345 | { |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | pktbuf* pb = head_; |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | if (pb == 0) { |
|---|
| 356 | is_buf_empty_ = true; |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | return; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | rtphdr* rh = (rtphdr *) pb->data; |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | int magic = (int) tfwc_magic(); |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | int jack = (int) tfwc_sndr_jacked(); |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | while (ntohs(rh->rh_seqno) <= magic + jack) { |
|---|
| 379 | |
|---|
| 380 | tfwc_sndr_send(ntohs(rh->rh_seqno), tx_now()-tx_now_offset_); |
|---|
| 381 | |
|---|
| 382 | head_ = pb->next; |
|---|
| 383 | |
|---|
| 384 | output(pb); |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | if (head_ != 0) { |
|---|
| 389 | pb = head_; |
|---|
| 390 | rh = (rtphdr *) pb->data; |
|---|
| 391 | } else { |
|---|
| 392 | break; |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | void Transmitter::cc_tfrc_output() { |
|---|
| 404 | |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | void Transmitter::timeout() |
|---|
| 408 | { |
|---|
| 409 | double now = gettimeofday_secs(); |
|---|
| 410 | for (;;) { |
|---|
| 411 | pktbuf* p = head_; |
|---|
| 412 | if (p != 0) { |
|---|
| 413 | head_ = p->next; |
|---|
| 414 | nextpkttime_ += txtime(p); |
|---|
| 415 | output(p); |
|---|
| 416 | int ms = int(1e-3 * (nextpkttime_ - now)); |
|---|
| 417 | |
|---|
| 418 | if (ms > 1000) { |
|---|
| 419 | msched(ms); |
|---|
| 420 | return; |
|---|
| 421 | } |
|---|
| 422 | } else { |
|---|
| 423 | busy_ = 0; |
|---|
| 424 | break; |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | void Transmitter::flush() |
|---|
| 430 | { |
|---|
| 431 | if (!is_cc_on()) { |
|---|
| 432 | if (busy_) { |
|---|
| 433 | busy_ = 0; |
|---|
| 434 | cancel(); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | pktbuf* p = head_; |
|---|
| 438 | while (p != 0) { |
|---|
| 439 | pktbuf* n = p->next; |
|---|
| 440 | output(p); |
|---|
| 441 | p = n; |
|---|
| 442 | } |
|---|
| 443 | head_ = 0; |
|---|
| 444 | } |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | void Transmitter::output(pktbuf* pb) |
|---|
| 448 | { |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | transmit(pb); |
|---|
| 454 | loopback(pb); |
|---|
| 455 | |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | void Transmitter::output_data_only(pktbuf* pb) |
|---|
| 459 | { |
|---|
| 460 | tx_data_only(pb); |
|---|
| 461 | loopback(pb); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | |
|---|