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