Переглянути джерело

Stream thread will automaticly destroy itself when a stream stops.

Jordy Sipkema 9 роки тому
батько
коміт
4e579834f7
2 змінених файлів з 30 додано та 7 видалено
  1. 1 1
      main.c
  2. 29 6
      mp3stream.c

+ 1 - 1
main.c

@@ -354,7 +354,7 @@ int main(void)
             //> "62.195.226.247";
             printf("KEY_01 DETECTED\n");
 
-            bool success = connectToStream(inet_addr("62.195.226.247"), 80, "/test.mp3");
+            bool success = connectToStream(inet_addr("62.195.226.247"), 80, "/test5.mp3");
             if (success == true){
                 play();
             }else {

+ 29 - 6
mp3stream.c

@@ -24,9 +24,11 @@ typedef struct _StreamArgs {
 
 // Prototypes - Functions for internal use only! (They wont be visible outside this file!)
 THREAD(Mp3Player, args);
+void stopMp3PlayerThread(void);
 int ProcessStreamMetaData(FILE *stream);
 
 // Variables
+static bool stream_isplaying = false;
 static bool stream_connected = false;
 static bool stream_stopped = false;
 
@@ -39,6 +41,9 @@ TCPSOCKET *socket;
 bool connectToStream(u_long ipAddressStream, u_short port, char *radioUrl
 )
 {
+    if (stream_connected == true)
+        return false;
+
     stream_connected = false;
     bool result = true;
     char* data;
@@ -103,7 +108,11 @@ bool play()
     if (stream_connected == false)
         return false;
 
+    if (stream_isplaying == true)
+        return false;
+
     // else:
+    stream_isplaying == true;
 
     TStreamArgs *streamArgs = &(TStreamArgs){
             .stream = stream,
@@ -188,6 +197,9 @@ THREAD(Mp3Player, args)
         }
     }
 
+    // Set the volume to the correct level
+    setVolume();
+
     for(;;)
     {
         /*
@@ -209,7 +221,7 @@ THREAD(Mp3Player, args)
             }
         }
 
-        setVolume();
+
 
         /*
          * Do not read pass metadata.
@@ -224,11 +236,7 @@ THREAD(Mp3Player, args)
          */
         while (rbytes) {
             if (stream_stopped == true) {
-                printf("Signal to stop the stream recieved\n.");
-                stream_connected = false;
-                VsPlayerStop();
-                NutTcpCloseSocket(socket);
-                NutThreadExit();
+                stopMp3PlayerThread();
             }
 
             if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) {
@@ -262,6 +270,21 @@ THREAD(Mp3Player, args)
 
     } // end for(;;)
 
+    while (NutSegBufUsed() > 10){
+        NutSleep(250);
+    }
+
+    stopMp3PlayerThread();
+}
+
+void stopMp3PlayerThread(void)
+{
+    printf("Signal to stop the stream recieved\n.");
+    stream_connected = false;
+    stream_isplaying = false;
+    VsPlayerStop();
+    NutTcpCloseSocket(socket);
+    NutThreadExit();
 }
 
 int ProcessStreamMetaData(FILE *stream)