Debian Codegraph @

rplayd/spool.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /* $Id: spool.c,v 1.6 1999/03/10 21:14:38 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include <stdio.h>
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #include <unistd.h>
34 #include "rplayd.h"
35 #include "rplay.h"
36 #include "spool.h"
37 #include "sound.h"
38 #include "buffer.h"
39 #include "timer.h"
40 #include "connection.h"
41 #include "native.h"
42 #ifdef TEST_FLANGE
43 #include "flange.h"
44 #endif
45
46 /* Make sure MAXPATHLEN is defined. */
47 #ifndef MAXPATHLEN
48 #define MAXPATHLEN 4096
49 #endif
50
51 SPOOL *spool = NULL; /* List of spool entries. */
52 int spool_size = 0; /* Number of entries in the spool. */
53 int spool_nplaying = 0; /* Number of playing sounds. */
54 int spool_npaused = 0; /* Number of paused sounds. */
55 int spool_prio = 0; /* Highest priority spool entry. */
56 int spool_needs_update = 0; /* Need to call spool_update(). */
57
58 /*
59 * initialize the sound spool
60 */
61 void
62 spool_init()
63 {
64 SPOOL *sp, *sp_next;
65
66 /* Destroy any existing spool entries (reset). */
67 for (sp = spool; sp; sp = sp_next)
68 {
69 sp_next = sp->next;
70 spool_destroy(sp);
71 }
72
73 spool = NULL;
74 spool_nplaying = 0;
75 spool_prio = 0;
76 spool_size = 0;
77 }
78
79 /* Create a new spool entry. */
80 SPOOL *
81 spool_create()
82 {
83 SPOOL *sp = (SPOOL *) malloc(sizeof(SPOOL));
84 if (sp == NULL)
85 {
86 report(REPORT_ERROR, "spool_create: out of memory\n");
87 done(1);
88 }
89
90 /* `sp' must be initialized before being reset. */
91 memset((char *) sp, 0, sizeof(SPOOL));
92 sp->id = -1; /* XXX */
93
94 spool_reset(sp);
95
96 /* Insert the new entry into the spool list. */
97 sp->next = spool;
98 sp->prev = NULL;
99 if (spool)
100 {
101 spool->prev = sp;
102 }
103 spool = sp;
104
105 spool_size++;
106
107 return sp;
108 }
109
110 /* Destroy the given spool entry. */
111 #ifdef __STDC__
112 void
113 spool_destroy(SPOOL *sp)
114 #else
115 void
116 spool_destroy(sp)
117 SPOOL *sp;
118 #endif
119 {
120 /* Reset `sp' in case any resources are allocated. */
121 spool_reset(sp);
122
123 /* Remove `sp' from the spool list. */
124 if (sp->prev)
125 {
126 sp->prev->next = sp->next;
127 }
128 else
129 {
130 spool = sp->next;
131 }
132 if (sp->next)
133 {
134 sp->next->prev = sp->prev;
135 }
136
137 spool_size--;
138
139 free((char *) sp);
140 }
141
142 /*
143 * Return the next available spool entry.
144 *
145 * `priority' is the priority of a new sound and is used to replace
146 * lower priority spool entries when the spool is full.
147 */
148 #ifdef __STDC__
149 SPOOL *
150 spool_next(int priority)
151 #else
152 SPOOL *
153 spool_next(priority)
154 int priority;
155 #endif
156 {
157 int i;
158 int min_priority = RPLAY_MAX_PRIORITY, index = -1;
159 SPOOL *sp, *lowest = NULL;
160
161 /* There's room for a new entry. */
162 if (spool_size < SPOOL_SIZE)
163 {
164 sp = spool_create();
165 sp->id = spool_id();
166 return sp;
167 }
168
169 /* The spool is full, try to replace an entry. */
170
171 /* Find the lowest priority sound. */
172 for (sp = spool; sp; sp = sp->next)
173 {
174 if (sp->state == SPOOL_PLAY)
175 {
176 if (sp->rp->priority < min_priority)
177 {
178 min_priority = sp->rp->priority;
179 lowest = sp;
180 }
181 }
182 }
183
184 /* Replace the lowest priority sound, if possible. */
185 if (priority > min_priority && lowest)
186 {
187 sp = lowest;
188 if (sp->notify_position)
189 {
190 connection_notify(0, NOTIFY_POSITION, sp);
191 sp->notify_position = 0;
192 }
193 connection_notify(0, NOTIFY_DONE, sp);
194 spool_reset(sp);
195 spool_nplaying--;
196 sp->id = spool_id();
197 return sp;
198 }
199 else
200 {
201 return NULL;
202 }
203 }
204
205 /* Reset a spool entry to an initial state.
206 `next' and `prev' are NOT changed. */
207 #ifdef __STDC__
208 void
209 spool_reset(SPOOL *sp)
210 #else
211 void
212 spool_reset(sp)
213 SPOOL *sp;
214 #endif
215 {
216 sp->state = SPOOL_NULL;
217 if (sp->id > 0)
218 {
219 /* Wakeup any connections that may be waiting for this
220 spool entry. Their flow data will be ignored. */
221 connection_flow_continue(sp);
222 }
223 sp->id = -1;
224 sp->time = 0;
225 if (sp->rp)
226 {
227 int i;
228
229 for (i = 0; i < sp->rp->nsounds; i++)
230 {
231 sound_clean(sp->sound[i]);
232 }
233
234 rplay_destroy(sp->rp);
235 }
236 sp->rp = NULL;
237 sp->ptr = NULL;
238 sp->ptr_end = NULL;
239 sp->sample_rate = 0;
240 sp->sample_index = 0;
241 sp->sample_factor = 0;
242 sp->oversample[0] = 0;
243 sp->oversample[1] = 0;
244 sp->oversample_inc[0] = 0;
245 sp->oversample_inc[1] = 0;
246 if (sp->curr_buffer)
247 {
248 buffer_dealloc(sp->curr_buffer, 1);
249 }
250 sp->curr_buffer = NULL;
251 if (sp->next_buffer)
252 {
253 buffer_dealloc(sp->next_buffer, 1);
254 }
255 sp->next_buffer = NULL;
256 sp->offset = 0;
257 if (sp->si)
258 {
259 sound_close(sp->si);
260 }
261 sp->si = NULL;
262 sp->to_native = NULL;
263 sp->skip_count = 0;
264 sp->auto_pause = 0;
265 sp->notify_position = 0;
266
267 spool_setprio();
268 }
269
270 /* Find the maximum spool priority. */
271 #ifdef __STDC__
272 void
273 spool_setprio(void)
274 #else
275 void
276 spool_setprio()
277 #endif
278 {
279 SPOOL *sp;
280
281 spool_prio = 0;
282 for (sp = spool; sp; sp = sp->next)
283 {
284 if (sp->state == SPOOL_PLAY)
285 {
286 spool_prio = MAX(spool_prio, sp->rp->priority);
287 }
288 }
289 }
290
291 /* Find the given RPLAY object in the spool. */
292 #ifdef __STDC__
293 int
294 spool_match(RPLAY *match, void (*action) (SPOOL *), struct sockaddr_in sin)
295 #else
296 int
297 spool_match(match, action, sin)
298 RPLAY *match;
299 void (*action) ();
300 struct sockaddr_in sin;
301 #endif
302 {
303 int i, id, nmatch = 0;
304 RPLAY *rp;
305 RPLAY_ATTRS *a1, *a2;
306 SPOOL *sp, *sp_next;
307
308 for (sp = spool; sp; sp = sp_next)
309 {
310 sp_next = sp->next; /* sp may be destroyed */
311
312 if (sp->state == SPOOL_NULL)
313 {
314 continue;
315 }
316
317 a2 = match->attrs;
318 if (a2 && a2->sound[0] == '#')
319 {
320 for (; a2; a2 = a2->next)
321 {
322 id = atoi(a2->sound + 1);
323 if (id == 0 || id == sp->id)
324 {
325 sp->sin = sin;
326 (*action) (sp);
327 nmatch++;
328 break;
329 }
330 }
331 continue;
332 }
333
334 rp = sp->rp;
335 for (a1 = rp->attrs, a2 = match->attrs; a1 && a2; a1 = a1->next, a2 = a2->next)
336 {
337 if (strcmp(a1->sound, a2->sound))
338 {
339 break;
340 }
341 }
342 if (!a1 && !a2)
343 {
344 sp->sin = sin;
345 (*action) (sp);
346 nmatch++;
347 }
348 }
349
350 return nmatch;
351 }
352
353 /* Remove any spool entries with `sound' from the spool. */
354 #ifdef __STDC__
355 void
356 spool_remove(SOUND *sound)
357 #else
358 void
359 spool_remove(sound)
360 SOUND *sound;
361 #endif
362 {
363 int i, j, n;
364 SPOOL *sp, *sp_next;
365
366 for (sp = spool; sp; sp = sp_next)
367 {
368 sp_next = sp->next; /* sp may be destroyed */
369
370 if (sp->state == SPOOL_NULL)
371 {
372 continue;
373 }
374
375 n = 0;
376 for (j = 0; j < sp->rp->nsounds; j++)
377 {
378 if (sp->sound[j] == sound)
379 {
380 n++;
381 }
382 }
383 if (n)
384 {
385 spool_done(sp);
386 }
387 }
388 }
389
390 /*
391 * Make all spool entries waiting for `sound' ready to be played.
392 */
393 #ifdef __STDC__
394 void
395 spool_ready(SOUND *sound)
396 #else
397 void
398 spool_ready(sound)
399 SOUND *sound;
400 #endif
401 {
402 int j, number_not_ready, has_the_sound;
403 SPOOL *sp;
404
405 for (sp = spool; sp; sp = sp->next)
406 {
407 if (sp->state == SPOOL_WAIT)
408 {
409 number_not_ready = 0;
410 has_the_sound = 0;
411 for (j = 0; j < sp->rp->nsounds; j++)
412 {
413 if (sp->sound[j]->status != SOUND_READY)
414 {
415 number_not_ready++;
416 if (sp->sound[j] == sound)
417 {
418 number_not_ready--;
419 }
420 }
421 if (sp->sound[j] == sound)
422 {
423 has_the_sound++;
424 }
425 }
426 if (number_not_ready == 0 && has_the_sound)
427 {
428 for (j = 0; j < sp->rp->nsounds; j++)
429 {
430 sound_map(sp->sound[j]);
431 }
432
433 spool_play(sp);
434 }
435 }
436 }
437 }
438
439 /* Return a buffer list for the `list spool' command. */
440 BUFFER *
441 spool_list_create()
442 {
443 BUFFER *spool_list, *b;
444 int i, n;
445 char buf[RPTP_MAX_LINE];
446 char line[RPTP_MAX_LINE];
447 SOUND *s;
448 SPOOL *sp;
449 int length;
450
451 b = buffer_create();
452 spool_list = b;
453 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "+message=\"spool\"\r\n");
454 b->nbytes += strlen(b->buf);
455
456 for (sp = spool; sp; sp = sp->next)
457 {
458 length = 0;
459 line[0] = '\0';
460
461 SNPRINTF(SIZE(buf, sizeof(buf)), "id=#%d state=", sp->id);
462 strncat(line + length, buf, sizeof(line) - length);
463 length += strlen(buf);
464
465 switch (sp->state)
466 {
467 case SPOOL_PLAY:
468 SNPRINTF(SIZE(buf, sizeof(buf)), "play");
469 break;
470
471 case SPOOL_PAUSE:
472 SNPRINTF(SIZE(buf, sizeof(buf)), "pause");
473 break;
474
475 case SPOOL_WAIT:
476 SNPRINTF(SIZE(buf, sizeof(buf)), "wait");
477 break;
478
479 case SPOOL_NEXT:
480 SNPRINTF(SIZE(buf, sizeof(buf)), "next");
481 break;
482
483 case SPOOL_SKIP:
484 SNPRINTF(SIZE(buf, sizeof(buf)), "skip");
485 break;
486
487 default:
488 continue;
489 }
490 strncat(line + length, buf, sizeof(line) - length);
491 length += strlen(buf);
492
493 s = sp->sound[sp->curr_sound];
494
495 SNPRINTF(SIZE(buf, sizeof(buf)), "\
496 sound=\"%s\" host=%s volume=%d priority=%d count=%d position=%.2f remain=%.2f seconds=%.2f size=%d\
497 sample-rate=%d channels=%d bits=%g input=%s client-data=\"%s\" list-name=\"%s\"\r\n",
498 sp->curr_attrs->sound,
499 inet_ntoa(sp->sin.sin_addr),
500 sp->curr_attrs->volume,
501 sp->rp->priority,
502 sp->curr_count,
503 sp->sample_rate && s->samples ? sp->sample_index / sp->sample_rate : 0,
504 sp->sample_rate && s->samples ? ((double) s->samples - sp->sample_index) / sp->sample_rate : 0,
505 sp->sample_rate && s->samples ? (double) s->samples / sp->sample_rate : 0,
506 s->size,
507 sp->sample_rate,
508 s->channels,
509 s->input_precision,
510 input_to_string(s->type),
511 sp->curr_attrs->client_data,
512 sp->rp->list_name);
513
514 strncat(line + length, buf, sizeof(line) - length);
515 length += strlen(buf);
516
517 if (b->nbytes + length > BUFFER_SIZE)
518 {
519 b->next = buffer_create();
520 b = b->next;
521 }
522
523 strncat(b->buf + b->nbytes, line, BUFFER_SIZE - b->nbytes);
524 b->nbytes += length;
525 }
526
527 if (b->nbytes + 3 > BUFFER_SIZE)
528 {
529 b->next = buffer_create();
530 b = b->next;
531 }
532
533 strncat(b->buf + b->nbytes, ".\r\n", BUFFER_SIZE - b->nbytes);
534 b->nbytes += 3;
535
536 return spool_list;
537 }
538
539 /*
540 * Return a unique spool id.
541 */
542 int
543 spool_id()
544 {
545 static int id = 0;
546 SPOOL *sp;
547
548 for (;;)
549 {
550 id++;
551 if (id > SPOOL_MAX_ID)
552 {
553 id = SPOOL_MIN_ID;
554 }
555 for (sp = spool; sp; sp = sp->next) /* see if the id is in use */
556 {
557 if (sp->state != SPOOL_NULL && sp->id == id)
558 {
559 break;
560 }
561 }
562 if (!sp)
563 {
564 return id;
565 }
566 }
567 }
568
569 #ifdef __STDC__
570 void
571 spool_stop(SPOOL *sp)
572 #else
573 void
574 spool_stop(sp)
575 SPOOL *sp;
576 #endif
577 {
578 timer_block();
579 switch (sp->state)
580 {
581 case SPOOL_PLAY:
582 case SPOOL_PAUSE:
583 case SPOOL_WAIT:
584 if (sp->state == SPOOL_PLAY)
585 {
586 spool_nplaying--;
587 }
588 else if (sp->state == SPOOL_PAUSE)
589 {
590 spool_npaused--;
591 }
592 if (sp->notify_position)
593 {
594 connection_notify(0, NOTIFY_POSITION, sp);
595 sp->notify_position = 0;
596 }
597 connection_notify(0, NOTIFY_STOP, sp);
598 connection_notify(0, NOTIFY_DONE, sp);
599 spool_destroy(sp);
600 break;
601 }
602 timer_unblock();
603 }
604
605 #ifdef __STDC__
606 void
607 spool_pause(SPOOL *sp)
608 #else
609 void
610 spool_pause(sp)
611 SPOOL *sp;
612 #endif
613 {
614 timer_block();
615 if (sp->state == SPOOL_PLAY)
616 {
617 sp->state = SPOOL_PAUSE;
618 spool_nplaying--;
619 spool_npaused++;
620 if (sp->notify_position)
621 {
622 connection_notify(0, NOTIFY_POSITION, sp);
623 sp->notify_position = 0;
624 }
625 connection_notify(0, NOTIFY_PAUSE, sp);
626 }
627 timer_unblock();
628 }
629
630 #ifdef __STDC__
631 void
632 spool_continue(SPOOL *sp)
633 #else
634 void
635 spool_continue(sp)
636 SPOOL *sp;
637 #endif
638 {
639 timer_block();
640 if (sp->state == SPOOL_PAUSE)
641 {
642 sp->state = SPOOL_PLAY;
643 if (rplay_audio_match)
644 {
645 rplayd_audio_match(sp);
646 }
647 spool_nplaying++;
648 spool_npaused--;
649 spool_needs_update++;
650 connection_notify(0, NOTIFY_CONTINUE, sp);
651 }
652 timer_unblock();
653 }
654
655 #ifdef __STDC__
656 void
657 spool_done(SPOOL *sp)
658 #else
659 void
660 spool_done(sp)
661 SPOOL *sp;
662 #endif
663 {
664 timer_block();
665 switch (sp->state)
666 {
667 case SPOOL_PLAY:
668 case SPOOL_PAUSE:
669 case SPOOL_WAIT:
670 if (sp->si && sp->si->is_flow && sp->state == SPOOL_PLAY)
671 {
672 sp->si->eof = 1; /* spool_update will deal with this */
673 }
674 else
675 {
676 if (sp->state == SPOOL_PLAY)
677 {
678 spool_nplaying--;
679 }
680 else if (sp->state == SPOOL_PAUSE)
681 {
682 spool_npaused--;
683 }
684 if (sp->notify_position)
685 {
686 connection_notify(0, NOTIFY_POSITION, sp);
687 sp->notify_position = 0;
688 }
689 connection_notify(0, NOTIFY_DONE, sp);
690 spool_destroy(sp);
691 }
692 break;
693 }
694 timer_unblock();
695 }
696
697 #ifdef __STDC__
698 void
699 spool_flow_pause(SPOOL *sp)
700 #else
701 void
702 spool_flow_pause(sp)
703 SPOOL *sp;
704 #endif
705 {
706 timer_block();
707
708 if (sp->state == SPOOL_PLAY)
709 {
710 sp->state = SPOOL_WAIT;
711 spool_nplaying--;
712 }
713
714 timer_unblock();
715 }
716
717 #ifdef __STDC__
718 void
719 spool_flow_continue(SPOOL *sp)
720 #else
721 void
722 spool_flow_continue(sp)
723 SPOOL *sp;
724 #endif
725 {
726 timer_block();
727
728 if (sp->state == SPOOL_WAIT)
729 {
730 sp->state = SPOOL_PLAY;
731 spool_nplaying++;
732 spool_needs_update++;
733 }
734
735 timer_unblock();
736 }
737
738 #ifdef __STDC__
739 int
740 spool_flow_insert(SPOOL *sp, BUFFER *b)
741 #else
742 int
743 spool_flow_insert(sp, b)
744 SPOOL *sp;
745 BUFFER *b;
746 #endif
747 {
748 SOUND *s;
749 BUFFER *tmp_buf;
750
751 s = sp->sound[sp->curr_sound];
752 *s->flowp = b;
753 for (tmp_buf = b; tmp_buf->next; tmp_buf = tmp_buf->next) ;
754 s->flowp = &tmp_buf->next;
755
756 /* Enable the sound. */
757 if (s->status == SOUND_NOT_READY)
758 {
759 s->status = SOUND_READY;
760 if (sound_map(s) < 0)
761 {
762 /* Punt - can't determine what type of sound it is. */
763 spool_remove(s);
764 /* spool_remove now deletes the sound - sound_delete (s, 0); */
765 return -1;
766 }
767 spool_ready(s);
768 }
769
770 for (tmp_buf = b; tmp_buf; tmp_buf = tmp_buf->next)
771 {
772 //s->size += tmp_buf->nbytes;
773 sp->si->water_mark += tmp_buf->nbytes;
774 }
775
776 s->samples = number_of_samples(s, sp->si->water_mark);
777
778 /* Wakeup the spool entry. */
779 if (sp->state == SPOOL_WAIT)
780 {
781 spool_flow_continue(sp);
782 }
783
784 return 0;
785 }
786
787 #ifdef __STDC__
788 void
789 spool_skip(SPOOL *sp, int count)
790 #else
791 void
792 spool_skip(sp, count)
793 SPOOL *sp;
794 int count;
795 #endif
796 {
797 SOUND *s;
798
799 timer_block();
800 s = sp->sound[sp->curr_sound];
801 /* Only sounds that are playing or paused can be skipped.
802 Flows can't be skipped right now. */
803 if ((sp->state == SPOOL_PLAY || sp->state == SPOOL_PAUSE)
804 && s->type == SOUND_FILE)
805 {
806 if (sp->state == SPOOL_PAUSE)
807 {
808 spool_npaused--;
809 sp->auto_pause++;
810 }
811 sp->state = SPOOL_SKIP;
812 sp->skip_count = count;
813 spool_needs_update++;
814 connection_notify(0, NOTIFY_SKIP, sp);
815 }
816 timer_unblock();
817 }
818
819 #ifdef __STDC__
820 void
821 spool_play(SPOOL *sp)
822 #else
823 void
824 spool_play(sp)
825 SPOOL *sp;
826 #endif
827 {
828 SOUND *s;
829
830 s = sp->sound[sp->curr_sound];
831
832 #ifdef HAVE_CDROM
833 /* Parse the name of the cdrom device that was specified. */
834 if (s->type == SOUND_CDROM)
835 {
836 char name_buf[MAXPATHLEN];
837 char *p;
838
839 strncpy(name_buf, sp->curr_attrs->sound, sizeof(name_buf));
840 p = strchr(name_buf, ':');
841 if (p == NULL)
842 {
843 report(REPORT_DEBUG, "spool_play: can't parse cdrom name `%s'\n", s->name);
844 return;
845 }
846 *p++ = '\0';
847
848 if (strchr(p, '-'))
849 {
850 p = strtok(p, "-");
851 if (p)
852 {
853 s->starting_track = atoi(p);
854 p = strtok(NULL, "");
855 if (p)
856 {
857 s->ending_track = atoi(p);
858 }
859 }
860 }
861 else
862 {
863 s->starting_track = atoi(p);
864 s->ending_track = atoi(p);
865 }
866 }
867 #endif /* HAVE_CDROM */
868
869 sp->si = sound_open(s, 1);
870 if (sp->si == NULL)
871 {
872 spool_destroy(sp);
873 return;
874 }
875
876 sound_seek(sp->si, s->offset, SEEK_SET); /* skip the audio header */
877
878 sp->curr_buffer = NULL;
879 sp->next_buffer = NULL;
880 sp->sample_index = 0;
881 sp->to_native = native_table[s->format].to_native[s->byte_order - 1];
882
883
884 spool_set_sample_rate(sp,
885 (sp->curr_attrs->sample_rate == RPLAY_DEFAULT_SAMPLE_RATE)
886 ? s->sample_rate : sp->curr_attrs->sample_rate);
887
888 /* Change the state to SPOOL_PLAY when ALL fields have been assigned. */
889 sp->state = SPOOL_PLAY;
890
891 spool_nplaying++;
892 spool_setprio();
893 spool_needs_update++;
894 connection_notify(0, NOTIFY_PLAY, sp);
895 connection_notify(0, NOTIFY_POSITION, sp);
896
897 if (sp->auto_pause)
898 {
899 spool_pause(sp);
900 sp->auto_pause = 0;
901 }
902 }
903
904 #ifdef __STDC__
905 void
906 spool_update(void)
907 #else
908 void
909 spool_update()
910 #endif
911 {
912 int i, n;
913 SPOOL *sp, *sp_next;
914 SOUND *s;
915 BUFFER *b;
916 time_t now;
917
918 spool_needs_update = 0;
919 now = time(0);
920
921 for (sp = spool; sp; sp = sp_next)
922 {
923 sp_next = sp->next; /* sp may be destroyed */
924
925 /* Some sounds must end here. */
926 if (sp->state == SPOOL_PLAY && !sp->curr_buffer && !sp->si)
927 {
928 sp->state = SPOOL_NEXT;
929 }
930
931 switch (sp->state)
932 {
933 case SPOOL_SKIP:
934 case SPOOL_NEXT:
935 if (sp->notify_position)
936 {
937 connection_notify(0, NOTIFY_POSITION, sp);
938 sp->notify_position = 0;
939 }
940
941 if (!sp->auto_pause) /* something was playing */
942 {
943 spool_nplaying--;
944 }
945
946 if (sp->si)
947 {
948 sound_close(sp->si);
949 sp->si = NULL;
950 }
951 buffer_dealloc(sp->curr_buffer, 0);
952 buffer_dealloc(sp->next_buffer, 0);
953 sp->curr_buffer = NULL;
954 sp->next_buffer = NULL;
955 sp->offset = 0;
956
957 if (sp->state == SPOOL_SKIP)
958 {
959 /* The current sound is now `done'. */
960 connection_notify(0, NOTIFY_DONE, sp);
961
962 if (sp->skip_count == 0)
963 {
964 sp->curr_count++; /* "play it again Sam" */
965 }
966 else
967 {
968 sp->curr_count = 1; /* end the current sound */
969
970 /*
971 * Skip to the next sound.
972 */
973
974 /* Repeat the current sound if more than 1 second
975 has been played. Otherwise, move to the
976 previous sound. */
977 if (sp->skip_count < 0
978 && sp->sample_index > rplay_audio_sample_rate)
979 {
980 sp->skip_count++;
981 }
982 sp->curr_sound += sp->skip_count - 1;
983
984 /* Skipping before the first sound will always
985 skip to the first sound. */
986 if (sp->curr_sound < 0)
987 {
988 sp->curr_sound = -1;
989 }
990 /* Skipping after the last sound will always
991 skip to the last sound. */
992 else if (sp->curr_sound == sp->rp->nsounds)
993 {
994 sp->curr_sound = sp->rp->nsounds - 1;
995 }
996 }
997 }
998
999 if (sp->curr_count > 1)
1000 {
1001 sp->curr_count--;
1002 }
1003 else if (sp->curr_count)
1004 {
1005 /* Save a pointer to the sound that was playing. */
1006 s = sp->sound[sp->curr_sound];
1007
1008 sp->curr_sound++;
1009 if (sp->curr_sound == sp->rp->nsounds)
1010 {
1011 if (sp->list_count > 1)
1012 {
1013 sp->list_count--;
1014 sp->curr_attrs = sp->rp->attrs;
1015 sp->curr_sound = 0;
1016 }
1017 else if (sp->list_count == 0)
1018 {
1019 sp->curr_attrs = sp->rp->attrs;
1020 sp->curr_sound = 0;
1021 }
1022 else
1023 /* The spool entry is finished. */
1024 {
1025 if (sp->state != SPOOL_SKIP) /* already sent `done' */
1026 {
1027 connection_notify(0, NOTIFY_DONE, sp);
1028 }
1029
1030 spool_destroy(sp);
1031 continue;
1032 }
1033 }
1034 else
1035 {
1036 /* Update `curr_attrs' to point to the next
1037 set of attributes. */
1038 if (sp->state == SPOOL_SKIP)
1039 {
1040 /* This does a linear search. Bad, bad, bad */
1041 int i;
1042 sp->curr_attrs = sp->rp->attrs;
1043 for (i = 0; i < sp->curr_sound; i++)
1044 {
1045 sp->curr_attrs = sp->curr_attrs->next;
1046 }
1047 sp->skip_count = 0;
1048 }
1049 else
1050 {
1051 sp->curr_attrs = sp->curr_attrs->next;
1052 }
1053 }
1054
1055 sp->curr_count = sp->curr_attrs->count;
1056 }
1057 spool_play(sp);
1058 /* break; */
1059
1060 case SPOOL_PLAY:
1061 if (!sp->curr_buffer ||
1062 (!sp->next_buffer && sp->si && !sp->si->eof))
1063 {
1064 s = sp->sound[sp->curr_sound];
1065
1066 #ifdef HAVE_OSS
1067 b = buffer_alloc(MIN(s->sample_rate * s->output_sample_size, SOUND_FILL_SIZE),
1068 BUFFER_REUSE);
1069 #else
1070 b = buffer_alloc(MIN(MAX(curr_bufsize * curr_rate,
1071 s->sample_rate * s->output_sample_size),
1072 SOUND_FILL_SIZE),
1073 BUFFER_REUSE);
1074 #endif
1075
1076 n = sound_fill(sp->si, b, 0);
1077
1078 if (n <= 0)
1079 {
1080 buffer_dealloc(b, 1);
1081
1082 if (s->needs_helper)
1083 {
1084 if (!sp->curr_buffer && !sp->next_buffer)
1085 {
1086 if (sp->si->eof)
1087 {
1088 sound_close(sp->si);
1089 sp->si = NULL; /* rplayd_write will see this as a dead sound */
1090 spool_needs_update++;
1091 }
1092 else
1093 {
1094 spool_flow_pause(sp);
1095 }
1096 }
1097 }
1098 else if (s->type == SOUND_FILE)
1099 {
1100 sound_close(sp->si);
1101 sp->si = NULL; /* rplayd_write will see this as a dead sound */
1102 }
1103 else if (s->type == SOUND_FLOW)
1104 {
1105 if (!sp->curr_buffer && !sp->next_buffer)
1106 {
1107 if (sp->si->eof)
1108 {
1109 sound_close(sp->si);
1110 sp->si = NULL; /* rplayd_write will see this as a dead sound */
1111 spool_needs_update++;
1112 }
1113 else
1114 {
1115 spool_flow_pause(sp);
1116 }
1117 }
1118 }
1119 #ifdef HAVE_CDROM
1120 else if (s->type == SOUND_CDROM)
1121 {
1122 if (!sp->curr_buffer && !sp->next_buffer)
1123 {
1124 if (sp->si->eof)
1125 {
1126 sound_close(sp->si);
1127 sp->si = NULL; /* rplayd_write will see this as a dead sound */
1128 spool_needs_update++;
1129 }
1130 else
1131 {
1132 spool_flow_pause(sp);
1133 }
1134 }
1135 }
1136 #endif /* HAVE_CDROM */
1137 }
1138 else
1139 {
1140 sp->time = now;
1141
1142 if (!sp->curr_buffer)
1143 {
1144 timer_block();
1145 sp->ptr = (unsigned char *) b->buf + sp->offset;
1146 sp->ptr_end = (unsigned char *) b->buf + b->nbytes;
1147 sp->curr_buffer = b;
1148 timer_unblock();
1149 }
1150 else
1151 {
1152 sp->next_buffer = b;
1153 }
1154
1155 #ifdef HAVE_HELPERS
1156 if (s->needs_helper
1157 && ((sp->si->water_mark - sp->si->offset) < sp->si->low_water_mark))
1158 {
1159 FD_SET(sp->si->fd, &read_mask);
1160 }
1161 #else
1162 if (0)
1163 {
1164 /* hack */
1165 }
1166 #endif /* HAVE_HELPERS */
1167 else if (s->type == SOUND_FLOW
1168 && ((sp->si->water_mark - sp->si->offset) < sp->si->low_water_mark))
1169 {
1170 connection_notify(0, NOTIFY_FLOW, sp);
1171 connection_flow_continue(sp);
1172 }
1173 #ifdef HAVE_CDROM
1174 else if (s->type == SOUND_CDROM
1175 && ((sp->si->water_mark - sp->si->offset) < sp->si->low_water_mark))
1176 {
1177 FD_SET(sp->si->fd, &read_mask);
1178 }
1179 #endif /* HAVE_CDROM */
1180 }
1181 }
1182 break;
1183 }
1184 }
1185 }
1186
1187 #ifdef __STDC__
1188 int
1189 spool_process(char *buf, int nbytes)
1190 #else
1191 int
1192 spool_process(buf, nbytes)
1193 char *buf;
1194 int nbytes;
1195 #endif
1196 {
1197 SPOOL *sp, *sp_next;
1198 int number_playing = 0;
1199 int nsamples;
1200
1201 /* Determine the number of samples that need to be processed. */
1202 nsamples = nbytes / ((rplay_audio_precision >> 3) * rplay_audio_channels);
1203
1204 zero_native(rplay_audio_buf, nsamples, rplay_audio_channels);
1205
1206 for (sp = spool; sp; sp = sp_next)
1207 {
1208 sp_next = sp->next;
1209
1210 if (sp->state == SPOOL_PLAY)
1211 {
1212 number_playing++;
1213
1214 /* See if the end of sound has been reached. */
1215 if (!(*sp->to_native) (sp, rplay_audio_buf, nsamples, rplay_audio_channels))
1216 {
1217 sp->state = SPOOL_NEXT;
1218 spool_needs_update++;
1219 }
1220 /* See if more buffers need to be filled by `spool_update'. */
1221 else if (sp->curr_buffer == NULL ||
1222 (sp->si && !sp->si->eof && !sp->next_buffer))
1223 {
1224 spool_needs_update++;
1225 }
1226
1227 sp->notify_position++;
1228 }
1229 }
1230
1231 if (number_playing)
1232 {
1233 #ifdef TEST_FLANGE
1234 flange(rplay_audio_buf, nsamples, rplay_audio_channels);
1235 #endif
1236
1237 #ifdef FAKE_VOLUME
1238 fake_volume(rplay_audio_buf, nsamples, rplay_audio_channels);
1239 #endif
1240
1241 /* Only calculate left and right levels if there's a connection
1242 that needs the information. */
1243 if (connection_want_level_notify)
1244 {
1245 level(rplay_audio_buf, nsamples, rplay_audio_channels);
1246 connection_level_notify++;
1247 }
1248
1249 /* Convert rplay_audio_buf to the audio device format. */
1250 (*native_table[rplay_audio_format].from_native[RPLAY_AUDIO_BYTE_ORDER - 1])
1251 (rplay_audio_buf, nsamples, rplay_audio_channels);
1252
1253 rplay_audio_size = nbytes;
1254 }
1255 }
1256
1257 #ifdef __STDC__
1258 SPOOL *
1259 spool_find(int id)
1260 #else
1261 SPOOL *
1262 spool_find(id)
1263 int id;
1264 #endif
1265 {
1266 SPOOL *sp;
1267
1268 for (sp = spool; sp; sp = sp->next)
1269 {
1270 if (sp->id == id)
1271 {
1272 break;
1273 }
1274 }
1275
1276 return sp;
1277 }
1278
1279 /* This routine can be used to remove idle flows from the spool. */
1280 #ifdef __STDC__
1281 void
1282 spool_cleanup(void)
1283 #else
1284 void
1285 spool_cleanup()
1286 #endif
1287 {
1288 SPOOL *sp;
1289 int flows_cleaned = 0;
1290
1291 for (sp = spool; sp; sp = sp->next)
1292 {
1293 if (sp->state == SPOOL_WAIT
1294 && sp->sound[sp->curr_sound]->type == SOUND_FLOW)
1295 {
1296 spool_done(sp);
1297 flows_cleaned++;
1298 }
1299 }
1300
1301 report(REPORT_DEBUG, "cleaning up the spool - %d flows\n",
1302 flows_cleaned);
1303 }
1304
1305 #ifdef __STDC__
1306 void
1307 spool_set_count(SPOOL *sp, int count)
1308 #else
1309 void
1310 spool_set_count(sp, count)
1311 SPOOL *sp;
1312 int count;
1313 #endif
1314 {
1315 if (sp)
1316 {
1317 sp->curr_count = count;
1318 sp->curr_attrs->count = count;
1319 }
1320 }
1321
1322 #ifdef __STDC__
1323 void
1324 spool_set_list_count(SPOOL *sp, int count)
1325 #else
1326 void
1327 spool_set_list_count(sp, count)
1328 SPOOL *sp;
1329 int count;
1330 #endif
1331 {
1332 if (sp)
1333 {
1334 sp->list_count = count;
1335 sp->rp->count = count;
1336 }
1337 }
1338
1339 #ifdef __STDC__
1340 void
1341 spool_set_priority(SPOOL *sp, int priority)
1342 #else
1343 void
1344 spool_set_priority(sp, priority)
1345 SPOOL *sp;
1346 int priority;
1347 #endif
1348 {
1349 if (sp)
1350 {
1351 sp->rp->priority = priority;
1352 spool_setprio();
1353 }
1354 }
1355
1356 #ifdef __STDC__
1357 void
1358 spool_set_sample_rate(SPOOL *sp, int sample_rate)
1359 #else
1360 void
1361 spool_set_sample_rate(sp, sample_rate)
1362 SPOOL *sp;
1363 int sample_rate;
1364 #endif
1365 {
1366 if (sp)
1367 {
1368 sp->sample_rate = sample_rate > 0 ? sample_rate : sp->sound[sp->curr_sound]->sample_rate;
1369 sp->curr_attrs->sample_rate = sample_rate;
1370 sp->sample_factor = (double) sp->sample_rate / (double) rplay_audio_sample_rate;
1371
1372 if (rplay_audio_match)
1373 {
1374 rplayd_audio_match(sp);
1375 }
1376 }
1377 }
1378
1379 #ifdef __STDC__
1380 void
1381 spool_set_volume(SPOOL *sp, int volume)
1382 #else
1383 void
1384 spool_set_volume(sp, volume)
1385 SPOOL *sp;
1386 int volume;
1387 #endif
1388 {
1389 if (sp)
1390 {
1391 sp->curr_attrs->volume = volume;
1392 }
1393 }
1394
1395 #ifdef __STDC__
1396 void
1397 spool_set_client_data(SPOOL *sp, char *client_data)
1398 #else
1399 void
1400 spool_set_client_data(sp, client_data)
1401 SPOOL *sp;
1402 char *client_data;
1403 #endif
1404 {
1405 if (sp)
1406 {
1407 if (*sp->curr_attrs->client_data)
1408 {
1409 free((char *) sp->curr_attrs->client_data);
1410 }
1411
1412 sp->curr_attrs->client_data = strdup(client_data);
1413 }
1414 }
1415
1416 #ifdef __STDC__
1417 SPOOL *
1418 spool_find_pid(int pid)
1419 #else
1420 SPOOL *
1421 spool_find_pid(pid)
1422 int pid;
1423 #endif
1424 {
1425 SPOOL *sp;
1426
1427 #if defined(HAVE_CDROM) || defined(HAVE_HELPERS)
1428 for (sp = spool; sp; sp = sp->next)
1429 {
1430 if (sp->si && sp->si->pid == pid)
1431 {
1432 return sp;
1433 }
1434 }
1435 #endif
1436
1437 return NULL;
1438 }