Blame


1 0ea0da1e 2022-02-23 op /*
2 0ea0da1e 2022-02-23 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 0ea0da1e 2022-02-23 op *
4 0ea0da1e 2022-02-23 op * Permission to use, copy, modify, and distribute this software for any
5 0ea0da1e 2022-02-23 op * purpose with or without fee is hereby granted, provided that the above
6 0ea0da1e 2022-02-23 op * copyright notice and this permission notice appear in all copies.
7 0ea0da1e 2022-02-23 op *
8 0ea0da1e 2022-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0ea0da1e 2022-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0ea0da1e 2022-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0ea0da1e 2022-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0ea0da1e 2022-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0ea0da1e 2022-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0ea0da1e 2022-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 0ea0da1e 2022-02-23 op */
16 0ea0da1e 2022-02-23 op
17 0ea0da1e 2022-02-23 op #include <sys/mman.h>
18 0ea0da1e 2022-02-23 op #include <sys/stat.h>
19 0ea0da1e 2022-02-23 op #include <sys/types.h>
20 0ea0da1e 2022-02-23 op #include <sys/queue.h>
21 0ea0da1e 2022-02-23 op #include <sys/uio.h>
22 0ea0da1e 2022-02-23 op
23 0ea0da1e 2022-02-23 op #include <err.h>
24 0ea0da1e 2022-02-23 op #include <event.h>
25 0ea0da1e 2022-02-23 op #include <limits.h>
26 0ea0da1e 2022-02-23 op #include <imsg.h>
27 0ea0da1e 2022-02-23 op #include <unistd.h>
28 0ea0da1e 2022-02-23 op
29 0ea0da1e 2022-02-23 op #include <mpg123.h>
30 0ea0da1e 2022-02-23 op
31 0ea0da1e 2022-02-23 op #include "amused.h"
32 0ea0da1e 2022-02-23 op #include "log.h"
33 0ea0da1e 2022-02-23 op
34 0ea0da1e 2022-02-23 op static int
35 0ea0da1e 2022-02-23 op setup(mpg123_handle *mh)
36 0ea0da1e 2022-02-23 op {
37 0ea0da1e 2022-02-23 op long rate;
38 0ea0da1e 2022-02-23 op int chan, enc;
39 0ea0da1e 2022-02-23 op
40 0ea0da1e 2022-02-23 op if (mpg123_getformat(mh, &rate, &chan, &enc) != MPG123_OK) {
41 0ea0da1e 2022-02-23 op log_warnx("mpg123_getformat failed");
42 0ea0da1e 2022-02-23 op return 0;
43 0ea0da1e 2022-02-23 op }
44 0ea0da1e 2022-02-23 op
45 0ea0da1e 2022-02-23 op if (player_setup(mpg123_encsize(enc) * 8, rate, chan) == -1)
46 1fb06c31 2022-06-10 op err(1, "player_setup");
47 0ea0da1e 2022-02-23 op
48 0ea0da1e 2022-02-23 op return 1;
49 0ea0da1e 2022-02-23 op }
50 0ea0da1e 2022-02-23 op
51 0da0ad46 2022-03-09 op int
52 17ef54d6 2022-06-22 op play_mp3(int fd, const char **errstr)
53 0ea0da1e 2022-02-23 op {
54 0ea0da1e 2022-02-23 op static char buf[4096];
55 0ea0da1e 2022-02-23 op size_t len;
56 0ea0da1e 2022-02-23 op mpg123_handle *mh;
57 791d3db3 2022-07-09 op int64_t seek = -1;
58 0da0ad46 2022-03-09 op int err, ret = -1;
59 0ea0da1e 2022-02-23 op
60 0ea0da1e 2022-02-23 op if ((mh = mpg123_new(NULL, NULL)) == NULL)
61 0ea0da1e 2022-02-23 op fatal("mpg123_new");
62 0ea0da1e 2022-02-23 op
63 0ea0da1e 2022-02-23 op if (mpg123_open_fd(mh, fd) != MPG123_OK) {
64 17ef54d6 2022-06-22 op *errstr = "mpg123_open_fd failed";
65 0ea0da1e 2022-02-23 op close(fd);
66 0da0ad46 2022-03-09 op return -1;
67 0ea0da1e 2022-02-23 op }
68 0ea0da1e 2022-02-23 op
69 0ea0da1e 2022-02-23 op if (!setup(mh))
70 0ea0da1e 2022-02-23 op goto done;
71 0ea0da1e 2022-02-23 op
72 ff06024f 2022-07-08 op player_setduration(mpg123_length(mh));
73 ff06024f 2022-07-08 op
74 0ea0da1e 2022-02-23 op for (;;) {
75 791d3db3 2022-07-09 op if (seek != -1) {
76 791d3db3 2022-07-09 op seek = mpg123_seek(mh, seek, SEEK_SET);
77 791d3db3 2022-07-09 op if (seek < 0) {
78 791d3db3 2022-07-09 op ret = 0;
79 791d3db3 2022-07-09 op break;
80 791d3db3 2022-07-09 op }
81 791d3db3 2022-07-09 op player_setpos(seek);
82 791d3db3 2022-07-09 op }
83 791d3db3 2022-07-09 op
84 0ea0da1e 2022-02-23 op err = mpg123_read(mh, buf, sizeof(buf), &len);
85 0ea0da1e 2022-02-23 op switch (err) {
86 0ea0da1e 2022-02-23 op case MPG123_DONE:
87 0da0ad46 2022-03-09 op ret = 0;
88 0ea0da1e 2022-02-23 op goto done;
89 0ea0da1e 2022-02-23 op case MPG123_NEW_FORMAT:
90 0ea0da1e 2022-02-23 op if (!setup(mh))
91 0ea0da1e 2022-02-23 op goto done;
92 0ea0da1e 2022-02-23 op break;
93 0ea0da1e 2022-02-23 op case MPG123_OK:
94 791d3db3 2022-07-09 op if (!play(buf, len, &seek)) {
95 0da0ad46 2022-03-09 op ret = 1;
96 2139c525 2022-03-09 op goto done;
97 0da0ad46 2022-03-09 op }
98 0ea0da1e 2022-02-23 op break;
99 0ea0da1e 2022-02-23 op default:
100 5f42a6c6 2022-05-23 op log_warnx("error decoding mp3, "
101 5f42a6c6 2022-05-23 op "continuing nevertheless");
102 5f42a6c6 2022-05-23 op break;
103 0ea0da1e 2022-02-23 op }
104 0ea0da1e 2022-02-23 op }
105 0ea0da1e 2022-02-23 op
106 0ea0da1e 2022-02-23 op done:
107 0ea0da1e 2022-02-23 op mpg123_delete(mh);
108 0ea0da1e 2022-02-23 op close(fd);
109 0da0ad46 2022-03-09 op return ret;
110 0ea0da1e 2022-02-23 op }