]>
code.delx.au - pulseaudio/blob - src/modules/rtp/rtp.c
4 This file is part of polypaudio.
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.
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.
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
31 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
35 #include <polypcore/log.h>
39 pa_rtp_context
* pa_rtp_context_init_send(pa_rtp_context
*c
, int fd
, uint32_t ssrc
, uint8_t payload
, size_t frame_size
) {
44 c
->sequence
= (uint16_t) (rand()*rand());
46 c
->ssrc
= ssrc
? ssrc
: (uint32_t) (rand()*rand());
47 c
->payload
= payload
& 127;
48 c
->frame_size
= frame_size
;
55 int pa_rtp_send(pa_rtp_context
*c
, size_t size
, pa_memblockq
*q
) {
56 struct iovec iov
[MAX_IOVECS
];
57 pa_memblock
* mb
[MAX_IOVECS
];
59 size_t n
= 0, skip
= 0;
65 if (pa_memblockq_get_length(q
) < size
)
72 if ((r
= pa_memblockq_peek(q
, &chunk
)) >= 0) {
74 size_t k
= n
+ chunk
.length
> size
? size
- n
: chunk
.length
;
77 iov
[iov_idx
].iov_base
= (uint8_t*) chunk
.memblock
->data
+ chunk
.index
;
78 iov
[iov_idx
].iov_len
= k
;
79 mb
[iov_idx
] = chunk
.memblock
;
86 pa_memblockq_drop(q
, &chunk
, k
);
89 if (r
< 0 || !chunk
.memblock
|| n
>= size
|| iov_idx
>= MAX_IOVECS
) {
95 header
[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c
->payload
<< 16) | ((uint32_t) c
->sequence
));
96 header
[1] = htonl(c
->timestamp
);
97 header
[2] = htonl(c
->ssrc
);
99 iov
[0].iov_base
= header
;
100 iov
[0].iov_len
= sizeof(header
);
105 m
.msg_iovlen
= iov_idx
;
106 m
.msg_control
= NULL
;
107 m
.msg_controllen
= 0;
110 k
= sendmsg(c
->fd
, &m
, MSG_DONTWAIT
);
112 for (i
= 1; i
< iov_idx
; i
++)
113 pa_memblock_unref(mb
[i
]);
119 c
->timestamp
+= skip
/c
->frame_size
;
122 if (errno
!= EAGAIN
) /* If the queue is full, just ignore it */
123 pa_log(__FILE__
": sendmsg() failed: %s", strerror(errno
));
127 if (r
< 0 || pa_memblockq_get_length(q
) < size
)
139 pa_rtp_context
* pa_rtp_context_init_recv(pa_rtp_context
*c
, int fd
, size_t frame_size
) {
143 c
->frame_size
= frame_size
;
147 int pa_rtp_recv(pa_rtp_context
*c
, pa_memchunk
*chunk
, pa_memblock_stat
*st
) {
158 chunk
->memblock
= NULL
;
160 if (ioctl(c
->fd
, FIONREAD
, &size
) < 0) {
161 pa_log(__FILE__
": FIONREAD failed: %s", strerror(errno
));
168 chunk
->memblock
= pa_memblock_new(size
, st
);
170 iov
.iov_base
= chunk
->memblock
->data
;
177 m
.msg_control
= NULL
;
178 m
.msg_controllen
= 0;
181 if ((r
= recvmsg(c
->fd
, &m
, 0)) != size
) {
182 pa_log(__FILE__
": recvmsg() failed: %s", r
< 0 ? strerror(errno
) : "size mismatch");
187 pa_log(__FILE__
": RTP packet too short.");
191 memcpy(&header
, chunk
->memblock
->data
, sizeof(uint32_t));
192 memcpy(&c
->timestamp
, (uint8_t*) chunk
->memblock
->data
+ 4, sizeof(uint32_t));
193 memcpy(&c
->ssrc
, (uint8_t*) chunk
->memblock
->data
+ 8, sizeof(uint32_t));
195 header
= ntohl(header
);
196 c
->timestamp
= ntohl(c
->timestamp
);
197 c
->ssrc
= ntohl(c
->ssrc
);
199 if ((header
>> 30) != 2) {
200 pa_log(__FILE__
": Unsupported RTP version.");
204 if ((header
>> 29) & 1) {
205 pa_log(__FILE__
": RTP padding not supported.");
209 if ((header
>> 28) & 1) {
210 pa_log(__FILE__
": RTP header extensions not supported.");
214 cc
= (header
>> 24) & 0xF;
215 c
->payload
= (header
>> 16) & 127;
216 c
->sequence
= header
& 0xFFFF;
218 if (12 + cc
*4 > size
) {
219 pa_log(__FILE__
": RTP packet too short. (CSRC)");
223 chunk
->index
= 12 + cc
*4;
224 chunk
->length
= size
- chunk
->index
;
226 if (chunk
->length
% c
->frame_size
!= 0) {
227 pa_log(__FILE__
": Vad RTP packet size.");
235 pa_memblock_unref(chunk
->memblock
);
240 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec
*ss
) {
243 if (ss
->format
== PA_SAMPLE_ULAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
245 if (ss
->format
== PA_SAMPLE_ALAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
247 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 2)
249 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 1)
255 pa_sample_spec
*pa_rtp_sample_spec_from_payload(uint8_t payload
, pa_sample_spec
*ss
) {
261 ss
->format
= PA_SAMPLE_ULAW
;
267 ss
->format
= PA_SAMPLE_ALAW
;
273 ss
->format
= PA_SAMPLE_S16BE
;
279 ss
->format
= PA_SAMPLE_S16BE
;
290 pa_sample_spec
*pa_rtp_sample_spec_fixup(pa_sample_spec
* ss
) {
293 if (!pa_rtp_sample_spec_valid(ss
))
294 ss
->format
= PA_SAMPLE_S16BE
;
296 assert(pa_rtp_sample_spec_valid(ss
));
300 int pa_rtp_sample_spec_valid(const pa_sample_spec
*ss
) {
303 if (!pa_sample_spec_valid(ss
))
307 ss
->format
== PA_SAMPLE_U8
||
308 ss
->format
== PA_SAMPLE_ALAW
||
309 ss
->format
== PA_SAMPLE_ULAW
||
310 ss
->format
== PA_SAMPLE_S16BE
;
313 void pa_rtp_context_destroy(pa_rtp_context
*c
) {
319 const char* pa_rtp_format_to_string(pa_sample_format_t f
) {
321 case PA_SAMPLE_S16BE
:
334 pa_sample_format_t
pa_rtp_string_to_format(const char *s
) {
337 if (!(strcmp(s
, "L16")))
338 return PA_SAMPLE_S16BE
;
339 else if (!strcmp(s
, "L8"))
341 else if (!strcmp(s
, "PCMA"))
342 return PA_SAMPLE_ALAW
;
343 else if (!strcmp(s
, "PCMU"))
344 return PA_SAMPLE_ULAW
;
346 return PA_SAMPLE_INVALID
;