From 957e046bf507f6c6139be64082185b12043e97a4 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 6 Jun 2021 11:16:17 +0200 Subject: Use socketpair instead of unix domain socket --- src/VideoPlayer.cpp | 63 ++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp index 8d9bf46..ed9bbc9 100644 --- a/src/VideoPlayer.cpp +++ b/src/VideoPlayer.cpp @@ -26,7 +26,6 @@ namespace QuickMedia { resume_playback(resume_playback), keep_open(keep_open), video_process_id(-1), - ipc_socket(-1), connected_to_ipc(false), connect_tries(0), find_window_tries(0), @@ -48,6 +47,8 @@ namespace QuickMedia { abort(); } fprintf(stderr, "Video max height: %d\n", monitor_height); + for(int i = 0; i < 2; ++i) + sockets[i] = -1; } VideoPlayer::~VideoPlayer() { @@ -55,12 +56,11 @@ namespace QuickMedia { kill(video_process_id, SIGTERM); wait_program(video_process_id); } - - if(ipc_socket != -1) - close(ipc_socket); - if(video_process_id != -1) - remove(ipc_server_path); + for(int i = 0; i < 2; ++i) { + if(sockets[i] != -1) + close(sockets[i]); + } if(display) XCloseDisplay(display); @@ -82,8 +82,10 @@ namespace QuickMedia { VideoPlayer::Error VideoPlayer::launch_video_process(const char *path, sf::WindowHandle _parent_window, const std::string &plugin_name, const std::string &) { parent_window = _parent_window; - if(!tmpnam(ipc_server_path)) { - perror("Failed to generate ipc file name"); + if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) { + sockets[0] = -1; + sockets[1] = -1; + perror("Failed to create socketpair to mpv"); return Error::FAIL_TO_GENERATE_IPC_FILENAME; } @@ -94,8 +96,8 @@ namespace QuickMedia { const std::string parent_window_str = std::to_string(parent_window); std::vector args; - std::string input_ipc_server_arg = "--input-ipc-server="; - input_ipc_server_arg += ipc_server_path; + std::string input_ipc_client_fd_arg = "--input-ipc-client=fd://"; + input_ipc_client_fd_arg += std::to_string(sockets[1]); std::string wid_arg = "--wid="; wid_arg += parent_window_str; @@ -119,7 +121,7 @@ namespace QuickMedia { // TODO: Resume playback if the last video played matches the first video played next time QuickMedia is launched args.insert(args.end(), { "mpv", - input_ipc_server_arg.c_str(), + input_ipc_client_fd_arg.c_str(), "--cursor-autohide=no", /* "--no-input-default-bindings", "--input-vo-keyboard=no", "--no-input-cursor", */ "--no-terminal", "--save-position-on-quit=yes", @@ -173,18 +175,9 @@ namespace QuickMedia { if(exec_program_async(args.data(), &video_process_id) != 0) return Error::FAIL_TO_LAUNCH_PROCESS; - fprintf(stderr, "mpv input ipc server: %s\n", ipc_server_path); - - if((ipc_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - perror("Failed to create socket for video player"); - return Error::FAIL_TO_CREATE_SOCKET; - } - - ipc_addr.sun_family = AF_UNIX; - strcpy(ipc_addr.sun_path, ipc_server_path); - - int flags = fcntl(ipc_socket, F_GETFL, 0); - fcntl(ipc_socket, F_SETFL, flags | O_NONBLOCK); + int flags = fcntl(sockets[0], F_GETFL, 0); + fcntl(sockets[0], F_SETFL, flags | O_NONBLOCK); + connected_to_ipc = true; return Error::OK; } @@ -226,7 +219,7 @@ namespace QuickMedia { VideoPlayer::Error VideoPlayer::update() { const int max_retries_find_window = 1000; - if(ipc_socket == -1) + if(sockets[0] == -1) return Error::INIT_FAILED; if(connect_tries == MAX_RETRIES_CONNECT) @@ -235,19 +228,6 @@ namespace QuickMedia { if(find_window_tries == max_retries_find_window) return Error::FAIL_TO_FIND_WINDOW; - if(!connected_to_ipc && retry_timer.getElapsedTime().asMilliseconds() >= RETRY_TIME_MS) { - retry_timer.restart(); - if(connect(ipc_socket, (struct sockaddr*)&ipc_addr, sizeof(ipc_addr)) == -1) { - ++connect_tries; - if(connect_tries == MAX_RETRIES_CONNECT) { - fprintf(stderr, "Failed to connect to mpv ipc after %d seconds, last error: %s\n", (RETRY_TIME_MS * MAX_RETRIES_CONNECT)/1000, strerror(errno)); - return Error::FAIL_TO_CONNECT_TIMEOUT; - } - } else { - connected_to_ipc = true; - } - } - if(connected_to_ipc && window_handle == 0 && retry_timer.getElapsedTime().asMilliseconds() >= RETRY_TIME_MS) { retry_timer.restart(); std::vector child_windows = get_child_window(display, parent_window); @@ -271,10 +251,9 @@ namespace QuickMedia { if(video_process_id != -1) { if(wait_program_non_blocking(video_process_id, &exit_status)) { fprintf(stderr, "The video player exited!, status: %d\n", exit_status); - close(ipc_socket); - remove(ipc_server_path); + close(sockets[0]); video_process_id = -1; - ipc_socket = -1; + sockets[0] = -1; return Error::EXITED; } } @@ -296,7 +275,7 @@ namespace QuickMedia { std::string json_errors; char buffer[2048]; - ssize_t bytes_read = read(ipc_socket, buffer, sizeof(buffer)); + ssize_t bytes_read = read(sockets[0], buffer, sizeof(buffer)); if(bytes_read == -1) { int err = errno; if(err != EAGAIN) { @@ -475,7 +454,7 @@ namespace QuickMedia { if(!connected_to_ipc) return Error::FAIL_NOT_CONNECTED; - if(send(ipc_socket, cmd, size, 0) == -1) { + if(send(sockets[0], cmd, size, 0) == -1) { fprintf(stderr, "Failed to send to ipc socket, error: %s, command: %.*s\n", strerror(errno), (int)size, cmd); return Error::FAIL_TO_SEND; } -- cgit v1.2.3