]> code.delx.au - pulseaudio/blob - src/modules/rtp/rtp.c
* change default multicast address
[pulseaudio] / src / modules / rtp / rtp.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <arpa/inet.h>
32 #include <unistd.h>
33
34 #include <polypcore/log.h>
35
36 #include "rtp.h"
37
38 pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssrc, uint8_t payload, size_t frame_size) {
39 assert(c);
40 assert(fd >= 0);
41
42 c->fd = fd;
43 c->sequence = (uint16_t) (rand()*rand());
44 c->timestamp = 0;
45 c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
46 c->payload = payload & 127;
47 c->frame_size = frame_size;
48
49 return c;
50 }
51
52 #define MAX_IOVECS 16
53
54 int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
55 struct iovec iov[MAX_IOVECS];
56 pa_memblock* mb[MAX_IOVECS];
57 int iov_idx = 1;
58 size_t n = 0, skip = 0;
59
60 assert(c);
61 assert(size > 0);
62 assert(q);
63
64 if (pa_memblockq_get_length(q) < size)
65 return 0;
66
67 for (;;) {
68 int r;
69 pa_memchunk chunk;
70
71 if ((r = pa_memblockq_peek(q, &chunk)) >= 0) {
72
73 size_t k = n + chunk.length > size ? size - n : chunk.length;
74
75 if (chunk.memblock) {
76 iov[iov_idx].iov_base = (uint8_t*) chunk.memblock->data + chunk.index;
77 iov[iov_idx].iov_len = k;
78 mb[iov_idx] = chunk.memblock;
79 iov_idx ++;
80
81 n += k;
82 }
83
84 skip += k;
85 pa_memblockq_drop(q, &chunk, k);
86 }
87
88 if (r < 0 || !chunk.memblock || n >= size || iov_idx >= MAX_IOVECS) {
89 uint32_t header[3];
90 struct msghdr m;
91 int k, i;
92
93 if (n > 0) {
94 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
95 header[1] = htonl(c->timestamp);
96 header[2] = htonl(c->ssrc);
97
98 iov[0].iov_base = header;
99 iov[0].iov_len = sizeof(header);
100
101 m.msg_name = NULL;
102 m.msg_namelen = 0;
103 m.msg_iov = iov;
104 m.msg_iovlen = iov_idx;
105 m.msg_control = NULL;
106 m.msg_controllen = 0;
107 m.msg_flags = 0;
108
109 k = sendmsg(c->fd, &m, MSG_DONTWAIT);
110
111 for (i = 1; i < iov_idx; i++)
112 pa_memblock_unref(mb[i]);
113
114 c->sequence++;
115 } else
116 k = 0;
117
118 c->timestamp += skip/c->frame_size;
119
120 if (k < 0) {
121 if (errno != EAGAIN) /* If the queue is full, just ignore it */
122 pa_log(__FILE__": sendmsg() failed: %s", strerror(errno));
123 return -1;
124 }
125
126 if (r < 0 || pa_memblockq_get_length(q) < size)
127 break;
128
129 n = 0;
130 skip = 0;
131 iov_idx = 1;
132 }
133 }
134
135 return 0;
136 }
137
138 pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd) {
139 assert(c);
140
141 c->fd = fd;
142 return c;
143 }
144
145 int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk) {
146 assert(c);
147 assert(chunk);
148
149 return 0;
150 }
151
152 uint8_t pa_rtp_payload_type(const pa_sample_spec *ss) {
153 assert(ss);
154
155 if (ss->format == PA_SAMPLE_ULAW && ss->rate == 8000 && ss->channels == 1)
156 return 0;
157 if (ss->format == PA_SAMPLE_ALAW && ss->rate == 8000 && ss->channels == 1)
158 return 0;
159 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
160 return 10;
161 if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
162 return 11;
163
164 return 127;
165 }
166
167 pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss) {
168 assert(ss);
169
170 if (!pa_rtp_sample_spec_valid(ss))
171 ss->format = PA_SAMPLE_S16BE;
172
173 assert(pa_rtp_sample_spec_valid(ss));
174 return ss;
175 }
176
177 int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
178 assert(ss);
179
180 if (!pa_sample_spec_valid(ss))
181 return 0;
182
183 return
184 ss->format == PA_SAMPLE_U8 ||
185 ss->format == PA_SAMPLE_ALAW ||
186 ss->format == PA_SAMPLE_ULAW ||
187 ss->format == PA_SAMPLE_S16BE;
188 }
189
190 void pa_rtp_context_destroy(pa_rtp_context *c) {
191 assert(c);
192
193 close(c->fd);
194 }