[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 | |
---|
| 66 | static const struct roar_audio_info * _pt2info (int pt) { |
---|
| 67 | int i; |
---|
| 68 | |
---|
| 69 | for (i = 0; _g_rtp_pt[i].pt != -1; i++) { |
---|
| 70 | if ( _g_rtp_pt[i].pt == pt ) { |
---|
| 71 | return &(_g_rtp_pt[i].info); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | return NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
[3272] | 78 | int roar_vio_open_rtp (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
[3274] | 79 | char * dstr, struct roar_vio_defaults * odef) { |
---|
| 80 | struct roar_rtp_inst * self = NULL; |
---|
| 81 | |
---|
[3278] | 82 | ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = ?", calls, dst, dstr, odef); |
---|
| 83 | |
---|
[3274] | 84 | if ( calls == NULL || dst == NULL ) |
---|
| 85 | return -1; |
---|
| 86 | |
---|
| 87 | if ( (self = roar_mm_malloc(sizeof(struct roar_rtp_inst))) == NULL ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
[3278] | 90 | ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = ?", calls, dst, dstr, odef); |
---|
| 91 | |
---|
[3274] | 92 | memset(self, 0, sizeof(struct roar_rtp_inst)); |
---|
| 93 | |
---|
| 94 | self->vio = dst; |
---|
[3285] | 95 | self->bpf = 0; |
---|
| 96 | self->mtu = 768; |
---|
| 97 | |
---|
| 98 | memset(&(self->info), 0, sizeof(struct roar_audio_info)); |
---|
[3274] | 99 | |
---|
| 100 | self->header.version = 2; |
---|
| 101 | self->header.payload_type = ROAR_RTP_PT_UNKNOWN; |
---|
| 102 | |
---|
[3280] | 103 | // TODO: init with random values: |
---|
| 104 | // Sequence Number |
---|
[3285] | 105 | // ts |
---|
[3280] | 106 | // SSRC |
---|
| 107 | |
---|
[3274] | 108 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 109 | |
---|
| 110 | calls->inst = self; |
---|
| 111 | // calls->read = roar_vio_rtp_read; |
---|
[3279] | 112 | calls->write = roar_vio_rtp_write; |
---|
[3274] | 113 | // calls->lseek = roar_vio_rtp_lseek; |
---|
| 114 | calls->nonblock = roar_vio_rtp_nonblock; |
---|
| 115 | calls->sync = roar_vio_rtp_sync; |
---|
| 116 | calls->ctl = roar_vio_rtp_ctl; |
---|
| 117 | calls->close = roar_vio_rtp_close; |
---|
| 118 | |
---|
[3278] | 119 | ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = 0", calls, dst, dstr, odef); |
---|
| 120 | |
---|
[3274] | 121 | return 0; |
---|
| 122 | } |
---|
[3272] | 123 | |
---|
| 124 | ssize_t roar_vio_rtp_read (struct roar_vio_calls * vio, void *buf, size_t count); |
---|
[3275] | 125 | ssize_t roar_vio_rtp_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 126 | struct roar_rtp_inst * self = vio->inst; |
---|
| 127 | size_t len_need = count + sizeof(struct roar_rtp_header); // this is a bit more than we need |
---|
| 128 | // we ignore this at the moment |
---|
| 129 | size_t len_have; |
---|
| 130 | union { |
---|
| 131 | void * vp; |
---|
| 132 | char * cp; |
---|
| 133 | uint16_t * u16; |
---|
| 134 | uint32_t * u32; |
---|
| 135 | } data; |
---|
| 136 | size_t dataoffset; |
---|
| 137 | ssize_t ret; |
---|
| 138 | int i; |
---|
| 139 | |
---|
[3278] | 140 | ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 141 | |
---|
[3286] | 142 | if ( self->mtu < (sizeof(struct roar_rtp_header) + self->bpf) ) |
---|
| 143 | return -1; |
---|
| 144 | |
---|
| 145 | if ( len_need > self->mtu ) { |
---|
| 146 | len_have = 0; |
---|
| 147 | ret = 0; |
---|
| 148 | |
---|
| 149 | while (count) { |
---|
| 150 | len_need = self->mtu - sizeof(struct roar_rtp_header); |
---|
| 151 | |
---|
| 152 | if ( count < len_need ) |
---|
| 153 | len_need = count; |
---|
| 154 | |
---|
| 155 | if ( (ret = roar_vio_rtp_write(vio, buf, len_need)) == -1 ) |
---|
| 156 | break; |
---|
| 157 | |
---|
| 158 | len_have += ret; |
---|
| 159 | buf += ret; |
---|
| 160 | count -= ret; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return len_have ? len_have : ret; |
---|
| 164 | } |
---|
| 165 | |
---|
[3287] | 166 | if ( self->io == NULL ) { |
---|
| 167 | if ( roar_buffer_new(&(self->io), len_need) == -1 ) |
---|
[3275] | 168 | return -1; |
---|
| 169 | |
---|
| 170 | len_have = len_need; |
---|
| 171 | } else { |
---|
[3287] | 172 | if ( roar_buffer_get_len(self->io, &len_have) == -1 ) |
---|
[3275] | 173 | return -1; |
---|
| 174 | |
---|
| 175 | if ( len_have < len_need ) { |
---|
[3287] | 176 | if ( roar_buffer_set_len(self->io, len_need) == -1 ) { |
---|
| 177 | if ( roar_buffer_free(self->io) == -1 ) |
---|
[3275] | 178 | return -1; |
---|
| 179 | |
---|
[3287] | 180 | self->io = NULL; |
---|
[3275] | 181 | return roar_vio_rtp_write(vio, buf, count); // restart ower self from the beginning with no buffer |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
[3278] | 186 | ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 187 | |
---|
[3287] | 188 | if ( roar_buffer_get_data(self->io, &(data.vp)) == -1 ) |
---|
[3275] | 189 | return -1; |
---|
| 190 | |
---|
| 191 | memset(data.vp, 0, len_need); |
---|
| 192 | |
---|
[3280] | 193 | self->header.seq_num++; |
---|
| 194 | |
---|
[3275] | 195 | data.cp[0] = 2; |
---|
| 196 | data.cp[0] |= self->header.csrc_count << 4; |
---|
| 197 | data.cp[1] |= self->header.payload_type << 1; |
---|
| 198 | |
---|
| 199 | data.u16[1] = self->header.seq_num; |
---|
| 200 | |
---|
| 201 | data.u32[1] = self->header.ts; |
---|
| 202 | data.u32[2] = self->header.ssrc; |
---|
| 203 | |
---|
| 204 | for (i = 0; i < self->header.csrc_count; i++) { |
---|
| 205 | data.u32[3+i] = self->header.csrc[i]; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | dataoffset = 3*4 + self->header.csrc_count*4; |
---|
| 209 | |
---|
| 210 | memcpy(data.vp + dataoffset, buf, count); |
---|
| 211 | |
---|
[3278] | 212 | ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count); |
---|
| 213 | |
---|
[3275] | 214 | if ( (ret = roar_vio_write(self->vio, data.vp, count+dataoffset)) == -1 ) |
---|
| 215 | return -1; |
---|
| 216 | |
---|
[3285] | 217 | len_have = ret - dataoffset; |
---|
| 218 | |
---|
| 219 | self->header.ts += len_have / self->bpf; |
---|
| 220 | |
---|
| 221 | return len_have; |
---|
[3275] | 222 | } |
---|
| 223 | |
---|
[3272] | 224 | off_t roar_vio_rtp_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
[3274] | 225 | |
---|
| 226 | int roar_vio_rtp_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 227 | struct roar_rtp_inst * self = vio->inst; |
---|
| 228 | |
---|
[3278] | 229 | ROAR_DBG("roar_vio_rtp_nonblock(vio=%p, state=%i) = ?", vio, state); |
---|
| 230 | |
---|
[3274] | 231 | return roar_vio_nonblock(self->vio, state); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | int roar_vio_rtp_sync (struct roar_vio_calls * vio) { |
---|
| 235 | struct roar_rtp_inst * self = vio->inst; |
---|
| 236 | |
---|
[3278] | 237 | ROAR_DBG("roar_vio_rtp_sync(vio=%p) = ?", vio); |
---|
| 238 | |
---|
[3274] | 239 | return roar_vio_sync(self->vio); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | int roar_vio_rtp_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 243 | struct roar_rtp_inst * self = vio->inst; |
---|
[3280] | 244 | struct roar_stream * s = NULL; |
---|
| 245 | struct roar_stream_server * ss = NULL; |
---|
[3274] | 246 | |
---|
[3278] | 247 | ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data); |
---|
| 248 | |
---|
[3274] | 249 | if (vio == NULL || cmd == -1) |
---|
| 250 | return -1; |
---|
| 251 | |
---|
[3278] | 252 | ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data); |
---|
| 253 | |
---|
[3274] | 254 | switch (cmd) { |
---|
[3280] | 255 | case ROAR_VIO_CTL_SET_SSTREAM: |
---|
| 256 | s = ROAR_STREAM(ss = data); |
---|
| 257 | break; |
---|
| 258 | case ROAR_VIO_CTL_SET_STREAM: |
---|
| 259 | s = ROAR_STREAM(data); |
---|
| 260 | break; |
---|
[3274] | 261 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 262 | *(struct roar_vio_calls **)data = self->vio; |
---|
| 263 | return 0; |
---|
| 264 | break; |
---|
| 265 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 266 | self->vio = *(struct roar_vio_calls **)data; |
---|
| 267 | return 0; |
---|
| 268 | break; |
---|
| 269 | } |
---|
| 270 | |
---|
[3278] | 271 | ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data); |
---|
| 272 | |
---|
[3280] | 273 | if ( s != NULL ) { |
---|
[3284] | 274 | switch (s->info.codec) { |
---|
| 275 | case ROAR_CODEC_PCM_S_LE: |
---|
| 276 | case ROAR_CODEC_PCM_S_PDP: |
---|
| 277 | s->info.codec = ROAR_CODEC_PCM_S_BE; |
---|
| 278 | break; |
---|
| 279 | case ROAR_CODEC_PCM_U_LE: |
---|
| 280 | case ROAR_CODEC_PCM_U_PDP: |
---|
| 281 | s->info.codec = ROAR_CODEC_PCM_U_BE; |
---|
| 282 | break; |
---|
| 283 | } |
---|
| 284 | |
---|
[3285] | 285 | memcpy(&(self->info), &(s->info), sizeof(struct roar_audio_info)); |
---|
| 286 | |
---|
[3281] | 287 | self->header.payload_type = _info2pt(&(s->info)); |
---|
[3285] | 288 | |
---|
| 289 | self->bpf = s->info.channels * s->info.bits / 8; |
---|
| 290 | |
---|
[3280] | 291 | roar_vio_ctl(self->vio, cmd, data); |
---|
| 292 | return 0; |
---|
| 293 | } |
---|
| 294 | |
---|
[3274] | 295 | return roar_vio_ctl(self->vio, cmd, data); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | int roar_vio_rtp_close (struct roar_vio_calls * vio) { |
---|
| 299 | struct roar_rtp_inst * self = vio->inst; |
---|
| 300 | int ret; |
---|
| 301 | |
---|
[3278] | 302 | ROAR_DBG("roar_vio_rtp_close(vio=%p) = ?", vio); |
---|
| 303 | |
---|
[3274] | 304 | ret = roar_vio_close(self->vio); |
---|
| 305 | |
---|
[3287] | 306 | if ( self->io != NULL ) |
---|
| 307 | if ( roar_buffer_free(self->io) == -1 ) |
---|
[3275] | 308 | ret = -1; |
---|
| 309 | |
---|
[3274] | 310 | roar_mm_free(self); |
---|
| 311 | |
---|
[3278] | 312 | ROAR_DBG("roar_vio_rtp_close(vio=%p) = %i", vio, ret); |
---|
| 313 | |
---|
[3274] | 314 | return ret; |
---|
| 315 | } |
---|
[3272] | 316 | |
---|
| 317 | //ll |
---|