2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ortp_send.c para_server's ortp sender */
10 #include <ortp/port.h>
12 #include "server.cmdline.h"
24 /** Convert in_addr to ascii. */
25 #define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
27 /** Describes one entry in the list of targets for the ortp sender. */
31 /** Whether the ortp sender is activated. */
33 /** The ortp timestamp increases by this amount. */
35 /** The currently used timestamp for this target. */
37 /** The position of this target in the list of targets. */
38 struct list_head node
;
41 /** Non-zero if at least one chunk has been sent to this target. */
43 /** The session pointer from libortp. */
47 static struct list_head targets
;
48 static struct sender
*self
;
49 static int sender_status
;
51 static void ortp_delete_target(struct ortp_target
*ot
, const char *msg
)
53 PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ot
),
56 rtp_session_destroy(ot
->session
);
63 static void ortp_send_buf(char *buf
, size_t len
, long unsigned chunks_sent
)
65 struct ortp_target
*ot
, *tmp
;
66 int ret
, ortp_len
= len
; /* rtp_session_send_with_ts expects int */
70 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
74 WRITE_CHUNK_TS(buf
, ot
->chunk_ts
);
75 ts
= ot
->chunk_ts
* chunks_sent
;
76 ret
= rtp_session_send_with_ts(ot
->session
,
77 (unsigned char*)buf
, ortp_len
, ts
);
80 ortp_delete_target(ot
, "send error");
82 PARA_NOTICE_LOG("short write %d\n", ret
);
86 static int set_multicast(RtpSession
*s
)
88 unsigned char loop
= 1;
91 ret
= setsockopt(s
->rtp
.socket
,
92 IPPROTO_IP
, IP_MULTICAST_LOOP
, &loop
, sizeof(loop
));
94 PARA_ERROR_LOG("IP_MULTICAST_LOOP error %d\n", ret
);
100 static void ortp_init_session(struct ortp_target
*ot
)
105 PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ot
), ot
->port
);
106 ot
->session
= rtp_session_new(RTP_SESSION_SENDONLY
);
110 if (conf
.ortp_jitter_compensation_arg
) {
111 rtp_session_enable_adaptive_jitter_compensation(ot
->session
, TRUE
);
112 rtp_session_set_jitter_compensation(ot
->session
,
113 conf
.ortp_jitter_compensation_arg
);
115 /* always successful */
116 rtp_session_set_send_payload_type(s
, PAYLOAD_AUDIO_CONTINUOUS
);
117 ret
= rtp_session_set_remote_addr(s
, TARGET_ADDR(ot
), ot
->port
);
119 rtp_session_destroy(ot
->session
);
127 static void ortp_shutdown_targets(void)
129 unsigned char buf
[ORTP_AUDIO_HEADER_LEN
];
130 struct ortp_target
*ot
, *tmp
;
132 WRITE_PACKET_TYPE(buf
, ORTP_EOF
);
133 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
134 if (!ot
->session
|| !ot
->streaming
)
136 PARA_INFO_LOG("sending eof to ortp target %s:%d, ts = %d\n",
137 TARGET_ADDR(ot
), ot
->port
, ot
->last_ts
);
138 rtp_session_send_with_ts(ot
->session
, buf
,
139 ORTP_AUDIO_HEADER_LEN
, ot
->last_ts
);
142 rtp_session_reset(ot
->session
);
146 static int need_extra_header(long unsigned current_chunk
)
148 static struct timeval last_header
;
149 struct timeval now
, diff
;
153 gettimeofday(&now
, NULL
);
154 tv_diff(&now
, &last_header
, &diff
);
155 if (tv2ms(&diff
) < conf
.ortp_header_interval_arg
)
161 static void ortp_send(long unsigned current_chunk
, long unsigned chunks_sent
,
162 const char *buf
, size_t len
, const char *header_buf
,
165 struct ortp_target
*ot
, *tmp
;
167 int packet_type
= ORTP_DATA
;
169 struct timeval
*chunk_tv
;
171 if (sender_status
!= SENDER_ON
)
174 // PARA_NOTICE_LOG("sending %lu\n", current_chunk);
175 chunk_tv
= vss_chunk_time();
178 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
180 ortp_init_session(ot
);
185 ot
->chunk_ts
= rtp_session_time_to_ts(ot
->session
,
186 (int)tv2ms(chunk_tv
));
187 // PARA_DEBUG_LOG("len: %d, ts: %lu, ts: %d\n",
188 // len, ot->chunk_ts * chunks_sent, ot->chunk_ts);
191 if (list_empty(&targets
))
193 if (!need_extra_header(current_chunk
))
195 sendbuf_len
= ORTP_AUDIO_HEADER_LEN
+ header_len
+ len
;
196 sendbuf
= para_malloc(sendbuf_len
);
198 packet_type
= ORTP_BOF
;
200 packet_type
= ORTP_HEADER
;
201 WRITE_PACKET_TYPE(sendbuf
, packet_type
);
202 WRITE_CHUNK_TIME(sendbuf
, chunk_tv
->tv_usec
);
203 WRITE_STREAM_TYPE(sendbuf
, header_buf
? 1 : 0);
204 WRITE_HEADER_LEN(sendbuf
, header_len
);
206 memcpy(sendbuf
+ ORTP_AUDIO_HEADER_LEN
, header_buf
,
208 memcpy(sendbuf
+ ORTP_AUDIO_HEADER_LEN
+ header_len
, buf
, len
);
209 ortp_send_buf(sendbuf
, sendbuf_len
, chunks_sent
);
213 static int ortp_com_on(__a_unused
struct sender_command_data
*scd
)
216 sender_status
= SENDER_ON
;
220 static int ortp_com_off(__a_unused
struct sender_command_data
*scd
)
222 ortp_shutdown_targets();
223 sender_status
= SENDER_OFF
;
227 static int ortp_com_delete(struct sender_command_data
*scd
)
229 char *a
= para_strdup(inet_ntoa(scd
->addr
));
230 struct ortp_target
*ot
, *tmp
;
231 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
232 if (scd
->port
!= ot
->port
)
234 if (strcmp(TARGET_ADDR(ot
), a
))
236 ortp_delete_target(ot
, "com_delete");
241 static void ortp_add_target(int port
, struct in_addr
*addr
)
243 struct ortp_target
*ot
= para_calloc(sizeof(struct ortp_target
));
246 PARA_INFO_LOG("adding to target list (%s:%d)\n",
247 TARGET_ADDR(ot
), ot
->port
);
248 para_list_add(&ot
->node
, &targets
);
251 static int ortp_com_add(struct sender_command_data
*scd
)
253 int port
= (scd
->port
> 0)? scd
->port
: conf
.ortp_default_port_arg
;
254 ortp_add_target(port
, &scd
->addr
);
258 static char *ortp_info(void)
260 struct ortp_target
*ot
;
261 char *ret
, *tgts
= NULL
;
263 list_for_each_entry(ot
, &targets
, node
) {
264 char *tmp
= make_message("%s%s:%d ", tgts
? tgts
: "",
265 TARGET_ADDR(ot
), ot
->port
);
274 (sender_status
== SENDER_ON
)? "on" : "off",
275 conf
.ortp_default_port_arg
,
276 tgts
? tgts
: "(none)"
282 static void ortp_init_target_list(void)
286 INIT_LIST_HEAD(&targets
);
287 for (i
= 0; i
< conf
.ortp_target_given
; i
++) {
288 char *arg
= para_strdup(conf
.ortp_target_arg
[i
]);
289 char *p
= strchr(arg
, ':');
296 if (!inet_pton(AF_INET
, arg
, &addr
))
299 if (port
< 0 || port
> 65535)
300 port
= conf
.ortp_default_port_arg
;
301 ortp_add_target(port
, &addr
);
304 PARA_CRIT_LOG("syntax error for ortp_target option "
305 "#%d, ignoring\n", i
);
312 static char *ortp_help(void)
316 "usage: {add|delete} IP port\n"
317 "example: add 224.0.1.38 1500 (LAN-streaming)\n"
322 * the init function of para_server's ortp sender
324 * \param s pointer to the http sender struct
326 * It initializes all function pointers of \a s and the list of ortp targets.
328 void ortp_send_init(struct sender
*s
)
331 INIT_LIST_HEAD(&targets
);
335 s
->pre_select
= NULL
;
336 s
->post_select
= NULL
;
337 s
->shutdown_clients
= ortp_shutdown_targets
;
338 s
->client_cmds
[SENDER_ON
] = ortp_com_on
;
339 s
->client_cmds
[SENDER_OFF
] = ortp_com_off
;
340 s
->client_cmds
[SENDER_DENY
] = NULL
;
341 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
342 s
->client_cmds
[SENDER_ADD
] = ortp_com_add
;
343 s
->client_cmds
[SENDER_DELETE
] = ortp_com_delete
;
345 sender_status
= SENDER_OFF
;
346 ortp_init_target_list();
347 if (!conf
.ortp_no_autostart_given
)
348 sender_status
= SENDER_ON
;
349 PARA_DEBUG_LOG("ortp sender init complete\n");