source: roaraudio/libroar/vio_rtp.c @ 5276:0eb24ca6810e

Last change on this file since 5276:0eb24ca6810e was 5276:0eb24ca6810e, checked in by phi, 12 years ago

merged VIO's _nonblock() into _ctl() (Closes: #135)

File size: 11.3 KB
Line 
1//vio_rtp.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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}}
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
113 calls->inst               = self;
114 calls->read               = roar_vio_rtp_read;
115 calls->write              = roar_vio_rtp_write;
116// calls->lseek              = roar_vio_rtp_lseek;
117 calls->sync               = roar_vio_rtp_sync;
118 calls->ctl                = roar_vio_rtp_ctl;
119 calls->close              = roar_vio_rtp_close;
120
121 ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = 0", calls, dst, dstr, odef);
122
123 return 0;
124}
125
126ssize_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 our 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
189 self->header.seq_num      = ROAR_NET2HOST16(data.u16[1]);
190
191 // TODO: check timestamp:
192 self->header.ts           = ROAR_NET2HOST32(data.u32[1]);
193
194 self->header.ssrc         = ROAR_NET2HOST32(data.u32[2]);
195
196 for (i = 0; i < self->header.csrc_count; i++) {
197  self->header.csrc[i]     = ROAR_NET2HOST16(data.u32[3+i]);
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
216ssize_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
231 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
232
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
257 if ( self->io == NULL ) {
258  if ( roar_buffer_new(&(self->io), len_need) == -1 )
259   return -1;
260
261  len_have = len_need;
262 } else {
263  if ( roar_buffer_get_len(self->io, &len_have) == -1 )
264   return -1;
265
266  if ( len_have < len_need ) {
267   if ( roar_buffer_set_len(self->io, len_need) == -1 ) {
268    if ( roar_buffer_free(self->io) == -1 )
269     return -1;
270
271    self->io = NULL;
272    return roar_vio_rtp_write(vio, buf, count); // restart ower self from the beginning with no buffer
273   }
274  }
275 }
276
277 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
278
279 if ( roar_buffer_get_data(self->io, &(data.vp)) == -1 )
280  return -1;
281
282 memset(data.vp, 0, len_need);
283
284 self->header.seq_num++;
285
286 data.cp[0]   = 2;
287 data.cp[0]  |= self->header.csrc_count   << 4;
288 data.cp[1]  |= self->header.payload_type << 1;
289
290 data.u16[1]  = ROAR_HOST2NET16(self->header.seq_num);
291
292 data.u32[1]  = ROAR_HOST2NET32(self->header.ts);
293 data.u32[2]  = ROAR_HOST2NET32(self->header.ssrc);
294
295 for (i = 0; i < self->header.csrc_count; i++) {
296  data.u32[3+i] = ROAR_HOST2NET32(self->header.csrc[i]);
297 }
298
299 dataoffset   = 3*4 + self->header.csrc_count*4;
300
301 memcpy(data.vp + dataoffset, buf, count);
302
303 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
304
305 if ( (ret = roar_vio_write(self->vio, data.vp, count+dataoffset)) == -1 )
306  return -1;
307
308 len_have = ret - dataoffset;
309
310 self->header.ts += len_have / self->bpf;
311
312 return len_have;
313}
314
315off_t   roar_vio_rtp_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
316
317int     roar_vio_rtp_sync    (struct roar_vio_calls * vio) {
318 struct roar_rtp_inst * self = vio->inst;
319
320 ROAR_DBG("roar_vio_rtp_sync(vio=%p) = ?", vio);
321
322 return roar_vio_sync(self->vio);
323}
324
325int     roar_vio_rtp_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
326 struct roar_rtp_inst * self = vio->inst;
327 struct roar_stream          * s    = NULL;
328 struct roar_stream_server   * ss   = NULL;
329 struct roar_audio_info      * info = NULL;
330
331 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
332
333 if (vio == NULL || cmd == -1)
334  return -1;
335
336 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
337
338 switch (cmd) {
339  case ROAR_VIO_CTL_GET_NAME:
340    if ( data == NULL )
341     return -1;
342
343    *(char**)data = "rtp";
344    return 0;
345   break;
346  case ROAR_VIO_CTL_SET_SSTREAM:
347    s = ROAR_STREAM(ss = data);
348    info = &(s->info);
349   break;
350  case ROAR_VIO_CTL_SET_STREAM:
351    s = ROAR_STREAM(data);
352    info = &(s->info);
353   break;
354  case ROAR_VIO_CTL_SET_AUINFO:
355    info = data;
356   break;
357  case ROAR_VIO_CTL_GET_NEXT:
358    *(struct roar_vio_calls **)data = self->vio;
359    return 0;
360   break;
361  case ROAR_VIO_CTL_SET_NEXT:
362    self->vio = *(struct roar_vio_calls **)data;
363    return 0;
364   break;
365 }
366
367 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
368
369 if ( info != NULL ) {
370  switch (info->codec) {
371   case ROAR_CODEC_PCM_S_LE:
372   case ROAR_CODEC_PCM_S_PDP:
373     info->codec = ROAR_CODEC_PCM_S_BE;
374    break;
375   case ROAR_CODEC_PCM_U_LE:
376   case ROAR_CODEC_PCM_U_PDP:
377     info->codec = ROAR_CODEC_PCM_U_BE;
378    break;
379  }
380
381  memcpy(&(self->info), info, sizeof(struct roar_audio_info));
382
383  self->header.payload_type = _info2pt(info);
384
385  self->bpf                 = info->channels * info->bits / 8;
386
387  _LIBROAR_IGNORE_RET(roar_vio_ctl(self->vio, cmd, data));
388  return 0;
389 }
390
391 return roar_vio_ctl(self->vio, cmd, data);
392}
393
394int     roar_vio_rtp_close   (struct roar_vio_calls * vio) {
395 struct roar_rtp_inst * self = vio->inst;
396 int ret;
397
398 ROAR_DBG("roar_vio_rtp_close(vio=%p) = ?", vio);
399
400 ret = roar_vio_close(self->vio);
401
402 if ( self->io != NULL )
403  if ( roar_buffer_free(self->io) == -1 )
404   ret = -1;
405
406 roar_mm_free(self);
407
408 ROAR_DBG("roar_vio_rtp_close(vio=%p) = %i", vio, ret);
409
410 return ret;
411}
412
413//ll
Note: See TracBrowser for help on using the repository browser.