Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <err.h>
22 #include <event.h>
23 #include <fcntl.h>
24 #include <math.h>
25 #include <inttypes.h>
26 #include <limits.h>
27 #include <sndio.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <imsg.h>
31 #include <unistd.h>
33 #include <vorbis/codec.h>
34 #include <vorbis/vorbisfile.h>
36 #include "amused.h"
37 #include "log.h"
39 #ifndef nitems
40 #define nitems(x) (sizeof(x)/sizeof(x[0]))
41 #endif
43 void
44 play_oggvorbis(int fd)
45 {
46 static uint8_t pcmout[4096];
47 FILE *f;
48 OggVorbis_File vf;
49 int current_section, eof = 0;
51 if ((f = fdopen(fd, "r")) == NULL)
52 err(1, "fdopen");
54 if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
55 log_warnx("input is not an Ogg bitstream");
56 goto end;
57 }
59 {
60 char **ptr;
61 vorbis_info *vi;
63 vi = ov_info(&vf, -1);
64 for (ptr = ov_comment(&vf, -1)->user_comments; *ptr; ++ptr)
65 printf("%s\n", *ptr);
67 printf("bitstream is %d channel, %ldHz\n", vi->channels, vi->rate);
69 if (player_setup(vi->rate, vi->channels) == -1)
70 err(1, "player_setrate");
71 }
73 while (!eof) {
74 long ret;
76 if (player_shouldstop())
77 break;
79 ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
80 &current_section);
81 if (ret == 0)
82 eof = 1;
83 else if (ret < 0)
84 log_warnx("non-fatal error in the stream %ld", ret);
85 else {
86 /* TODO: deal with sample rate changes */
87 sio_write(hdl, pcmout, ret);
88 }
89 }
91 ov_clear(&vf);
93 end:
94 fclose(f);
95 }