]>
code.delx.au - pulseaudio/blob - src/modules/rtp/rtp.c
4 This file is part of PulseAudio.
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
31 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_FILIO_H
36 #include <sys/filio.h>
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/log.h>
44 pa_rtp_context
* pa_rtp_context_init_send(pa_rtp_context
*c
, int fd
, uint32_t ssrc
, uint8_t payload
, size_t frame_size
) {
49 c
->sequence
= (uint16_t) (rand()*rand());
51 c
->ssrc
= ssrc
? ssrc
: (uint32_t) (rand()*rand());
52 c
->payload
= payload
& 127;
53 c
->frame_size
= frame_size
;
60 int pa_rtp_send(pa_rtp_context
*c
, size_t size
, pa_memblockq
*q
) {
61 struct iovec iov
[MAX_IOVECS
];
62 pa_memblock
* mb
[MAX_IOVECS
];
64 size_t n
= 0, skip
= 0;
70 if (pa_memblockq_get_length(q
) < size
)
77 if ((r
= pa_memblockq_peek(q
, &chunk
)) >= 0) {
79 size_t k
= n
+ chunk
.length
> size
? size
- n
: chunk
.length
;
82 iov
[iov_idx
].iov_base
= (void*)((uint8_t*) chunk
.memblock
->data
+ chunk
.index
);
83 iov
[iov_idx
].iov_len
= k
;
84 mb
[iov_idx
] = chunk
.memblock
;
91 pa_memblockq_drop(q
, &chunk
, k
);
94 if (r
< 0 || !chunk
.memblock
|| n
>= size
|| iov_idx
>= MAX_IOVECS
) {
100 header
[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c
->payload
<< 16) | ((uint32_t) c
->sequence
));
101 header
[1] = htonl(c
->timestamp
);
102 header
[2] = htonl(c
->ssrc
);
104 iov
[0].iov_base
= (void*)header
;
105 iov
[0].iov_len
= sizeof(header
);
110 m
.msg_iovlen
= iov_idx
;
111 m
.msg_control
= NULL
;
112 m
.msg_controllen
= 0;
115 k
= sendmsg(c
->fd
, &m
, MSG_DONTWAIT
);
117 for (i
= 1; i
< iov_idx
; i
++)
118 pa_memblock_unref(mb
[i
]);
124 c
->timestamp
+= skip
/c
->frame_size
;
127 if (errno
!= EAGAIN
) /* If the queue is full, just ignore it */
128 pa_log(__FILE__
": sendmsg() failed: %s", pa_cstrerror(errno
));
132 if (r
< 0 || pa_memblockq_get_length(q
) < size
)
144 pa_rtp_context
* pa_rtp_context_init_recv(pa_rtp_context
*c
, int fd
, size_t frame_size
) {
148 c
->frame_size
= frame_size
;
152 int pa_rtp_recv(pa_rtp_context
*c
, pa_memchunk
*chunk
, pa_memblock_stat
*st
) {
163 chunk
->memblock
= NULL
;
165 if (ioctl(c
->fd
, FIONREAD
, &size
) < 0) {
166 pa_log(__FILE__
": FIONREAD failed: %s", pa_cstrerror(errno
));
173 chunk
->memblock
= pa_memblock_new(size
, st
);
175 iov
.iov_base
= chunk
->memblock
->data
;
182 m
.msg_control
= NULL
;
183 m
.msg_controllen
= 0;
186 if ((r
= recvmsg(c
->fd
, &m
, 0)) != size
) {
187 pa_log(__FILE__
": recvmsg() failed: %s", r
< 0 ? pa_cstrerror(errno
) : "size mismatch");
192 pa_log(__FILE__
": RTP packet too short.");
196 memcpy(&header
, chunk
->memblock
->data
, sizeof(uint32_t));
197 memcpy(&c
->timestamp
, (uint8_t*) chunk
->memblock
->data
+ 4, sizeof(uint32_t));
198 memcpy(&c
->ssrc
, (uint8_t*) chunk
->memblock
->data
+ 8, sizeof(uint32_t));
200 header
= ntohl(header
);
201 c
->timestamp
= ntohl(c
->timestamp
);
202 c
->ssrc
= ntohl(c
->ssrc
);
204 if ((header
>> 30) != 2) {
205 pa_log(__FILE__
": Unsupported RTP version.");
209 if ((header
>> 29) & 1) {
210 pa_log(__FILE__
": RTP padding not supported.");
214 if ((header
>> 28) & 1) {
215 pa_log(__FILE__
": RTP header extensions not supported.");
219 cc
= (header
>> 24) & 0xF;
220 c
->payload
= (header
>> 16) & 127;
221 c
->sequence
= header
& 0xFFFF;
223 if (12 + cc
*4 > size
) {
224 pa_log(__FILE__
": RTP packet too short. (CSRC)");
228 chunk
->index
= 12 + cc
*4;
229 chunk
->length
= size
- chunk
->index
;
231 if (chunk
->length
% c
->frame_size
!= 0) {
232 pa_log(__FILE__
": Vad RTP packet size.");
240 pa_memblock_unref(chunk
->memblock
);
245 uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec
*ss
) {
248 if (ss
->format
== PA_SAMPLE_ULAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
250 if (ss
->format
== PA_SAMPLE_ALAW
&& ss
->rate
== 8000 && ss
->channels
== 1)
252 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 2)
254 if (ss
->format
== PA_SAMPLE_S16BE
&& ss
->rate
== 44100 && ss
->channels
== 1)
260 pa_sample_spec
*pa_rtp_sample_spec_from_payload(uint8_t payload
, pa_sample_spec
*ss
) {
266 ss
->format
= PA_SAMPLE_ULAW
;
272 ss
->format
= PA_SAMPLE_ALAW
;
278 ss
->format
= PA_SAMPLE_S16BE
;
284 ss
->format
= PA_SAMPLE_S16BE
;
295 pa_sample_spec
*pa_rtp_sample_spec_fixup(pa_sample_spec
* ss
) {
298 if (!pa_rtp_sample_spec_valid(ss
))
299 ss
->format
= PA_SAMPLE_S16BE
;
301 assert(pa_rtp_sample_spec_valid(ss
));
305 int pa_rtp_sample_spec_valid(const pa_sample_spec
*ss
) {
308 if (!pa_sample_spec_valid(ss
))
312 ss
->format
== PA_SAMPLE_U8
||
313 ss
->format
== PA_SAMPLE_ALAW
||
314 ss
->format
== PA_SAMPLE_ULAW
||
315 ss
->format
== PA_SAMPLE_S16BE
;
318 void pa_rtp_context_destroy(pa_rtp_context
*c
) {
324 const char* pa_rtp_format_to_string(pa_sample_format_t f
) {
326 case PA_SAMPLE_S16BE
:
339 pa_sample_format_t
pa_rtp_string_to_format(const char *s
) {
342 if (!(strcmp(s
, "L16")))
343 return PA_SAMPLE_S16BE
;
344 else if (!strcmp(s
, "L8"))
346 else if (!strcmp(s
, "PCMA"))
347 return PA_SAMPLE_ALAW
;
348 else if (!strcmp(s
, "PCMU"))
349 return PA_SAMPLE_ULAW
;
351 return PA_SAMPLE_INVALID
;