Movieplayer streaming patch

Marcus999
Neugieriger
Neugieriger
Beiträge: 4
Registriert: Mittwoch 19. April 2006, 21:04

Movieplayer streaming patch

Beitrag von Marcus999 »

Hi,

this patch makes movieplayer streaming more tolerant to network errors It also makes it possible to pause VLC during streaming.

Code: Alles auswählen

Index: apps/tuxbox/enigma/lib/movieplayer/movieplayer.cpp
===================================================================
RCS file: /cvs/tuxbox/apps/tuxbox/enigma/lib/movieplayer/movieplayer.cpp,v
retrieving revision 1.42
diff -u -r1.42 movieplayer.cpp
--- a/apps/tuxbox/enigma/lib/movieplayer/movieplayer.cpp	28 Jan 2006 15:58:27 -0000	1.42
+++ b/apps/tuxbox/enigma/lib/movieplayer/movieplayer.cpp	18 Apr 2006 15:53:21 -0000
@@ -3,6 +3,9 @@
  *
  * (C) 2005 by digi_casi <digi_casi@tuxbox.org>
  *
+ * (C) 2006 by Marcus Andersson
+ *     Robustness improvements.
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -51,6 +54,7 @@
 #endif
 
 eIOBuffer tsBuffer(BLOCKSIZE*4);
+bool streamOpen;
 
 static pthread_mutex_t mutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
 
@@ -253,37 +257,25 @@
 {
 	int len = 0;
 	int rc = 1;
-	int error = 0;
 	char tempBuffer[BLOCKSIZE];
-	fd_set rfds;
-	struct timeval tv;
 	int tsBufferSize = 0;
 	
 	eDebug("[MOVIEPLAYER] buffering stream...");
 	
 	// fill buffer and temp file
+	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
 	while ((tsBufferSize < bufferSize) && (rc > 0))
 	{
-		tv.tv_sec = 5;
-		tv.tv_usec = 0;
-		FD_ZERO(&rfds);
-		FD_SET(fd, &rfds);
-		rc = select(fd + 1, &rfds, NULL, NULL, &tv);
-		if (rc)
+		len = recv(fd, tempBuffer, BLOCKSIZE, 0);
+		if (len > 0)
 		{
-			len = recv(fd, tempBuffer, BLOCKSIZE, 0);
-			if (len > 0)
-			{
-				error = 0;
-				tsBuffer.write(tempBuffer, len);
-				tsBufferSize = tsBuffer.size();
-				eDebug("[MOVIEPLAYER] writing %d bytes to buffer, total: %d", len, tsBufferSize);
-			}
-			else 
-			{
-				if (error++ > 100)
-					rc = 0;
-			}
+			tsBuffer.write(tempBuffer, len);
+			tsBufferSize = tsBuffer.size();
+			eDebug("[MOVIEPLAYER] writing %d bytes to buffer, total: %d", len, tsBufferSize);
+		}
+		else
+		{
+			rc = 0;
 		}
 		pthread_mutex_lock(&mutex);
 		if (cancelBuffering > 0)
@@ -294,7 +286,10 @@
 		}
 		pthread_mutex_unlock(&mutex);
 	}
-	
+	if (rc > 0)
+	{
+	    	streamOpen = true;
+	}
 	return tsBufferSize;
 }
 
@@ -491,6 +486,7 @@
 				// receive and play ts stream
 				if (playStream(mrl) < 0)
 				{
+					sendRequest2VLC("?control=stop");
 					setErrorStatus();
 					break;
 				}
@@ -594,8 +590,6 @@
 {
 	char tempBuffer[BLOCKSIZE];
 	int rd = 0;
-	int tsBufferSize = 0;
-	int errors = 0;
 	eDebug("[MOVIEPLAYER] pvrThread starting: pvrfd = %d", *(int *)pvrfd);
 	pthread_cleanup_push(pvrThreadCleanup, (void *)pvrfd);
 	nice(-1);
@@ -604,22 +598,19 @@
 		pthread_testcancel();
 		pthread_mutex_lock(&mutex);
 		rd = tsBuffer.read(tempBuffer, BLOCKSIZE);
-		tsBufferSize = tsBuffer.size();
 		pthread_mutex_unlock(&mutex);
 		if (rd > 0)
 		{
-			errors = 0;
 			write(*(int *)pvrfd, tempBuffer, rd);
-			eDebug("[MOVIEPLAYER] %d >>> writing %d bytes to pvr...", tsBufferSize, rd);
+			eDebug("[MOVIEPLAYER] >>> writing %d bytes to pvr...", rd);
+		}
+		else if (streamOpen)
+		{
+			usleep(10000);
 		}
 		else
 		{
-			errors++;
-			if (errors > 100000 && tsBufferSize == 0)
-			{
-				sleep(7);
-				break;
-			}
+			break;
 		}
 	}
 	pthread_exit(NULL);
@@ -629,6 +620,7 @@
 void receiverThreadCleanup(void *fd)
 {
 	close(*(int *)fd);
+	streamOpen = false;
 	eDebug("[MOVIEPLAYER] receiverThreadCleanup done.");
 }
 
@@ -658,6 +650,14 @@
 				tsBuffer.write(tempBuffer, len);
 				pthread_mutex_unlock(&mutex);
 			}
+			else
+			{
+				break;
+			}
+		}
+		else
+		{
+			usleep(10000);
 		}
 	}
 	pthread_exit(NULL);
Index: apps/tuxbox/enigma/lib/movieplayer/utils.cpp
===================================================================
RCS file: /cvs/tuxbox/apps/tuxbox/enigma/lib/movieplayer/utils.cpp,v
retrieving revision 1.12
diff -u -r1.12 utils.cpp
--- a/apps/tuxbox/enigma/lib/movieplayer/utils.cpp	12 Oct 2005 20:46:23 -0000	1.12
+++ b/apps/tuxbox/enigma/lib/movieplayer/utils.cpp	18 Apr 2006 15:53:21 -0000
@@ -3,6 +3,9 @@
  *
  * (C) 2005 by digi_casi <digi_casi@tuxbox.org>
  *
+ * (C) 2006 by Marcus Andersson
+ *     Robustness improvements.
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -65,19 +68,19 @@
 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1)
 	{
 		fcntl(fd, F_SETFL, O_NONBLOCK);
-		retry = 100 * i;
-		while (retry-- > 0 && rc < 0)
+		do
 		{
-			if ((rc = connect(fd, (struct sockaddr *)&ads, adsLen)) < 0)
-				usleep(10000); // 10 milliseconds
+			if ((rc = connect(fd, (struct sockaddr *)&ads, adsLen)) < 0 && retry < i)
+				sleep(1);
 		}
+		while (retry++ < i && rc < 0);
 		if (rc < 0)
 		{
 			close(fd);
 			fd = -1;
 		}
 	}
-	eDebug("[MOVIEPLAYER] tcpOpen: socket fd = %d, waited %d milliseconds", fd, (100 * i - retry) * 10);
+	printf("[MOVIEPLAYER] tcpOpen: socket fd = %d, waited %d seconds\n", fd, retry - 1);
 
 	return fd;
 }
Edit: Code correctly inserted.