-/*
- * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
- *
- * 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 "para.h"
-#include "error.h"
-#include "string.h"
-#include "afh.h"
-#include "afs.h"
-#include "server.h"
-#include "vss.h"
-#include "list.h"
-#include "send.h"
-#include "ortp.h"
-
-/** Convert in_addr to ascii. */
-#define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
-
-/** Describes one entry in the list of targets for the ortp sender. */
-struct ortp_target {
- /** Address info. */
- struct in_addr addr;
- /** Whether the ortp sender is activated. */
- int status;
- /** 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. */
- int port;
- /** Non-zero if at least one chunk has been sent to this target. */
- int streaming;
- /** The session pointer from libortp. */
- RtpSession *session;
-};
-
-static struct list_head targets;
-static struct sender *self;
-static int sender_status;
-
-static void ortp_delete_target(struct ortp_target *ot, const char *msg)
-{
- PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ot),
- ot->port, msg);
- if (ot->session) {
- rtp_session_destroy(ot->session);
- ot->session = NULL;
- }
- list_del(&ot->node);
- free(ot);
-}
-
-static void ortp_send_buf(char *buf, size_t len, long unsigned chunks_sent)
-{
- struct ortp_target *ot, *tmp;
- 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) {
- 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,
- (unsigned char*)buf, ortp_len, ts);
- ot->last_ts = ts;
- if (ret < 0)
- ortp_delete_target(ot, "send error");
- 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;
- int ret;
-
- PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ot), ot->port);
- ot->session = rtp_session_new(RTP_SESSION_SENDONLY);
- if (!ot->session)
- return;
- s = ot->session;
- if (conf.ortp_jitter_compensation_arg) {
- rtp_session_enable_adaptive_jitter_compensation(ot->session, TRUE);
- rtp_session_set_jitter_compensation(ot->session,
- conf.ortp_jitter_compensation_arg);
- }
- /* always successful */
- rtp_session_set_send_payload_type(s, PAYLOAD_AUDIO_CONTINUOUS);
- ret = rtp_session_set_remote_addr(s, TARGET_ADDR(ot), ot->port);
- if (ret < 0) {
- rtp_session_destroy(ot->session);
- ot->session = NULL;
- return;
- }
- set_multicast(s);
-}
-
-/* called by vss */
-static void ortp_shutdown_targets(void)
-{
- unsigned char buf[ORTP_AUDIO_HEADER_LEN];
- struct ortp_target *ot, *tmp;
-
- 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,
- ORTP_AUDIO_HEADER_LEN, ot->last_ts);
- ot->streaming = 0;
- ot->chunk_ts = 0;
- rtp_session_reset(ot->session);
- }
-}
-
-static int need_extra_header(long unsigned current_chunk)
-{
- 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(long unsigned current_chunk, long unsigned chunks_sent,
- const char *buf, size_t len, const char *header_buf,
- size_t header_len)
-{
- struct ortp_target *ot, *tmp;
- size_t sendbuf_len;
- int packet_type = ORTP_DATA;
- char *sendbuf;
- struct timeval *chunk_tv;
-
- if (sender_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);
- if (!ot->session)
- continue;
- }
- if (!ot->chunk_ts)
- ot->chunk_ts = rtp_session_time_to_ts(ot->session,
- (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 (!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)
- packet_type = ORTP_BOF;
- else if (header_len)
- packet_type = ORTP_HEADER;
- WRITE_PACKET_TYPE(sendbuf, packet_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,
- header_len);
- memcpy(sendbuf + ORTP_AUDIO_HEADER_LEN + header_len, buf, len);
- ortp_send_buf(sendbuf, sendbuf_len, chunks_sent);
- free(sendbuf);
-}
-
-static int ortp_com_on(__a_unused struct sender_command_data *scd)
-{
-
- sender_status = SENDER_ON;
- return 1;
-}
-
-static int ortp_com_off(__a_unused struct sender_command_data *scd)
-{
- ortp_shutdown_targets();
- sender_status = SENDER_OFF;
- return 1;
-}
-
-static int ortp_com_delete(struct sender_command_data *scd)
-{
- char *a = para_strdup(inet_ntoa(scd->addr));
- struct ortp_target *ot, *tmp;
- list_for_each_entry_safe(ot, tmp, &targets, node) {
- if (scd->port != ot->port)
- continue;
- if (strcmp(TARGET_ADDR(ot), a))
- continue;
- ortp_delete_target(ot, "com_delete");
- }
- return 1;
-}
-
-static void ortp_add_target(int port, struct in_addr *addr)
-{
- struct ortp_target *ot = para_calloc(sizeof(struct ortp_target));
- ot->port = port;
- ot->addr = *addr;
- PARA_INFO_LOG("adding to target list (%s:%d)\n",
- TARGET_ADDR(ot), ot->port);
- para_list_add(&ot->node, &targets);
-}
-
-static int ortp_com_add(struct sender_command_data *scd)
-{
- int port = (scd->port > 0)? scd->port : conf.ortp_default_port_arg;
- ortp_add_target(port, &scd->addr);
- return 1;
-}
-
-static char *ortp_info(void)
-{
- struct ortp_target *ot;
- char *ret, *tgts = NULL;
-
- list_for_each_entry(ot, &targets, node) {
- char *tmp = make_message("%s%s:%d ", tgts? tgts : "",
- TARGET_ADDR(ot), ot->port);
- free(tgts);
- tgts = tmp;
- }
- ret = make_message(
- "ortp sender:\n"
- "\tstatus: %s\n"
- "\tport: udp %d\n"
- "\ttargets: %s\n",
- (sender_status == SENDER_ON)? "on" : "off",
- conf.ortp_default_port_arg,
- tgts? tgts : "(none)"
- );
- free(tgts);
- return ret;
-}
-
-static void ortp_init_target_list(void)
-{
- int i;
-
- INIT_LIST_HEAD(&targets);
- for (i = 0; i < conf.ortp_target_given; i++) {
- char *arg = para_strdup(conf.ortp_target_arg[i]);
- char *p = strchr(arg, ':');
- int port;
- struct in_addr addr;
-
- if (!p)
- goto err;
- *p = '\0';
- if (!inet_pton(AF_INET, arg, &addr))
- goto err;
- port = atoi(++p);
- if (port < 0 || port > 65535)
- port = conf.ortp_default_port_arg;
- ortp_add_target(port, &addr);
- goto success;
-err:
- PARA_CRIT_LOG("syntax error for ortp_target option "
- "#%d, ignoring\n", i);
-success:
- free(arg);
- continue;
- }
-}
-
-static char *ortp_help(void)
-{
- return make_message(
- "usage: {on|off}\n"
- "usage: {add|delete} IP port\n"
- "example: add 224.0.1.38 1500 (LAN-streaming)\n"
- );
-}
-
-/**
- * the init function of para_server's ortp sender
- *
- * \param s pointer to the http sender struct
- *
- * It initializes all function pointers of \a s and the list of ortp targets.
- */
-void ortp_send_init(struct sender *s)
-{
- ortp_init();
- INIT_LIST_HEAD(&targets);
- s->info = ortp_info;
- s->help = ortp_help;
- s->send = ortp_send;
- s->pre_select = NULL;
- s->post_select = NULL;
- s->shutdown_clients = ortp_shutdown_targets;
- s->client_cmds[SENDER_ON] = ortp_com_on;
- s->client_cmds[SENDER_OFF] = ortp_com_off;
- s->client_cmds[SENDER_DENY] = NULL;
- s->client_cmds[SENDER_ALLOW] = NULL;
- s->client_cmds[SENDER_ADD] = ortp_com_add;
- s->client_cmds[SENDER_DELETE] = ortp_com_delete;
- self = s;
- sender_status = SENDER_OFF;
- ortp_init_target_list();
- if (!conf.ortp_no_autostart_given)
- sender_status = SENDER_ON;
- PARA_DEBUG_LOG("ortp sender init complete\n");
-}