~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/mus_player.c -rw-r--r-- 7.9 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <fcntl.h>
#include <getopt.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <ao/ao.h>

#include "flac.h"
#include "ipc.h"
#include "log.h"
#include "misc.h"
#include "status.h"
#include "tag.h"

#define LOG_QUIT(...)							\
	do											\
	{											\
		log_append(LOG_ERR, __VA_ARGS__);		\
		goto QUIT;								\
	}											\
	while (0)

#define PCM_BUF_SIZE (BUFSIZ << 2)
static char pcm_buf[PCM_BUF_SIZE];


static int ao_init(const char *ao_dri_name)
{
	ao_initialize();
	int ret = ao_dri_name ? ao_driver_id(ao_dri_name) : ao_default_driver_id();
	ao_info *aoinfo = ao_driver_info(ret);
	log_append(LOG_INFO, "Using driver %s", aoinfo->name);
	return ret;
}

static void decoder_thread_cancel(Decoder *dec, Action action, int pipefd)
{
	dec->cancel_flag = true;
	if (action != ACTION_TRACK_NEXT)
	{
		if (dec->next_fd != -1)
			xclose(dec->next_fd);
		dec->next_fd = -1;
	}
	/* Read some decoded PCM so the decode thread can read its cancellation
	 * flag */
	size_t dummy = read(pipefd, pcm_buf, PCM_BUF_SIZE); (void)dummy;
}

static inline bool aofmt_cmp(const ao_sample_format *a, const ao_sample_format *b)
{
	return a->bits == b->bits && a->rate == b->rate &&
		a->channels == b->channels && a->byte_format == b->byte_format;
}

_Noreturn void usage(int exit_status)
{
	printf(
		"NAME\n"
		"    %s - Audio player\n"
		"\n"
		"SYNOPSIS\n"
		"    %s [-hqQv] [-d AO_DRIVER] [-i SOCKET] [-n START_POS]\n"
		"    %*s [-r album|track|none] [-s CMD] FILE...\n"
		"\n"
		"DESCRIPTION\n"
		"    Simple audio player supporting 16 bits mono and stereo files.\n"
		"\n"
		"    This binary was compiled with support for the following formats:\n",
		PROG_NAME, PROG_NAME, (int)strlen(PROG_NAME), "");
#ifdef USE_FLAC
	printf("        %s\n", audio_format_str[AUDIO_FORMAT_FLAC]);
#endif
#ifdef USE_OGG_VORBIS
	printf("        %s\n", audio_format_str[AUDIO_FORMAT_OGG_VORBIS]);
#endif
#ifdef USE_OGG_OPUS
	printf("        %s\n", audio_format_str[AUDIO_FORMAT_OGG_OPUS]);
#endif
	printf(
		"\n"
		"OPTIONS\n"
		"    -h\n"
		"        Print this notice and exit.\n"
		"\n"
		"    -d AO_DRIVER\n"
		"        Force the libao driver to AO_DRIVER.\n"
		"\n"
		"    -i SOCKET\n"
		"        Enable IPC with SOCKET as the UNIX domain socket used to "
		"communicate. \n"
		"        See IPC.txt for more information.\n"
		"\n"
		"    -n START_POS\n"
		"        Start playback at track number START_POS.\n"
		"\n"
		"    -q\n"
		"        Suppress all output.\n"
		"\n"
		"    -r album|track|none\n"
		"        Choose the ReplayGain type to use; \"none\" will disable "
		"ReplayGain.\n"
		"        Defaults to \"album\".\n"
		"\n"
		"    -s CMD\n"
		"        Execute CMD via sh(1) with the output of the STATUS "
		"command as stdin\n"
		"        for each player status and track change.\n"
		"\n"
		"    -v\n"
		"        Print version, supported formats and exit.\n"
		"\n");
	exit(exit_status);
}


