2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file ortp_send.c para_server's ortp sender */
21 #include <ortp/ortp.h>
23 #include "server.cmdline.h"
31 /** \cond convert in_addr to ascii */
32 #define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
35 extern struct gengetopt_args_info conf
;
37 /** describes one entry in the list of targets for the ortp sender */
41 /** whether the ortp sender is activated */
43 /** the ortp timestamp increases by this amount */
45 /** the currently used timestamp for this target */
47 /** the position of this target in the list of targets */
48 struct list_head node
;
51 /** non-zero if at least one chunk has been sent to this target */
53 /** the session pointer from libortp */
57 static struct list_head targets
;
58 static struct sender
*self
;
60 static void ortp_delete_target(struct ortp_target
*ot
, const char *msg
)
62 PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ot
),
65 rtp_session_destroy(ot
->session
);
72 static void ortp_send_buf(char *buf
, int len
, long unsigned chunks_sent
)
74 struct ortp_target
*ot
, *tmp
;
77 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
81 WRITE_CHUNK_TS(buf
, ot
->chunk_ts
);
82 ts
= ot
->chunk_ts
* chunks_sent
;
83 ret
= rtp_session_send_with_ts(ot
->session
, buf
, len
, ts
);
86 ortp_delete_target(ot
, "send error");
88 PARA_NOTICE_LOG("short write %d\n", ret
);
92 static void ortp_init_session(struct ortp_target
*ot
)
97 PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ot
), ot
->port
);
98 ot
->session
= rtp_session_new(RTP_SESSION_SENDONLY
);
102 if (conf
.ortp_jitter_compensation_arg
) {
103 rtp_session_enable_adaptive_jitter_compensation(ot
->session
, TRUE
);
104 rtp_session_set_jitter_compensation(ot
->session
,
105 conf
.ortp_jitter_compensation_arg
);
107 /* always successful */
108 rtp_session_set_send_payload_type(s
, PAYLOAD_AUDIO_CONTINUOUS
);
109 ret
= rtp_session_set_remote_addr(s
, TARGET_ADDR(ot
), ot
->port
);
111 rtp_session_destroy(ot
->session
);
117 static void ortp_shutdown_targets(void)
119 char buf
[ORTP_AUDIO_HEADER_LEN
];
120 struct ortp_target
*ot
, *tmp
;
122 WRITE_PACKET_TYPE(buf
, ORTP_EOF
);
123 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
124 if (!ot
->session
|| !ot
->streaming
)
126 PARA_INFO_LOG("sending eof to ortp target %s:%d, ts = %d\n",
127 TARGET_ADDR(ot
), ot
->port
, ot
->last_ts
);
128 rtp_session_send_with_ts(ot
->session
, buf
,
129 ORTP_AUDIO_HEADER_LEN
, ot
->last_ts
);
132 rtp_session_reset(ot
->session
);
136 static int need_extra_header(struct audio_format_handler
*af
,
137 long unsigned chunks_sent
)
139 /* FIXME: No need to compute this on every run */
140 int mod
= conf
.ortp_header_interval_arg
/ (tv2ms(&af
->chunk_tv
) + 1);
141 if (mod
&& (chunks_sent
% mod
))
146 static void ortp_send(struct audio_format_handler
*af
, long unsigned current_chunk
,
147 long unsigned chunks_sent
, const char *buf
, size_t len
)
149 struct ortp_target
*ot
, *tmp
;
152 int packet_type
= ORTP_DATA
, stream_type
= af
&& af
->get_header_info
; /* header stream? */
153 char *sendbuf
, *header_buf
= NULL
;
155 if (self
->status
!= SENDER_ON
)
157 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
159 ortp_init_session(ot
);
164 ot
->chunk_ts
= rtp_session_time_to_ts(ot
->session
,
165 tv2ms(&af
->chunk_tv
));
166 // PARA_DEBUG_LOG("len: %d, ts: %lu, ts: %d\n",
167 // len, ot->chunk_ts * chunks_sent, ot->chunk_ts);
170 if (list_empty(&targets
))
173 header_buf
= af
->get_header_info(&header_len
);
174 if (!need_extra_header(af
, chunks_sent
))
177 sendbuf_len
= ORTP_AUDIO_HEADER_LEN
+ header_len
+ len
;
178 sendbuf
= para_malloc(sendbuf_len
);
180 packet_type
= ORTP_BOF
;
182 packet_type
= ORTP_HEADER
;
183 WRITE_PACKET_TYPE(sendbuf
, packet_type
);
184 WRITE_CHUNK_TIME(sendbuf
, af
->chunk_tv
.tv_usec
);
185 WRITE_STREAM_TYPE(sendbuf
, stream_type
);
186 WRITE_HEADER_LEN(sendbuf
, header_len
);
188 memcpy(sendbuf
+ ORTP_AUDIO_HEADER_LEN
, header_buf
,
190 memcpy(sendbuf
+ ORTP_AUDIO_HEADER_LEN
+ header_len
, buf
, len
);
191 ortp_send_buf(sendbuf
, sendbuf_len
, chunks_sent
);
195 static int ortp_com_on(__a_unused
struct sender_command_data
*scd
)
198 self
->status
= SENDER_ON
;
202 static int ortp_com_off(__a_unused
struct sender_command_data
*scd
)
204 ortp_shutdown_targets();
205 self
->status
= SENDER_OFF
;
209 static int ortp_com_delete(struct sender_command_data
*scd
)
211 char *a
= para_strdup(inet_ntoa(scd
->addr
));
212 struct ortp_target
*ot
, *tmp
;
213 list_for_each_entry_safe(ot
, tmp
, &targets
, node
) {
214 if (scd
->port
!= ot
->port
)
216 if (strcmp(TARGET_ADDR(ot
), a
))
218 ortp_delete_target(ot
, "com_delete");
223 static void ortp_add_target(int port
, struct in_addr
*addr
)
225 struct ortp_target
*ot
= para_calloc(sizeof(struct ortp_target
));
228 PARA_INFO_LOG("adding to target list (%s:%d)\n",
229 TARGET_ADDR(ot
), ot
->port
);
230 list_add(&ot
->node
, &targets
);
233 static int ortp_com_add(struct sender_command_data
*scd
)
235 int port
= (scd
->port
> 0)? scd
->port
: conf
.ortp_default_port_arg
;
236 ortp_add_target(port
, &scd
->addr
);
240 static char *ortp_info(void)
242 struct ortp_target
*ot
;
243 char *ret
, *tgts
= NULL
;
245 list_for_each_entry(ot
, &targets
, node
) {
246 char *tmp
= make_message("%s%s:%d ", tgts
? tgts
: "",
247 TARGET_ADDR(ot
), ot
->port
);
253 "ortp default port: udp %d\n"
254 "ortp targets: %s\n",
255 (self
->status
== SENDER_ON
)? "on" : "off",
256 conf
.ortp_default_port_arg
,
257 tgts
? tgts
: "(none)"
263 static void ortp_init_target_list(void)
267 INIT_LIST_HEAD(&targets
);
268 for (i
= 0; i
< conf
.ortp_target_given
; i
++) {
269 char *arg
= para_strdup(conf
.ortp_target_arg
[i
]);
270 char *p
= strchr(arg
, ':');
277 if (!inet_aton(arg
, &addr
))
280 if (port
< 0 || port
> 65535)
281 port
= conf
.ortp_default_port_arg
;
282 ortp_add_target(port
, &addr
);
285 PARA_CRIT_LOG("syntax error for ortp_target option "
286 "#%d, ignoring\n", i
);
293 static void ortp_pre_select(__a_unused
struct audio_format_handler
*af
,
294 __a_unused
int *max_fileno
, __a_unused fd_set
*rfds
,
295 __a_unused fd_set
*wfds
)
300 static char *ortp_help(void)
304 "usage: {add|delete} IP port\n"
305 "example: add 224.0.1.38 1500 (LAN-streaming)\n"
310 * the init function of para_server's ortp sender
312 * \param s pointer to the http sender struct
314 * It initializes all function pointers of \a s and the list of ortp targets.
316 void ortp_send_init(struct sender
*s
)
319 INIT_LIST_HEAD(&targets
);
323 s
->pre_select
= ortp_pre_select
;
324 s
->post_select
= NULL
;
325 s
->shutdown_clients
= ortp_shutdown_targets
;
326 s
->client_cmds
[SENDER_ON
] = ortp_com_on
;
327 s
->client_cmds
[SENDER_OFF
] = ortp_com_off
;
328 s
->client_cmds
[SENDER_DENY
] = NULL
;
329 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
330 s
->client_cmds
[SENDER_ADD
] = ortp_com_add
;
331 s
->client_cmds
[SENDER_DELETE
] = ortp_com_delete
;
333 s
->status
= SENDER_OFF
;
334 ortp_init_target_list();
335 if (!conf
.ortp_no_autostart_given
)
336 s
->status
= SENDER_ON
;
337 PARA_DEBUG_LOG("%s", "ortp sender init complete\n");