Better diagnostics in the stagefright commandline tool, support for playing sine-tones using a filename of "sine:[samplingrate]", i.e. sine:44100. Support for playing audio through to the speakers by using "-o" in addition to "-a".

related-to-bug: 2553359
Change-Id: Ic49eb89feb62474d9513bc21571e0e8fe3a15e0b
diff --git a/cmds/stagefright/Android.mk b/cmds/stagefright/Android.mk
index 52f767e..34648b5 100644
--- a/cmds/stagefright/Android.mk
+++ b/cmds/stagefright/Android.mk
@@ -5,7 +5,8 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:=       \
-	stagefright.cpp
+	stagefright.cpp \
+	SineSource.cpp
 
 LOCAL_SHARED_LIBRARIES := \
 	libstagefright libmedia libutils libbinder
diff --git a/cmds/stagefright/stagefright.cpp b/cmds/stagefright/stagefright.cpp
index fec9e1a..4405da6 100644
--- a/cmds/stagefright/stagefright.cpp
+++ b/cmds/stagefright/stagefright.cpp
@@ -20,9 +20,12 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "SineSource.h"
+
 #include <binder/IServiceManager.h>
 #include <binder/ProcessState.h>
 #include <media/IMediaPlayerService.h>
+#include <media/stagefright/AudioPlayer.h>
 #include <media/stagefright/CachingDataSource.h>
 #include <media/stagefright/FileSource.h>
 #include <media/stagefright/HTTPDataSource.h>
@@ -42,6 +45,7 @@
 static long gMaxNumFrames;  // 0 means decode all available.
 static long gReproduceBug;  // if not -1.
 static bool gPreferSoftwareCodec;
+static bool gPlaybackAudio;
 
 static int64_t getNowUs() {
     struct timeval tv;
@@ -73,7 +77,20 @@
 
     rawSource->start();
 
-    if (gReproduceBug >= 3 && gReproduceBug <= 5) {
+    if (gPlaybackAudio) {
+        AudioPlayer *player = new AudioPlayer(NULL);
+        player->setSource(rawSource);
+
+        player->start(true /* sourceAlreadyStarted */);
+
+        status_t finalStatus;
+        while (!player->reachedEOS(&finalStatus)) {
+            usleep(100000ll);
+        }
+
+        delete player;
+        player = NULL;
+    } else if (gReproduceBug >= 3 && gReproduceBug <= 5) {
         int64_t durationUs;
         CHECK(meta->findInt64(kKeyDuration, &durationUs));
 
@@ -245,6 +262,7 @@
     fprintf(stderr, "       -p(rofiles) dump decoder profiles supported\n");
     fprintf(stderr, "       -t(humbnail) extract video thumbnail or album art\n");
     fprintf(stderr, "       -s(oftware) prefer software codec\n");
+    fprintf(stderr, "       -o playback audio\n");
 }
 
 int main(int argc, char **argv) {
@@ -258,9 +276,10 @@
     gMaxNumFrames = 0;
     gReproduceBug = -1;
     gPreferSoftwareCodec = false;
+    gPlaybackAudio = false;
 
     int res;
-    while ((res = getopt(argc, argv, "han:lm:b:pts")) >= 0) {
+    while ((res = getopt(argc, argv, "han:lm:b:ptso")) >= 0) {
         switch (res) {
             case 'a':
             {
@@ -314,6 +333,12 @@
                 break;
             }
 
+            case 'o':
+            {
+                gPlaybackAudio = true;
+                break;
+            }
+
             case '?':
             case 'h':
             default:
@@ -325,6 +350,11 @@
         }
     }
 
+    if (gPlaybackAudio && !audioOnly) {
+        // This doesn't make any sense if we're decoding the video track.
+        gPlaybackAudio = false;
+    }
+
     argc -= optind;
     argv += optind;
 
@@ -456,6 +486,11 @@
             dataSource = new FileSource(filename);
         }
 
+        if (dataSource == NULL) {
+            fprintf(stderr, "Unable to create data source.\n");
+            return 1;
+        }
+
         bool isJPEG = false;
 
         size_t len = strlen(filename);
@@ -467,10 +502,18 @@
 
         if (isJPEG) {
             mediaSource = new JPEGSource(dataSource);
+        } else if (!strncasecmp("sine:", filename, 5)) {
+            char *end;
+            long sampleRate = strtol(filename + 5, &end, 10);
+
+            if (end == filename + 5) {
+                sampleRate = 44100;
+            }
+            mediaSource = new SineSource(sampleRate, 1);
         } else {
             sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
             if (extractor == NULL) {
-                fprintf(stderr, "could not create data source\n");
+                fprintf(stderr, "could not create extractor.\n");
                 return -1;
             }
 
@@ -492,6 +535,17 @@
                 if (!audioOnly && !strncasecmp(mime, "video/", 6)) {
                     break;
                 }
+
+                meta = NULL;
+            }
+
+            if (meta == NULL) {
+                fprintf(stderr,
+                        "No suitable %s track found. The '-a' option will "
+                        "target audio tracks only, the default is to target "
+                        "video tracks only.\n",
+                        audioOnly ? "audio" : "video");
+                return -1;
             }
 
             int64_t thumbTimeUs;
diff --git a/media/libstagefright/AudioPlayer.cpp b/media/libstagefright/AudioPlayer.cpp
index 005c64a..bcf2463 100644
--- a/media/libstagefright/AudioPlayer.cpp
+++ b/media/libstagefright/AudioPlayer.cpp
@@ -102,7 +102,7 @@
                 (numChannels == 2)
                     ? AudioSystem::CHANNEL_OUT_STEREO
                     : AudioSystem::CHANNEL_OUT_MONO,
-                8192, 0, &AudioCallback, this, 0);
+                0, 0, &AudioCallback, this, 0);
 
         if ((err = mAudioTrack->initCheck()) != OK) {
             delete mAudioTrack;