diff --git a/stream/stream.c b/stream/stream.c index e16ee966fe..e51a1666f0 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -438,6 +438,16 @@ static int stream_reconnect(stream_t *s) return 0; } +static void stream_capture_write(stream_t *s, void *buf, size_t len) +{ + if (s->capture_file && len > 0) { + if (fwrite(buf, len, 1, s->capture_file) < 1) { + MP_ERR(s, "Error writing capture file: %s\n", strerror(errno)); + stream_set_capture_file(s, NULL); + } + } +} + void stream_set_capture_file(stream_t *s, const char *filename) { if (!bstr_equals(bstr0(s->capture_filename), bstr0(filename))) { @@ -450,6 +460,8 @@ void stream_set_capture_file(stream_t *s, const char *filename) s->capture_file = fopen(filename, "wb"); if (s->capture_file) { s->capture_filename = talloc_strdup(NULL, filename); + if (s->buf_pos < s->buf_len) + stream_capture_write(s, s->buffer, s->buf_len); } else { MP_ERR(s, "Error opening capture file: %s\n", strerror(errno)); } @@ -457,16 +469,6 @@ void stream_set_capture_file(stream_t *s, const char *filename) } } -static void stream_capture_write(stream_t *s, void *buf, size_t len) -{ - if (s->capture_file && len > 0) { - if (fwrite(buf, len, 1, s->capture_file) < 1) { - MP_ERR(s, "Error writing capture file: %s\n", strerror(errno)); - stream_set_capture_file(s, NULL); - } - } -} - // Read function bypassing the local stream buffer. This will not write into // s->buffer, but into buf[0..len] instead. // Returns < 0 on error, 0 on EOF, and length of bytes read on success.