paraslash 0.3.0
[paraslash.git] / ortp_send.c
index aabbb7da09abf953457dfb4915f593eb0f0a6612..597068a82184ce93262ac6befd60566464207b2c 100644 (file)
@@ -1,60 +1,49 @@
 /*
- * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file ortp_send.c para_server's ortp sender */
 
 #include <ortp/ortp.h>
+#include <ortp/port.h>
 
 #include "server.cmdline.h"
-#include "server.h"
+#include "para.h"
+#include "error.h"
+#include "string.h"
+#include "afh.h"
 #include "afs.h"
+#include "server.h"
+#include "vss.h"
 #include "send.h"
 #include "list.h"
 #include "ortp.h"
-#include "string.h"
 
-/** \cond convert in_addr to ascii */
+/** Convert in_addr to ascii. */
 #define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
-/** \endcond */
-
-extern struct gengetopt_args_info conf;
 
-/** describes one entry in the list of targets for the ortp sender */
+/** Describes one entry in the list of targets for the ortp sender. */
 struct ortp_target {
-/** address info */
+       /** Address info. */
        struct in_addr addr;
-/** whether the ortp sender is activated */
+       /** Whether the ortp sender is activated. */
        int status;
-/** the ortp timestamp increases by this amount */
-       int chunk_ts;
-/** the currently used timestamp for this target */
-       int last_ts;
-/** the position of this target in the list of targets */
+       /** The ortp timestamp increases by this amount. */
+       uint32_t chunk_ts;
+       /** The currently used timestamp for this target. */
+       uint32_t last_ts;
+       /** The position of this target in the list of targets. */
        struct list_head node;
-/** the UDP port */
+       /** The UDP port. */
        int port;
-/** non-zero if at least one chunk has been sent to this target */
+       /** Non-zero if at least one chunk has been sent to this target. */
        int streaming;
-/** the session pointer from libortp */
+       /** The session pointer from libortp. */
        RtpSession *session;
 };
 
-static int numtargets;
 static struct list_head targets;
 static struct sender *self;
 
@@ -66,33 +55,47 @@ static void ortp_delete_target(struct ortp_target *ot, const char *msg)
                rtp_session_destroy(ot->session);
                ot->session = NULL;
        }
-       numtargets--;
        list_del(&ot->node);
        free(ot);
 }
 
-static void ortp_send_buf(char *buf, int len, long unsigned chunks_sent)
+static void ortp_send_buf(char *buf, size_t len, long unsigned chunks_sent)
 {
        struct ortp_target *ot, *tmp;
-       int ret;
+       int ret, ortp_len = len; /* rtp_session_send_with_ts expects int */
 
+       if (ortp_len < 0)
+               return;
        list_for_each_entry_safe(ot, tmp, &targets, node) {
-               struct timeval now;
-               int ts;
-               gettimeofday(&now, NULL);
+               uint32_t ts;
                if (!ot->session)
                        continue;
                WRITE_CHUNK_TS(buf, ot->chunk_ts);
                ts = ot->chunk_ts * chunks_sent;
-               ret = rtp_session_send_with_ts(ot->session, buf, len, ts);
+               ret = rtp_session_send_with_ts(ot->session,
+                       (unsigned char*)buf, ortp_len, ts);
                ot->last_ts = ts;
                if (ret < 0)
                        ortp_delete_target(ot, "send error");
-               if (ret != len +12)
+               if (ret != len + 12)
                        PARA_NOTICE_LOG("short write %d\n", ret);
        }
 }
 
