29bae161682bc00f6fd133e3c29ca7e5df06994d
[paraslash.git] / afs.c
1 /*
2  * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /** \file afs.c audio file sending functions
20  *
21  * This contains the audio sending part of para_server which is independent of
22  * the current audio format, audio file selector and of the activated senders.
23  */
24
25 #include "server.h"
26 #include <sys/time.h> /* gettimeofday */
27 #include "server.cmdline.h"
28 #include "db.h"
29 #include "afs.h"
30 #include "send.h"
31 #include "error.h"
32 #include "string.h"
33
34 extern const char *status_item_list[];
35
36 static struct timeval announce_tv;
37 static struct timeval data_send_barrier;
38 static struct timeval eof_barrier;
39
40 extern struct misc_meta_data *mmd;
41 extern struct audio_file_selector selectors[];
42 extern struct sender senders[];
43 extern struct gengetopt_args_info conf;
44
45 static FILE *audio_file = NULL;
46
47 #if 1
48         void mp3_init(void *);
49 #endif
50
51 #ifdef HAVE_OGGVORBIS
52         void ogg_init(void *);
53 #endif
54 #ifdef HAVE_FAAD
55         void aac_afh_init(void *);
56 #endif
57
58 /**
59  * the list of supported  audio formats
60  */
61 struct audio_format afl[] = {
62 #if 1
63         {
64                 .name = "mp3",
65                 .init = mp3_init,
66         },
67 #endif
68 #ifdef HAVE_OGGVORBIS
69         {
70                 .name = "ogg",
71                 .init = ogg_init,
72         },
73 #endif
74 #ifdef HAVE_FAAD
75         {
76                 .name = "aac",
77                 .init = aac_afh_init,
78         },
79 #endif
80         {
81                 .name = NULL,
82         }
83 };
84
85
86 /**
87  * check if audio file sender is playing
88  *
89  * \return greater than zero if playing, zero otherwise.
90  *
91  */
92 unsigned int afs_playing(void)
93 {
94         return mmd->new_afs_status_flags & AFS_PLAYING;
95 }
96
97 /**
98  * check if 'next' flag is set afs_status_flags
99  *
100  * \return greater than zero if set, zero if not.
101  *
102  */
103 unsigned int afs_next(void)
104 {
105         return mmd->new_afs_status_flags & AFS_NEXT;
106 }
107
108 /**
109  * check if a reposition request is pending
110  *
111  * \return greater than zero if true, zero otherwise.
112  *
113  */
114 unsigned int afs_repos(void)
115 {
116         return mmd->new_afs_status_flags & AFS_REPOS;
117 }
118
119 /**
120  * check if audio file sender is paused
121  *
122  * \return greater than zero if paused, zero otherwise.
123  *
124  */
125 unsigned int afs_paused(void)
126 {
127         return !(mmd->new_afs_status_flags & AFS_NEXT)
128                 && !(mmd->new_afs_status_flags & AFS_PLAYING);
129 }
130
131 /**
132  * get the name of the given audio format
133  * \param i the audio format number
134  *
135  * This returns a pointer to statically allocated memory so it
136  * must not be freed by the caller.
137  */
138 const char *audio_format_name(int i)
139 {
140         return i >= 0?  afl[i].name : "(none)";
141 }
142
143 /**
144  * initialize the audio file sender
145  *
146  * Call the init functions of all supported audio format handlers and
147  * initialize all supported senders.
148  */
149 void afs_init(void)
150 {
151         int i;
152         char *hn = para_hostname(), *home = para_homedir();
153
154         PARA_DEBUG_LOG("supported audio formats: %s\n",
155                 SUPPORTED_AUDIO_FORMATS);
156         for (i = 0; afl[i].name; i++) {
157                 PARA_NOTICE_LOG("initializing %s handler\n",
158                         afl[i].name);
159                 afl[i].init(&afl[i]);
160         }
161         ms2tv(conf.announce_time_arg, &announce_tv);
162         PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
163         for (i = 0; senders[i].name; i++) {
164                 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
165                 senders[i].init(&senders[i]);
166         }
167         free(hn);
168         free(home);
169 }
170
171 static int get_file_info(int i)
172 {
173         return  afl[i].get_file_info(audio_file, mmd->audio_file_info,
174                 &mmd->chunks_total, &mmd->seconds_total);
175 }
176
177 /*
178  * guess the audio format judging from filename
179  * \param name the filename
180  *
181  * \return This function returns -1 if it has no idea what kind of audio
182  * file this might be. Otherwise the (non-negative) number of the audio format
183  * is returned.
184  */
185 static int guess_audio_format(const char *name)
186 {
187
188         int i, len1 = strlen(name), len2;
189
190         for (i = 0; afl[i].name; i++) {
191                 len2 = strlen(afl[i].name);
192                 if (len1 < len2)
193                         continue;
194                 if (!strncasecmp(name + (len1 - len2), afl[i].name, len2)) {
195                         PARA_DEBUG_LOG("might be %s\n", afl[i].name);
196                         return i;
197                 }
198         }
199         return -1;
200 }
201
202 static int get_audio_format(int omit)
203 {
204         int i;
205
206         for (i = 0; afl[i].name; i++) {
207                 if (i == omit || !afl[i].get_file_info)
208                         continue;
209                 rewind(audio_file);
210                 if (get_file_info(i) > 0)
211                         return i;
212                 rewind(audio_file);
213         }
214         return -E_AUDIO_FORMAT;
215 }
216
217 /*
218  * upddate shared mem
219  */
220 static int update_mmd(void)
221 {
222         int i;
223         struct stat file_status;
224
225         i = guess_audio_format(mmd->filename);
226         if (i < 0 || get_file_info(i) < 0)
227                 i = get_audio_format(i);
228         if (i < 0)
229                 return i;
230         mmd->audio_format = i;
231         mmd->chunks_sent = 0;
232         mmd->current_chunk = 0;
233         mmd->offset = 0;
234         if (fstat(fileno(audio_file), &file_status) == -1)
235                 return -E_FSTAT;
236         mmd->size = file_status.st_size;
237         mmd->mtime = file_status.st_mtime;
238         mmd->events++;
239         PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
240         return 1;
241 }
242
243 static void get_song(void)
244 {
245         char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
246         int i;
247
248         if (!sl)
249                 goto err_out;
250         for (i = 0; sl[i]; i++) {
251                 struct timeval now;
252                 PARA_INFO_LOG("trying %s\n", sl[i]);
253                 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
254                         continue;
255                 audio_file = fopen(sl[i], "r");
256                 if (!audio_file)
257                         continue;
258                 strcpy(mmd->filename, sl[i]);
259                 if (update_mmd() < 0) {
260                         fclose(audio_file);
261                         audio_file = NULL;
262                         continue;
263                 }
264                 mmd->num_played++;
265                 if (selectors[mmd->selector_num].update_audio_file)
266                         selectors[mmd->selector_num].update_audio_file(sl[i]);
267                 PARA_DEBUG_LOG("%s", "success\n");
268                 mmd->new_afs_status_flags &= (~AFS_NEXT);
269                 gettimeofday(&now, NULL);
270                 tv_add(&now, &announce_tv, &data_send_barrier);
271
272                 goto free;
273         }
274         PARA_ERROR_LOG("%s", "no valid files found\n");
275 err_out:
276         mmd->new_afs_status_flags = AFS_NEXT;
277 free:
278         if (sl) {
279                 for (i = 0; sl[i]; i++)
280                         free(sl[i]);
281                 free(sl);
282         }
283 }
284
285 static int chk_barrier(const char *bname, const struct timeval *now,
286                 const struct timeval *barrier, struct timeval *diff, int log)
287 {
288         long ms;
289
290         if (tv_diff(now, barrier, diff) > 0)
291                 return 1;
292         ms = tv2ms(diff);
293         if (log && ms)
294                 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
295         return -1;
296 }
297
298 static void afs_next_chunk_time(struct timeval *due)
299 {
300         struct timeval tmp;
301
302         tv_scale(mmd->chunks_sent, &afl[mmd->audio_format].chunk_tv, &tmp);
303         tv_add(&tmp, &mmd->stream_start, due);
304 }
305
306 /*
307  * != NULL: timeout for next chunk
308  * NULL: nothing to do
309  */
310 static struct timeval *afs_compute_timeout(void)
311 {
312         static struct timeval the_timeout;
313         struct timeval now, next_chunk;
314
315         if (afs_next() && mmd->audio_format >= 0) {
316                 /* only sleep a bit, nec*/
317                 the_timeout.tv_sec = 0;
318                 the_timeout.tv_usec = 100;
319                 return &the_timeout;
320         }
321         gettimeofday(&now, NULL);
322         if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
323                 return &the_timeout;
324         if (chk_barrier("data send", &now, &data_send_barrier,
325                         &the_timeout, 1) < 0)
326                 return &the_timeout;
327         if (mmd->audio_format < 0 || !afs_playing() || !audio_file)
328                 return NULL;
329         afs_next_chunk_time(&next_chunk);
330         if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
331                         &the_timeout, 0) < 0)
332                 return &the_timeout;
333         /* chunk is due or bof */
334         the_timeout.tv_sec = 0;
335         the_timeout.tv_usec = 0;
336         return &the_timeout;
337 }
338
339 static void afs_eof(struct audio_format *af)
340 {
341         struct timeval now;
342         int i;
343         char *tmp;
344
345         if (!af || !audio_file) {
346                 for (i = 0; senders[i].name; i++)
347                         senders[i].shutdown_clients();
348                 return;
349         }
350         gettimeofday(&now, NULL);
351         tv_add(&af->eof_tv, &now, &eof_barrier);
352         af->close_audio_file();
353         audio_file = NULL;
354         mmd->audio_format = -1;
355         af = NULL;
356         mmd->chunks_sent = 0;
357         mmd->offset = 0;
358         mmd->seconds_total = 0;
359         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
360                 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
361         strcpy(mmd->audio_file_info, tmp);
362         free(tmp);
363         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
364                 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
365         strcpy(mmd->selector_info, tmp);
366         free(tmp);
367         mmd->filename[0] = '\0';
368         mmd->size = 0;
369         mmd->events++;
370 }
371
372 /**
373  * compute the timeout for para_server's main select-loop
374  *
375  * This function gets called from para_server to determine the timeout value
376  * for its main select loop.
377  *
378  * Before the timeout is computed, the current afs status flags are evaluated
379  * and acted upon by calling appropriate functions from the lower layers.
380  * Possible actions include
381  *
382  *      - request a new file list from the current dabase tool (audio file change)
383  *      - shutdown of all senders (stop/pause command)
384  *      - repositioning of the stream (ff/jmp command)
385  *
386  * \return A pointer to a struct timeval containing the timeout for the next
387  * chunk of data to be sent, or NULL if we're not sending right now.
388  */
389 struct timeval *afs_preselect(void)
390 {
391         struct audio_format *af = NULL;
392         int i, format;
393         struct timeval *ret;
394 again:
395         format = mmd->audio_format;
396         if (format >= 0)
397                 af = afl + format;
398         else
399                 for (i = 0; senders[i].name; i++)
400                         senders[i].shutdown_clients();
401         if (afs_next() && af) {
402                 afs_eof(af);
403                 return afs_compute_timeout();
404         }
405         if (afs_paused() || afs_repos()) {
406                 for (i = 0; senders[i].name; i++)
407                         senders[i].shutdown_clients();
408                 if (af) {
409                         struct timeval now;
410                         gettimeofday(&now, NULL);
411                         if (!afs_paused() || mmd->chunks_sent)
412                                 tv_add(&af->eof_tv, &now, &eof_barrier);
413                         if (afs_repos())
414                                 tv_add(&now, &announce_tv, &data_send_barrier);
415                         if (mmd->new_afs_status_flags & AFS_NOMORE)
416                                 mmd->new_afs_status_flags = AFS_NEXT;
417                 }
418                 mmd->chunks_sent = 0;
419         }
420         if (af && afs_repos() && mmd->current_chunk != mmd->repos_request)
421                 af->reposition_stream(mmd->repos_request);
422         if (afs_repos()) {
423                 mmd->new_afs_status_flags &= ~(AFS_REPOS);
424                 mmd->current_chunk = mmd->repos_request;
425         }
426         ret = afs_compute_timeout();
427         if (!ret && !audio_file && afs_playing() &&
428                         !(mmd->new_afs_status_flags & AFS_NOMORE)) {
429                 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
430                 get_song();
431                 goto again;
432         }
433         return ret;
434 }
435
436 /**
437  * afs_send_chunk - paraslash's main sending function
438  *
439  * This function gets called from para_server as soon as the next chunk of
440  * data should be pushed out. It first calls the read_chunk() function of
441  * the current audio format handler to obtain a pointer to the data to be
442  * sent out as well as its length. This information is then passed to each
443  * supported sender's send() function which does the actual sending.
444  *
445  * Return value: Positive return value on success, zero on eof and negative
446  * on errors.
447  */
448
449 void afs_send_chunk(void)
450 {
451         int i;
452         struct audio_format *af;
453         char *buf;
454         ssize_t ret;
455         struct timeval now, due;
456
457         if (mmd->audio_format < 0 || !audio_file || !afs_playing())
458                 return;
459         af = &afl[mmd->audio_format];
460         gettimeofday(&now, NULL);
461         afs_next_chunk_time(&due);
462         if (tv_diff(&due, &now, NULL) > 0)
463                 return;
464         if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
465                 return;
466         if (chk_barrier("data send", &now, &data_send_barrier,
467                         &due, 1) < 0)
468                 return;
469         buf = af->read_chunk(mmd->current_chunk, &ret);
470         mmd->new_afs_status_flags &= ~AFS_REPOS;
471         if (!buf) {
472                 if (ret < 0)
473                         mmd->new_afs_status_flags = AFS_NEXT;
474                 else
475                         mmd->new_afs_status_flags |= AFS_NEXT;
476                 afs_eof(af);
477                 return;
478         }
479         if (!mmd->chunks_sent) {
480                 struct timeval tmp;
481                 gettimeofday(&mmd->stream_start, NULL);
482                 tv_scale(mmd->current_chunk, &af->chunk_tv, &tmp);
483                 mmd->offset = tv2ms(&tmp);
484                 mmd->events++;
485         }
486         for (i = 0; senders[i].name; i++)
487                 senders[i].send(af, mmd->current_chunk,
488                         mmd->chunks_sent, buf, ret);
489         mmd->new_afs_status_flags |= AFS_PLAYING;
490         mmd->chunks_sent++;
491         mmd->current_chunk++;
492 }