int main(int argc, char **argv)
{
	signals_nointerrupt();
	log_init(LOG_TYPE_STDERR, PROG_NAME);

	ReplayGain_type rgtype = ALBUM_GAIN;
	char *ao_dri_name = NULL, *notify_cmd = NULL, *ipc_sock = NULL;
	int start_pos = 0;

	for (int opt; (opt = getopt(argc, argv, "hd:i:n:qr:s:v")) != -1; )
	{
		switch (opt)
		{
			case 'h':
				usage(EXIT_SUCCESS);

			case 'd':
				ao_dri_name = optarg;
				break;

			case 'i':
				ipc_sock = optarg;
				break;

			case 'n':
				start_pos = xstrtoi(optarg) - 1;
				if (start_pos < 0)
					LOG_DIE("Option -n requires an argument > 0");
				break;

			case 'q':
				/* Equivalent to quiet, since LOG_EMERG isn't used
				 * TODO: find a way to really silence log functions */
				log_setmask(LOG_MASK(LOG_EMERG));
				break;

			case 'r':
				if (!strcmp(optarg, "track"))
					rgtype = TRACK_GAIN;
				else if (!strcmp(optarg, "none"))
					rgtype = NO_GAIN;
				else if (strcmp(optarg, "album"))
					LOG_DIE("%s: unknown ReplayGain type", optarg);
				break;

			case 's':
				notify_cmd = optarg;
				break;

			case 'v':
				printf("Git commit: %s\n", PROG_VERSION);
				printf("Supported formats:\n");
#ifdef USE_FLAC
				printf("    %s\n", audio_format_str[AUDIO_FORMAT_FLAC]);
#endif
#ifdef USE_OGG_VORBIS
				printf("    %s\n", audio_format_str[AUDIO_FORMAT_OGG_VORBIS]);
#endif
#ifdef USE_OGG_OPUS
				printf("    %s\n", audio_format_str[AUDIO_FORMAT_OGG_OPUS]);
#endif
				exit(EXIT_SUCCESS);

			default:
				usage(EXIT_FAILURE);
		}
	}
	if (argc - optind < 1)
		usage(EXIT_FAILURE);

	ao_sample_format ao_fmt_prev;
	ao_device *ao_dev = NULL;
	int ao_dri = ao_init(ao_dri_name);
	Player_state pstate =  STATE_PLAYING;
	Track_info tinfo = {.format = {0, 0, 0, AO_FMT_NATIVE, NULL}};
	Decoder dec;
	dec.next_fd = -1;

	Ipc_ctx *ipc_ctx = NULL;
	if (ipc_sock)
	{
		ipc_ctx = ipc_thread_spawn(ipc_sock);
		ipc_ctx->tinfo = &tinfo;
		ipc_ctx->pstate = &pstate;
	}

	for (int track = optind + start_pos; track < argc;)
	{
		tinfo.path = argv[track];
		const char *next = (track == argc ? NULL : argv[track + 1]);

		int pipefd[2];
		if (pipe(pipefd) != 0)
			LOG_QUIT("Creating pipe: %s", strerror(errno));
		/* To avoid exec_status_cmd() from blocking the audio pipe closure */
		fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
		fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);

		if (!decoder_init(&dec, pipefd[1], &tinfo, rgtype, next))
		{
			xclose(pipefd[0]);
			++track;
			continue;
		}
		log_append(LOG_INFO, "Playing %s", argv[track]);

		/* Open the audio device at first or reopen it if format changed */
		if (!ao_dev || !aofmt_cmp(&tinfo.format, &ao_fmt_prev))
		{
			if (ao_dev)
			{
				log_append(LOG_INFO, "Reopening audio device");
				ao_close(ao_dev);
			}
			if ((ao_dev = ao_open_live(ao_dri, &tinfo.format, NULL)) == NULL)
				LOG_QUIT("Couldn't open audio device");
		}
		ao_fmt_prev = tinfo.format;

		if (!decoder_spawn_thread(&dec))
			goto QUIT;

		/* Signal IPC thread that track change is finished */
		if (ipc_sock)
			pthread_mutex_unlock(&ipc_ctx->track_lock);

		status_exec_notify_cmd(notify_cmd, pstate, &tinfo); /* Initial status */

		ssize_t ret;
		while ((ret = read(pipefd[0], pcm_buf, PCM_BUF_SIZE)) > 0)
		{
			if (ipc_sock)
			{
				do
				{
					const Action action = ipc_ctx->action;
					switch (action)
					{
						case ACTION_PAUSE:
							pstate = STATE_PAUSED;
							status_exec_notify_cmd(notify_cmd, pstate, &tinfo);
							/* Wait for wakeup from IPC thread */
							pthread_mutex_lock(&ipc_ctx->pause_lock);
							if (ipc_ctx->action != ACTION_PLAY)
							{
								pthread_cond_wait(&ipc_ctx->pause_cond,
									&ipc_ctx->pause_lock);
							}
							pthread_mutex_unlock(&ipc_ctx->pause_lock);
							if (ipc_ctx->action == ACTION_PLAY)
							{
								pstate = STATE_PLAYING;
								status_exec_notify_cmd(notify_cmd, pstate,
									&tinfo);
							}
							break;

						case ACTION_QUIT:
							decoder_thread_cancel(&dec, action, pipefd[0]);
							goto QUIT;

						case ACTION_TRACK_NEXT:
							++track;
							decoder_thread_cancel(&dec, action, pipefd[0]);
							goto TRACK_CHANGE;

						case ACTION_TRACK_PREV:
							if (track > optind)
								--track;
							decoder_thread_cancel(&dec, action, pipefd[0]);
							goto TRACK_CHANGE;

						case ACTION_TRACK_REWIND:
							decoder_thread_cancel(&dec, action, pipefd[0]);
							goto TRACK_CHANGE;

						case ACTION_TRACK_FIRST:
							track = optind;
							decoder_thread_cancel(&dec, action, pipefd[0]);
							goto TRACK_CHANGE;

						case ACTION_NONE:
						case ACTION_PLAY:
							;
					}
				}
				while (pstate != STATE_PLAYING);
			}
			ao_play(ao_dev, pcm_buf, ret);
		}
		if (ret == -1)
			LOG_QUIT("Reading pipe: %s", strerror(errno));
		++track;
	TRACK_CHANGE:
		pthread_join(dec.tid, NULL);
		decoder_clear(&dec);
		xclose(pipefd[0]);
		/* Keep the player paused even when changing tracks */
		if (ipc_sock)
		{
			ipc_ctx->action =
				(pstate == STATE_PAUSED ? ACTION_PAUSE : ACTION_NONE);
		}
	}

QUIT:
	if (ipc_sock)
		ipc_thread_join(ipc_ctx);
	if (ao_dev)
		ao_close(ao_dev);
	ao_shutdown();
	log_free();
	return EXIT_SUCCESS;
}