aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-02-14 06:59:23 +0100
committerdec05eba <dec05eba@protonmail.com>2022-02-16 02:04:34 +0100
commit4efce988240473a84a19dc2d378289b875d99a9e (patch)
treee17a2a3739c7aaddc69f423c253cb1a322125b04 /src
parent66312068bde937b0a5455800d1806e3f3077689c (diff)
Finish implementing time-pos, sub-add and event ipc
Diffstat (limited to 'src')
-rw-r--r--src/VideoPlayer.cpp32
-rw-r--r--src/plugins/youtube/YoutubeMediaProxy.cpp40
2 files changed, 62 insertions, 10 deletions
diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp
index f9ae04d..75871da 100644
--- a/src/VideoPlayer.cpp
+++ b/src/VideoPlayer.cpp
@@ -16,6 +16,32 @@
#include <fcntl.h>
#include <signal.h>
+static ssize_t read_eintr(int fd, void *buffer, size_t size) {
+ while(true) {
+ ssize_t bytes_read = read(fd, buffer, size);
+ if(bytes_read == -1) {
+ if(errno != EINTR)
+ return -1;
+ } else {
+ return bytes_read;
+ }
+ }
+}
+
+static ssize_t write_all(int fd, const void *buffer, size_t size) {
+ ssize_t bytes_written = 0;
+ while((size_t)bytes_written < size) {
+ ssize_t written = write(fd, (char*)buffer + bytes_written, size - bytes_written);
+ if(written == -1) {
+ if(errno != EINTR)
+ return -1;
+ } else {
+ bytes_written += written;
+ }
+ }
+ return bytes_written;
+}
+
namespace QuickMedia {
static const double RETRY_TIME_SEC = 0.5;
static const int MAX_RETRIES_CONNECT = 1000;
@@ -162,7 +188,7 @@ namespace QuickMedia {
"--cache=yes",
"--cache-on-disk=yes",
"--cache-secs=86400", // 24 hours
- "--sub-font-size=40",
+ "--sub-font-size=60",
"--sub-margin-y=45",
"--sub-border-size=1.95",
//"--force_all_formats=no",
@@ -376,7 +402,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_eintr(ipc_socket, buffer, sizeof(buffer));
if(bytes_read == -1) {
int err = errno;
if(err != EAGAIN && err != EWOULDBLOCK) {
@@ -517,7 +543,7 @@ namespace QuickMedia {
if(!connected_to_ipc)
return Error::FAIL_NOT_CONNECTED;
- if(send(ipc_socket, cmd, size, 0) == -1) {
+ if(write_all(ipc_socket, cmd, size) == -1) {
fprintf(stderr, "Failed to send to ipc socket, error: %s, command: %.*s\n", strerror(errno), (int)size, cmd);
return Error::FAIL_TO_SEND;
}
diff --git a/src/plugins/youtube/YoutubeMediaProxy.cpp b/src/plugins/youtube/YoutubeMediaProxy.cpp
index 6e2c5c4..b33c122 100644
--- a/src/plugins/youtube/YoutubeMediaProxy.cpp
+++ b/src/plugins/youtube/YoutubeMediaProxy.cpp
@@ -18,6 +18,32 @@
// TODO: What if the client sends a new header without reconnecting? is that even allowed by http standard?
// TODO: Detect when download has finished (and close connection).
+static ssize_t read_eintr(int fd, void *buffer, size_t size) {
+ while(true) {
+ ssize_t bytes_read = read(fd, buffer, size);
+ if(bytes_read == -1) {
+ if(errno != EINTR)
+ return -1;
+ } else {
+ return bytes_read;
+ }
+ }
+}
+
+static ssize_t write_all(int fd, const void *buffer, size_t size) {
+ ssize_t bytes_written = 0;
+ while((size_t)bytes_written < size) {
+ ssize_t written = write(fd, (char*)buffer + bytes_written, size - bytes_written);
+ if(written == -1) {
+ if(errno != EINTR)
+ return -1;
+ } else {
+ bytes_written += written;
+ }
+ }
+ return bytes_written;
+}
+
namespace QuickMedia {
static const int MAX_BUFFER_SIZE = 65536;
static const int64_t RANGE = 5242870;
@@ -232,7 +258,7 @@ namespace QuickMedia {
return Error::OK;
char read_buffer[4096];
- const ssize_t num_bytes_read = read(client_fd, read_buffer, sizeof(read_buffer));
+ const ssize_t num_bytes_read = read_eintr(client_fd, read_buffer, sizeof(read_buffer));
if(num_bytes_read == -1) {
const int err = errno;
if(err == EAGAIN || err == EWOULDBLOCK) {
@@ -308,7 +334,7 @@ namespace QuickMedia {
if(program_status != 0) {
//fprintf(stderr, "YoutubeStaticMediaProxy::update_download_program_status: download failed, exit status: %d\n", program_status);
if(client_fd != -1) {
- write(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1);
+ write_all(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1);
close(client_fd);
client_fd = -1;
client_request_buffer.clear();
@@ -354,7 +380,7 @@ namespace QuickMedia {
if(!start_download_success) {
fprintf(stderr, "YoutubeStaticMediaProxy::update_download_program_status: failed to start download\n");
if(client_fd != -1) {
- write(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1);
+ write_all(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1);
close(client_fd);
client_fd = -1;
client_request_buffer.clear();
@@ -392,7 +418,7 @@ namespace QuickMedia {
return Error::OK;
}
- const ssize_t num_bytes_written = write(client_fd, buffer_start + buffer_offset, num_bytes_to_write);
+ const ssize_t num_bytes_written = write_all(client_fd, buffer_start + buffer_offset, num_bytes_to_write);
if(num_bytes_written == -1) {
const int err = errno;
if(err == EAGAIN || err == EWOULDBLOCK) {
@@ -446,7 +472,7 @@ namespace QuickMedia {
YoutubeStaticMediaProxy::Error YoutubeStaticMediaProxy::handle_download() {
// TODO: Maybe read even if write is being slow and failing?
if(download_read_buffer_offset == 0) {
- downloader_num_read_bytes = read(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer));
+ downloader_num_read_bytes = read_eintr(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer));
if(downloader_num_read_bytes == -1) {
const int err = errno;
if(err == EAGAIN || err == EWOULDBLOCK) {
@@ -673,7 +699,7 @@ namespace QuickMedia {
return Error::OK;
}
- const ssize_t num_bytes_written = write(fd[1], buffer_start + buffer_offset, num_bytes_to_write);
+ const ssize_t num_bytes_written = write_all(fd[1], buffer_start + buffer_offset, num_bytes_to_write);
if(num_bytes_written == -1) {
const int err = errno;
if(err == EAGAIN || err == EWOULDBLOCK) {
@@ -703,7 +729,7 @@ namespace QuickMedia {
return Error::OK;
if(download_read_buffer_offset == 0) {
- downloader_num_read_bytes = read(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer));
+ downloader_num_read_bytes = read_eintr(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer));
if(downloader_num_read_bytes == -1) {
const int err = errno;
if(err == EAGAIN || err == EWOULDBLOCK) {