source: roaraudio/libroar/vio_rtp.c @ 5430:70a234a359df

Last change on this file since 5430:70a234a359df was 5388:e5cc8e03a3e1, checked in by phi, 12 years ago

Added support for refcount based VIOs as well as dynamically allocated VIOs (non-stack or global VIOs) (Closes: #127)

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