+static int set_multicast(RtpSession *s)
+{
+       unsigned char loop = 1;
+       int ret;
+
+       ret = setsockopt(s->rtp.socket,
+               IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
+       if (ret < 0) {
+               PARA_ERROR_LOG("IP_MULTICAST_LOOP error %d\n", ret);
+
+       }
+       return 1;
+}
+
 static void ortp_init_session(struct ortp_target *ot)
 {
        RtpSession *s;
@@ -114,48 +117,63 @@ static void ortp_init_session(struct ortp_target *ot)
        if (ret < 0) {
                rtp_session_destroy(ot->session);
                ot->session = NULL;
+               return;
        }
+       set_multicast(s);
 }
 
-/* called by afs */
+/* called by vss */
 static void ortp_shutdown_targets(void)
 {
-       char buf[2];
+       unsigned char buf[ORTP_AUDIO_HEADER_LEN];
        struct ortp_target *ot, *tmp;
 
-       buf[0] = ORTP_EOF;
+       WRITE_PACKET_TYPE(buf, ORTP_EOF);
        list_for_each_entry_safe(ot, tmp, &targets, node) {
                if (!ot->session || !ot->streaming)
                        continue;
-               PARA_INFO_LOG("sending eof to ortp target %s:%d, ts = %d\n", TARGET_ADDR(ot), ot->port,
-                       ot->last_ts);
-               rtp_session_send_with_ts(ot->session, buf, 1, ot->last_ts);
+               PARA_INFO_LOG("sending eof to ortp target %s:%d, ts = %d\n",
+                       TARGET_ADDR(ot), ot->port, ot->last_ts);
+               rtp_session_send_with_ts(ot->session, buf,
+                       ORTP_AUDIO_HEADER_LEN, ot->last_ts);
                ot->streaming = 0;
                ot->chunk_ts = 0;
                rtp_session_reset(ot->session);
        }
 }
 
-static int need_extra_header(struct audio_format *af, long unsigned chunks_sent)
+static int need_extra_header(long unsigned current_chunk)
 {
-       /* FIXME: No need to compute this on every run */
-       int mod = conf.ortp_header_interval_arg / (tv2ms(&af->chunk_tv) + 1);
-       if (mod && (chunks_sent % mod))
+       static struct timeval last_header;
+       struct timeval now, diff;
+
+       if (!current_chunk)
+               return 0;
+       gettimeofday(&now, NULL);
+       tv_diff(&now, &last_header, &diff);
+       if (tv2ms(&diff) < conf.ortp_header_interval_arg)
                return 0;
+       last_header = now;
        return 1;
 }
 
-static void ortp_send(struct audio_format *af, long unsigned current_chunk,
-               long unsigned chunks_sent, const char *buf, size_t len)
+static void ortp_send(long unsigned current_chunk, long unsigned chunks_sent,
+               const char *buf, size_t len)
 {
        struct ortp_target *ot, *tmp;
        size_t sendbuf_len;
-       int header_len = 0;
-       int packet_type = ORTP_DATA, stream_type = af && af->get_header_info; /* header stream? */
+       size_t header_len = 0;
+       int packet_type = ORTP_DATA;
        char *sendbuf, *header_buf = NULL;
+       struct timeval *chunk_tv;
 
        if (self->status != SENDER_ON)
                return;
+
+//     PARA_NOTICE_LOG("sending %lu\n", current_chunk);
+       chunk_tv = vss_chunk_time();
+       if (!chunk_tv)
+               return;
        list_for_each_entry_safe(ot, tmp, &targets, node) {
                if (!ot->session) {
                        ortp_init_session(ot);
@@ -164,18 +182,16 @@ static void ortp_send(struct audio_format *af, long unsigned current_chunk,
                }
                if (!ot->chunk_ts)
                        ot->chunk_ts = rtp_session_time_to_ts(ot->session,
-                               tv2ms(&af->chunk_tv));
+                               (int)tv2ms(chunk_tv));
 //             PARA_DEBUG_LOG("len: %d, ts: %lu, ts: %d\n",
 //                     len, ot->chunk_ts * chunks_sent, ot->chunk_ts);
                ot->streaming = 1;
        }
        if (list_empty(&targets))
                return;
-       if (stream_type) {
-               header_buf = af->get_header_info(&header_len);
-               if (!need_extra_header(af, chunks_sent))
-                       header_len = 0;
-       }
+       header_buf = vss_get_header(&header_len);
+       if (!need_extra_header(current_chunk))
+               header_len = 0;
        sendbuf_len = ORTP_AUDIO_HEADER_LEN + header_len + len;
        sendbuf = para_malloc(sendbuf_len);
        if (!current_chunk)
@@ -183,8 +199,8 @@ static void ortp_send(struct audio_format *af, long unsigned current_chunk,
        else if (header_len)
                packet_type = ORTP_HEADER;
        WRITE_PACKET_TYPE(sendbuf, packet_type);
-       WRITE_CHUNK_TIME(sendbuf, af->chunk_tv.tv_usec);
-       WRITE_STREAM_TYPE(sendbuf, stream_type);
+       WRITE_CHUNK_TIME(sendbuf, chunk_tv->tv_usec);
+       WRITE_STREAM_TYPE(sendbuf,  header_buf? 1 : 0);
        WRITE_HEADER_LEN(sendbuf, header_len);
        if (header_len)
                memcpy(sendbuf + ORTP_AUDIO_HEADER_LEN, header_buf,
@@ -194,14 +210,14 @@ static void ortp_send(struct audio_format *af, long unsigned current_chunk,
        free(sendbuf);
 }
 
-static int ortp_com_on(struct sender_command_data *scd)
+static int ortp_com_on(__a_unused struct sender_command_data *scd)
 {
 
        self->status = SENDER_ON;
        return 1;
 }
 
-static int ortp_com_off(struct sender_command_data *scd)
+static int ortp_com_off(__a_unused struct sender_command_data *scd)
 {
        ortp_shutdown_targets();
        self->status = SENDER_OFF;
@@ -229,7 +245,7 @@ static void ortp_add_target(int port, struct in_addr *addr)
        ot->addr = *addr;
        PARA_INFO_LOG("adding to target list (%s:%d)\n",
                TARGET_ADDR(ot), ot->port);
-       list_add(&ot->node, &targets);
+       para_list_add(&ot->node, &targets);
 }
 
 static int ortp_com_add(struct sender_command_data *scd)
@@ -276,7 +292,7 @@ static void ortp_init_target_list(void)
                if (!p)
                        goto err;
                *p = '\0';
-               if (!inet_aton(arg, &addr))
+               if (!inet_pton(AF_INET, arg, &addr))
                        goto err;
                port = atoi(++p);
                if (port < 0 || port > 65535)
@@ -292,8 +308,8 @@ success:
        }
 }
 
-static void ortp_pre_select(struct audio_format *af, int *max_fileno,
-               fd_set *rfds, fd_set *wfds)
+static void ortp_pre_select(__a_unused  int *max_fileno, __a_unused fd_set *rfds,
+       __a_unused fd_set *wfds)
 {
        return;
 }