[3272] | 1 | //vio_rtp.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "libroar.h" |
---|
| 36 | |
---|
[3281] | 37 | static const struct { |
---|
| 38 | int pt; |
---|
| 39 | struct roar_audio_info info; |
---|
| 40 | } _g_rtp_pt[] = { |
---|
| 41 | {ROAR_RTP_PT_A_PCMU, {.codec = ROAR_CODEC_MULAW, .bits = 8, .rate = 8000, .channels = 1}}, |
---|
| 42 | {ROAR_RTP_PT_A_PCMA, {.codec = ROAR_CODEC_ALAW, .bits = 8, .rate = 8000, .channels = 1}}, |
---|
| 43 | {ROAR_RTP_PT_A_L16_441_2, {.codec = ROAR_CODEC_PCM_S_BE, .bits = 16, .rate = 44100, .channels = 2}}, |
---|
| 44 | {ROAR_RTP_PT_A_L16_441_1, {.codec = ROAR_CODEC_PCM_S_BE, .bits = 16, .rate = 44100, .channels = 1}}, |
---|
| 45 | {-1, {-1, -1, -1}} |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | static int _info2pt (struct roar_audio_info * info) { |
---|
| 49 | int i; |
---|
| 50 | |
---|
[3283] | 51 | ROAR_DBG("_info2pt(info=%p{.codec=%s(%i), .bits=%i, .rate=%i, .channels=%i}) = ?", info, roar_codec2str(info->codec), info->codec, info->bits, info->rate, info->channels); |
---|
| 52 | |
---|
[3281] | 53 | for (i = 0; _g_rtp_pt[i].pt != -1; i++) { |
---|
| 54 | if ( info->codec == _g_rtp_pt[i].info.codec && |
---|
| 55 | info->bits == _g_rtp_pt[i].info.bits && |
---|
| 56 | info->rate == _g_rtp_pt[i].info.rate && |
---|
| 57 | info->channels == _g_rtp_pt[i].info.channels ) { |
---|
| 58 | return _g_rtp_pt[i].pt; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
[3283] | 62 | ROAR_DBG("_info2pt(info=%p{.codec=%s(%i), .bits=%i, .rate=%i, .channels=%i}) = -1", info, roar_codec2str(info->codec), info->codec, info->bits, info->rate, info->channels); |
---|
[3281] | 63 | return -1; |
---|
| 64 | } |
---|
| 65 | |
---|
[3324] | 66 | #if 0 |
---|
[3281] | 67 | static const struct roar_audio_info * _pt2info (int pt) { |
---|
| 68 | int i; |
---|
| 69 | |
---|
| 70 | for (i = 0; _g_rtp_pt[i].pt != -1; i++) { |
---|
| 71 | if ( _g_rtp_pt[i].pt == pt ) { |
---|
| 72 | return &(_g_rtp_pt[i].info); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return NULL; |
---|
| 77 | } |
---|
[3324] | 78 | #endif |
---|
[3281] | 79 | |
---|
[3272] | 80 | int roar_vio_open_rtp (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
[3274] | 81 | char * dstr, struct roar_vio_defaults * odef) { |
---|
| 82 | struct roar_rtp_inst * self = NULL; |
---|
| 83 | |
---|
[3278] | 84 | ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = ?", calls, dst, dstr, odef); |
---|
| 85 | |
---|
[3274] | 86 | if ( calls == NULL || dst == NULL ) |
---|
| 87 | return -1; |
---|
| 88 | |
---|
| 89 | if ( (self = roar_mm_malloc(sizeof(struct roar_rtp_inst))) == NULL ) |
---|
| 90 | return -1; |
---|
| 91 | |
---|
[3278] | 92 | ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = ?", calls, dst, dstr, odef); |
---|
| 93 | |
---|
[3274] | 94 | memset(self, 0, sizeof(struct roar_rtp_inst)); |
---|
| 95 | |
---|
| 96 | self->vio = dst; |
---|
[3285] | 97 | self->bpf = 0; |
---|
| 98 | self->mtu = 768; |
---|
| 99 | |
---|
| 100 | memset(&(self->info), 0, sizeof(struct roar_audio_info)); |
---|
[3274] | 101 | |
---|
| 102 | self->header.version = 2; |
---|
| 103 | self->header.payload_type = ROAR_RTP_PT_UNKNOWN; |
---|
| 104 | |
---|
[3280] | 105 | // TODO: init with random values: |
---|
| 106 | // Sequence Number |
---|
[3285] | 107 | // ts |
---|
[3280] | 108 | // SSRC |
---|
| 109 | |
---|
[3274] | 110 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 111 | |
---|
| 112 | calls->inst = self; |
---|
[3288] | 113 | calls->read = roar_vio_rtp_read; |
---|
[3279] | 114 | calls->write = roar_vio_rtp_write; |
---|
[3274] | 115 | // calls->lseek = roar_vio_rtp_lseek; |
---|
| 116 | calls->nonblock = roar_vio_rtp_nonblock; |
---|
| 117 | calls->sync = roar_vio_rtp_sync; |
---|
| 118 | calls->ctl = roar_vio_rtp_ctl; |
---|
| 119 | calls->close = roar_vio_rtp_close; |
---|
| 120 | |
---|
[3278] | 121 | ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = 0", calls, dst, dstr, odef); |
---|
| 122 | |
---|
[3274] | 123 | return 0; |
---|
| 124 | } |
---|
[3272] | 125 | |
---|
[3288] | 126 | ssize_t roar_vio_rtp_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 127 | struct roar_rtp_inst * self = vio->inst; |
---|
| 128 | size_t len_need = self->mtu * 4 + sizeof(struct roar_rtp_header); // we hope to never get pkgs with size > 4*mtu |
---|
| 129 | size_t len_have; |
---|
| 130 | size_t have = 0; |
---|
| 131 | ssize_t ret; |
---|
| 132 | union { |
---|
| 133 | void * vp; |
---|
| 134 | char * cp; |
---|
| 135 | uint16_t * u16; |
---|
| 136 | uint32_t * u32; |
---|
| 137 | } data; |
---|
| 138 | size_t dataoffset; |
---|
| 139 | int i; |
---|
| 140 | |
---|
| 141 | ROAR_DBG("roar_vio_rtp_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 142 | |
---|
| 143 | if ( self->rx_decoded != NULL ) { |
---|
| 144 | // handle this case and set to NULL if the buffer is empty |
---|
| 145 | // set have; |
---|
| 146 | // increment buf, decrement count |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | if ( count == 0 ) |
---|
| 150 | return have; |
---|
| 151 | |
---|
| 152 | if ( self->io == NULL ) { |
---|
| 153 | if ( roar_buffer_new(&(self->io), len_need) == -1 ) |
---|
| 154 | return have ? have : -1; |
---|
| 155 | |
---|
| 156 | len_have = len_need; |
---|
| 157 | } else { |
---|
| 158 | if ( roar_buffer_get_len(self->io, &len_have) == -1 ) |
---|
| 159 | return have ? have : -1; |
---|
| 160 | |
---|
| 161 | if ( len_have < len_need ) { |
---|
| 162 | if ( roar_buffer_set_len(self->io, len_need) == -1 ) { |
---|
| 163 | if ( roar_buffer_free(self->io) == -1 ) |
---|
| 164 | return have ? have : -1; |
---|
| 165 | |
---|
| 166 | self->io = NULL; |
---|
| 167 | if ( have != 0 ) { |
---|
| 168 | return have; |
---|
| 169 | } else { |
---|
| 170 | return roar_vio_rtp_read(vio, buf, count); // restart ower self from the beginning with no buffer |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if ( roar_buffer_get_data(self->io, &(data.vp)) == -1 ) |
---|
| 177 | return have ? have : -1; |
---|
| 178 | |
---|
| 179 | if ( (ret = roar_vio_read(self->vio, data.vp, len_need)) == -1 ) |
---|
| 180 | return have ? have : -1; |
---|
| 181 | |
---|
| 182 | if ( (data.cp[0] && 0x02) == 0x02 ) /* version check */ |
---|
| 183 | return have ? have : -1; |
---|
| 184 | |
---|
| 185 | self->header.csrc_count = (data.cp[0] & 0xF0) >> 4; |
---|
| 186 | self->header.payload_type = (data.cp[1] & 0xFE) >> 1; |
---|
| 187 | |
---|
| 188 | // TODO: check old seqnum < new seqnum |
---|
[3289] | 189 | self->header.seq_num = ROAR_NET2HOST16(data.u16[1]); |
---|
[3288] | 190 | |
---|
| 191 | // TODO: check timestamp: |
---|
[3289] | 192 | self->header.ts = ROAR_NET2HOST32(data.u32[1]); |
---|
[3288] | 193 | |
---|
[3289] | 194 | self->header.ssrc = ROAR_NET2HOST32(data.u32[2]); |
---|
[3288] | 195 | |
---|
| 196 | for (i = 0; i < self->header.csrc_count; i++) { |
---|
[3289] | 197 | self->header.csrc[i] = ROAR_NET2HOST16(data.u32[3+i]); |
---|
[3288] | 198 | } |
---|
| 199 | |
---|
| 200 | dataoffset = 3*4 + self->header.csrc_count*4; |
---|
| 201 | |
---|
| 202 | ret -= dataoffset; |
---|
| 203 | data.vp += dataoffset; |
---|
| 204 | |
---|
| 205 | if ( ret <= count ) { |
---|
| 206 | memcpy(buf, data.vp, ret); |
---|
| 207 | ROAR_DBG("roar_vio_rtp_read(vio=%p, buf=%p, count=?) = %llu", vio, buf, (long long unsigned)(have+ret)); |
---|
| 208 | return have + ret; |
---|
| 209 | } else { |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | ROAR_DBG("roar_vio_rtp_read(vio=%p, buf=%p, count=?) = -1", vio, buf); |
---|
| 213 | return -1; |
---|
| 214 | } |
---|
| 215 | |
---|
[3275] | 216 | ssize_t roar_vio_rtp_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 217 | struct roar_rtp_inst * self = vio->inst; |
---|
| 218 | size_t len_need = count + sizeof(struct roar_rtp_header); // this is a bit more than we need |
---|
| 219 | // we ignore this at the moment |
---|
| 220 | size_t len_have; |
---|
| 221 | union { |
---|
| 222 | void * vp; |
---|
| 223 | char * cp; |
---|
| 224 | uint16_t * u16; |
---|
| 225 | uint32_t * u32; |
---|
| 226 | } data; |
---|
| 227 | size_t dataoffset; |
---|
| 228 | ssize_t ret; |
---|
| 229 | int i; |
---|
| 230 | |
---|
[3278] | 231 | ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 232 | |
---|
[3286] | 233 | if ( self->mtu < (sizeof(struct roar_rtp_header) + self->bpf) ) |
---|
| 234 | return -1; |
---|
| 235 | |
---|
| 236 | if ( len_need > self->mtu ) { |
---|
| 237 | len_have = 0; |
---|
| 238 | ret = 0; |
---|
| 239 | |
---|
| 240 | while (count) { |
---|
| 241 | len_need = self->mtu - sizeof(struct roar_rtp_header); |
---|
| 242 | |
---|
| 243 | if ( count < len_need ) |
---|
| 244 | len_need = count; |
---|
| 245 | |
---|
| 246 | if ( (ret = roar_vio_rtp_write(vio, buf, len_need)) == -1 ) |
---|
| 247 | break; |
---|
| 248 | |
---|
| 249 | len_have += ret; |
---|
| 250 | buf += ret; |
---|
| 251 | count -= ret; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | return len_have ? len_have : ret; |
---|
| 255 | } |
---|
| 256 | |
---|
[3287] | 257 | if ( self->io == NULL ) { |
---|
| 258 | if ( roar_buffer_new(&(self->io), len_need) == -1 ) |
---|
[3275] | 259 | return -1; |
---|
| 260 | |
---|
| 261 | len_have = len_need; |
---|
| 262 | } else { |
---|
[3287] | 263 | if ( roar_buffer_get_len(self->io, &len_have) == -1 ) |
---|
[3275] | 264 | return -1; |
---|
| 265 | |
---|
| 266 | if ( len_have < len_need ) { |
---|
[3287] | 267 | if ( roar_buffer_set_len(self->io, len_need) == -1 ) { |
---|
| 268 | if ( roar_buffer_free(self->io) == -1 ) |
---|
[3275] | 269 | return -1; |
---|
| 270 | |
---|
[3287] | 271 | self->io = NULL; |
---|
[3275] | 272 | return roar_vio_rtp_write(vio, buf, count); // restart ower self from the beginning with no buffer |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
[3278] | 277 | ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 278 | |
---|
[3287] | 279 | if ( roar_buffer_get_data(self->io, &(data.vp)) == -1 ) |
---|
[3275] | 280 | return -1; |
---|
| 281 | |
---|
| 282 | memset(data.vp, 0, len_need); |
---|
| 283 | |
---|
[3280] | 284 | self->header.seq_num++; |
---|
| 285 | |
---|
[3275] | 286 | data.cp[0] = 2; |
---|
| 287 | data.cp[0] |= self->header.csrc_count << 4; |
---|
| 288 | data.cp[1] |= self->header.payload_type << 1; |
---|
| 289 | |
---|
[3289] | 290 | data.u16[1] = ROAR_HOST2NET16(self->header.seq_num); |
---|
[3275] | 291 | |
---|
[3289] | 292 | data.u32[1] = ROAR_HOST2NET32(self->header.ts); |
---|
| 293 | data.u32[2] = ROAR_HOST2NET32(self->header.ssrc); |
---|
[3275] | 294 | |
---|
| 295 | for (i = 0; i < self->header.csrc_count; i++) { |
---|
[3289] | 296 | data.u32[3+i] = ROAR_HOST2NET32(self->header.csrc[i]); |
---|
[3275] | 297 | } |
---|
| 298 | |
---|
| 299 | dataoffset = 3*4 + self->header.csrc_count*4; |
---|
| 300 | |
---|
| 301 | memcpy(data.vp + dataoffset, buf, count); |
---|
| 302 | |
---|
[3278] | 303 | ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 304 | |
---|
[3275] | 305 | if ( (ret = roar_vio_write(self->vio, data.vp, count+dataoffset)) == -1 ) |
---|
| 306 | return -1; |
---|
| 307 | |
---|
[3285] | 308 | len_have = ret - dataoffset; |
---|
| 309 | |
---|
| 310 | self->header.ts += len_have / self->bpf; |
---|
| 311 | |
---|
| 312 | return len_have; |
---|
[3275] | 313 | } |
---|
| 314 | |
---|
[3272] | 315 | off_t roar_vio_rtp_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
[3274] | 316 | |
---|
| 317 | int roar_vio_rtp_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 318 | struct roar_rtp_inst * self = vio->inst; |
---|
| 319 | |
---|
[3278] | 320 | ROAR_DBG("roar_vio_rtp_nonblock(vio=%p, state=%i) = ?", vio, state); |
---|
| 321 | |
---|
[3274] | 322 | return roar_vio_nonblock(self->vio, state); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | int roar_vio_rtp_sync (struct roar_vio_calls * vio) { |
---|
| 326 | struct roar_rtp_inst * self = vio->inst; |
---|
| 327 | |
---|
[3278] | 328 | ROAR_DBG("roar_vio_rtp_sync(vio=%p) = ?", vio); |
---|
| 329 | |
---|
[3274] | 330 | return roar_vio_sync(self->vio); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | int roar_vio_rtp_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 334 | struct roar_rtp_inst * self = vio->inst; |
---|
[3323] | 335 | struct roar_stream * s = NULL; |
---|
| 336 | struct roar_stream_server * ss = NULL; |
---|
| 337 | struct roar_audio_info * info = NULL; |
---|
[3274] | 338 | |
---|
[3278] | 339 | ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data); |
---|
| 340 | |
---|
[3274] | 341 | if (vio == NULL || cmd == -1) |
---|
| 342 | return -1; |
---|
| 343 | |
---|
[3278] | 344 | ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data); |
---|
| 345 | |
---|
[3274] | 346 | switch (cmd) { |
---|
[3280] | 347 | case ROAR_VIO_CTL_SET_SSTREAM: |
---|
| 348 | s = ROAR_STREAM(ss = data); |
---|
[3323] | 349 | info = &(s->info); |
---|
[3280] | 350 | break; |
---|
| 351 | case ROAR_VIO_CTL_SET_STREAM: |
---|
| 352 | s = ROAR_STREAM(data); |
---|
[3323] | 353 | info = &(s->info); |
---|
| 354 | break; |
---|
| 355 | case ROAR_VIO_CTL_SET_AUINFO: |
---|
| 356 | info = data; |
---|
[3280] | 357 | break; |
---|
[3274] | 358 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 359 | *(struct roar_vio_calls **)data = self->vio; |
---|
| 360 | return 0; |
---|
| 361 | break; |
---|
| 362 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 363 | self->vio = *(struct roar_vio_calls **)data; |
---|
| 364 | return 0; |
---|
| 365 | break; |
---|
| 366 | } |
---|
| 367 | |
---|
[3278] | 368 | ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data); |
---|
| 369 | |
---|
[3323] | 370 | if ( info != NULL ) { |
---|
| 371 | switch (info->codec) { |
---|
[3284] | 372 | case ROAR_CODEC_PCM_S_LE: |
---|
| 373 | case ROAR_CODEC_PCM_S_PDP: |
---|
[3323] | 374 | info->codec = ROAR_CODEC_PCM_S_BE; |
---|
[3284] | 375 | break; |
---|
| 376 | case ROAR_CODEC_PCM_U_LE: |
---|
| 377 | case ROAR_CODEC_PCM_U_PDP: |
---|
[3323] | 378 | info->codec = ROAR_CODEC_PCM_U_BE; |
---|
[3284] | 379 | break; |
---|
| 380 | } |
---|
| 381 | |
---|
[3323] | 382 | memcpy(&(self->info), info, sizeof(struct roar_audio_info)); |
---|
[3285] | 383 | |
---|
[3323] | 384 | self->header.payload_type = _info2pt(info); |
---|
[3285] | 385 | |
---|
[3323] | 386 | self->bpf = info->channels * info->bits / 8; |
---|
[3285] | 387 | |
---|
[3280] | 388 | roar_vio_ctl(self->vio, cmd, data); |
---|
| 389 | return 0; |
---|
| 390 | } |
---|
| 391 | |
---|
[3274] | 392 | return roar_vio_ctl(self->vio, cmd, data); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | int roar_vio_rtp_close (struct roar_vio_calls * vio) { |
---|
| 396 | struct roar_rtp_inst * self = vio->inst; |
---|
| 397 | int ret; |
---|
| 398 | |
---|
[3278] | 399 | ROAR_DBG("roar_vio_rtp_close(vio=%p) = ?", vio); |
---|
| 400 | |
---|
[3274] | 401 | ret = roar_vio_close(self->vio); |
---|
| 402 | |
---|
[3287] | 403 | if ( self->io != NULL ) |
---|
| 404 | if ( roar_buffer_free(self->io) == -1 ) |
---|
[3275] | 405 | ret = -1; |
---|
| 406 | |
---|
[3274] | 407 | roar_mm_free(self); |
---|
| 408 | |
---|
[3278] | 409 | ROAR_DBG("roar_vio_rtp_close(vio=%p) = %i", vio, ret); |
---|
| 410 | |
---|
[3274] | 411 | return ret; |
---|
| 412 | } |
---|
[3272] | 413 | |
---|
| 414 | //ll |
---|