| 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 | |
|---|
| 103 | inline double Transmitter::gettimeofday_secs() const |
|---|
| 104 | { |
|---|
| 105 | timeval tv; |
|---|
| 106 | ::gettimeofday(&tv, 0); |
|---|
| 107 | return (tv.tv_sec + 1e-6 * tv.tv_usec); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void Transmitter::loopback(pktbuf* pb) |
|---|
| 111 | { |
|---|
| 112 | int layer = pb->layer; |
|---|
| 113 | rtphdr* rh = (rtphdr*)pb->data; |
|---|
| 114 | int cc = pb->len; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | if (layer >= loop_layer_) { |
|---|
| 119 | |
|---|
| 120 | pb->release(); |
|---|
| 121 | return; |
|---|
| 122 | } |
|---|
| 123 | nb_ += cc; |
|---|
| 124 | ++np_; |
|---|
| 125 | |
|---|
| 126 | SourceManager& sm = SourceManager::instance(); |
|---|
| 127 | Source* s = sm.localsrc(); |
|---|
| 128 | timeval now = unixtime(); |
|---|
| 129 | Source::Layer& sl = s->layer(pb->layer); |
|---|
| 130 | |
|---|
| 131 | sl.lts_data(now); |
|---|
| 132 | s->action(); |
|---|
| 133 | sl.sts_data(rh->rh_ts); |
|---|
| 134 | sl.np(1); |
|---|
| 135 | sl.nb(cc); |
|---|
| 136 | sl.cs((u_int16_t)ntohs(rh->rh_seqno),s); |
|---|
| 137 | |
|---|
| 138 | int flags = ntohs(rh->rh_flags); |
|---|
| 139 | if (flags & RTP_M) { |
|---|
| 140 | ++nf_; |
|---|
| 141 | sl.nf(1); |
|---|
| 142 | } |
|---|
| 143 | int fmt = flags & 0x7f; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | PacketHandler* h = s->handler(); |
|---|
| 149 | if (h == 0) |
|---|
| 150 | h = s->activate(fmt); |
|---|
| 151 | else if (s->format() != fmt) { |
|---|
| 152 | h = s->change_format(fmt); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | if (s->mute()) { |
|---|
| 156 | pb->release(); |
|---|
| 157 | return; |
|---|
| 158 | } |
|---|
| 159 | h->recv(pb); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | int Transmitter::dumpfd_ = -1; |
|---|
| 163 | void Transmitter::dump(int fd) |
|---|
| 164 | { |
|---|
| 165 | dumpfd_ = fd; |
|---|
| 166 | #define MAGIC "RTPCLIP 1.0" |
|---|
| 167 | (void)write(fd, MAGIC, sizeof(MAGIC)); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | #ifdef WIN32 |
|---|
| 172 | int writev(int fd, iovec* iov, int iovlen) |
|---|
| 173 | { |
|---|
| 174 | int len = 0, n; |
|---|
| 175 | for (int i = 0; i < iovlen; i++) { |
|---|
| 176 | if ((n = write(fd, iov[i].iov_base, iov[i].iov_len)) == -1) { |
|---|
| 177 | perror("writev"); |
|---|
| 178 | exit(1); |
|---|
| 179 | } |
|---|
| 180 | len += n; |
|---|
| 181 | } |
|---|
| 182 | return(len); |
|---|
| 183 | } |
|---|
| 184 | #endif |
|---|
| 185 | |
|---|
| 186 | void Transmitter::dump(int fd, iovec* iov, int iovlen) const |
|---|
| 187 | { |
|---|
| 188 | register int length = 0; |
|---|
| 189 | for (int i = iovlen; --i >= 0; ) |
|---|
| 190 | length += iov[i].iov_len; |
|---|
| 191 | |
|---|
| 192 | char cliphdr[4]; |
|---|
| 193 | *(short*)cliphdr = htons(length); |
|---|
| 194 | cliphdr[2] = 0; |
|---|
| 195 | cliphdr[3] = 0; |
|---|
| 196 | |
|---|
| 197 | (void)write(fd, cliphdr, 4); |
|---|
| 198 | if (writev(fd, iov, iovlen) < 0) { |
|---|
| 199 | perror("write"); |
|---|
| 200 | exit(1); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | double Transmitter::txtime(pktbuf* pb) |
|---|
| 209 | { |
|---|
| 210 | |
|---|
| 211 | int cc = pb->len; |
|---|
| 212 | return (8 * cc / (1000. * kbps_)); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | void Transmitter::send(pktbuf* pb) |
|---|
| 216 | { |
|---|
| 217 | switch (cc_type_) { |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | case WBCC: |
|---|
| 222 | |
|---|
| 223 | if(is_buf_empty_) { |
|---|
| 224 | if (head_ != 0) { |
|---|
| 225 | tail_->next = pb; |
|---|
| 226 | tail_ = pb; |
|---|
| 227 | } else |
|---|
| 228 | tail_ = head_ = pb; |
|---|
| 229 | pb->next = 0; |
|---|
| 230 | cc_tfwc_output(); |
|---|
| 231 | is_buf_empty_ = false; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | else { |
|---|
| 235 | if (head_ != 0) { |
|---|
| 236 | tail_->next = pb; |
|---|
| 237 | tail_ = pb; |
|---|
| 238 | } else |
|---|
| 239 | tail_ = head_ = pb; |
|---|
| 240 | pb->next = 0; |
|---|
| 241 | cc_tfwc_output(pb); |
|---|
| 242 | } |
|---|
| 243 | break; |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | case RBCC: |
|---|
| 249 | |
|---|
| 250 | if(is_buf_empty_) { |
|---|
| 251 | if (head_ != 0) { |
|---|
| 252 | tail_->next = pb; |
|---|
| 253 | tail_ = pb; |
|---|
| 254 | } else |
|---|
| 255 | tail_ = head_ = pb; |
|---|
| 256 | pb->next = 0; |
|---|
| 257 | cc_tfrc_output(); |
|---|
| 258 | is_buf_empty_ = false; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | else { |
|---|
| 262 | if (head_ != 0) { |
|---|
| 263 | tail_->next = pb; |
|---|
| 264 | tail_ = pb; |
|---|
| 265 | } else |
|---|
| 266 | tail_ = head_ = pb; |
|---|
| 267 | pb->next = 0; |
|---|
| 268 | } |
|---|
| 269 | break; |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | case NOCC: |
|---|
| 275 | default: |
|---|
| 276 | |
|---|
| 277 | if (!busy_) { |
|---|
| 278 | double delay = txtime(pb); |
|---|
| 279 | nextpkttime_ = gettimeofday_secs() + delay; |
|---|
| 280 | output(pb); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | msched(int(delay * 1e-3)); |
|---|
| 286 | busy_ = 1; |
|---|
| 287 | } else { |
|---|
| 288 | if (head_ != 0) { |
|---|
| 289 | tail_->next = pb; |
|---|
| 290 | tail_ = pb; |
|---|
| 291 | } else |
|---|
| 292 | tail_ = head_ = pb; |
|---|
| 293 | pb->next = 0; |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | void Transmitter::cc_tfwc_output(pktbuf* pb) |
|---|
| 299 | { |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | rtphdr* rh = (rtphdr *) pb->data; |
|---|
| 306 | |
|---|
| 307 | int magic = (int) tfwc_magic(); |
|---|
| 308 | |
|---|
| 309 | int jack = (int) tfwc_sndr_jacked(); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | if (ntohs(rh->rh_seqno) <= magic + jack) { |
|---|
| 313 | fprintf(stderr, "\n\tnow: %f\tseqno: %d\n\n", |
|---|
| 314 | tx_now()-tx_now_offset_, ntohs(rh->rh_seqno)); |
|---|
| 315 | |
|---|
| 316 | tfwc_sndr_send(pb); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | head_ = pb->next; |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | output(pb); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | void Transmitter::cc_tfwc_output() |
|---|
| 333 | { |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | pktbuf* pb = head_; |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | if (pb == 0) { |
|---|
| 344 | is_buf_empty_ = true; |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | return; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | rtphdr* rh = (rtphdr *) pb->data; |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | int magic = (int) tfwc_magic(); |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | int jack = (int) tfwc_sndr_jacked(); |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | while (ntohs(rh->rh_seqno) <= magic + jack) { |
|---|
| 367 | fprintf(stderr, "\n\tnow: %f\tseqno: %d\n\n", |
|---|
| 368 | tx_now()-tx_now_offset_, ntohs(rh->rh_seqno)); |
|---|
| 369 | |
|---|
| 370 | tfwc_sndr_send(pb); |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | head_ = pb->next; |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | output(pb); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | if (head_ != 0) { |
|---|
| 381 | pb = head_; |
|---|
| 382 | rh = (rtphdr *) pb->data; |
|---|
| 383 | } else { |
|---|
| 384 | break; |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | void Transmitter::cc_tfrc_output() { |
|---|
| 396 | |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | void Transmitter::timeout() |
|---|
| 400 | { |
|---|
| 401 | double now = gettimeofday_secs(); |
|---|
| 402 | for (;;) { |
|---|
| 403 | pktbuf* p = head_; |
|---|
| 404 | if (p != 0) { |
|---|
| 405 | head_ = p->next; |
|---|
| 406 | nextpkttime_ += txtime(p); |
|---|
| 407 | output(p); |
|---|
| 408 | int ms = int(1e-3 * (nextpkttime_ - now)); |
|---|
| 409 | |
|---|
| 410 | if (ms > 1000) { |
|---|
| 411 | msched(ms); |
|---|
| 412 | return; |
|---|
| 413 | } |
|---|
| 414 | } else { |
|---|
| 415 | busy_ = 0; |
|---|
| 416 | break; |
|---|
| 417 | } |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | void Transmitter::flush() |
|---|
| 422 | { |
|---|
| 423 | if (!is_cc_on()) { |
|---|
| 424 | if (busy_) { |
|---|
| 425 | busy_ = 0; |
|---|
| 426 | cancel(); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | pktbuf* p = head_; |
|---|
| 430 | while (p != 0) { |
|---|
| 431 | pktbuf* n = p->next; |
|---|
| 432 | output(p); |
|---|
| 433 | p = n; |
|---|
| 434 | } |
|---|
| 435 | head_ = 0; |
|---|
| 436 | } |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | void Transmitter::output(pktbuf* pb) |
|---|
| 440 | { |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | transmit(pb); |
|---|
| 446 | loopback(pb); |
|---|
| 447 | |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|