aboutsummaryrefslogtreecommitdiff
path: root/video_player/src
diff options
context:
space:
mode:
Diffstat (limited to 'video_player/src')
-rw-r--r--video_player/src/main.cpp47
1 files changed, 37 insertions, 10 deletions
diff --git a/video_player/src/main.cpp b/video_player/src/main.cpp
index 42a9b4f..4ec9278 100644
--- a/video_player/src/main.cpp
+++ b/video_player/src/main.cpp
@@ -409,7 +409,7 @@ static int64_t size_fn(void *cookie) {
buffer.resize(8192);
size_t read_offset = 0;
for(;;) {
- ssize_t bytes_read = read(header_program.read_fd, &buffer[read_offset], buffer.size() - read_offset);
+ ssize_t bytes_read = read_eintr(header_program.read_fd, &buffer[read_offset], buffer.size() - read_offset);
if(bytes_read == 0) {
buffer.resize(read_offset);
break;
@@ -448,26 +448,36 @@ static int64_t size_fn(void *cookie) {
close(header_program.read_fd);
if(res == 0) {
- size_t content_length_index = str_find_case_insensitive(buffer, 0, "content-length:", 15);
+ size_t header_start = str_find_case_insensitive(buffer, 0, "200 OK", 6);
+ if(header_start == std::string::npos)
+ goto end;
+
+ header_start += 6;
+ size_t content_length_index = str_find_case_insensitive(buffer, header_start, "content-length:", 15);
if(content_length_index == std::string::npos)
- return res;
+ goto end;
content_length_index += 15;
size_t content_length_end = buffer.find('\r', content_length_index);
if(content_length_end == std::string::npos)
- return res;
+ goto end;
buffer[content_length_end] = '\0';
errno = 0;
char *endptr;
int64_t content_length = strtoll(&buffer[content_length_index], &endptr, 10);
if(endptr == &buffer[content_length_index] || errno != 0)
- return res;
+ goto end;
res = content_length;
}
+ end:
program->content_length = res;
+ if(res < 0) {
+ fprintf(stderr, "Error: video size failed\n");
+ exit(2);
+ }
return res;
}
@@ -497,26 +507,32 @@ static int64_t seek_fn(void *cookie, int64_t offset) {
"-g", "-s", "-L", "-f",
"-r", range, "--", program->url, nullptr };
int res = exec_program_pipe(args, program);
- if(res != 0)
- return MPV_ERROR_GENERIC;
+ if(res != 0) {
+ fprintf(stderr, "Error: video seek failed\n");
+ exit(2);
+ }
return offset;
}
static int64_t read_fn(void *cookie, char *buf, uint64_t nbytes) {
ReadProgram *program = (ReadProgram*)cookie;
- ssize_t bytes_read = read(program->read_fd, buf, nbytes);
+ ssize_t bytes_read = read_eintr(program->read_fd, buf, nbytes);
if(bytes_read > 0) {
program->offset += bytes_read;
} else if(bytes_read == 0) {
// End of current range, progress to next range
bytes_read = seek_fn(program, program->offset);
if(bytes_read >= 0) {
- bytes_read = read(program->read_fd, buf, nbytes);
+ bytes_read = read_eintr(program->read_fd, buf, nbytes);
if(bytes_read > 0)
program->offset += bytes_read;
}
}
+ if(bytes_read < 0) {
+ fprintf(stderr, "Error: video read failed\n");
+ exit(2);
+ }
return bytes_read;
}
@@ -550,6 +566,10 @@ static int open_fn(void*, char *uri, mpv_stream_cb_info *info) {
read_program->url = strdup((const char*)uri + 8);
read_program->offset = 0;
int64_t res = seek_fn(read_program, read_program->offset);
+ if(res < 0) {
+ fprintf(stderr, "Error: video open failed\n");
+ exit(2);
+ }
info->cookie = read_program;
info->size_fn = size_fn;
@@ -601,6 +621,7 @@ int main(int argc, char **argv) {
std::string json_errors;
bool file_started = false;
+ int exit_code = 0;
while (running) {
mpv_event *event = mpv_wait_event(mpv_ctx, -1.0);
@@ -613,6 +634,12 @@ int main(int argc, char **argv) {
} else if(event->event_id == MPV_EVENT_SHUTDOWN) {
running = false;
break;
+ } else if(event->event_id == MPV_EVENT_END_FILE) {
+ mpv_event_end_file *end_file_event = (mpv_event_end_file*)event->data;
+ if(end_file_event->error == MPV_END_FILE_REASON_ERROR) {
+ exit_code = 2;
+ break;
+ }
} else if(event->event_id == MPV_EVENT_PROPERTY_CHANGE) {
// End of file (idle)
mpv_event_property *property = (mpv_event_property*)event->data;
@@ -637,5 +664,5 @@ int main(int argc, char **argv) {
}
mpv_terminate_destroy(mpv_ctx);
- return 0;
+ return exit_code;
}