Merge "Fixed some meta data issues in the recorded mp4 file" into gingerbread
diff --git a/media/libmedia/MediaScanner.cpp b/media/libmedia/MediaScanner.cpp
index 843a8fd..6f581d3 100644
--- a/media/libmedia/MediaScanner.cpp
+++ b/media/libmedia/MediaScanner.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+//#define LOG_NDEBUG 0
+#define LOG_TAG "MediaScanner"
+#include <utils/Log.h>
+
 #include <media/mediascanner.h>
 
 #include <sys/stat.h>
diff --git a/media/libstagefright/MP3Extractor.cpp b/media/libstagefright/MP3Extractor.cpp
index f9251e1..69c3a72 100644
--- a/media/libstagefright/MP3Extractor.cpp
+++ b/media/libstagefright/MP3Extractor.cpp
@@ -634,6 +634,7 @@
     }
 
     size_t frame_size;
+    int bitrate;
     for (;;) {
         ssize_t n = mDataSource->readAt(mCurrentPos, buffer->data(), 4);
         if (n < 4) {
@@ -646,7 +647,7 @@
         uint32_t header = U32_AT((const uint8_t *)buffer->data());
 
         if ((header & kMask) == (mFixedHeader & kMask)
-            && get_mp3_frame_size(header, &frame_size)) {
+            && get_mp3_frame_size(header, &frame_size, NULL, NULL, &bitrate)) {
             break;
         }
 
@@ -683,7 +684,7 @@
     buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
 
     mCurrentPos += frame_size;
-    mCurrentTimeUs += 1152 * 1000000 / 44100;
+    mCurrentTimeUs += frame_size * 8000ll / bitrate;
 
     *out = buffer;
 
diff --git a/media/libstagefright/codecs/mp3dec/MP3Decoder.cpp b/media/libstagefright/codecs/mp3dec/MP3Decoder.cpp
index efcb476..f40bd11 100644
--- a/media/libstagefright/codecs/mp3dec/MP3Decoder.cpp
+++ b/media/libstagefright/codecs/mp3dec/MP3Decoder.cpp
@@ -27,13 +27,35 @@
 
 MP3Decoder::MP3Decoder(const sp<MediaSource> &source)
     : mSource(source),
+      mNumChannels(0),
       mStarted(false),
       mBufferGroup(NULL),
       mConfig(new tPVMP3DecoderExternal),
       mDecoderBuf(NULL),
       mAnchorTimeUs(0),
-      mNumSamplesOutput(0),
+      mNumFramesOutput(0),
       mInputBuffer(NULL) {
+    init();
+}
+
+void MP3Decoder::init() {
+    sp<MetaData> srcFormat = mSource->getFormat();
+
+    int32_t sampleRate;
+    CHECK(srcFormat->findInt32(kKeyChannelCount, &mNumChannels));
+    CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate));
+
+    mMeta = new MetaData;
+    mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
+    mMeta->setInt32(kKeyChannelCount, mNumChannels);
+    mMeta->setInt32(kKeySampleRate, sampleRate);
+
+    int64_t durationUs;
+    if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
+        mMeta->setInt64(kKeyDuration, durationUs);
+    }
+
+    mMeta->setCString(kKeyDecoderComponent, "MP3Decoder");
 }
 
 MP3Decoder::~MP3Decoder() {
@@ -62,7 +84,7 @@
     mSource->start();
 
     mAnchorTimeUs = 0;
-    mNumSamplesOutput = 0;
+    mNumFramesOutput = 0;
     mStarted = true;
 
     return OK;
@@ -90,26 +112,7 @@
 }
 
 sp<MetaData> MP3Decoder::getFormat() {
-    sp<MetaData> srcFormat = mSource->getFormat();
-
-    int32_t numChannels;
-    int32_t sampleRate;
-    CHECK(srcFormat->findInt32(kKeyChannelCount, &numChannels));
-    CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate));
-
-    sp<MetaData> meta = new MetaData;
-    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
-    meta->setInt32(kKeyChannelCount, numChannels);
-    meta->setInt32(kKeySampleRate, sampleRate);
-
-    int64_t durationUs;
-    if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
-        meta->setInt64(kKeyDuration, durationUs);
-    }
-
-    meta->setCString(kKeyDecoderComponent, "MP3Decoder");
-
-    return meta;
+    return mMeta;
 }
 
 status_t MP3Decoder::read(
@@ -122,7 +125,7 @@
     if (options && options->getSeekTo(&seekTimeUs)) {
         CHECK(seekTimeUs >= 0);
 
-        mNumSamplesOutput = 0;
+        mNumFramesOutput = 0;
 
         if (mInputBuffer) {
             mInputBuffer->release();
@@ -142,7 +145,7 @@
         int64_t timeUs;
         if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) {
             mAnchorTimeUs = timeUs;
-            mNumSamplesOutput = 0;
+            mNumFramesOutput = 0;
         } else {
             // We must have a new timestamp after seeking.
             CHECK(seekTimeUs < 0);
@@ -179,7 +182,7 @@
 
         // This is recoverable, just ignore the current frame and
         // play silence instead.
-        memset(buffer->data(), 0, mConfig->outputFrameSize);
+        memset(buffer->data(), 0, mConfig->outputFrameSize * sizeof(int16_t));
         mConfig->inputBufferUsedLength = mInputBuffer->range_length();
     }
 
@@ -198,9 +201,9 @@
     buffer->meta_data()->setInt64(
             kKeyTime,
             mAnchorTimeUs
-                + (mNumSamplesOutput * 1000000) / mConfig->samplingRate);
+                + (mNumFramesOutput * 1000000) / mConfig->samplingRate);
 
-    mNumSamplesOutput += mConfig->outputFrameSize / sizeof(int16_t);
+    mNumFramesOutput += mConfig->outputFrameSize / mNumChannels;
 
     *out = buffer;
 
diff --git a/media/libstagefright/include/MP3Decoder.h b/media/libstagefright/include/MP3Decoder.h
index 88aa4c6..4086fb6 100644
--- a/media/libstagefright/include/MP3Decoder.h
+++ b/media/libstagefright/include/MP3Decoder.h
@@ -42,6 +42,9 @@
 
 private:
     sp<MediaSource> mSource;
+    sp<MetaData> mMeta;
+    int32_t mNumChannels;
+
     bool mStarted;
 
     MediaBufferGroup *mBufferGroup;
@@ -49,10 +52,12 @@
     tPVMP3DecoderExternal *mConfig;
     void *mDecoderBuf;
     int64_t mAnchorTimeUs;
-    int64_t mNumSamplesOutput;
+    int64_t mNumFramesOutput;
 
     MediaBuffer *mInputBuffer;
 
+    void init();
+
     MP3Decoder(const MP3Decoder &);
     MP3Decoder &operator=(const MP3Decoder &);
 };
diff --git a/media/libstagefright/omx/OMXMaster.cpp b/media/libstagefright/omx/OMXMaster.cpp
index 9a45bea..56b169a 100644
--- a/media/libstagefright/omx/OMXMaster.cpp
+++ b/media/libstagefright/omx/OMXMaster.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+//#define LOG_NDEBUG 0
+#define LOG_TAG "OMXMaster"
+#include <utils/Log.h>
+
 #include "OMXMaster.h"
 
 #include <dlfcn.h>