]>
code.delx.au - pulseaudio/blob - src/modules/rtp/rtp.c
2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
6 PulseAudio 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 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/core-util.h>
45 pa_rtp_context
* pa_rtp_context_init_send(pa_rtp_context
*c
, int fd
, uint32_t ssrc
, uint8_t payload
, size_t frame_size
) {
50 c
->sequence
= (uint16_t) (rand()*rand());
52 c
->ssrc
= ssrc
? ssrc
: (uint32_t) (rand()*rand());
53 c
->payload
= (uint8_t) (payload
& 127U);
54 c
->frame_size
= frame_size
;
56 pa_memchunk_reset(&c
->memchunk
);
63 int pa_rtp_send(pa_rtp_context
*c
, size_t size
, pa_memblockq
*q
) {
64 struct iovec iov
[MAX_IOVECS
];
65 pa_memblock
* mb
[MAX_IOVECS
];
73 if (pa_memblockq_get_length(q
) < size
)
80 pa_memchunk_reset(&chunk
);
82 if ((r
= pa_memblockq_peek(q
, &chunk
)) >= 0) {
84 size_t k
= n
+ chunk
.length
> size
? size
- n
: chunk
.length
;
86 pa_assert(chunk
.memblock
);
88 iov
[iov_idx
].iov_base
= ((uint8_t*) pa_memblock_acquire(chunk
.memblock
) + chunk
.index
);
89 iov
[iov_idx
].iov_len
= k
;
90 mb
[iov_idx
] = chunk
.memblock
;
94 pa_memblockq_drop(q
, k
);
97 pa_assert(n
% c
->frame_size
== 0);
99 if (r
< 0 || n
>= size
|| iov_idx
>= MAX_IOVECS
) {
106 header
[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c
->payload
<< 16) | ((uint32_t) c
->sequence
));
107 header
[1] = htonl(c
->timestamp
);
108 header
[2] = htonl(c
->ssrc
);
110 iov
[0].iov_base
= (void*)header
;
111 iov
[0].iov_len
= sizeof(header
);
116 m
.msg_iovlen
= (size_t) iov_idx
;
117 m
.msg_control
= NULL
;
118 m
.msg_controllen
= 0;
121 k
= sendmsg(c
->fd
, &m
, MSG_DONTWAIT
);
123 for (i
= 1; i
< iov_idx
; i
++) {
124 pa_memblock_release(mb
[i
]);
125 pa_memblock_unref(mb
[i
]);
132 c
->timestamp
+= (unsigned) (n
/c
->frame_size
);
135 if (errno
!= EAGAIN
&& errno
!= EINTR
) /* If the queue is full, just ignore it */
136 pa_log("sendmsg() failed: %s", pa_cstrerror(errno
));
140 if (r
< 0 || pa_memblockq_get_length(q
) < size
)
151 pa_rtp_context
* pa_rtp_context_init_recv(pa_rtp_context
*c
, int fd
, size_t frame_size
) {
155 c
->frame_size
= frame_size
;
157 pa_memchunk_reset(&c
->memchunk
);
161 int pa_rtp_recv(pa_rtp_context
*c
, pa_memchunk
*chunk
, pa_mempool
*pool
) {
172 pa_memchunk_reset(chunk
);
174 if (ioctl(c
->fd
, FIONREAD
, &size
) < 0) {
175 pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno
));
182 if (c
->memchunk
.length
< (unsigned) size
) {
185 if (c
->memchunk
.memblock
)
186 pa_memblock_unref(c
->memchunk
.memblock
);
188 l
= PA_MAX((size_t) size
, pa_mempool_block_size_max(pool
));
190 c
->memchunk
.memblock
= pa_memblock_new(pool
, l
);
191 c
->memchunk
.index
= 0;
192 c
->memchunk
.length
= pa_memblock_get_length(c
->memchunk
.memblock
);
195 pa_assert(c
->memchunk
.length
>= (size_t) size
);
197 chunk
->memblock
= pa_memblock_ref(c
->memchunk
.memblock
);
198 chunk
->index
= c
->memchunk
.index
;
200 iov
.iov_base
= (uint8_t*) pa_memblock_acquire(chunk
->memblock
) + chunk
->index
;
201 iov
.iov_len
= (size_t) size
;
207 m
.msg_control
= NULL
;
208 m
.msg_controllen
= 0;
211 r
= recvmsg(c
->fd
, &m
, 0);
212 pa_memblock_release(chunk
->memblock
);
215 if (r
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
216 pa_log_warn("recvmsg() failed: %s", r
< 0 ? pa_cstrerror(errno
) : "size mismatch");
222 pa_log_warn("RTP packet too short.");
226 memcpy(&header
, iov
.iov_base
, sizeof(uint32_t));
227 memcpy(&c
->timestamp
, (uint8_t*) iov
.iov_base
+ 4, sizeof(uint32_t));
228 memcpy(&c
->ssrc
, (uint8_t*) iov
.iov_base
+ 8, sizeof(uint32_t));
230 header
= ntohl(header
);
231 c
->timestamp
= ntohl(c
->timestamp
);
232 c
->ssrc
= ntohl(c
->ssrc
);
234 if ((header
>> 30) != 2) {
235 pa_log_warn("Unsupported RTP version.");
239 if ((header
>> 29) & 1) {
240 pa_log_warn("RTP padding not supported.");
244 if ((header
>> 28) & 1) {
245 pa_log_warn("RTP header extensions not supported.");
249 cc
= (header
>> 24) & 0xF;
250 c
->payload
= (uint8_t) ((header
>> 16) & 127U);
251 c
->sequence
= (uint16_t) (header
& 0xFFFFU
);
253 if (12 + cc
*4 > (unsigned) size
) {
254 pa_log_warn("RTP packet too short. (CSRC)");
258 chunk
->index
+= 12 + cc
*4;
259 chunk
->length
= (size_t) size
- 12 + cc
*4;
261 if (chunk
->length
% c
->frame_size
!= 0) {
262 pa_log_warn("Bad RTP packet size.");
266 c
->memchunk
.index
= chunk
->index
+ chunk
->length
;
267 c
->memchunk
.length
= pa_memblock_get_length(c
->memchunk
.memblock
) - c
->memchunk
.index
;
269 if (c
->memchunk
.length
<= 0) {
270 pa_memblock_unref(c
->memchunk
.memblock
);
271 pa_memchunk_reset(&c
->memchunk
);
278 pa_memblock_unref(chunk
->memblock
);
283 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec
*ss
) {
286 if (ss
->format
== PA_SAMPLE_ULAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
288 if (ss
->format
== PA_SAMPLE_ALAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
290 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 2)
292 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 1)
298 pa_sample_spec
*pa_rtp_sample_spec_from_payload(uint8_t payload
, pa_sample_spec
*ss
) {
304 ss
->format
= PA_SAMPLE_ULAW
;
310 ss
->format
= PA_SAMPLE_ALAW
;
316 ss
->format
= PA_SAMPLE_S16BE
;
322 ss
->format
= PA_SAMPLE_S16BE
;
333 pa_sample_spec
*pa_rtp_sample_spec_fixup(pa_sample_spec
* ss
) {
336 if (!pa_rtp_sample_spec_valid(ss
))
337 ss
->format
= PA_SAMPLE_S16BE
;
339 pa_assert(pa_rtp_sample_spec_valid(ss
));
343 int pa_rtp_sample_spec_valid(const pa_sample_spec
*ss
) {
346 if (!pa_sample_spec_valid(ss
))
350 ss
->format
== PA_SAMPLE_U8
||
351 ss
->format
== PA_SAMPLE_ALAW
||
352 ss
->format
== PA_SAMPLE_ULAW
||
353 ss
->format
== PA_SAMPLE_S16BE
;
356 void pa_rtp_context_destroy(pa_rtp_context
*c
) {
359 pa_assert_se(pa_close(c
->fd
) == 0);
361 if (c
->memchunk
.memblock
)
362 pa_memblock_unref(c
->memchunk
.memblock
);
365 const char* pa_rtp_format_to_string(pa_sample_format_t f
) {
367 case PA_SAMPLE_S16BE
:
380 pa_sample_format_t
pa_rtp_string_to_format(const char *s
) {
383 if (!(strcmp(s
, "L16")))
384 return PA_SAMPLE_S16BE
;
385 else if (!strcmp(s
, "L8"))
387 else if (!strcmp(s
, "PCMA"))
388 return PA_SAMPLE_ALAW
;
389 else if (!strcmp(s
, "PCMU"))
390 return PA_SAMPLE_ULAW
;
392 return PA_SAMPLE_INVALID
;