]>
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.1 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 <sys/ioctl.h>
32 #ifdef HAVE_SYS_FILIO_H
33 #include <sys/filio.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/arpa-inet.h>
48 pa_rtp_context
* pa_rtp_context_init_send(pa_rtp_context
*c
, int fd
, uint32_t ssrc
, uint8_t payload
, size_t frame_size
) {
53 c
->sequence
= (uint16_t) (rand()*rand());
55 c
->ssrc
= ssrc
? ssrc
: (uint32_t) (rand()*rand());
56 c
->payload
= (uint8_t) (payload
& 127U);
57 c
->frame_size
= frame_size
;
59 pa_memchunk_reset(&c
->memchunk
);
66 int pa_rtp_send(pa_rtp_context
*c
, size_t size
, pa_memblockq
*q
) {
67 struct iovec iov
[MAX_IOVECS
];
68 pa_memblock
* mb
[MAX_IOVECS
];
76 if (pa_memblockq_get_length(q
) < size
)
83 pa_memchunk_reset(&chunk
);
85 if ((r
= pa_memblockq_peek(q
, &chunk
)) >= 0) {
87 size_t k
= n
+ chunk
.length
> size
? size
- n
: chunk
.length
;
89 pa_assert(chunk
.memblock
);
91 iov
[iov_idx
].iov_base
= pa_memblock_acquire_chunk(&chunk
);
92 iov
[iov_idx
].iov_len
= k
;
93 mb
[iov_idx
] = chunk
.memblock
;
97 pa_memblockq_drop(q
, k
);
100 pa_assert(n
% c
->frame_size
== 0);
102 if (r
< 0 || n
>= size
|| iov_idx
>= MAX_IOVECS
) {
109 header
[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c
->payload
<< 16) | ((uint32_t) c
->sequence
));
110 header
[1] = htonl(c
->timestamp
);
111 header
[2] = htonl(c
->ssrc
);
113 iov
[0].iov_base
= (void*)header
;
114 iov
[0].iov_len
= sizeof(header
);
119 m
.msg_iovlen
= (size_t) iov_idx
;
120 m
.msg_control
= NULL
;
121 m
.msg_controllen
= 0;
124 k
= sendmsg(c
->fd
, &m
, MSG_DONTWAIT
);
126 for (i
= 1; i
< iov_idx
; i
++) {
127 pa_memblock_release(mb
[i
]);
128 pa_memblock_unref(mb
[i
]);
135 c
->timestamp
+= (unsigned) (n
/c
->frame_size
);
138 if (errno
!= EAGAIN
&& errno
!= EINTR
) /* If the queue is full, just ignore it */
139 pa_log("sendmsg() failed: %s", pa_cstrerror(errno
));
143 if (r
< 0 || pa_memblockq_get_length(q
) < size
)
154 pa_rtp_context
* pa_rtp_context_init_recv(pa_rtp_context
*c
, int fd
, size_t frame_size
) {
158 c
->frame_size
= frame_size
;
160 pa_memchunk_reset(&c
->memchunk
);
164 int pa_rtp_recv(pa_rtp_context
*c
, pa_memchunk
*chunk
, pa_mempool
*pool
, struct timeval
*tstamp
) {
173 bool found_tstamp
= false;
178 pa_memchunk_reset(chunk
);
180 if (ioctl(c
->fd
, FIONREAD
, &size
) < 0) {
181 pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno
));
188 if (c
->memchunk
.length
< (unsigned) size
) {
191 if (c
->memchunk
.memblock
)
192 pa_memblock_unref(c
->memchunk
.memblock
);
194 l
= PA_MAX((size_t) size
, pa_mempool_block_size_max(pool
));
196 c
->memchunk
.memblock
= pa_memblock_new(pool
, l
);
197 c
->memchunk
.index
= 0;
198 c
->memchunk
.length
= pa_memblock_get_length(c
->memchunk
.memblock
);
201 pa_assert(c
->memchunk
.length
>= (size_t) size
);
203 chunk
->memblock
= pa_memblock_ref(c
->memchunk
.memblock
);
204 chunk
->index
= c
->memchunk
.index
;
206 iov
.iov_base
= pa_memblock_acquire_chunk(chunk
);
207 iov
.iov_len
= (size_t) size
;
214 m
.msg_controllen
= sizeof(aux
);
217 r
= recvmsg(c
->fd
, &m
, 0);
218 pa_memblock_release(chunk
->memblock
);
221 if (r
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
222 pa_log_warn("recvmsg() failed: %s", r
< 0 ? pa_cstrerror(errno
) : "size mismatch");
228 pa_log_warn("RTP packet too short.");
232 memcpy(&header
, iov
.iov_base
, sizeof(uint32_t));
233 memcpy(&c
->timestamp
, (uint8_t*) iov
.iov_base
+ 4, sizeof(uint32_t));
234 memcpy(&c
->ssrc
, (uint8_t*) iov
.iov_base
+ 8, sizeof(uint32_t));
236 header
= ntohl(header
);
237 c
->timestamp
= ntohl(c
->timestamp
);
238 c
->ssrc
= ntohl(c
->ssrc
);
240 if ((header
>> 30) != 2) {
241 pa_log_warn("Unsupported RTP version.");
245 if ((header
>> 29) & 1) {
246 pa_log_warn("RTP padding not supported.");
250 if ((header
>> 28) & 1) {
251 pa_log_warn("RTP header extensions not supported.");
255 cc
= (header
>> 24) & 0xF;
256 c
->payload
= (uint8_t) ((header
>> 16) & 127U);
257 c
->sequence
= (uint16_t) (header
& 0xFFFFU
);
259 if (12 + cc
*4 > (unsigned) size
) {
260 pa_log_warn("RTP packet too short. (CSRC)");
264 chunk
->index
+= 12 + cc
*4;
265 chunk
->length
= (size_t) size
- 12 + cc
*4;
267 if (chunk
->length
% c
->frame_size
!= 0) {
268 pa_log_warn("Bad RTP packet size.");
272 c
->memchunk
.index
= chunk
->index
+ chunk
->length
;
273 c
->memchunk
.length
= pa_memblock_get_length(c
->memchunk
.memblock
) - c
->memchunk
.index
;
275 if (c
->memchunk
.length
<= 0) {
276 pa_memblock_unref(c
->memchunk
.memblock
);
277 pa_memchunk_reset(&c
->memchunk
);
280 for (cm
= CMSG_FIRSTHDR(&m
); cm
; cm
= CMSG_NXTHDR(&m
, cm
))
281 if (cm
->cmsg_level
== SOL_SOCKET
&& cm
->cmsg_type
== SCM_TIMESTAMP
) {
282 memcpy(tstamp
, CMSG_DATA(cm
), sizeof(struct timeval
));
288 pa_log_warn("Couldn't find SCM_TIMESTAMP data in auxiliary recvmsg() data!");
296 pa_memblock_unref(chunk
->memblock
);
301 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec
*ss
) {
304 if (ss
->format
== PA_SAMPLE_ULAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
306 if (ss
->format
== PA_SAMPLE_ALAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
308 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 2)
310 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 1)
316 pa_sample_spec
*pa_rtp_sample_spec_from_payload(uint8_t payload
, pa_sample_spec
*ss
) {
322 ss
->format
= PA_SAMPLE_ULAW
;
328 ss
->format
= PA_SAMPLE_ALAW
;
334 ss
->format
= PA_SAMPLE_S16BE
;
340 ss
->format
= PA_SAMPLE_S16BE
;
351 pa_sample_spec
*pa_rtp_sample_spec_fixup(pa_sample_spec
* ss
) {
354 if (!pa_rtp_sample_spec_valid(ss
))
355 ss
->format
= PA_SAMPLE_S16BE
;
357 pa_assert(pa_rtp_sample_spec_valid(ss
));
361 int pa_rtp_sample_spec_valid(const pa_sample_spec
*ss
) {
364 if (!pa_sample_spec_valid(ss
))
368 ss
->format
== PA_SAMPLE_U8
||
369 ss
->format
== PA_SAMPLE_ALAW
||
370 ss
->format
== PA_SAMPLE_ULAW
||
371 ss
->format
== PA_SAMPLE_S16BE
;
374 void pa_rtp_context_destroy(pa_rtp_context
*c
) {
377 pa_assert_se(pa_close(c
->fd
) == 0);
379 if (c
->memchunk
.memblock
)
380 pa_memblock_unref(c
->memchunk
.memblock
);
383 const char* pa_rtp_format_to_string(pa_sample_format_t f
) {
385 case PA_SAMPLE_S16BE
:
398 pa_sample_format_t
pa_rtp_string_to_format(const char *s
) {
401 if (pa_streq(s
, "L16"))
402 return PA_SAMPLE_S16BE
;
403 else if (pa_streq(s
, "L8"))
405 else if (pa_streq(s
, "PCMA"))
406 return PA_SAMPLE_ALAW
;
407 else if (pa_streq(s
, "PCMU"))
408 return PA_SAMPLE_ULAW
;
410 return PA_SAMPLE_INVALID
;