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 <stdio.h>
28 #include <stdint.h>
29 #include <imsg.h>
30 #include <unistd.h>
32 #include <vorbis/codec.h>
33 #include <vorbis/vorbisfile.h>
35 #include "amused.h"
36 #include "log.h"
38 #ifndef nitems
39 #define nitems(x) (sizeof(x)/sizeof(x[0]))
40 #endif
42 int
43 play_oggvorbis(int fd, const char **errstr)
44 {
45 static uint8_t pcmout[4096];
46 FILE *f;
47 OggVorbis_File vf;
48 vorbis_info *vi;
49 int64_t seek = -1;
50 int current_section, eof = 0, ret = 0;
52 if ((f = fdopen(fd, "r")) == NULL)
53 err(1, "fdopen");
55 if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
56 *errstr = "input is not an Ogg bitstream";
57 ret = -1;
58 goto end;
59 }
61 /*
62 * we could extract some tags by looping over the NULL
63 * terminated array returned by ov_comment(&vf, -1), see
64 * previous revision of this file.
65 */
66 vi = ov_info(&vf, -1);
67 if (player_setup(16, vi->rate, vi->channels) == -1)
68 err(1, "player_setup");
70 player_setduration(ov_time_total(&vf, -1) * vi->rate);
72 while (!eof) {
73 long r;
75 if (seek != -1) {
76 r = ov_pcm_seek(&vf, seek);
77 if (r != 0)
78 break;
79 player_setpos(seek);
80 }
82 r = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
83 &current_section);
84 if (r == 0)
85 eof = 1;
86 else if (r > 0) {
87 /* TODO: deal with sample rate changes */
88 if (!play(pcmout, r, &seek)) {
89 ret = 1;
90 break;
91 }
92 }
93 }
95 ov_clear(&vf);
97 end:
98 fclose(f);
99 return ret;