diff stream/stream.c @ 32438:faefba58f413

Implement a basic capture feature, available through -capture. If a specified key is pressed during playback, the current stream is captured to a file, similar to what -dumpstream achieves. patch by P«”sztor Szil«”rd, don tricon hu
author diego
date Thu, 21 Oct 2010 18:19:30 +0000
parents 4614728cab25
children 5d1d67cf8718
line wrap: on
line diff
--- a/stream/stream.c	Thu Oct 21 17:35:56 2010 +0000
+++ b/stream/stream.c	Thu Oct 21 18:19:30 2010 +0000
@@ -179,6 +179,7 @@
     }
   }
   s = new_stream(-2,-2);
+  s->capture_file = NULL;
   s->url=strdup(filename);
   s->flags |= mode;
   *ret = sinfo->open(s,mode,arg,file_format);
@@ -269,6 +270,16 @@
 
 //=================== STREAMER =========================
 
+void stream_capture_do(stream_t *s)
+{
+  if (fwrite(s->buffer, s->buf_len, 1, s->capture_file) < 1) {
+    mp_msg(MSGT_GLOBAL, MSGL_ERR, "Error writing capture file: %s\n",
+           strerror(errno));
+    fclose(s->capture_file);
+    s->capture_file = NULL;
+  }
+}
+
 int stream_fill_buffer(stream_t *s){
   int len;
   // we will retry even if we already reached EOF previously.
@@ -300,6 +311,8 @@
   s->buf_len=len;
   s->pos+=len;
 //  printf("[%d]",len);fflush(stdout);
+  if (s->capture_file)
+    stream_capture_do(s);
   return len;
 }
 
@@ -463,6 +476,11 @@
 #ifdef CONFIG_STREAM_CACHE
     cache_uninit(s);
 #endif
+  if (s->capture_file) {
+    fclose(s->capture_file);
+    s->capture_file = NULL;
+  }
+
   if(s->close) s->close(s);
   if(s->fd>0){
     /* on unix we define closesocket to close