Fix to make probatron stop transcoding when the file is complete.  Now
to get it to not do real-time...

Change-Id: I7d70440b1bd99389872217124122f54a01349aeb
diff --git a/probatron/probatron.sh b/probatron/probatron.sh
index a2996fe..384b6d5 100755
--- a/probatron/probatron.sh
+++ b/probatron/probatron.sh
@@ -63,7 +63,6 @@
 TIME_HHMMSS=$(ffprobe -i $INPUT_FILE 2>&1 | grep "Duration" | awk '{split($2, a, "."); print a[1]}')
 IFS=: read -r h m s <<<"$TIME_HHMMSS"
 DURATION=$(((h * 60 + m) * 60 + s))
-echo $DURATION
 
 ## Let's Get To Work!
 
diff --git a/probatron/probe/probe.c b/probatron/probe/probe.c
index df06863..71de57b 100644
--- a/probatron/probe/probe.c
+++ b/probatron/probe/probe.c
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
 #include <assert.h>
 #include "bstd.h"
 #include "bkni.h"
@@ -57,26 +58,35 @@
 #define MAX_DSP_ENCODER_WIDTH   416
 #define MAX_DSP_ENCODER_HEIGHT  224
 
-#define DEFAULT_XCODE_DURATION_S 3600 // 60 minutes
+#define POLLING_PERIOD 1;
 
-BDBG_MODULE(BVP);
+bool volatile transcodeComplete = false;
+
+BDBG_MODULE(PROBATRON);
 
 /* ============= utilities ==============*/
 
-void ACL_Delay(unsigned int delay)
+// Wait for a key press or transcode complete
+void waitForTranscodeComplete(void)
 {
-    // Wait up to delay microseconds, or a key is pressed
-    struct timeval tv;
-    int rv = 1;
-    tv.tv_sec = delay/1000000;
-    tv.tv_usec = (delay%1000000);
-
-    // Monitor stdin
     fd_set rfds;
-    FD_ZERO(&rfds);
-    FD_SET(0, &rfds);
+    struct timespec tv;
+    int rv = 0;
+    tv.tv_sec = POLLING_PERIOD;
+    tv.tv_nsec = 0;
 
-    rv = select(1, &rfds, NULL, NULL, &tv);
+    while (!transcodeComplete) {
+        // Monitor stdin
+        FD_ZERO(&rfds);
+        FD_SET(0, &rfds);
+        rv = pselect(1, &rfds, NULL, NULL, &tv, NULL); // Returns 0 on timeout, >0 on input
+
+        if (rv != 0) {
+            // Key Press!
+            BDBG_ERR(("Key Pressed, stopping early!"));
+            transcodeComplete = true;
+        }
+    }
 }
 
 /* Generate a CRC for the specified data/length */
@@ -98,10 +108,16 @@
 static void message_callback(void *context, int param)
 {
     BSTD_UNUSED(context);
-    BDBG_ERR(("message buffer %d overflows!", param));;
+    BDBG_ERR(("message buffer %d overflows!", param));
 }
 #endif
 
+static void endOfInputStreamCallback(void *context, int param)
+{
+    BSTD_UNUSED(context);
+    BDBG_ERR(("Input Complete!"));
+    transcodeComplete = true;
+}
 static void transcoderFinishCallback(void *context, int param)
 {
     BKNI_EventHandle finishEvent = (BKNI_EventHandle)context;
@@ -138,7 +154,7 @@
 {
     char* inputFilename;
     char* outputFilename;
-    unsigned int duration = DEFAULT_XCODE_DURATION_S;
+    int duration = 0; // Only used for display purposes to the user
 
     if (argc == 3 || argc == 4)
     {
@@ -146,13 +162,15 @@
         outputFilename = argv[2];
         if (argc == 4) duration = atol(argv[3]);
     } else {
-        fprintf(stderr, "Please specify both an input and output file, and optionally a duration in seconds!\n");
+        fprintf(stderr, "Please specify both an input and output file, in that order!\n");
         return 1;
     }
 
     BDBG_WRN(("\n\n *** All your transcoders are belong to us. *** \n"));
     BDBG_WRN(("Reading from file %s and writing to file %s", inputFilename, outputFilename));
 
+    transcodeComplete = false;
+
     NEXUS_PlatformSettings platformSettings;
     NEXUS_PlatformConfiguration platformConfig;
     NEXUS_StcChannelHandle stcChannel;
@@ -279,6 +297,7 @@
     /* set a stream format, it could be any audio video transport type or file format, i.e NEXUS_TransportType_eMp4, NEXUS_TransportType_eAvi ... */
     playbackSettings.playpumpSettings.transportType = TRANSPORT_TYPE;
     playbackSettings.stcChannel = stcChannel;
+    playbackSettings.endOfStreamCallback.callback = endOfInputStreamCallback;
     NEXUS_Playback_SetSettings(playback, &playbackSettings);
 
 
@@ -308,7 +327,7 @@
         if ( !hdmiStatus.videoFormatSupported[displaySettings.format] ) {
             displaySettings.format = hdmiStatus.preferredVideoFormat;
             NEXUS_Display_SetSettings(display, &displaySettings);
-    }
+        }
     }
 #endif
 
@@ -824,8 +843,12 @@
 
     NEXUS_VideoEncoder_Start(videoEncoder, &videoEncoderStartConfig);
 
-    BDBG_WRN(("\n\nTranscoding for %d seconds, press any key to stop now...\n", duration));
-    ACL_Delay(duration * 1000000);
+    if (duration == 0) {
+        BDBG_WRN(("\n\nTranscoding, press any key to stop early...\n"));
+    } else {
+        BDBG_WRN(("\n\nTranscoding for %d seconds, press any key to stop early...\n", duration));
+    }
+    waitForTranscodeComplete();
 
     /* Done, bring down system */
     NEXUS_Playback_Stop(playback);