Debian Codegraph @

rplayd/spool.c

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