2 * Copyright (C) 2005-2006 Andre Noll <noll@mathematik.tu-darmstadt.de>
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 audiod.c the paraslash's audio daemon */
23 #include "audiod.cmdline.h"
25 #include "close_on_fork.h"
29 #include "grab_client.cmdline.h"
30 #include "grab_client.h"
39 #include "write_common.h"
41 /** define the array of error lists needed by para_audiod */
43 /** define the array containing all supported audio formats */
44 DEFINE_AUDIO_FORMAT_ARRAY
;
47 * the possible modes of operation
49 * - off: disconnect from para_server
50 * - on: receive status information from para_server and play the audio stream
51 * - sb: only receive status information but not the audio stream
53 enum {AUDIOD_OFF
, AUDIOD_ON
, AUDIOD_STANDBY
};
55 /** defines how to handle one supported audio format */
56 struct audio_format_info
{
57 /** pointer to the receiver for this audio format */
58 struct receiver
*receiver
;
59 /** the receiver configuration */
61 /** the number of filters that should be activated for this audio format */
62 unsigned int num_filters
;
63 /** pointer to the array of filters to be activated */
64 struct filter
**filters
;
65 /** pointer to the array of filter configurations */
67 /** the number of filters that should be activated for this audio format */
68 unsigned int num_writers
;
69 /** pointer to the array of writers to be activated */
70 struct writer
**writers
;
71 /** pointer to the array of writer configurations */
73 /** do not start receiver/filters/writer before this time */
74 struct timeval restart_barrier
;
78 * describes one instance of a receiver-filter-writer chain
80 * \sa receier_node, receiver, filter, filter_node, filter_chain, writer,
81 * writer_node, writer_node_group.
84 /** number of the audio format in this slot */
86 /** writer start time */
87 struct timeval wstime
;
88 /** the receiver info associated with this slot */
89 struct receiver_node
*receiver_node
;
90 /** the active filter chain */
91 struct filter_chain
*fc
;
92 /** the active writer node group */
93 struct writer_node_group
*wng
;
95 static struct slot_info slot
[MAX_STREAM_SLOTS
];
97 extern const char *status_item_list
[NUM_STAT_ITEMS
];
99 static struct gengetopt_args_info conf
;
100 static struct timeval server_stream_start
, sa_time_diff
;
101 static int playing
, current_decoder
= -1,
102 audiod_status
= AUDIOD_ON
, offset_seconds
, length_seconds
,
103 sa_time_diff_sign
= 1;
104 static char *af_status
, /* the audio format announced in server status */
105 *socket_name
, *hostname
;
106 static char *stat_item_values
[NUM_STAT_ITEMS
];
107 static FILE *logfile
;
108 static const struct timeval restart_delay
= {0, 200 * 1000};
109 static struct audio_format_info afi
[NUM_AUDIO_FORMATS
];
110 static struct timeval
*now
;
112 static struct signal_task signal_task_struct
, *sig_task
= &signal_task_struct
;
115 * the task for handling audiod commands
117 * \sa struct task, struct sched
119 struct command_task
{
120 /** the local listening socket */
122 /** the associated task structure */
127 * the task for audiod's child (para_client stat)
129 * \sa struct task, struct sched
132 /** the output of the stat command is read from this fd */
134 /** stat data is stored here */
135 char buf
[STRINGSIZE
];
136 /** number of bytes loaded in \a buf */
138 /** the associated task structure */
140 /** do not restart client command until this time */
141 struct timeval restart_barrier
;
143 static struct status_task status_task_struct
, *stat_task
= &status_task_struct
;
151 /** defines one command of para_audiod */
152 struct audiod_command
{
153 /** the name of the command */
155 /** pointer to the function that handles the command */
156 int (*handler
)(int, int, char**);
158 * if the command prefers to handle the full line (rather than the usual
159 * argv[] array), it stores a pointer to the corresponding line handling
160 * function here. In this case, the above \a handler pointer must be NULL.
162 int (*line_handler
)(int, char*);
163 /** one-line description of the command */
164 const char *description
;
165 /** summary of the command line options */
166 const char *synopsis
;
167 /** the long help text */
170 static int com_grab(int, char *);
171 static int com_cycle(int, int, char **);
172 static int com_help(int, int, char **);
173 static int com_kill(int, int, char **);
174 static int com_off(int, int, char **);
175 static int com_on(int, int, char **);
176 static int com_sb(int, int, char **);
177 static int com_stat(int, int, char **);
178 static int com_tasks(int, int, char **);
179 static int com_term(int, int, char **);
180 static struct audiod_command cmds
[] = {
183 .handler
= com_cycle
,
184 .description
= "switch to next mode",
188 "on -> standby -> off -> on\n"
193 .line_handler
= com_grab
,
194 .description
= "grab the audio stream",
195 .synopsis
= "-- grab [grab_options]",
198 "grab ('splice') the audio stream at any position in the filter \n"
199 "chain and send that data back to the client. Try\n"
200 "\t para_audioc -- grab -h\n"
201 "for the list of available options.\n"
207 .description
= "display command list or help for given command",
208 .synopsis
= "help [command]",
211 "When I was younger, so much younger than today, I never needed\n"
212 "anybody's help in any way. But now these days are gone, I'm not so\n"
213 "self assured. Now I find I've changed my mind and opened up the doors.\n"
215 " -- Beatles: Help\n"
221 .description
= "kill an active audiod task",
222 .synopsis
= "kill task_id [task_id ...]",
225 "call sched_unregister() and the event_handler of the given task(s)\n"
231 .description
= "deactivate para_audiod",
235 "Close connection to para_server and stop all decoders.\n"
241 .description
= "activate para_audiod",
245 "Establish connection to para_server, retrieve para_server's current\n"
246 "status. If playing, start corresponding decoder. Otherwise stop\n"
253 .description
= "enter standby mode",
257 "Stop all decoders but leave connection to para_server open.\n"
263 .description
= "print status information",
264 .synopsis
= "stat [item1 ...]",
267 "Dump given status items (all if none given) to stdout.\n"
272 .handler
= com_tasks
,
273 .description
= "list current tasks",
277 "print the list of task ids together with the status of each task\n"
283 .description
= "terminate audiod",
287 "Stop all decoders, shut down connection to para_server and exit.\n"
295 /** iterate over all slots */
296 #define FOR_EACH_SLOT(_slot) for (_slot = 0; _slot < MAX_STREAM_SLOTS; _slot++)
297 /** iterate over all supported audio formats */
298 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
299 /** iterate over the array of all audiod commands */
300 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
303 * get the audio format number
304 * \param name the name of the audio format
306 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
307 * \a name is not a supported audio format.
309 int get_audio_format_num(char *name
)
312 FOR_EACH_AUDIO_FORMAT(i
)
313 if (!strcmp(name
, audio_formats
[i
]))
315 return -E_UNSUPPORTED_AUDIO_FORMAT
;
319 * log function. first argument is loglevel.
321 void para_log(int ll
, const char* fmt
,...)
327 char str
[MAXLINE
] = "";
329 if (ll
< conf
.loglevel_arg
)
331 outfd
= logfile
? logfile
: stderr
;
334 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
335 fprintf(outfd
, "%s %s ", str
, hostname
);
336 if (conf
.loglevel_arg
<= INFO
)
337 fprintf(outfd
, "%i ", ll
);
339 vfprintf(outfd
, fmt
, argp
);
343 static int client_write(int fd
, const char *buf
)
345 size_t len
= strlen(buf
);
346 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
349 static char *get_time_string(struct timeval
*newest_stime
)
351 struct timeval diff
, adj_stream_start
, tmp
;
352 int total
= 0, use_server_time
= 1;
357 return make_message("%s:\n", status_item_list
[SI_PLAY_TIME
]);
359 if (audiod_status
== AUDIOD_OFF
)
361 if (sa_time_diff_sign
> 0)
362 tv_diff(&server_stream_start
, &sa_time_diff
,
365 tv_add(&server_stream_start
, &sa_time_diff
,
367 tmp
= adj_stream_start
;
368 if (newest_stime
&& audiod_status
== AUDIOD_ON
) {
369 tv_diff(newest_stime
, &adj_stream_start
, &diff
);
370 if (tv2ms(&diff
) < 5000) {
375 tv_diff(now
, &tmp
, &diff
);
376 total
= diff
.tv_sec
+ offset_seconds
;
377 if (total
> length_seconds
)
378 total
= length_seconds
;
383 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
384 status_item_list
[SI_PLAY_TIME
],
385 use_server_time
? "~" : "",
388 (length_seconds
- total
) / 60,
389 (length_seconds
- total
) % 60,
390 length_seconds
? (total
* 100 + length_seconds
/ 2) /
397 __malloc
static char *audiod_status_string(void)
399 const char *status
= (audiod_status
== AUDIOD_ON
)?
400 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
401 return make_message("%s:%s\n", status_item_list
[SI_AUDIOD_STATUS
], status
);
404 static struct timeval
*wstime(void)
407 struct timeval
*max
= NULL
;
409 struct slot_info
*s
= &slot
[i
];
412 if (max
&& tv_diff(&s
->wstime
, max
, NULL
) <= 0)
418 __malloc
static char *decoder_flags(void)
421 char decoder_flags
[MAX_STREAM_SLOTS
+ 1];
424 struct slot_info
*s
= &slot
[i
];
426 if (s
->receiver_node
)
430 decoder_flags
[i
] = flag
;
432 decoder_flags
[MAX_STREAM_SLOTS
] = '\0';
433 return make_message("%s:%s\n", status_item_list
[SI_DECODER_FLAGS
],
437 static char *configfile_exists(void)
439 static char *config_file
;
442 char *home
= para_homedir();
443 config_file
= make_message("%s/.paraslash/audiod.conf", home
);
446 return file_exists(config_file
)? config_file
: NULL
;
449 static void setup_signal_handling(void)
451 sig_task
->fd
= para_signal_init();
452 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task
->fd
);
453 para_install_sighandler(SIGINT
);
454 para_install_sighandler(SIGTERM
);
455 para_install_sighandler(SIGCHLD
);
456 para_install_sighandler(SIGHUP
);
457 para_install_sighandler(SIGPIPE
);
458 // signal(SIGPIPE, SIG_IGN);
461 static void audiod_status_dump(void)
463 static char *p_ts
, *p_us
, *p_as
, *p_df
;
464 struct timeval
*t
= wstime();
465 char *us
, *tmp
= get_time_string(t
);
467 if (tmp
&& (!p_ts
|| strcmp(tmp
, p_ts
)))
468 stat_client_write(tmp
, SI_PLAY_TIME
);
473 tmp
= make_message("%s:%s\n", status_item_list
[SI_AUDIOD_UPTIME
], us
);
475 if (!p_us
|| strcmp(p_us
, tmp
))
476 stat_client_write(tmp
, SI_AUDIOD_UPTIME
);
480 tmp
= audiod_status_string();
481 if (!p_as
|| strcmp(p_as
, tmp
))
482 stat_client_write(tmp
, SI_AUDIOD_STATUS
);
486 tmp
= decoder_flags();
487 if (!p_df
|| strcmp(p_df
, tmp
))
488 stat_client_write(tmp
, SI_DECODER_FLAGS
);
493 static void clear_slot(int slot_num
)
495 struct slot_info
*s
= &slot
[slot_num
];
497 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
498 memset(s
, 0, sizeof(struct slot_info
));
502 static void close_receiver(int slot_num
)
504 struct slot_info
*s
= &slot
[slot_num
];
505 struct audio_format_info
*a
;
507 if (s
->format
< 0 || !s
->receiver_node
)
510 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
511 audio_formats
[s
->format
] , slot_num
, s
->receiver_node
->eof
);
512 // if (!s->receiver_node->eof)
513 // unregister_task(&s->receiver_node->task);
514 a
->receiver
->close(s
->receiver_node
);
515 free(s
->receiver_node
);
516 s
->receiver_node
= NULL
;
517 /* set restart barrier */
518 tv_add(now
, &restart_delay
, &afi
[s
->format
].restart_barrier
);
521 static void kill_all_decoders(void)
526 struct slot_info
*s
= &slot
[i
];
527 if (s
->wng
&& !s
->wng
->eof
) {
528 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
530 wng_unregister(s
->wng
);
533 if (s
->fc
&& !s
->fc
->eof
) {
534 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i
);
535 unregister_task(&s
->fc
->task
);
538 if (s
->receiver_node
&& !s
->receiver_node
->eof
) {
539 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i
);
540 unregister_task(&s
->receiver_node
->task
);
541 s
->receiver_node
->eof
= 1;
546 static int get_empty_slot(void)
557 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
562 return -E_NO_MORE_SLOTS
;
565 static int decoder_running(int format
)
572 if (s
->format
== format
&& s
->receiver_node
)
574 if (s
->format
== format
&& s
->wng
)
580 static void close_stat_pipe(void)
584 if (stat_task
->fd
< 0)
586 PARA_NOTICE_LOG("%s", "closing status pipe\n");
587 close(stat_task
->fd
);
588 del_close_on_fork_list(stat_task
->fd
);
590 // kill_all_decoders();
591 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
592 free(stat_item_values
[i
]);
593 stat_item_values
[i
] = NULL
;
598 audiod_status_dump();
600 stat_item_values
[SI_STATUS_BAR
] = make_message(
601 "%s:no connection to para_server\n",
602 status_item_list
[SI_STATUS_BAR
]);
603 stat_client_write(stat_item_values
[SI_STATUS_BAR
], SI_STATUS_BAR
);
606 static void __noreturn
clean_exit(int status
, const char *msg
)
608 PARA_EMERG_LOG("%s\n", msg
);
611 if (stat_task
->fd
>= 0)
617 * get the number of filters
619 * \param audio_format_num the number identifying the audio format
621 * \return the number of filters for the given audio format
625 int num_filters(int audio_format_num
)
627 return afi
[audio_format_num
].num_filters
;
630 static void filter_event_handler(struct task
*t
)
632 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
633 struct filter_chain
*fc
= t
->private_data
;
638 static void open_filters(int slot_num
)
640 struct slot_info
*s
= &slot
[slot_num
];
641 struct audio_format_info
*a
= &afi
[s
->format
];
642 int nf
= a
->num_filters
;
648 PARA_INFO_LOG("opening %s filters\n", audio_formats
[s
->format
]);
649 s
->fc
= para_calloc(sizeof(struct filter_chain
));
650 INIT_LIST_HEAD(&s
->fc
->filters
);
651 s
->fc
->inbuf
= s
->receiver_node
->buf
;
652 s
->fc
->in_loaded
= &s
->receiver_node
->loaded
;
653 s
->fc
->input_eof
= &s
->receiver_node
->eof
;
654 s
->fc
->task
.pre_select
= filter_pre_select
;
655 s
->fc
->task
.event_handler
= filter_event_handler
;
656 s
->fc
->task
.private_data
= s
->fc
;
659 s
->receiver_node
->output_eof
= &s
->fc
->eof
;
660 sprintf(s
->fc
->task
.status
, "filter chain");
661 for (i
= 0; i
< nf
; i
++) {
662 struct filter_node
*fn
= para_calloc(sizeof(struct filter_node
));
663 fn
->conf
= a
->filter_conf
[i
];
665 fn
->filter
= a
->filters
[i
];
666 INIT_LIST_HEAD(&fn
->callbacks
);
667 list_add_tail(&fn
->node
, &s
->fc
->filters
);
668 fn
->filter
->open(fn
);
669 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
670 audio_formats
[s
->format
], i
+ 1, nf
,
671 fn
->filter
->name
, slot_num
);
672 s
->fc
->outbuf
= fn
->buf
;
673 s
->fc
->out_loaded
= &fn
->loaded
;
675 register_task(&s
->fc
->task
);
678 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
680 struct filter_node
*fn
;
684 struct slot_info
*s
= &slot
[i
];
685 if (s
->format
< 0 || !s
->fc
)
687 if (slot_num
>= 0 && slot_num
!= i
)
689 if (format
>= 0 && s
->format
!= format
)
691 if (num_filters(i
) < filternum
)
695 list_for_each_entry(fn
, &s
->fc
->filters
, node
)
696 if (filternum
<= 0 || j
++ == filternum
)
703 static void wng_event_handler(struct task
*t
)
705 struct writer_node_group
*wng
= t
->private_data
;
707 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
712 static void open_writers(int slot_num
)
715 struct slot_info
*s
= &slot
[slot_num
];
716 struct audio_format_info
*a
= &afi
[s
->format
];
718 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
720 s
->wng
= setup_default_wng();
722 s
->wng
= wng_new(a
->num_writers
);
724 s
->wng
->buf
= s
->fc
->outbuf
;
725 s
->wng
->loaded
= s
->fc
->out_loaded
;
726 s
->wng
->input_eof
= &s
->fc
->eof
;
727 s
->wng
->channels
= &s
->fc
->channels
;
728 s
->wng
->samplerate
= &s
->fc
->samplerate
;
729 s
->fc
->output_eof
= &s
->wng
->eof
;
730 PARA_INFO_LOG("samplerate: %d\n", *s
->wng
->samplerate
);
732 s
->wng
->buf
= s
->receiver_node
->buf
;
733 s
->wng
->loaded
= &s
->receiver_node
->loaded
;
734 s
->wng
->input_eof
= &s
->receiver_node
->eof
;
736 s
->wng
->task
.event_handler
= wng_event_handler
;
737 for (i
= 0; i
< a
->num_writers
; i
++) {
738 s
->wng
->writer_nodes
[i
].conf
= a
->writer_conf
[i
];
739 s
->wng
->writer_nodes
[i
].writer
= a
->writers
[i
];
740 sprintf(s
->wng
->writer_nodes
[i
].task
.status
, "writer_node");
742 ret
= wng_open(s
->wng
);
744 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
748 current_decoder
= slot_num
;
749 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fc
->filters
);
752 static void rn_event_handler(struct task
*t
)
754 struct receiver_node
*rn
= t
->private_data
;
756 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
761 static void open_receiver(int format
)
763 struct audio_format_info
*a
= &afi
[format
];
766 struct receiver_node
*rn
;
768 slot_num
= get_empty_slot();
770 clean_exit(EXIT_FAILURE
, PARA_STRERROR(-slot_num
));
773 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
774 rn
= s
->receiver_node
;
775 rn
->receiver
= a
->receiver
;
776 rn
->conf
= a
->receiver_conf
;
777 ret
= a
->receiver
->open(s
->receiver_node
);
779 PARA_ERROR_LOG("failed to open receiver (%s)\n",
780 PARA_STRERROR(-ret
));
781 free(s
->receiver_node
);
782 s
->receiver_node
= NULL
;
785 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
786 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
787 rn
->task
.private_data
= s
->receiver_node
;
788 rn
->task
.pre_select
= a
->receiver
->pre_select
;
789 rn
->task
.post_select
= a
->receiver
->post_select
;
790 rn
->task
.event_handler
= rn_event_handler
;
791 sprintf(rn
->task
.status
, "%s receiver node", rn
->receiver
->name
);
792 register_task(&rn
->task
);
795 static int is_frozen(int format
)
797 struct audio_format_info
*a
= &afi
[format
];
799 return (tv_diff(now
, &a
->restart_barrier
, NULL
) > 0)? 0 : 1;
802 static void open_current_receiver(struct sched
*s
)
808 i
= get_audio_format_num(af_status
);
811 if (decoder_running(i
) || is_frozen(i
))
815 * the receiver didn't get a chance to put his fd to the fd set.
816 * Make the upcoming select() return immediately to avoid a long
819 s
->timeout
.tv_sec
= 0;
820 s
->timeout
.tv_usec
= 1;
823 static void compute_time_diff(const struct timeval
*status_time
)
825 struct timeval tmp
, diff
;
828 const struct timeval max_deviation
= {0, 500 * 1000};
829 const int time_smooth
= 5;
831 sign
= tv_diff(status_time
, now
, &diff
);
832 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
833 // sign, sa_time_diff_sign);
835 sa_time_diff_sign
= sign
;
841 int s
= tv_diff(&diff
, &sa_time_diff
, &tmp
);
842 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
843 PARA_WARNING_LOG("time diff jump: %lims\n",
847 sa_time_diff_sign
= tv_convex_combination(
848 sa_time_diff_sign
* time_smooth
, &sa_time_diff
,
849 count
> 10? sign
: sign
* time_smooth
, &diff
,
852 PARA_DEBUG_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
855 sa_time_diff_sign
? "+" : "-",
860 static void check_stat_line(char *line
)
864 long unsigned sec
, usec
;
867 // PARA_INFO_LOG("line: %s\n", line);
870 itemnum
= stat_line_valid(line
);
872 PARA_WARNING_LOG("invalid status line: %s\n", line
);
875 tmp
= make_message("%s\n", line
);
876 stat_client_write(tmp
, itemnum
);
878 free(stat_item_values
[itemnum
]);
879 stat_item_values
[itemnum
] = para_strdup(line
);
880 ilen
= strlen(status_item_list
[itemnum
]);
883 playing
= strstr(line
, "playing")? 1 : 0;
887 af_status
= para_strdup(line
+ ilen
+ 1);
890 offset_seconds
= atoi(line
+ ilen
+ 1);
893 length_seconds
= atoi(line
+ ilen
+ 1);
895 case SI_STREAM_START
:
896 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
897 server_stream_start
.tv_sec
= sec
;
898 server_stream_start
.tv_usec
= usec
;
901 case SI_CURRENT_TIME
:
902 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
903 struct timeval tv
= {sec
, usec
};
904 compute_time_diff(&tv
);
910 static void handle_signal(int sig
)
915 pid_t pid
= para_reap_child();
918 PARA_CRIT_LOG("para_client died (pid %d)\n", pid
);
923 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
924 clean_exit(EXIT_FAILURE
, "caught deadly signal");
929 static void try_to_close_slot(int slot_num
)
931 struct slot_info
*s
= &slot
[slot_num
];
935 if (s
->receiver_node
&& !s
->receiver_node
->eof
)
937 if (s
->fc
&& !s
->fc
->eof
)
939 if (s
->wng
&& !s
->wng
->eof
)
941 PARA_INFO_LOG("closing slot %d \n", slot_num
);
943 close_filters(s
->fc
);
945 close_receiver(slot_num
);
946 clear_slot(slot_num
);
949 static void audiod_pre_select(struct sched
*s
, __a_unused
struct task
*t
)
955 if (audiod_status
!= AUDIOD_ON
|| !playing
)
958 open_current_receiver(s
);
960 struct slot_info
*s
= &slot
[i
];
961 struct audio_format_info
*a
;
966 if (!s
->receiver_node
)
968 if (!a
->num_filters
) {
969 if (s
->receiver_node
->loaded
&& !s
->wng
)
973 if (s
->receiver_node
->loaded
&& !s
->fc
) {
977 if (s
->fc
&& *s
->fc
->out_loaded
&& !s
->wng
)
982 static void audiod_post_select(struct sched
*s
, __a_unused
struct task
*t
)
986 /* save away the current time for other users */
990 try_to_close_slot(i
);
993 static void init_audiod_task(struct task
*t
)
995 t
->pre_select
= audiod_pre_select
;
996 t
->post_select
= audiod_post_select
;
997 t
->event_handler
= NULL
;
999 sprintf(t
->status
, "audiod task");
1002 static int parse_stream_command(const char *txt
, char **cmd
)
1004 char *p
= strchr(txt
, ':');
1008 return -E_MISSING_COLON
;
1010 FOR_EACH_AUDIO_FORMAT(i
) {
1011 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
1016 return -E_UNSUPPORTED_AUDIO_FORMAT
;
1019 static int add_filter(int format
, char *cmdline
)
1021 struct audio_format_info
*a
= &afi
[format
];
1022 int filter_num
, nf
= a
->num_filters
;
1024 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
1027 a
->filters
[nf
] = &filters
[filter_num
];
1029 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
1030 a
->filters
[nf
]->name
);
1034 static int init_writers(void)
1038 struct audio_format_info
*a
;
1040 init_supported_writers();
1041 nw
= PARA_MAX(1, conf
.writer_given
);
1042 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
1043 FOR_EACH_AUDIO_FORMAT(i
) {
1045 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
1046 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
1049 for (i
= 0; i
< conf
.writer_given
; i
++) {
1052 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
1056 nw
= a
->num_writers
;
1057 wconf
= check_writer_arg(cmd
, &writer_num
);
1062 a
->writers
[nw
] = &writers
[writer_num
];
1063 a
->writer_conf
[nw
] = wconf
;
1064 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
1065 nw
, writer_names
[writer_num
]);
1073 static int init_receivers(void)
1075 int i
, ret
, receiver_num
;
1077 struct audio_format_info
*a
;
1079 for (i
= 0; receivers
[i
].name
; i
++) {
1080 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
1081 receivers
[i
].init(&receivers
[i
]);
1083 for (i
= 0; i
< conf
.receiver_given
; i
++) {
1084 char *arg
= conf
.receiver_arg
[i
];
1085 char *recv
= strchr(arg
, ':');
1086 ret
= -E_MISSING_COLON
;
1091 ret
= get_audio_format_num(arg
);
1094 afi
[ret
].receiver_conf
= check_receiver_arg(recv
, &receiver_num
);
1095 if (!afi
[ret
].receiver_conf
) {
1096 ret
= -E_RECV_SYNTAX
;
1099 afi
[ret
].receiver
= &receivers
[receiver_num
];
1101 /* use the first available receiver with no arguments
1102 * for those audio formats for which no receiver
1105 cmd
= para_strdup(receivers
[0].name
);
1106 FOR_EACH_AUDIO_FORMAT(i
) {
1108 if (a
->receiver_conf
)
1110 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
1111 if (!a
->receiver_conf
)
1112 return -E_RECV_SYNTAX
;
1113 a
->receiver
= &receivers
[receiver_num
];
1121 static int init_default_filters(void)
1125 FOR_EACH_AUDIO_FORMAT(i
) {
1126 struct audio_format_info
*a
= &afi
[i
];
1131 continue; /* no default -- nothing to to */
1132 /* add "dec" to audio format name */
1133 tmp
= make_message("%sdec", audio_formats
[i
]);
1134 for (j
= 0; filters
[j
].name
; j
++)
1135 if (!strcmp(tmp
, filters
[j
].name
))
1138 ret
= -E_UNSUPPORTED_FILTER
;
1139 if (!filters
[j
].name
)
1141 tmp
= para_strdup(filters
[j
].name
);
1142 ret
= add_filter(i
, tmp
);
1146 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
1153 static int init_filters(void)
1157 filter_init(filters
);
1158 nf
= PARA_MAX(1, conf
.filter_given
);
1159 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
1160 FOR_EACH_AUDIO_FORMAT(i
) {
1161 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
1162 afi
[i
].filters
= para_malloc(nf
* sizeof(struct filter
*));
1164 if (!conf
.no_default_filters_given
)
1165 return init_default_filters();
1166 for (i
= 0; i
< conf
.filter_given
; i
++) {
1167 char *arg
= conf
.filter_arg
[i
];
1168 char *filter_name
= strchr(arg
, ':');
1169 ret
= -E_MISSING_COLON
;
1172 *filter_name
= '\0';
1174 ret
= get_audio_format_num(arg
);
1177 ret
= add_filter(ret
, filter_name
);
1181 ret
= init_default_filters(); /* use default values for the rest */
1186 static int init_stream_io(void)
1190 ret
= init_writers();
1193 ret
= init_receivers();
1196 ret
= init_filters();
1202 static int dump_commands(int fd
)
1204 char *buf
= para_strdup(""), *tmp
= NULL
;
1208 FOR_EACH_COMMAND(i
) {
1209 tmp
= make_message("%s%s\t%s\n", buf
, cmds
[i
].name
,
1210 cmds
[i
].description
);
1214 ret
= client_write(fd
, buf
);
1220 * command handlers don't close their fd on errors (ret < 0) so that
1221 * its caller can send an error message. Otherwise (ret >= 0) it's up
1222 * to each individual command to close the fd if necessary.
1225 static int com_help(int fd
, int argc
, char **argv
)
1229 const char *dflt
= "No such command. Available commands:\n";
1232 ret
= dump_commands(fd
);
1235 FOR_EACH_COMMAND(i
) {
1236 if (strcmp(cmds
[i
].name
, argv
[1]))
1239 "NAME\n\t%s -- %s\n"
1240 "SYNOPSIS\n\tpara_audioc %s\n"
1241 "DESCRIPTION\n%s\n",
1243 cmds
[i
].description
,
1247 ret
= client_write(fd
, buf
);
1251 ret
= client_write(fd
, dflt
);
1253 ret
= dump_commands(fd
);
1260 static int com_tasks(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1262 char *tl
= get_task_list();
1265 ret
= client_write(fd
, tl
);
1272 static int com_kill(int fd
, int argc
, char **argv
)
1276 return -E_AUDIOD_SYNTAX
;
1277 for (i
= 1; i
< argc
; i
++) {
1278 ret
= kill_task(argv
[i
]);
1287 static int com_stat(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1291 long unsigned mask
= ~0LU;
1295 for (i
= 1; i
< argc
; i
++) {
1296 ret
= stat_item_valid(argv
[i
]);
1302 PARA_INFO_LOG("mask: 0x%lx\n", mask
);
1303 if (mask
& (1 << SI_PLAY_TIME
)) {
1304 struct timeval
*t
= wstime();
1305 char *ts
= get_time_string(t
);
1307 ret
= client_write(fd
, ts
);
1313 if (mask
& (1 << SI_AUDIOD_UPTIME
)) {
1314 char *tmp
, *us
= uptime_str();
1315 tmp
= make_message("%s:%s\n",
1316 status_item_list
[SI_AUDIOD_UPTIME
], us
);
1318 ret
= client_write(fd
, tmp
);
1323 if (mask
& (1 << SI_AUDIOD_STATUS
)) {
1324 char *s
= audiod_status_string();
1325 ret
= client_write(fd
, s
);
1330 if (mask
& (1 << SI_DECODER_FLAGS
)) {
1331 char *df
=decoder_flags();
1332 ret
= client_write(fd
, df
);
1338 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
1340 if (!((1 << i
) & mask
))
1342 v
= stat_item_values
[i
];
1343 tmp
= make_message("%s%s%s", buf
? buf
: "",
1344 v
? v
: "", v
? "\n" : "");
1348 ret
= client_write(fd
, buf
);
1351 ret
= stat_client_add(fd
, mask
);
1356 static int com_grab(int fd
, char *cmdline
)
1358 struct grab_client
*gc
;
1359 struct filter_node
*fn
;
1363 gc
= grab_client_new(fd
, cmdline
, &err
);
1366 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
1368 activate_grab_client(gc
, fn
);
1371 if (err
!= -E_GC_HELP_GIVEN
&& err
!= -E_GC_VERSION_GIVEN
)
1373 if (err
== -E_GC_HELP_GIVEN
) {
1374 msg
= make_message("%s\n\n", grab_client_args_info_usage
);
1375 for (i
= 0; grab_client_args_info_help
[i
]; i
++) {
1376 char *tmp
= make_message("%s%s\n", msg
,
1377 grab_client_args_info_help
[i
]);
1382 msg
= make_message("%s %s\n",
1383 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE
,
1384 GRAB_CLIENT_CMDLINE_PARSER_VERSION
);
1385 err
= client_write(fd
, msg
);
1393 static int __noreturn
com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1396 clean_exit(EXIT_SUCCESS
, "terminating on user request");
1399 static int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1401 audiod_status
= AUDIOD_ON
;
1406 static int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1408 audiod_status
= AUDIOD_OFF
;
1413 static int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1415 audiod_status
= AUDIOD_STANDBY
;
1420 static int com_cycle(int fd
, int argc
, char **argv
)
1422 switch (audiod_status
) {
1424 return com_sb(fd
, argc
, argv
);
1427 return com_on(fd
, argc
, argv
);
1429 case AUDIOD_STANDBY
:
1430 return com_off(fd
, argc
, argv
);
1437 static int check_perms(uid_t uid
)
1441 if (!conf
.user_allow_given
)
1443 for (i
= 0; i
< conf
.user_allow_given
; i
++)
1444 if (uid
== conf
.user_allow_arg
[i
])
1446 return -E_UCRED_PERM
;
1449 static int handle_connect(int accept_fd
)
1451 int i
, argc
, ret
, clifd
= -1;
1452 char *cmd
= NULL
, *p
, *buf
= para_calloc(MAXLINE
), **argv
= NULL
;
1453 struct sockaddr_un unix_addr
;
1455 ret
= para_accept(accept_fd
, &unix_addr
, sizeof(struct sockaddr_un
));
1459 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1);
1462 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
1463 ret
= check_perms(ret
);
1466 ret
= -E_INVALID_AUDIOD_CMD
;
1467 cmd
= para_strdup(buf
);
1468 p
= strchr(cmd
, '\n');
1475 for (i
= 0; cmds
[i
].name
; i
++) {
1477 if (strcmp(cmds
[i
].name
, cmd
))
1479 if (cmds
[i
].handler
) {
1480 argc
= split_args(buf
, &argv
, "\n");
1481 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv
[0], argc
);
1482 ret
= cmds
[i
].handler(clifd
, argc
, argv
);
1485 for (j
= 0; p
[j
]; j
++)
1488 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd
, p
);
1489 ret
= cmds
[i
].line_handler(clifd
, p
);
1492 ret
= -E_INVALID_AUDIOD_CMD
; /* cmd not found */
1497 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
1498 char *tmp
= make_message("%s\n", PARA_STRERROR(-ret
));
1499 client_write(clifd
, tmp
);
1506 static int audiod_get_socket(void)
1508 struct sockaddr_un unix_addr
;
1511 if (conf
.socket_given
)
1512 socket_name
= para_strdup(conf
.socket_arg
);
1514 char *hn
= para_hostname();
1515 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
1519 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
1520 if (conf
.force_given
)
1521 unlink(socket_name
);
1522 fd
= create_pf_socket(socket_name
, &unix_addr
,
1523 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
1525 PARA_EMERG_LOG("%s", "can not connect to socket\n");
1526 exit(EXIT_FAILURE
); /* do not unlink socket */
1528 if (listen(fd
, 5) < 0) {
1529 PARA_EMERG_LOG("%s", "can not listen on socket\n");
1530 exit(EXIT_FAILURE
); /* do not unlink socket */
1532 add_close_on_fork_list(fd
);
1536 static int open_stat_pipe(void)
1538 int ret
, fd
[3] = {-1, 1, 0};
1540 ret
= para_exec_cmdline_pid(&pid
, BINDIR
"/para_client stat", fd
);
1543 PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret
);
1544 add_close_on_fork_list(ret
);
1546 clean_exit(EXIT_FAILURE
, "failed to open status pipe");
1550 void signal_event_handler(struct task
*t
)
1552 struct signal_task
*st
= t
->private_data
;
1554 if (t
->ret
!= -E_SIGNAL_CAUGHT
)
1555 PARA_ERROR_LOG("%s (ignored)\n", PARA_STRERROR(-t
->ret
));
1557 handle_signal(st
->signum
);
1560 void signal_pre_select(struct sched
*s
, struct task
*t
)
1562 struct signal_task
*st
= t
->private_data
;
1564 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
1567 void signal_post_select(struct sched
*s
, struct task
*t
)
1569 struct signal_task
*st
= t
->private_data
;
1571 if (!FD_ISSET(st
->fd
, &s
->rfds
))
1573 t
->ret
= -E_SIGNAL_CAUGHT
;
1574 st
->signum
= para_next_signal();
1577 void signal_setup_default(struct signal_task
*st
)
1579 st
->task
.pre_select
= signal_pre_select
;
1580 st
->task
.post_select
= signal_post_select
;
1581 st
->task
.private_data
= st
;
1582 sprintf(st
->task
.status
, "signal task");
1585 static void command_pre_select(struct sched
*s
, struct task
*t
)
1587 struct command_task
*ct
= t
->private_data
;
1589 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
1593 static void command_post_select(struct sched
*s
, struct task
*t
)
1596 struct command_task
*ct
= t
->private_data
;
1598 t
->ret
= 1; /* always successful */
1599 if (audiod_status
!= AUDIOD_OFF
)
1600 audiod_status_dump();
1601 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
1603 ret
= handle_connect(ct
->fd
);
1605 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
1608 static void init_command_task(struct command_task
*ct
)
1610 ct
->task
.pre_select
= command_pre_select
;
1611 ct
->task
.post_select
= command_post_select
;
1612 ct
->task
.event_handler
= NULL
;
1613 ct
->task
.private_data
= ct
;
1614 ct
->fd
= audiod_get_socket(); /* doesn't return on errors */
1615 sprintf(ct
->task
.status
, "command task");
1618 static void status_event_handler(__a_unused
struct task
*t
)
1620 struct timeval delay
= {1, 0};
1622 struct status_task
*st
= t
->private_data
;
1624 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t
->ret
));
1626 /* avoid busy loop if server is down */
1627 tv_add(now
, &delay
, &st
->restart_barrier
);
1628 FOR_EACH_AUDIO_FORMAT(i
)
1629 afi
[i
].restart_barrier
= st
->restart_barrier
;
1632 static void status_pre_select(struct sched
*s
, struct task
*t
)
1634 struct status_task
*st
= t
->private_data
;
1636 if (st
->fd
>= 0 && audiod_status
== AUDIOD_OFF
)
1638 if (st
->fd
< 0 && audiod_status
!= AUDIOD_OFF
1639 && tv_diff(now
, &st
->restart_barrier
, NULL
) > 0) {
1640 st
->fd
= open_stat_pipe();
1644 if (st
->fd
>= 0 && audiod_status
!= AUDIOD_OFF
)
1645 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
1648 static void status_post_select(struct sched
*s
, struct task
*t
)
1650 struct status_task
*st
= t
->private_data
;
1653 if (st
->fd
< 0 || !FD_ISSET(st
->fd
, &s
->rfds
))
1655 t
->ret
= read(st
->fd
, st
->buf
+ st
->loaded
, STRINGSIZE
- 1 - st
->loaded
);
1658 t
->ret
= -E_STATUS_EOF
;
1661 st
->buf
[t
->ret
+ st
->loaded
] = '\0';
1662 st
->loaded
= for_each_line(st
->buf
, t
->ret
+ st
->loaded
,
1666 static void init_status_task(struct status_task
*st
)
1668 memset(st
, 0, sizeof(struct status_task
));
1669 st
->task
.pre_select
= status_pre_select
;
1670 st
->task
.post_select
= status_post_select
;
1671 st
->task
.event_handler
= status_event_handler
;
1672 st
->task
.private_data
= st
;
1674 sprintf(st
->task
.status
, "status task");
1677 static void set_initial_status(void)
1679 audiod_status
= AUDIOD_ON
;
1680 if (!conf
.mode_given
)
1682 if (!strcmp(conf
.mode_arg
, "sb")) {
1683 audiod_status
= AUDIOD_STANDBY
;
1686 if (!strcmp(conf
.mode_arg
, "off")) {
1687 audiod_status
= AUDIOD_OFF
;
1690 if (strcmp(conf
.mode_arg
, "on"))
1691 PARA_WARNING_LOG("%s", "invalid mode\n");
1694 int main(int argc
, char *argv
[])
1699 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1700 struct task audiod_task_struct
, *audiod_task
= &audiod_task_struct
;
1705 hostname
= para_hostname();
1706 cmdline_parser(argc
, argv
, &conf
);
1707 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1708 cf
= configfile_exists();
1710 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
1711 PARA_EMERG_LOG("%s", "parse error in config file\n");
1715 if (conf
.logfile_given
)
1716 logfile
= open_log(conf
.logfile_arg
);
1717 log_welcome("para_audiod", conf
.loglevel_arg
);
1718 i
= init_stream_io();
1720 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i
));
1723 server_uptime(UPTIME_SET
);
1724 set_initial_status();
1728 setup_signal_handling();
1729 signal_setup_default(sig_task
);
1730 sig_task
->task
.event_handler
= signal_event_handler
;
1732 init_status_task(stat_task
);
1733 init_command_task(cmd_task
);
1734 init_audiod_task(audiod_task
);
1736 if (conf
.daemon_given
)
1739 register_task(&sig_task
->task
);
1740 register_task(&cmd_task
->task
);
1741 register_task(&stat_task
->task
);
1742 register_task(audiod_task
);
1743 s
.default_timeout
.tv_sec
= 3;
1744 s
.default_timeout
.tv_usec
= 99 * 1000;
1747 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
1748 return EXIT_FAILURE
;