Allow playing internal file when player specified

Use "INTERNAL_FILE" for filename to specify playing the internal file.
This commit is contained in:
Stephen Seo 2024-06-14 21:48:47 +09:00
parent 7421872586
commit 889689a0e7
2 changed files with 61 additions and 18 deletions

View file

@ -4,6 +4,9 @@
More tweaks to README. More tweaks to README.
Allow playing internal file even if custom player command is specified.
Do this by specifying "INTERNAL_FILE" as the filename parameter.
## Version 1.2 ## Version 1.2
Minor tweaks to help text, README. Minor tweaks to help text, README.

View file

@ -26,13 +26,22 @@ void interval_notification_handle_signal(int sig) {
int play_jingle_from_file(char *player, int play_jingle_from_file(char *player,
char **player_args, char **player_args,
unsigned int args_count, unsigned int args_count,
char *jingle_filename) { char *jingle_filename,
int jingle_fd = open(jingle_filename, O_RDONLY); int use_internal_file_specified) {
if (jingle_fd == -1) { int pipe_filedes[2];
puts("ERROR: Failed to play jingle: Failed to open file!"); if (use_internal_file_specified == 0) {
return 2; int jingle_fd = open(jingle_filename, O_RDONLY);
if (jingle_fd == -1) {
puts("ERROR: Failed to play jingle: Failed to open file!");
return 2;
}
close(jingle_fd);
} else {
if (pipe(pipe_filedes) != 0) {
puts("ERROR: Failed to play jingle: Failed to create pipe!");
return 7;
}
} }
close(jingle_fd);
switch(fork()) { switch(fork()) {
case -1: case -1:
@ -48,10 +57,23 @@ int play_jingle_from_file(char *player,
dup2(null_fd, 1); dup2(null_fd, 1);
dup2(null_fd, 2); dup2(null_fd, 2);
if (use_internal_file_specified != 0) {
// Handle pipe into stdin.
close(pipe_filedes[1]);
dup2(pipe_filedes[0], 0);
jingle_filename = "/dev/stdin";
}
// Exec. // Exec.
if (args_count == 0 && strcmp(player, "/usr/bin/mpv") == 0) { if (args_count == 0 && strcmp(player, "/usr/bin/mpv") == 0) {
if (execl(player, player, "--vid=no", jingle_filename, (char*)NULL) == -1) { if (execl(player,
printf("ERROR: Failed to play with player \"%s\" and jingle \"%s\"!\n", player, jingle_filename); player,
"--vid=no",
jingle_filename,
(char*)NULL)
== -1) {
printf("ERROR: Failed to play with player \"%s\" and jingle \"%s\"!\n",
player, jingle_filename);
close(null_fd); close(null_fd);
exit(5); exit(5);
} }
@ -71,7 +93,7 @@ int play_jingle_from_file(char *player,
printf("ERROR: Failed to play with player \"%s\"!\n", player); printf("ERROR: Failed to play with player \"%s\"!\n", player);
free(argvs); free(argvs);
close(null_fd); close(null_fd);
exit(4); exit(6);
} }
} }
@ -80,7 +102,13 @@ int play_jingle_from_file(char *player,
exit(0); exit(0);
break; break;
default: default:
// Intentionally left blank. if (use_internal_file_specified != 0) {
close(pipe_filedes[0]);
write(pipe_filedes[1],
interval_notification_mp3,
interval_notification_mp3_len);
close(pipe_filedes[1]);
}
break; break;
} }
@ -88,8 +116,7 @@ int play_jingle_from_file(char *player,
} }
/// Only supports playing with /usr/bin/mpv . /// Only supports playing with /usr/bin/mpv .
int play_jingle_from_memory(const char *jingle_data, int play_jingle_from_memory(void) {
int jingle_size) {
int pipe_filedes[2]; int pipe_filedes[2];
if (pipe(pipe_filedes) != 0) { if (pipe(pipe_filedes) != 0) {
puts("ERROR: Failed to play jingle: Failed to create pipe!"); puts("ERROR: Failed to play jingle: Failed to create pipe!");
@ -116,7 +143,12 @@ int play_jingle_from_memory(const char *jingle_data,
dup2(null_fd, 2); dup2(null_fd, 2);
// Exec. // Exec.
if (execl("/usr/bin/mpv", "/usr/bin/mpv", "--vid=no", "-", (char*)NULL) == -1) { if (execl("/usr/bin/mpv",
"/usr/bin/mpv",
"--vid=no",
"-",
(char*)NULL)
== -1) {
printf("ERROR: Failed to play!\n"); printf("ERROR: Failed to play!\n");
close(pipe_filedes[0]); close(pipe_filedes[0]);
close(null_fd); close(null_fd);
@ -130,7 +162,9 @@ int play_jingle_from_memory(const char *jingle_data,
break; break;
default: default:
close(pipe_filedes[0]); close(pipe_filedes[0]);
write(pipe_filedes[1], jingle_data, jingle_size); write(pipe_filedes[1],
interval_notification_mp3,
interval_notification_mp3_len);
close(pipe_filedes[1]); close(pipe_filedes[1]);
break; break;
} }
@ -156,6 +190,7 @@ int main(int argc, char **argv) {
char *player_name = NULL; char *player_name = NULL;
char **player_args = NULL; char **player_args = NULL;
unsigned int args_count = 0; unsigned int args_count = 0;
int use_internal_file_specified = 0;
if (argc == 1) { if (argc == 1) {
// Intentionally left blank. // Intentionally left blank.
@ -193,7 +228,12 @@ int main(int argc, char **argv) {
} }
if (file_name) { if (file_name) {
printf("Using file \"%s\"...\n", file_name); if (strcmp(file_name, "INTERNAL_FILE") == 0) {
puts("Using internal file...");
use_internal_file_specified = 1;
} else {
printf("Using file \"%s\"...\n", file_name);
}
} }
// Setup for loop // Setup for loop
@ -221,10 +261,10 @@ int main(int argc, char **argv) {
player_name : DEFAULT_FILE_PLAYER_PROGRAM, player_name : DEFAULT_FILE_PLAYER_PROGRAM,
player_args, player_args,
args_count, args_count,
file_name); file_name,
use_internal_file_specified);
} else { } else {
ret = play_jingle_from_memory(interval_notification_mp3, ret = play_jingle_from_memory();
interval_notification_mp3_len);
} }
if (ret != 0) { if (ret != 0) {
printf("ERROR: Failed to play jingle! (returned \"%i\")\n", ret); printf("ERROR: Failed to play jingle! (returned \"%i\")\n", ret);