source: roaraudio/libroar/vio_rtp.c @ 3283:46e4d55ec507

Last change on this file since 3283:46e4d55ec507 was 3283:46e4d55ec507, checked in by phi, 14 years ago

added debug lions

File size: 7.9 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
96 self->header.version      = 2;
97 self->header.payload_type = ROAR_RTP_PT_UNKNOWN;
98
99 // TODO: init with random values:
100 //       Sequence Number
101 //       SSRC
102
103 memset(calls, 0, sizeof(struct roar_vio_calls));
104
105 calls->inst               = self;
106// calls->read               = roar_vio_rtp_read;
107 calls->write              = roar_vio_rtp_write;
108// calls->lseek              = roar_vio_rtp_lseek;
109 calls->nonblock           = roar_vio_rtp_nonblock;
110 calls->sync               = roar_vio_rtp_sync;
111 calls->ctl                = roar_vio_rtp_ctl;
112 calls->close              = roar_vio_rtp_close;
113
114 ROAR_DBG("roar_vio_open_rtp(calls=%p, dst=%p, dstr='%s', odef=%p) = 0", calls, dst, dstr, odef);
115
116 return 0;
117}
118
119ssize_t roar_vio_rtp_read    (struct roar_vio_calls * vio, void *buf, size_t count);
120ssize_t roar_vio_rtp_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
121 struct roar_rtp_inst * self = vio->inst;
122 size_t len_need = count + sizeof(struct roar_rtp_header); // this is a bit more than we need
123                                                           // we ignore this at the moment
124 size_t len_have;
125 union {
126  void     * vp;
127  char     * cp;
128  uint16_t * u16;
129  uint32_t * u32;
130 } data;
131 size_t  dataoffset;
132 ssize_t ret;
133 int     i;
134
135 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
136
137 if ( self->tx == NULL ) {
138  if ( roar_buffer_new(&(self->tx), len_need) == -1 )
139   return -1;
140
141  len_have = len_need;
142 } else {
143  if ( roar_buffer_get_len(self->tx, &len_have) == -1 )
144   return -1;
145
146  if ( len_have < len_need ) {
147   if ( roar_buffer_set_len(self->tx, len_need) == -1 ) {
148    if ( roar_buffer_free(self->tx) == -1 )
149     return -1;
150
151    self->tx = NULL;
152    return roar_vio_rtp_write(vio, buf, count); // restart ower self from the beginning with no buffer
153   }
154  }
155 }
156
157 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
158
159 if ( roar_buffer_get_data(self->tx, &(data.vp)) == -1 )
160  return -1;
161
162 memset(data.vp, 0, len_need);
163
164 self->header.seq_num++;
165
166 data.cp[0]   = 2;
167 data.cp[0]  |= self->header.csrc_count   << 4;
168 data.cp[1]  |= self->header.payload_type << 1;
169
170 data.u16[1]  = self->header.seq_num;
171
172 data.u32[1]  = self->header.ts;
173 data.u32[2]  = self->header.ssrc;
174
175 for (i = 0; i < self->header.csrc_count; i++) {
176  data.u32[3+i] = self->header.csrc[i];
177 }
178
179 dataoffset   = 3*4 + self->header.csrc_count*4;
180
181 memcpy(data.vp + dataoffset, buf, count);
182
183 ROAR_DBG("roar_vio_rtp_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned)count);
184
185 if ( (ret = roar_vio_write(self->vio, data.vp, count+dataoffset)) == -1 )
186  return -1;
187
188 return ret - dataoffset;
189}
190
191off_t   roar_vio_rtp_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
192
193int     roar_vio_rtp_nonblock(struct roar_vio_calls * vio, int state) {
194 struct roar_rtp_inst * self = vio->inst;
195
196 ROAR_DBG("roar_vio_rtp_nonblock(vio=%p, state=%i) = ?", vio, state);
197
198 return roar_vio_nonblock(self->vio, state);
199}
200
201int     roar_vio_rtp_sync    (struct roar_vio_calls * vio) {
202 struct roar_rtp_inst * self = vio->inst;
203
204 ROAR_DBG("roar_vio_rtp_sync(vio=%p) = ?", vio);
205
206 return roar_vio_sync(self->vio);
207}
208
209int     roar_vio_rtp_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
210 struct roar_rtp_inst * self = vio->inst;
211 struct roar_stream          * s  = NULL;
212 struct roar_stream_server   * ss = NULL;
213
214 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
215
216 if (vio == NULL || cmd == -1)
217  return -1;
218
219 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
220
221 switch (cmd) {
222  case ROAR_VIO_CTL_SET_SSTREAM:
223    s = ROAR_STREAM(ss = data);
224   break;
225  case ROAR_VIO_CTL_SET_STREAM:
226    s = ROAR_STREAM(data);
227   break;
228  case ROAR_VIO_CTL_GET_NEXT:
229    *(struct roar_vio_calls **)data = self->vio;
230    return 0;
231   break;
232  case ROAR_VIO_CTL_SET_NEXT:
233    self->vio = *(struct roar_vio_calls **)data;
234    return 0;
235   break;
236 }
237
238 ROAR_DBG("roar_vio_rtp_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
239
240 if ( s != NULL ) {
241  self->header.payload_type = _info2pt(&(s->info));
242  roar_vio_ctl(self->vio, cmd, data);
243  return 0;
244 }
245
246 return roar_vio_ctl(self->vio, cmd, data);
247}
248
249int     roar_vio_rtp_close   (struct roar_vio_calls * vio) {
250 struct roar_rtp_inst * self = vio->inst;
251 int ret;
252
253 ROAR_DBG("roar_vio_rtp_close(vio=%p) = ?", vio);
254
255 ret = roar_vio_close(self->vio);
256
257 if ( self->tx != NULL )
258  if ( roar_buffer_free(self->tx) == -1 )
259   ret = -1;
260
261 roar_mm_free(self);
262
263 ROAR_DBG("roar_vio_rtp_close(vio=%p) = %i", vio, ret);
264
265 return ret;
266}
267
268//ll
Note: See TracBrowser for help on using the repository browser.