Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * buffer.c |
||
3 | * |
||
4 | * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> |
||
5 | * Mon Apr 14 03:53:18 MET DST 1997 |
||
6 | */ |
||
7 | |||
8 | #include "mpg123.h" |
||
9 | |||
10 | int outburst = MAXOUTBURST; |
||
11 | |||
12 | static int intflag = FALSE; |
||
13 | static int usr1flag = FALSE; |
||
14 | |||
15 | static void catch_interrupt (void) |
||
16 | { |
||
17 | intflag = TRUE; |
||
18 | } |
||
19 | |||
20 | static void catch_usr1 (void) |
||
21 | { |
||
22 | usr1flag = TRUE; |
||
23 | } |
||
24 | |||
25 | #ifndef OS2 |
||
26 | |||
27 | void buffer_loop(struct audio_info_struct *ai, sigset_t *oldsigset) |
||
28 | { |
||
29 | int bytes; |
||
30 | int my_fd = buffermem->fd[XF_READER]; |
||
31 | txfermem *xf = buffermem; |
||
32 | int done = FALSE; |
||
33 | |||
34 | catchsignal (SIGINT, catch_interrupt); |
||
35 | catchsignal (SIGUSR1, catch_usr1); |
||
36 | sigprocmask (SIG_SETMASK, oldsigset, NULL); |
||
37 | if (outmode == DECODE_AUDIO) { |
||
38 | if (audio_open(ai) < 0) { |
||
39 | perror("audio"); |
||
40 | exit(1); |
||
41 | } |
||
42 | /* audio_set_rate (ai); already done by audio_open() */ |
||
43 | } |
||
44 | |||
45 | for (;;) { |
||
46 | if (intflag) { |
||
47 | intflag = FALSE; |
||
48 | #ifdef SOLARIS |
||
49 | if (outmode == DECODE_AUDIO) |
||
50 | audio_queueflush (ai); |
||
51 | #endif |
||
52 | xf->readindex = xf->freeindex; |
||
53 | } |
||
54 | if (usr1flag) { |
||
55 | usr1flag = FALSE; |
||
56 | /* close and re-open in order to flush |
||
57 | * the device's internal buffer before |
||
58 | * changing the sample rate. [OF] |
||
59 | */ |
||
60 | if (outmode == DECODE_AUDIO) { |
||
61 | audio_close (ai); |
||
62 | memcpy (&ai->rate, xf->metadata, sizeof(ai->rate)); |
||
63 | if (audio_open(ai) < 0) { |
||
64 | perror("audio"); |
||
65 | exit(1); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | if (!(bytes = xfermem_get_usedspace(xf))) { |
||
70 | if (done) |
||
71 | break; |
||
72 | if (xfermem_block(XF_READER, xf) != XF_CMD_WAKEUP) |
||
73 | done = TRUE; |
||
74 | continue; |
||
75 | } |
||
76 | if (bytes > xf->size - xf->readindex) |
||
77 | bytes = xf->size - xf->readindex; |
||
78 | if (bytes > outburst) |
||
79 | bytes = outburst; |
||
80 | |||
81 | if (outmode == DECODE_STDOUT) |
||
82 | bytes = write(1, xf->data + xf->readindex, bytes); |
||
83 | else if (outmode == DECODE_AUDIO) |
||
84 | bytes = audio_play_samples(ai, |
||
85 | (unsigned char *) (xf->data + xf->readindex), bytes); |
||
86 | |||
87 | xf->readindex = (xf->readindex + bytes) % xf->size; |
||
88 | if (xf->wakeme[XF_WRITER]) |
||
89 | xfermem_putcmd(my_fd, XF_CMD_WAKEUP); |
||
90 | } |
||
91 | |||
92 | if (outmode == DECODE_AUDIO) |
||
93 | audio_close (ai); |
||
94 | } |
||
95 | |||
96 | #endif |
||
97 | |||
98 | /* EOF */ |