Debian Codegraph @

rplayd/spool.c

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