]> code.delx.au - pulseaudio/blobdiff - src/modules/rtp/rtp.c
Remove unnecessary #includes
[pulseaudio] / src / modules / rtp / rtp.c
index 997fcc3482a39fb0a01de8c81437b36af81c6bd8..05c736a7525b836fce8a54eec33c1a9abc083cd7 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
@@ -7,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #include <config.h>
 #endif
 
-#include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <arpa/inet.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 
 #include <sys/filio.h>
 #endif
 
+#ifdef HAVE_SYS_UIO_H
+#include <sys/uio.h>
+#endif
+
 #include <pulsecore/core-error.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/core-util.h>
+#include <pulsecore/arpa-inet.h>
 
 #include "rtp.h"
 
@@ -52,9 +53,11 @@ pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssr
     c->sequence = (uint16_t) (rand()*rand());
     c->timestamp = 0;
     c->ssrc = ssrc ? ssrc : (uint32_t) (rand()*rand());
-    c->payload = payload & 127;
+    c->payload = (uint8_t) (payload & 127U);
     c->frame_size = frame_size;
 
+    pa_memchunk_reset(&c->memchunk);
+
     return c;
 }
 
@@ -99,7 +102,8 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
         if (r < 0 || n >= size || iov_idx >= MAX_IOVECS) {
             uint32_t header[3];
             struct msghdr m;
-            int k, i;
+            ssize_t k;
+            int i;
 
             if (n > 0) {
                 header[0] = htonl(((uint32_t) 2 << 30) | ((uint32_t) c->payload << 16) | ((uint32_t) c->sequence));
@@ -112,7 +116,7 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
                 m.msg_name = NULL;
                 m.msg_namelen = 0;
                 m.msg_iov = iov;
-                m.msg_iovlen = iov_idx;
+                m.msg_iovlen = (size_t) iov_idx;
                 m.msg_control = NULL;
                 m.msg_controllen = 0;
                 m.msg_flags = 0;
@@ -128,7 +132,7 @@ int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q) {
             } else
                 k = 0;
 
-            c->timestamp += n/c->frame_size;
+            c->timestamp += (unsigned) (n/c->frame_size);
 
             if (k < 0) {
                 if (errno != EAGAIN && errno != EINTR) /* If the queue is full, just ignore it */
@@ -152,16 +156,21 @@ pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame
 
     c->fd = fd;
     c->frame_size = frame_size;
+
+    pa_memchunk_reset(&c->memchunk);
     return c;
 }
 
-int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
+int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool, struct timeval *tstamp) {
     int size;
     struct msghdr m;
+    struct cmsghdr *cm;
     struct iovec iov;
     uint32_t header;
-    int cc;
+    unsigned cc;
     ssize_t r;
+    uint8_t aux[1024];
+    pa_bool_t found_tstamp = FALSE;
 
     pa_assert(c);
     pa_assert(chunk);
@@ -173,20 +182,36 @@ int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
         goto fail;
     }
 
-    if (!size)
+    if (size <= 0)
         return 0;
 
-    chunk->memblock = pa_memblock_new(pool, size);
+    if (c->memchunk.length < (unsigned) size) {
+        size_t l;
+
+        if (c->memchunk.memblock)
+            pa_memblock_unref(c->memchunk.memblock);
+
+        l = PA_MAX((size_t) size, pa_mempool_block_size_max(pool));
+
+        c->memchunk.memblock = pa_memblock_new(pool, l);
+        c->memchunk.index = 0;
+        c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock);
+    }
+
+    pa_assert(c->memchunk.length >= (size_t) size);
+
+    chunk->memblock = pa_memblock_ref(c->memchunk.memblock);
+    chunk->index = c->memchunk.index;
 
-    iov.iov_base = pa_memblock_acquire(chunk->memblock);
-    iov.iov_len = size;
+    iov.iov_base = (uint8_t*) pa_memblock_acquire(chunk->memblock) + chunk->index;
+    iov.iov_len = (size_t) size;
 
     m.msg_name = NULL;
     m.msg_namelen = 0;
     m.msg_iov = &iov;
     m.msg_iovlen = 1;
-    m.msg_control = NULL;
-    m.msg_controllen = 0;
+    m.msg_control = aux;
+    m.msg_controllen = sizeof(aux);
     m.msg_flags = 0;
 
     r = recvmsg(c->fd, &m, 0);
@@ -228,22 +253,42 @@ int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) {
     }
 
     cc = (header >> 24) & 0xF;
-    c->payload = (header >> 16) & 127;
-    c->sequence = header & 0xFFFF;
+    c->payload = (uint8_t) ((header >> 16) & 127U);
+    c->sequence = (uint16_t) (header & 0xFFFFU);
 
-    if (12 + cc*4 > size) {
+    if (12 + cc*4 > (unsigned) size) {
         pa_log_warn("RTP packet too short. (CSRC)");
         goto fail;
     }
 
-    chunk->index = 12 + cc*4;
-    chunk->length = size - chunk->index;
+    chunk->index += 12 + cc*4;
+    chunk->length = (size_t) size - 12 + cc*4;
 
     if (chunk->length % c->frame_size != 0) {
         pa_log_warn("Bad RTP packet size.");
         goto fail;
     }
 
+    c->memchunk.index = chunk->index + chunk->length;
+    c->memchunk.length = pa_memblock_get_length(c->memchunk.memblock) - c->memchunk.index;
+
+    if (c->memchunk.length <= 0) {
+        pa_memblock_unref(c->memchunk.memblock);
+        pa_memchunk_reset(&c->memchunk);
+    }
+
+    for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm))
+        if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) {
+            memcpy(tstamp, CMSG_DATA(cm), sizeof(struct timeval));
+            found_tstamp = TRUE;
+            break;
+        }
+
+    if (!found_tstamp) {
+        pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
+        memset(tstamp, 0, sizeof(tstamp));
+    }
+
     return 0;
 
 fail:
@@ -329,7 +374,10 @@ int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
 void pa_rtp_context_destroy(pa_rtp_context *c) {
     pa_assert(c);
 
-    pa_close(c->fd);
+    pa_assert_se(pa_close(c->fd) == 0);
+
+    if (c->memchunk.memblock)
+        pa_memblock_unref(c->memchunk.memblock);
 }
 
 const char* pa_rtp_format_to_string(pa_sample_format_t f) {
@@ -361,4 +409,3 @@ pa_sample_format_t pa_rtp_string_to_format(const char *s) {
     else
         return PA_SAMPLE_INVALID;
 }
-