source: roaraudio/libroar/vio_rtp.c @ 3289:1d9f8ad41d5d

Last change on this file since 3289:1d9f8ad41d5d was 3289:1d9f8ad41d5d, checked in by phi, 14 years ago

honor network byte order

File size: 11.3 KB
Line 
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
37static 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
48static int _info2pt (struct roar_audio_info * info) {
49 int i;
50
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
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
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);
63 return -1;
64}
65
66static 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
78int roar_vio_open_rtp        (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
79                              char * dstr, struct roar_vio_defaults * odef) {
80 struct roar_rtp_inst * self = NULL;
81
82 ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = ?", calls, dst, dstr, odef);
83
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
90 ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = ?", calls, dst, dstr, odef);
91
92 memset(self, 0, sizeof(struct roar_rtp_inst));
93
94 self->vio                 = dst;
95 self->bpf                 = 0;
96 self->mtu                 = 768;
97
98 memset(&(self->info), 0, sizeof(struct roar_audio_info));
99
100 self->header.version      = 2;
101 self->header.payload_type = ROAR_RTP_PT_UNKNOWN;
102
103 // TODO: init with random values:
104 //       Sequence Number
105 //       ts
106 //       SSRC
107
108 memset(calls, 0, sizeof(struct roar_vio_calls));
109
110 calls->inst               = self;
111 calls->read               = roar_vio_rtp_read;
112 calls->write              = roar_vio_rtp_write;
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
119 ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = 0", calls, dst, dstr, odef);
120
121 return 0;
122}
123
124ssize_t roar_vio_rtp_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
125 struct roar_rtp_inst * self = vio->inst;
126 size_t len_need = self->mtu * 4 + sizeof(struct roar_rtp_header); // we hope to never get pkgs with size > 4*mtu
127 size_t len_have;
128 size_t have = 0;
129 ssize_t ret;
130 union {
131  void     * vp;
132  char     * cp;
133  uint16_t * u16;
134  uint32_t * u32;
135 } data;
136 size_t  dataoffset;
137 int     i;
138
139 ROAR_DBG("roar_vio_rtp_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
140
141 if ( self->rx_decoded != NULL ) {
142  // handle this case and set to NULL if the buffer is empty
143  // set have;
144  // increment buf, decrement count
145 }
146
147 if ( count == 0 )
148  return have;
149
150 if ( self->io == NULL ) {
151  if ( roar_buffer_new(&(self->io), len_need) == -1 )
152   return have ? have : -1;
153
154  len_have = len_need;
155 } else {
156  if ( roar_buffer_get_len(self->io, &len_have) == -1 )
157   return have ? have : -1;
158
159  if ( len_have < len_need ) {
160   if ( roar_buffer_set_len(self->io, len_need) == -1 ) {
161    if ( roar_buffer_free(self->io) == -1 )
162     return have ? have : -1;
163
164    self->io = NULL;
165    if ( have != 0 ) {
166     return have;
167    } else {
168     return roar_vio_rtp_read(vio, buf, count); // restart ower self from the beginning with no buffer
169    }
170   }
171  }
172 }
173
174 if ( roar_buffer_get_data(self->io, &(data.vp)) == -1 )
175  return have ? have : -1;
176
177 if ( (ret = roar_vio_read(self->vio, data.vp, len_need)) == -1 )
178  return have ? have : -1;
179
180 if ( (data.cp[0] && 0x02) == 0x02 ) /* version check */
181  return have ? have : -1;
182
183 self->header.csrc_count   = (data.cp[0] & 0xF0) >> 4;
184 self->header.payload_type = (data.cp[1] & 0xFE) >> 1;
185
186 // TODO: check old seqnum < new seqnum
187 self->header.seq_num      = ROAR_NET2HOST16(data.u16[1]);
188
189 // TODO: check timestamp:
190 self->header.ts           = ROAR_NET2HOST32(data.u32[1]);
191
192 self->header.ssrc         = ROAR_NET2HOST32(data.u32[2]);
193
194 for (i = 0; i < self->header.csrc_count; i++) {
195  self->header.csrc[i]     = ROAR_NET2HOST16(data.u32[3+i]);
196 }
197
198 dataoffset   = 3*4 + self->header.csrc_count*4;
199
200 ret     -= dataoffset;
201 data.vp += dataoffset;
202
203 if ( ret <= count ) {
204  memcpy(buf, data.vp, ret);
205  ROAR_DBG("roar_vio_rtp_read(vio=%p, buf=%p, count=?) = %llu", vio, buf, (long long unsigned)(have+ret));
206  return have + ret;
207 } else {
208 }
209
210 ROAR_DBG("roar_vio_rtp_read(vio=%p, buf=%p, count=?) = -1", vio, buf);
211 return -1;
212}
213
214ssize_t roar_vio_rtp_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
215 struct roar_rtp_inst * self = vio->inst;
216 size_t len_need = count + sizeof(struct roar_rtp_header); // this is a bit more than we need
217                                                           // we ignore this at the moment
218 size_t len_have;
219 union {
220  void     * vp;
221  char     * cp;
222  uint16_t * u16;
223  uint32_t * u32;
224 } data;
225 size_t  dataoffset;
226 ssize_t ret;
227 int     i;
228
229 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
230
231 if ( self->mtu < (sizeof(struct roar_rtp_header) + self->bpf) )
232  return -1;
233
234 if ( len_need > self->mtu ) {
235  len_have = 0;
236  ret      = 0;
237
238  while (count) {
239   len_need = self->mtu - sizeof(struct roar_rtp_header);
240
241   if ( count < len_need )
242    len_need = count;
243
244   if ( (ret = roar_vio_rtp_write(vio, buf, len_need)) == -1 )
245    break;
246
247   len_have += ret;
248   buf      += ret;
249   count    -= ret;
250  }
251
252  return len_have ? len_have : ret;
253 }
254
255 if ( self->io == NULL ) {
256  if ( roar_buffer_new(&(self->io), len_need) == -1 )
257   return -1;
258
259  len_have = len_need;
260 } else {
261  if ( roar_buffer_get_len(self->io, &len_have) == -1 )
262   return -1;
263
264  if ( len_have < len_need ) {
265   if ( roar_buffer_set_len(self->io, len_need) == -1 ) {
266    if ( roar_buffer_free(self->io) == -1 )
267     return -1;
268
269    self->io = NULL;
270    return roar_vio_rtp_write(vio, buf, count); // restart ower self from the beginning with no buffer
271   }
272  }
273 }
274
275 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
276
277 if ( roar_buffer_get_data(self->io, &(data.vp)) == -1 )
278  return -1;
279
280 memset(data.vp, 0, len_need);
281
282 self->header.seq_num++;
283
284 data.cp[0]   = 2;
285 data.cp[0]  |= self->header.csrc_count   << 4;
286 data.cp[1]  |= self->header.payload_type << 1;
287
288 data.u16[1]  = ROAR_HOST2NET16(self->header.seq_num);
289
290 data.u32[1]  = ROAR_HOST2NET32(self->header.ts);
291 data.u32[2]  = ROAR_HOST2NET32(self->header.ssrc);
292
293 for (i = 0; i < self->header.csrc_count; i++) {
294  data.u32[3+i] = ROAR_HOST2NET32(self->header.csrc[i]);
295 }
296
297 dataoffset   = 3*4 + self->header.csrc_count*4;
298
299 memcpy(data.vp + dataoffset, buf, count);
300
301 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
302
303 if ( (ret = roar_vio_write(self->vio, data.vp, count+dataoffset)) == -1 )
304  return -1;
305
306 len_have = ret - dataoffset;
307
308 self->header.ts += len_have / self->bpf;
309
310 return len_have;
311}
312
313off_t   roar_vio_rtp_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
314
315int     roar_vio_rtp_nonblock(struct roar_vio_calls * vio, int state) {
316 struct roar_rtp_inst * self = vio->inst;
317
318 ROAR_DBG("roar_vio_rtp_nonblock(vio=%p, state=%i) = ?", vio, state);
319
320 return roar_vio_nonblock(self->vio, state);
321}
322
323int     roar_vio_rtp_sync    (struct roar_vio_calls * vio) {
324 struct roar_rtp_inst * self = vio->inst;
325
326 ROAR_DBG("roar_vio_rtp_sync(vio=%p) = ?", vio);
327
328 return roar_vio_sync(self->vio);
329}
330
331int     roar_vio_rtp_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
332 struct roar_rtp_inst * self = vio->inst;
333 struct roar_stream          * s  = NULL;
334 struct roar_stream_server   * ss = NULL;
335
336 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
337
338 if (vio == NULL || cmd == -1)
339  return -1;
340
341 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
342
343 switch (cmd) {
344  case ROAR_VIO_CTL_SET_SSTREAM:
345    s = ROAR_STREAM(ss = data);
346   break;
347  case ROAR_VIO_CTL_SET_STREAM:
348    s = ROAR_STREAM(data);
349   break;
350  case ROAR_VIO_CTL_GET_NEXT:
351    *(struct roar_vio_calls **)data = self->vio;
352    return 0;
353   break;
354  case ROAR_VIO_CTL_SET_NEXT:
355    self->vio = *(struct roar_vio_calls **)data;
356    return 0;
357   break;
358 }
359
360 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
361
362 if ( s != NULL ) {
363  switch (s->info.codec) {
364   case ROAR_CODEC_PCM_S_LE:
365   case ROAR_CODEC_PCM_S_PDP:
366     s->info.codec = ROAR_CODEC_PCM_S_BE;
367    break;
368   case ROAR_CODEC_PCM_U_LE:
369   case ROAR_CODEC_PCM_U_PDP:
370     s->info.codec = ROAR_CODEC_PCM_U_BE;
371    break;
372  }
373
374  memcpy(&(self->info), &(s->info), sizeof(struct roar_audio_info));
375
376  self->header.payload_type = _info2pt(&(s->info));
377
378  self->bpf                 = s->info.channels * s->info.bits / 8;
379
380  roar_vio_ctl(self->vio, cmd, data);
381  return 0;
382 }
383
384 return roar_vio_ctl(self->vio, cmd, data);
385}
386
387int     roar_vio_rtp_close   (struct roar_vio_calls * vio) {
388 struct roar_rtp_inst * self = vio->inst;
389 int ret;
390
391 ROAR_DBG("roar_vio_rtp_close(vio=%p) = ?", vio);
392
393 ret = roar_vio_close(self->vio);
394
395 if ( self->io != NULL )
396  if ( roar_buffer_free(self->io) == -1 )
397   ret = -1;
398
399 roar_mm_free(self);
400
401 ROAR_DBG("roar_vio_rtp_close(vio=%p) = %i", vio, ret);
402
403 return ret;
404}
405
406//ll
Note: See TracBrowser for help on using the repository browser.