Merge "The color conversion from YUV420Planar to RGB only requires the image width to be a multiple of 2, not 4."
diff --git a/camera/Camera.cpp b/camera/Camera.cpp
index 907f119..e288312 100644
--- a/camera/Camera.cpp
+++ b/camera/Camera.cpp
@@ -301,12 +301,12 @@
 }
 
 // take a picture
-status_t Camera::takePicture()
+status_t Camera::takePicture(int msgType)
 {
-    LOGV("takePicture");
+    LOGV("takePicture: 0x%x", msgType);
     sp <ICamera> c = mCamera;
     if (c == 0) return NO_INIT;
-    return c->takePicture();
+    return c->takePicture(msgType);
 }
 
 // set preview/capture parameters - key/value pairs
diff --git a/camera/CameraParameters.cpp b/camera/CameraParameters.cpp
index e9a5f8c..0fd79a4 100644
--- a/camera/CameraParameters.cpp
+++ b/camera/CameraParameters.cpp
@@ -132,10 +132,10 @@
 const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight";
 const char CameraParameters::SCENE_MODE_BARCODE[] = "barcode";
 
-const char CameraParameters::PIXEL_FORMAT_YUV420P[]  = "yuv420p";
 const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp";
 const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp";
 const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv";
+const char CameraParameters::PIXEL_FORMAT_YUV420P[]  = "yuv420p";
 const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565";
 const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg";
 
diff --git a/camera/ICamera.cpp b/camera/ICamera.cpp
index 0881d65..931b57d 100644
--- a/camera/ICamera.cpp
+++ b/camera/ICamera.cpp
@@ -223,11 +223,12 @@
     }
 
     // take a picture - returns an IMemory (ref-counted mmap)
-    status_t takePicture()
+    status_t takePicture(int msgType)
     {
-        LOGV("takePicture");
+        LOGV("takePicture: 0x%x", msgType);
         Parcel data, reply;
         data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
+        data.writeInt32(msgType);
         remote()->transact(TAKE_PICTURE, data, &reply);
         status_t ret = reply.readInt32();
         return ret;
@@ -401,7 +402,8 @@
         case TAKE_PICTURE: {
             LOGV("TAKE_PICTURE");
             CHECK_INTERFACE(ICamera, data, reply);
-            reply->writeInt32(takePicture());
+            int msgType = data.readInt32();
+            reply->writeInt32(takePicture(msgType));
             return NO_ERROR;
         } break;
         case SET_PARAMETERS: {
diff --git a/cmds/stagefright/Android.mk b/cmds/stagefright/Android.mk
index 178032d..1b13dd9 100644
--- a/cmds/stagefright/Android.mk
+++ b/cmds/stagefright/Android.mk
@@ -8,7 +8,7 @@
 
 LOCAL_SHARED_LIBRARIES := \
 	libstagefright libmedia libutils libbinder libstagefright_foundation \
-        libskia
+        libskia libsurfaceflinger_client libgui
 
 LOCAL_C_INCLUDES:= \
 	$(JNI_H_INCLUDE) \
diff --git a/cmds/stagefright/stagefright.cpp b/cmds/stagefright/stagefright.cpp
index a43b190..a875c3a 100644
--- a/cmds/stagefright/stagefright.cpp
+++ b/cmds/stagefright/stagefright.cpp
@@ -55,6 +55,11 @@
 
 #include <fcntl.h>
 
+#include <gui/SurfaceTextureClient.h>
+
+#include <surfaceflinger/ISurfaceComposer.h>
+#include <surfaceflinger/SurfaceComposerClient.h>
+
 using namespace android;
 
 static long gNumRepetitions;
@@ -66,6 +71,10 @@
 static bool gDisplayHistogram;
 static String8 gWriteMP4Filename;
 
+static sp<ANativeWindow> gSurface;
+
+#define USE_SURFACE_COMPOSER 0
+
 static int64_t getNowUs() {
     struct timeval tv;
     gettimeofday(&tv, NULL);
@@ -138,7 +147,8 @@
         rawSource = OMXCodec::Create(
             client->interface(), meta, false /* createEncoder */, source,
             NULL /* matchComponentName */,
-            gPreferSoftwareCodec ? OMXCodec::kPreferSoftwareCodecs : 0);
+            gPreferSoftwareCodec ? OMXCodec::kPreferSoftwareCodecs : 0,
+            gSurface);
 
         if (rawSource == NULL) {
             fprintf(stderr, "Failed to instantiate decoder for '%s'.\n", mime);
@@ -540,6 +550,7 @@
     fprintf(stderr, "       -k seek test\n");
     fprintf(stderr, "       -x display a histogram of decoding times/fps "
                     "(video only)\n");
+    fprintf(stderr, "       -S allocate buffers from a surface\n");
 }
 
 int main(int argc, char **argv) {
@@ -550,6 +561,7 @@
     bool dumpProfiles = false;
     bool extractThumbnail = false;
     bool seekTest = false;
+    bool useSurfaceAlloc = false;
     gNumRepetitions = 1;
     gMaxNumFrames = 0;
     gReproduceBug = -1;
@@ -563,7 +575,7 @@
     sp<LiveSession> liveSession;
 
     int res;
-    while ((res = getopt(argc, argv, "han:lm:b:ptsow:kx")) >= 0) {
+    while ((res = getopt(argc, argv, "han:lm:b:ptsow:kxS")) >= 0) {
         switch (res) {
             case 'a':
             {
@@ -642,6 +654,12 @@
                 break;
             }
 
+            case 'S':
+            {
+                useSurfaceAlloc = true;
+                break;
+            }
+
             case '?':
             case 'h':
             default:
@@ -780,6 +798,39 @@
         }
     }
 
+    sp<SurfaceComposerClient> composerClient;
+    sp<SurfaceControl> control;
+
+    if (useSurfaceAlloc && !audioOnly) {
+#if USE_SURFACE_COMPOSER
+        composerClient = new SurfaceComposerClient;
+        CHECK_EQ(composerClient->initCheck(), (status_t)OK);
+
+        control = composerClient->createSurface(
+                getpid(),
+                String8("A Surface"),
+                0,
+                1280,
+                800,
+                PIXEL_FORMAT_RGB_565,
+                0);
+
+        CHECK(control != NULL);
+        CHECK(control->isValid());
+
+        CHECK_EQ(composerClient->openTransaction(), (status_t)OK);
+        CHECK_EQ(control->setLayer(30000), (status_t)OK);
+        CHECK_EQ(control->show(), (status_t)OK);
+        CHECK_EQ(composerClient->closeTransaction(), (status_t)OK);
+
+        gSurface = control->getSurface();
+        CHECK(gSurface != NULL);
+#else
+        sp<SurfaceTexture> texture = new SurfaceTexture(0 /* tex */);
+        gSurface = new SurfaceTextureClient(texture);
+#endif
+    }
+
     DataSource::RegisterDefaultSniffers();
 
     OMXClient client;
@@ -957,6 +1008,14 @@
         }
     }
 
+    if (useSurfaceAlloc && !audioOnly) {
+        gSurface.clear();
+
+#if USE_SURFACE_COMPOSER
+        composerClient->dispose();
+#endif
+    }
+
     client.disconnect();
 
     return 0;
diff --git a/include/camera/Camera.h b/include/camera/Camera.h
index e5f7e62..f3c8f64 100644
--- a/include/camera/Camera.h
+++ b/include/camera/Camera.h
@@ -66,16 +66,17 @@
 
 // msgType in notifyCallback and dataCallback functions
 enum {
-    CAMERA_MSG_ERROR            = 0x001,
-    CAMERA_MSG_SHUTTER          = 0x002,
-    CAMERA_MSG_FOCUS            = 0x004,
-    CAMERA_MSG_ZOOM             = 0x008,
-    CAMERA_MSG_PREVIEW_FRAME    = 0x010,
-    CAMERA_MSG_VIDEO_FRAME      = 0x020,
-    CAMERA_MSG_POSTVIEW_FRAME   = 0x040,
-    CAMERA_MSG_RAW_IMAGE        = 0x080,
-    CAMERA_MSG_COMPRESSED_IMAGE = 0x100,
-    CAMERA_MSG_ALL_MSGS         = 0x1FF
+    CAMERA_MSG_ERROR            = 0x0001,
+    CAMERA_MSG_SHUTTER          = 0x0002,
+    CAMERA_MSG_FOCUS            = 0x0004,
+    CAMERA_MSG_ZOOM             = 0x0008,
+    CAMERA_MSG_PREVIEW_FRAME    = 0x0010,
+    CAMERA_MSG_VIDEO_FRAME      = 0x0020,
+    CAMERA_MSG_POSTVIEW_FRAME   = 0x0040,
+    CAMERA_MSG_RAW_IMAGE        = 0x0080,
+    CAMERA_MSG_COMPRESSED_IMAGE = 0x0100,
+    CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x0200,
+    CAMERA_MSG_ALL_MSGS         = 0xFFFF
 };
 
 // cmdType in sendCommand functions
@@ -207,7 +208,7 @@
             status_t    cancelAutoFocus();
 
             // take a picture - picture returned from callback
-            status_t    takePicture();
+            status_t    takePicture(int msgType);
 
             // set preview/capture parameters - key/value pairs
             status_t    setParameters(const String8& params);
diff --git a/include/camera/CameraParameters.h b/include/camera/CameraParameters.h
index 431aaa4..da2f049 100644
--- a/include/camera/CameraParameters.h
+++ b/include/camera/CameraParameters.h
@@ -417,11 +417,10 @@
 
     // Pixel color formats for KEY_PREVIEW_FORMAT, KEY_PICTURE_FORMAT,
     // and KEY_VIDEO_FRAME_FORMAT
-    // Planar variant of the YUV420 color format
-    static const char PIXEL_FORMAT_YUV420P[];
     static const char PIXEL_FORMAT_YUV422SP[];
     static const char PIXEL_FORMAT_YUV420SP[]; // NV21
     static const char PIXEL_FORMAT_YUV422I[]; // YUY2
+    static const char PIXEL_FORMAT_YUV420P[]; // YV12
     static const char PIXEL_FORMAT_RGB565[];
     static const char PIXEL_FORMAT_JPEG[];
 
diff --git a/include/camera/ICamera.h b/include/camera/ICamera.h
index b2310a6..2344b3f 100644
--- a/include/camera/ICamera.h
+++ b/include/camera/ICamera.h
@@ -70,7 +70,7 @@
     virtual status_t        startRecording() = 0;
 
     // stop recording mode
-    virtual void            stopRecording() = 0;    
+    virtual void            stopRecording() = 0;
 
     // get recording state
     virtual bool            recordingEnabled() = 0;
@@ -84,8 +84,14 @@
     // cancel auto focus
     virtual status_t        cancelAutoFocus() = 0;
 
-    // take a picture
-    virtual status_t        takePicture() = 0;
+    /*
+     * take a picture.
+     * @param msgType the message type an application selectively turn on/off
+     * on a photo-by-photo basis. The supported message types are:
+     * CAMERA_MSG_SHUTTER, CAMERA_MSG_RAW_IMAGE, CAMERA_MSG_COMPRESSED_IMAGE,
+     * and CAMERA_MSG_POSTVIEW_FRAME. Any other message types will be ignored.
+     */
+    virtual status_t        takePicture(int msgType) = 0;
 
     // set preview/capture parameters - key/value pairs
     virtual status_t        setParameters(const String8& params) = 0;
diff --git a/include/media/IMediaPlayer.h b/include/media/IMediaPlayer.h
index bba7ed7..70519ef 100644
--- a/include/media/IMediaPlayer.h
+++ b/include/media/IMediaPlayer.h
@@ -26,6 +26,7 @@
 class Parcel;
 class ISurface;
 class Surface;
+class ISurfaceTexture;
 
 class IMediaPlayer: public IInterface
 {
@@ -35,6 +36,8 @@
     virtual void            disconnect() = 0;
 
     virtual status_t        setVideoSurface(const sp<Surface>& surface) = 0;
+    virtual status_t        setVideoSurfaceTexture(
+                                    const sp<ISurfaceTexture>& surfaceTexture) = 0;
     virtual status_t        prepareAsync() = 0;
     virtual status_t        start() = 0;
     virtual status_t        stop() = 0;
diff --git a/include/media/IMediaPlayerService.h b/include/media/IMediaPlayerService.h
index 0bfb166..cce9129 100644
--- a/include/media/IMediaPlayerService.h
+++ b/include/media/IMediaPlayerService.h
@@ -54,6 +54,22 @@
     virtual sp<IMemory>         decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;
     virtual sp<IMemory>         decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) = 0;
     virtual sp<IOMX>            getOMX() = 0;
+
+    // codecs usage tracking for the battery app
+    enum BatteryDataBits {
+        // tracking audio codec
+        kBatteryDataTrackAudio          = 1,
+        // tracking video codec
+        kBatteryDataTrackVideo          = 2,
+        // codec is started, otherwise codec is paused
+        kBatteryDataCodecStarted        = 4,
+        // tracking decoder (for media player),
+        // otherwise tracking encoder (for media recorder)
+        kBatteryDataTrackDecoder        = 8,
+    };
+
+    virtual void addBatteryData(uint32_t params) = 0;
+    virtual status_t pullBatteryData(Parcel* reply) = 0;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/media/MediaPlayerInterface.h b/include/media/MediaPlayerInterface.h
index 048f041..117d7eb 100644
--- a/include/media/MediaPlayerInterface.h
+++ b/include/media/MediaPlayerInterface.h
@@ -34,6 +34,7 @@
 class Parcel;
 class ISurface;
 class Surface;
+class ISurfaceTexture;
 
 template<typename T> class SortedVector;
 
@@ -112,7 +113,13 @@
         return INVALID_OPERATION;
     }
 
+    // pass the buffered Surface to the media player service
     virtual status_t    setVideoSurface(const sp<Surface>& surface) = 0;
+
+    // pass the buffered ISurfaceTexture to the media player service
+    virtual status_t    setVideoSurfaceTexture(
+                                const sp<ISurfaceTexture>& surfaceTexture) = 0;
+
     virtual status_t    prepare() = 0;
     virtual status_t    prepareAsync() = 0;
     virtual status_t    start() = 0;
@@ -177,7 +184,7 @@
     sp<AudioSink>       mAudioSink;
 };
 
-// Implement this class for media players that output directo to hardware
+// Implement this class for media players that output audio directly to hardware
 class MediaPlayerHWInterface : public MediaPlayerBase
 {
 public:
diff --git a/include/media/mediaplayer.h b/include/media/mediaplayer.h
index 88b0c3e..528eeb9 100644
--- a/include/media/mediaplayer.h
+++ b/include/media/mediaplayer.h
@@ -28,6 +28,7 @@
 namespace android {
 
 class Surface;
+class ISurfaceTexture;
 
 enum media_event_type {
     MEDIA_NOP               = 0, // interface test message
@@ -146,6 +147,8 @@
 
             status_t        setDataSource(int fd, int64_t offset, int64_t length);
             status_t        setVideoSurface(const sp<Surface>& surface);
+            status_t        setVideoSurfaceTexture(
+                                    const sp<ISurfaceTexture>& surfaceTexture);
             status_t        setListener(const sp<MediaPlayerListener>& listener);
             status_t        prepare();
             status_t        prepareAsync();
diff --git a/include/media/stagefright/AudioSource.h b/include/media/stagefright/AudioSource.h
index d484d60..9e6f0e2 100644
--- a/include/media/stagefright/AudioSource.h
+++ b/include/media/stagefright/AudioSource.h
@@ -18,15 +18,17 @@
 
 #define AUDIO_SOURCE_H_
 
+#include <media/AudioRecord.h>
 #include <media/AudioSystem.h>
 #include <media/stagefright/MediaSource.h>
+#include <media/stagefright/MediaBuffer.h>
+#include <utils/List.h>
 
 namespace android {
 
 class AudioRecord;
-struct MediaBufferGroup;
 
-struct AudioSource : public MediaSource {
+struct AudioSource : public MediaSource, public MediaBufferObserver {
     // Note that the "channels" parameter is _not_ the number of channels,
     // but a bitmask of AudioSystem::audio_channels constants.
     AudioSource(
@@ -45,6 +47,9 @@
     virtual status_t read(
             MediaBuffer **buffer, const ReadOptions *options = NULL);
 
+    status_t dataCallbackTimestamp(const AudioRecord::Buffer& buffer, int64_t timeUs);
+    virtual void signalBufferReturned(MediaBuffer *buffer);
+
 protected:
     virtual ~AudioSource();
 
@@ -54,27 +59,31 @@
 
         // After the initial mute, we raise the volume linearly
         // over kAutoRampDurationUs.
-        kAutoRampDurationUs = 700000,
+        kAutoRampDurationUs = 300000,
 
         // This is the initial mute duration to suppress
         // the video recording signal tone
-        kAutoRampStartUs = 1000000,
-      };
+        kAutoRampStartUs = 0,
+    };
+
+    Mutex mLock;
+    Condition mFrameAvailableCondition;
+    Condition mFrameEncodingCompletionCondition;
 
     AudioRecord *mRecord;
     status_t mInitCheck;
     bool mStarted;
+    int32_t mSampleRate;
 
-    bool mCollectStats;
     bool mTrackMaxAmplitude;
     int64_t mStartTimeUs;
     int16_t mMaxAmplitude;
     int64_t mPrevSampleTimeUs;
-    int64_t mTotalLostFrames;
-    int64_t mPrevLostBytes;
     int64_t mInitialReadTimeUs;
+    int64_t mNumFramesReceived;
+    int64_t mNumClientOwnedBuffers;
 
-    MediaBufferGroup *mGroup;
+    List<MediaBuffer * > mBuffersReceived;
 
     void trackMaxAmplitude(int16_t *data, int nSamples);
 
@@ -84,6 +93,9 @@
         int32_t startFrame, int32_t rampDurationFrames,
         uint8_t *data,   size_t bytes);
 
+    void releaseQueuedFrames_l();
+    void waitOutstandingEncodingFrames_l();
+
     AudioSource(const AudioSource &);
     AudioSource &operator=(const AudioSource &);
 };
diff --git a/include/media/stagefright/MediaDefs.h b/include/media/stagefright/MediaDefs.h
index 31a549c..66dfff6 100644
--- a/include/media/stagefright/MediaDefs.h
+++ b/include/media/stagefright/MediaDefs.h
@@ -38,6 +38,7 @@
 extern const char *MEDIA_MIMETYPE_AUDIO_G711_MLAW;
 extern const char *MEDIA_MIMETYPE_AUDIO_RAW;
 extern const char *MEDIA_MIMETYPE_AUDIO_FLAC;
+extern const char *MEDIA_MIMETYPE_AUDIO_AAC_ADTS;
 
 extern const char *MEDIA_MIMETYPE_CONTAINER_MPEG4;
 extern const char *MEDIA_MIMETYPE_CONTAINER_WAV;
diff --git a/include/media/stagefright/MetaData.h b/include/media/stagefright/MetaData.h
index 18fd90e..f7f2235 100644
--- a/include/media/stagefright/MetaData.h
+++ b/include/media/stagefright/MetaData.h
@@ -48,6 +48,7 @@
     kKeyBitRate           = 'brte',  // int32_t (bps)
     kKeyESDS              = 'esds',  // raw data
     kKeyAVCC              = 'avcc',  // raw data
+    kKeyD263              = 'd263',  // raw data
     kKeyVorbisInfo        = 'vinf',  // raw data
     kKeyVorbisBooks       = 'vboo',  // raw data
     kKeyWantsNALFragments = 'NALf',
@@ -118,6 +119,7 @@
 enum {
     kTypeESDS        = 'esds',
     kTypeAVCC        = 'avcc',
+    kTypeD263        = 'd263',
 };
 
 class MetaData : public RefBase {
diff --git a/include/media/stagefright/NativeWindowWrapper.h b/include/media/stagefright/NativeWindowWrapper.h
new file mode 100644
index 0000000..f323cbc
--- /dev/null
+++ b/include/media/stagefright/NativeWindowWrapper.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef NATIVE_WINDOW_WRAPPER_H_
+
+#define NATIVE_WINDOW_WRAPPER_H_
+
+#include <surfaceflinger/Surface.h>
+#include <gui/SurfaceTextureClient.h>
+
+namespace android {
+
+// Both Surface and SurfaceTextureClient are RefBase that implement the
+// ANativeWindow interface, but at different addresses. ANativeWindow is not
+// a RefBase but acts like one for use with sp<>.  This wrapper converts a
+// Surface or SurfaceTextureClient into a single reference-counted object
+// that holds an sp reference to the underlying Surface or SurfaceTextureClient,
+// It provides a method to get the ANativeWindow.
+
+struct NativeWindowWrapper : RefBase {
+    NativeWindowWrapper(
+            const sp<Surface> &surface) :
+        mSurface(surface) { }
+
+    NativeWindowWrapper(
+            const sp<SurfaceTextureClient> &surfaceTextureClient) :
+        mSurfaceTextureClient(surfaceTextureClient) { }
+
+    sp<ANativeWindow> getNativeWindow() const {
+        if (mSurface != NULL) {
+            return mSurface;
+        } else {
+            return mSurfaceTextureClient;
+        }
+    }
+
+    // If needed later we can provide a method to ask what kind of native window
+
+private:
+    // At most one of mSurface and mSurfaceTextureClient will be non-NULL
+    const sp<Surface> mSurface;
+    const sp<SurfaceTextureClient> mSurfaceTextureClient;
+
+    DISALLOW_EVIL_CONSTRUCTORS(NativeWindowWrapper);
+};
+
+}  // namespace android
+
+#endif  // NATIVE_WINDOW_WRAPPER_H_
diff --git a/include/media/stagefright/foundation/ABitReader.h b/include/media/stagefright/foundation/ABitReader.h
index 5135211..5510b12 100644
--- a/include/media/stagefright/foundation/ABitReader.h
+++ b/include/media/stagefright/foundation/ABitReader.h
@@ -31,6 +31,8 @@
     uint32_t getBits(size_t n);
     void skipBits(size_t n);
 
+    void putBits(uint32_t x, size_t n);
+
     size_t numBitsLeft() const;
 
     const uint8_t *data() const;
@@ -43,7 +45,6 @@
     size_t mNumBitsLeft;
 
     void fillReservoir();
-    void putBits(uint32_t x, size_t n);
 
     DISALLOW_EVIL_CONSTRUCTORS(ABitReader);
 };
diff --git a/media/libmedia/Android.mk b/media/libmedia/Android.mk
index 74fb531..fd4c6c6 100644
--- a/media/libmedia/Android.mk
+++ b/media/libmedia/Android.mk
@@ -37,7 +37,8 @@
 
 LOCAL_SHARED_LIBRARIES := \
 	libui libcutils libutils libbinder libsonivox libicuuc libexpat \
-        libsurfaceflinger_client libcamera_client libstagefright_foundation
+        libsurfaceflinger_client libcamera_client libstagefright_foundation \
+        libgui
 
 LOCAL_MODULE:= libmedia
 
diff --git a/media/libmedia/IMediaPlayer.cpp b/media/libmedia/IMediaPlayer.cpp
index c287c0a..2399216 100644
--- a/media/libmedia/IMediaPlayer.cpp
+++ b/media/libmedia/IMediaPlayer.cpp
@@ -23,6 +23,7 @@
 #include <media/IMediaPlayer.h>
 #include <surfaceflinger/ISurface.h>
 #include <surfaceflinger/Surface.h>
+#include <gui/ISurfaceTexture.h>
 
 namespace android {
 
@@ -45,7 +46,8 @@
     SET_METADATA_FILTER,
     GET_METADATA,
     SET_AUX_EFFECT_SEND_LEVEL,
-    ATTACH_AUX_EFFECT
+    ATTACH_AUX_EFFECT,
+    SET_VIDEO_SURFACETEXTURE,
 };
 
 class BpMediaPlayer: public BpInterface<IMediaPlayer>
@@ -64,6 +66,7 @@
         remote()->transact(DISCONNECT, data, &reply);
     }
 
+    // pass the buffered Surface to the media player service
     status_t setVideoSurface(const sp<Surface>& surface)
     {
         Parcel data, reply;
@@ -73,6 +76,17 @@
         return reply.readInt32();
     }
 
+    // pass the buffered ISurfaceTexture to the media player service
+    status_t setVideoSurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture)
+    {
+        Parcel data, reply;
+        data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor());
+        sp<IBinder> b(surfaceTexture->asBinder());
+        data.writeStrongBinder(b);
+        remote()->transact(SET_VIDEO_SURFACETEXTURE, data, &reply);
+        return reply.readInt32();
+    }
+
     status_t prepareAsync()
     {
         Parcel data, reply;
@@ -220,6 +234,7 @@
         remote()->transact(ATTACH_AUX_EFFECT, data, &reply);
         return reply.readInt32();
     }
+
 };
 
 IMPLEMENT_META_INTERFACE(MediaPlayer, "android.media.IMediaPlayer");
@@ -241,6 +256,13 @@
             reply->writeInt32(setVideoSurface(surface));
             return NO_ERROR;
         } break;
+        case SET_VIDEO_SURFACETEXTURE: {
+            CHECK_INTERFACE(IMediaPlayer, data, reply);
+            sp<ISurfaceTexture> surfaceTexture =
+                    interface_cast<ISurfaceTexture>(data.readStrongBinder());
+            reply->writeInt32(setVideoSurfaceTexture(surfaceTexture));
+            return NO_ERROR;
+        } break;
         case PREPARE_ASYNC: {
             CHECK_INTERFACE(IMediaPlayer, data, reply);
             reply->writeInt32(prepareAsync());
diff --git a/media/libmedia/IMediaPlayerService.cpp b/media/libmedia/IMediaPlayerService.cpp
index 77199e1..17a0362 100644
--- a/media/libmedia/IMediaPlayerService.cpp
+++ b/media/libmedia/IMediaPlayerService.cpp
@@ -37,7 +37,9 @@
     DECODE_FD,
     CREATE_MEDIA_RECORDER,
     CREATE_METADATA_RETRIEVER,
-    GET_OMX
+    GET_OMX,
+    ADD_BATTERY_DATA,
+    PULL_BATTERY_DATA
 };
 
 class BpMediaPlayerService: public BpInterface<IMediaPlayerService>
@@ -156,6 +158,19 @@
         remote()->transact(GET_OMX, data, &reply);
         return interface_cast<IOMX>(reply.readStrongBinder());
     }
+
+    virtual void addBatteryData(uint32_t params) {
+        Parcel data, reply;
+        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
+        data.writeInt32(params);
+        remote()->transact(ADD_BATTERY_DATA, data, &reply);
+    }
+
+    virtual status_t pullBatteryData(Parcel* reply) {
+        Parcel data;
+        data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
+        return remote()->transact(PULL_BATTERY_DATA, data, reply);
+    }
 };
 
 IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
@@ -270,6 +285,17 @@
             reply->writeStrongBinder(omx->asBinder());
             return NO_ERROR;
         } break;
+        case ADD_BATTERY_DATA: {
+            CHECK_INTERFACE(IMediaPlayerService, data, reply);
+            uint32_t params = data.readInt32();
+            addBatteryData(params);
+            return NO_ERROR;
+        } break;
+        case PULL_BATTERY_DATA: {
+            CHECK_INTERFACE(IMediaPlayerService, data, reply);
+            pullBatteryData(reply);
+            return NO_ERROR;
+        } break;
         default:
             return BBinder::onTransact(code, data, reply, flags);
     }
diff --git a/media/libmedia/mediaplayer.cpp b/media/libmedia/mediaplayer.cpp
index 87c8fe4..0ee0249 100644
--- a/media/libmedia/mediaplayer.cpp
+++ b/media/libmedia/mediaplayer.cpp
@@ -201,6 +201,16 @@
     return mPlayer->setVideoSurface(surface);
 }
 
+status_t MediaPlayer::setVideoSurfaceTexture(
+        const sp<ISurfaceTexture>& surfaceTexture)
+{
+    LOGV("setVideoSurfaceTexture");
+    Mutex::Autolock _l(mLock);
+    if (mPlayer == 0) return NO_INIT;
+
+    return mPlayer->setVideoSurfaceTexture(surfaceTexture);
+}
+
 // must call with lock held
 status_t MediaPlayer::prepareAsync_l()
 {
diff --git a/media/libmedia/mediarecorder.cpp b/media/libmedia/mediarecorder.cpp
index fd575fe..0100a17 100644
--- a/media/libmedia/mediarecorder.cpp
+++ b/media/libmedia/mediarecorder.cpp
@@ -298,6 +298,17 @@
         return INVALID_OPERATION;
     }
 
+    // It appears that if an invalid file descriptor is passed through
+    // binder calls, the server-side of the inter-process function call
+    // is skipped. As a result, the check at the server-side to catch
+    // the invalid file descritpor never gets invoked. This is to workaround
+    // this issue by checking the file descriptor first before passing
+    // it through binder call.
+    if (fd < 0) {
+        LOGE("Invalid file descriptor: %d", fd);
+        return BAD_VALUE;
+    }
+
     status_t ret = mMediaRecorder->setOutputFile(fd, offset, length);
     if (OK != ret) {
         LOGV("setOutputFile failed: %d", ret);
diff --git a/media/libmediaplayerservice/Android.mk b/media/libmediaplayerservice/Android.mk
index f7f0d95..e65f6d8 100644
--- a/media/libmediaplayerservice/Android.mk
+++ b/media/libmediaplayerservice/Android.mk
@@ -32,7 +32,8 @@
 	libstagefright        			\
 	libstagefright_omx    			\
 	libstagefright_foundation               \
-	libsurfaceflinger_client
+	libsurfaceflinger_client                \
+	libgui
 
 LOCAL_STATIC_LIBRARIES := \
         libstagefright_rtsp                     \
diff --git a/media/libmediaplayerservice/MediaPlayerService.cpp b/media/libmediaplayerservice/MediaPlayerService.cpp
index 439e4ce..ec6188f 100644
--- a/media/libmediaplayerservice/MediaPlayerService.cpp
+++ b/media/libmediaplayerservice/MediaPlayerService.cpp
@@ -23,6 +23,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <dirent.h>
 #include <unistd.h>
 
@@ -51,6 +52,8 @@
 #include <media/Metadata.h>
 #include <media/AudioTrack.h>
 
+#include <private/android_filesystem_config.h>
+
 #include "MediaRecorderClient.h"
 #include "MediaPlayerService.h"
 #include "MetadataRetrieverClient.h"
@@ -732,18 +735,14 @@
         return TEST_PLAYER;
     }
 
-    char value[PROPERTY_VALUE_MAX];
-    if (!property_get("media.httplive.disable-nuplayer", value, NULL)
-            || (strcasecmp(value, "true") && strcmp(value, "1"))) {
-        if (!strncasecmp("http://", url, 7)) {
-            size_t len = strlen(url);
-            if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
-                return NU_PLAYER;
-            }
+    if (!strncasecmp("http://", url, 7)) {
+        size_t len = strlen(url);
+        if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
+            return NU_PLAYER;
+        }
 
-            if (strstr(url,"m3u8")) {
-                return NU_PLAYER;
-            }
+        if (strstr(url,"m3u8")) {
+            return NU_PLAYER;
         }
     }
 
@@ -936,6 +935,15 @@
     return p->setVideoSurface(surface);
 }
 
+status_t MediaPlayerService::Client::setVideoSurfaceTexture(
+        const sp<ISurfaceTexture>& surfaceTexture)
+{
+    LOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
+    sp<MediaPlayerBase> p = getPlayer();
+    if (p == 0) return UNKNOWN_ERROR;
+    return p->setVideoSurfaceTexture(surfaceTexture);
+}
+
 status_t MediaPlayerService::Client::invoke(const Parcel& request,
                                             Parcel *reply)
 {
@@ -1766,4 +1774,93 @@
     return 0;
 }
 
+void MediaPlayerService::addBatteryData(uint32_t params)
+{
+    Mutex::Autolock lock(mLock);
+    int uid = IPCThreadState::self()->getCallingUid();
+    if (uid == AID_MEDIA) {
+        return;
+    }
+    int index = mBatteryData.indexOfKey(uid);
+    int32_t time = systemTime() / 1000000L;
+
+    if (index < 0) { // create a new entry for this UID
+        BatteryUsageInfo info;
+        info.audioTotalTime = 0;
+        info.videoTotalTime = 0;
+        info.audioLastTime = 0;
+        info.videoLastTime = 0;
+        info.refCount = 0;
+
+        mBatteryData.add(uid, info);
+    }
+
+    BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
+
+    if (params & kBatteryDataCodecStarted) {
+        if (params & kBatteryDataTrackAudio) {
+            info.audioLastTime -= time;
+            info.refCount ++;
+        }
+        if (params & kBatteryDataTrackVideo) {
+            info.videoLastTime -= time;
+            info.refCount ++;
+        }
+    } else {
+        if (info.refCount == 0) {
+            LOGW("Battery track warning: refCount is already 0");
+            return;
+        } else if (info.refCount < 0) {
+            LOGE("Battery track error: refCount < 0");
+            mBatteryData.removeItem(uid);
+            return;
+        }
+
+        if (params & kBatteryDataTrackAudio) {
+            info.audioLastTime += time;
+            info.refCount --;
+        }
+        if (params & kBatteryDataTrackVideo) {
+            info.videoLastTime += time;
+            info.refCount --;
+        }
+
+        // no stream is being played by this UID
+        if (info.refCount == 0) {
+            info.audioTotalTime += info.audioLastTime;
+            info.audioLastTime = 0;
+            info.videoTotalTime += info.videoLastTime;
+            info.videoLastTime = 0;
+        }
+    }
+}
+
+status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
+    Mutex::Autolock lock(mLock);
+    BatteryUsageInfo info;
+    int size = mBatteryData.size();
+
+    reply->writeInt32(size);
+    int i = 0;
+
+    while (i < size) {
+        info = mBatteryData.valueAt(i);
+
+        reply->writeInt32(mBatteryData.keyAt(i)); //UID
+        reply->writeInt32(info.audioTotalTime);
+        reply->writeInt32(info.videoTotalTime);
+
+        info.audioTotalTime = 0;
+        info.videoTotalTime = 0;
+
+        // remove the UID entry where no stream is being played
+        if (info.refCount <= 0) {
+            mBatteryData.removeItemsAt(i);
+            size --;
+            i --;
+        }
+        i++;
+    }
+    return NO_ERROR;
+}
 } // namespace android
diff --git a/media/libmediaplayerservice/MediaPlayerService.h b/media/libmediaplayerservice/MediaPlayerService.h
index 62f8ed6..1175ed0 100644
--- a/media/libmediaplayerservice/MediaPlayerService.h
+++ b/media/libmediaplayerservice/MediaPlayerService.h
@@ -204,7 +204,31 @@
 
             void                removeClient(wp<Client> client);
 
+    // For battery usage tracking purpose
+    struct BatteryUsageInfo {
+        // how many streams are being played by one UID
+        int     refCount;
+        // a temp variable to store the duration(ms) of audio codecs
+        // when we start a audio codec, we minus the system time from audioLastTime
+        // when we pause it, we add the system time back to the audioLastTime
+        // so after the pause, audioLastTime = pause time - start time
+        // if multiple audio streams are played (or recorded), then audioLastTime
+        // = the total playing time of all the streams
+        int32_t audioLastTime;
+        // when all the audio streams are being paused, we assign audioLastTime to
+        // this variable, so this value could be provided to the battery app
+        // in the next pullBatteryData call
+        int32_t audioTotalTime;
 
+        int32_t videoLastTime;
+        int32_t videoTotalTime;
+    };
+    KeyedVector<int, BatteryUsageInfo>    mBatteryData;
+
+    // Collect info of the codec usage from media player and media recorder
+    virtual void                addBatteryData(uint32_t params);
+    // API for the Battery app to pull the data of codecs usage
+    virtual status_t            pullBatteryData(Parcel* reply);
 private:
 
     class Client : public BnMediaPlayer {
@@ -212,6 +236,8 @@
         // IMediaPlayer interface
         virtual void            disconnect();
         virtual status_t        setVideoSurface(const sp<Surface>& surface);
+        virtual status_t        setVideoSurfaceTexture(
+                                        const sp<ISurfaceTexture>& surfaceTexture);
         virtual status_t        prepareAsync();
         virtual status_t        start();
         virtual status_t        stop();
diff --git a/media/libmediaplayerservice/MidiFile.h b/media/libmediaplayerservice/MidiFile.h
index aa8f3f0..a98231c 100644
--- a/media/libmediaplayerservice/MidiFile.h
+++ b/media/libmediaplayerservice/MidiFile.h
@@ -36,6 +36,9 @@
 
     virtual status_t    setDataSource(int fd, int64_t offset, int64_t length);
     virtual status_t    setVideoSurface(const sp<Surface>& surface) { return UNKNOWN_ERROR; }
+    virtual status_t    setVideoSurfaceTexture(
+                                const sp<ISurfaceTexture>& surfaceTexture)
+                            { return UNKNOWN_ERROR; }
     virtual status_t    prepare();
     virtual status_t    prepareAsync();
     virtual status_t    start();
diff --git a/media/libmediaplayerservice/StagefrightPlayer.cpp b/media/libmediaplayerservice/StagefrightPlayer.cpp
index da564dc..e277121 100644
--- a/media/libmediaplayerservice/StagefrightPlayer.cpp
+++ b/media/libmediaplayerservice/StagefrightPlayer.cpp
@@ -33,7 +33,6 @@
 
 status_t StagefrightPlayer::setDataSource(
         const char *url, const KeyedVector<String8, String8> *headers) {
-    LOGI("setDataSource('%s')", url);
     return mPlayer->setDataSource(url, headers);
 }
 
@@ -55,6 +54,14 @@
     return OK;
 }
 
+status_t StagefrightPlayer::setVideoSurfaceTexture(
+        const sp<ISurfaceTexture> &surfaceTexture) {
+    LOGV("setVideoSurfaceTexture");
+
+    mPlayer->setSurfaceTexture(surfaceTexture);
+    return OK;
+}
+
 status_t StagefrightPlayer::prepare() {
     return mPlayer->prepare();
 }
diff --git a/media/libmediaplayerservice/StagefrightPlayer.h b/media/libmediaplayerservice/StagefrightPlayer.h
index fc72bfb..e2796d2 100644
--- a/media/libmediaplayerservice/StagefrightPlayer.h
+++ b/media/libmediaplayerservice/StagefrightPlayer.h
@@ -39,6 +39,8 @@
     virtual status_t setDataSource(const sp<IStreamSource> &source);
 
     virtual status_t setVideoSurface(const sp<Surface> &surface);
+    virtual status_t setVideoSurfaceTexture(
+            const sp<ISurfaceTexture> &surfaceTexture);
     virtual status_t prepare();
     virtual status_t prepareAsync();
     virtual status_t start();
diff --git a/media/libmediaplayerservice/StagefrightRecorder.cpp b/media/libmediaplayerservice/StagefrightRecorder.cpp
index 87fdbf2..e3dfabb 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.cpp
+++ b/media/libmediaplayerservice/StagefrightRecorder.cpp
@@ -20,6 +20,10 @@
 
 #include "StagefrightRecorder.h"
 
+#include <binder/IPCThreadState.h>
+#include <binder/IServiceManager.h>
+
+#include <media/IMediaPlayerService.h>
 #include <media/stagefright/AudioSource.h>
 #include <media/stagefright/AMRWriter.h>
 #include <media/stagefright/CameraSource.h>
@@ -46,9 +50,23 @@
 
 namespace android {
 
+// To collect the encoder usage for the battery app
+static void addBatteryData(uint32_t params) {
+    sp<IBinder> binder =
+        defaultServiceManager()->getService(String16("media.player"));
+    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
+    CHECK(service.get() != NULL);
+
+    service->addBatteryData(params);
+}
+
+
 StagefrightRecorder::StagefrightRecorder()
     : mWriter(NULL), mWriterAux(NULL),
-      mOutputFd(-1), mOutputFdAux(-1) {
+      mOutputFd(-1), mOutputFdAux(-1),
+      mAudioSource(AUDIO_SOURCE_LIST_END),
+      mVideoSource(VIDEO_SOURCE_LIST_END),
+      mStarted(false) {
 
     LOGV("Constructor");
     reset();
@@ -745,30 +763,54 @@
         return UNKNOWN_ERROR;
     }
 
+    status_t status = OK;
+
     switch (mOutputFormat) {
         case OUTPUT_FORMAT_DEFAULT:
         case OUTPUT_FORMAT_THREE_GPP:
         case OUTPUT_FORMAT_MPEG_4:
-            return startMPEG4Recording();
+            status = startMPEG4Recording();
+            break;
 
         case OUTPUT_FORMAT_AMR_NB:
         case OUTPUT_FORMAT_AMR_WB:
-            return startAMRRecording();
+            status = startAMRRecording();
+            break;
 
         case OUTPUT_FORMAT_AAC_ADIF:
         case OUTPUT_FORMAT_AAC_ADTS:
-            return startAACRecording();
+            status = startAACRecording();
+            break;
 
         case OUTPUT_FORMAT_RTP_AVP:
-            return startRTPRecording();
+            status = startRTPRecording();
+            break;
 
         case OUTPUT_FORMAT_MPEG2TS:
-            return startMPEG2TSRecording();
+            status = startMPEG2TSRecording();
+            break;
 
         default:
             LOGE("Unsupported output file format: %d", mOutputFormat);
-            return UNKNOWN_ERROR;
+            status = UNKNOWN_ERROR;
+            break;
     }
+
+    if ((status == OK) && (!mStarted)) {
+        mStarted = true;
+
+        uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted;
+        if (mAudioSource != AUDIO_SOURCE_LIST_END) {
+            params |= IMediaPlayerService::kBatteryDataTrackAudio;
+        }
+        if (mVideoSource != VIDEO_SOURCE_LIST_END) {
+            params |= IMediaPlayerService::kBatteryDataTrackVideo;
+        }
+
+        addBatteryData(params);
+    }
+
+    return status;
 }
 
 sp<MediaSource> StagefrightRecorder::createAudioSource() {
@@ -1458,6 +1500,21 @@
         mWriterAux->pause();
     }
 
+    if (mStarted) {
+        mStarted = false;
+
+        uint32_t params = 0;
+        if (mAudioSource != AUDIO_SOURCE_LIST_END) {
+            params |= IMediaPlayerService::kBatteryDataTrackAudio;
+        }
+        if (mVideoSource != VIDEO_SOURCE_LIST_END) {
+            params |= IMediaPlayerService::kBatteryDataTrackVideo;
+        }
+
+        addBatteryData(params);
+    }
+
+
     return OK;
 }
 
@@ -1494,6 +1551,21 @@
         }
     }
 
+    if (mStarted) {
+        mStarted = false;
+
+        uint32_t params = 0;
+        if (mAudioSource != AUDIO_SOURCE_LIST_END) {
+            params |= IMediaPlayerService::kBatteryDataTrackAudio;
+        }
+        if (mVideoSource != VIDEO_SOURCE_LIST_END) {
+            params |= IMediaPlayerService::kBatteryDataTrackVideo;
+        }
+
+        addBatteryData(params);
+    }
+
+
     return err;
 }
 
diff --git a/media/libmediaplayerservice/StagefrightRecorder.h b/media/libmediaplayerservice/StagefrightRecorder.h
index 72225db..2c440c1 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.h
+++ b/media/libmediaplayerservice/StagefrightRecorder.h
@@ -107,6 +107,8 @@
     bool mIsMetaDataStoredInVideoBuffers;
     MediaProfiles *mEncoderProfiles;
 
+    bool mStarted;
+
     status_t setupMPEG4Recording(
         bool useSplitCameraSource,
         int outputFd,
diff --git a/media/libmediaplayerservice/TestPlayerStub.h b/media/libmediaplayerservice/TestPlayerStub.h
index 6abd8e3..d9c3db3 100644
--- a/media/libmediaplayerservice/TestPlayerStub.h
+++ b/media/libmediaplayerservice/TestPlayerStub.h
@@ -78,6 +78,10 @@
     virtual status_t setVideoSurface(const android::sp<android::Surface>& s)  {
         return mPlayer->setVideoSurface(s);
     }
+    virtual status_t setVideoSurfaceTexture(
+            const android::sp<android::ISurfaceTexture>& st)  {
+        return mPlayer->setVideoSurfaceTexture(st);
+    }
     virtual status_t prepare() {return mPlayer->prepare();}
     virtual status_t prepareAsync()  {return mPlayer->prepareAsync();}
     virtual status_t start()  {return mPlayer->start();}
diff --git a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp
index 6bf6dd3..b3314be 100644
--- a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp
@@ -33,8 +33,9 @@
 
 namespace android {
 
-NuPlayer::HTTPLiveSource::HTTPLiveSource(const char *url)
+NuPlayer::HTTPLiveSource::HTTPLiveSource(const char *url, uint32_t flags)
     : mURL(url),
+      mFlags(flags),
       mEOS(false),
       mOffset(0) {
 }
@@ -49,7 +50,9 @@
     mLiveLooper->setName("http live");
     mLiveLooper->start();
 
-    mLiveSession = new LiveSession;
+    mLiveSession = new LiveSession(
+            (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0);
+
     mLiveLooper->registerHandler(mLiveSession);
 
     mLiveSession->connect(mURL.c_str());
diff --git a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h
index f3f539a..a8ce7f4 100644
--- a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h
+++ b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h
@@ -27,7 +27,11 @@
 struct LiveSession;
 
 struct NuPlayer::HTTPLiveSource : public NuPlayer::Source {
-    HTTPLiveSource(const char *url);
+    enum Flags {
+        // Don't log any URLs.
+        kFlagIncognito = 1,
+    };
+    HTTPLiveSource(const char *url, uint32_t flags = 0);
 
     virtual void start();
 
@@ -46,6 +50,7 @@
 
 private:
     AString mURL;
+    uint32_t mFlags;
     bool mEOS;
     off64_t mOffset;
     sp<ALooper> mLiveLooper;
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
index 1fcf92b..474c056 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
@@ -37,6 +37,7 @@
 #include <media/stagefright/MediaErrors.h>
 #include <media/stagefright/MetaData.h>
 #include <surfaceflinger/Surface.h>
+#include <gui/ISurfaceTexture.h>
 
 namespace android {
 
@@ -71,13 +72,31 @@
         const char *url, const KeyedVector<String8, String8> *headers) {
     sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
 
-    msg->setObject("source", new HTTPLiveSource(url));
+    uint32_t flags = 0;
+
+    if (headers) {
+        ssize_t index = headers->indexOfKey(String8("x-hide-urls-from-log"));
+
+        if (index >= 0) {
+            flags |= HTTPLiveSource::kFlagIncognito;
+        }
+    }
+
+    msg->setObject("source", new HTTPLiveSource(url, flags));
     msg->post();
 }
 
 void NuPlayer::setVideoSurface(const sp<Surface> &surface) {
-    sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, id());
-    msg->setObject("surface", surface);
+    sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
+    msg->setObject("native-window", new NativeWindowWrapper(surface));
+    msg->post();
+}
+
+void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
+    sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
+    sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
+                new SurfaceTextureClient(surfaceTexture) : NULL);
+    msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
     msg->post();
 }
 
@@ -144,14 +163,14 @@
             break;
         }
 
-        case kWhatSetVideoSurface:
+        case kWhatSetVideoNativeWindow:
         {
-            LOGV("kWhatSetVideoSurface");
+            LOGV("kWhatSetVideoNativeWindow");
 
             sp<RefBase> obj;
-            CHECK(msg->findObject("surface", &obj));
+            CHECK(msg->findObject("native-window", &obj));
 
-            mSurface = static_cast<Surface *>(obj.get());
+            mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
             break;
         }
 
@@ -529,7 +548,8 @@
         new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
                      id());
 
-    *decoder = new Decoder(notify, audio ? NULL : mSurface);
+    *decoder = audio ? new Decoder(notify) :
+                       new Decoder(notify, mNativeWindow);
     looper()->registerHandler(*decoder);
 
     (*decoder)->configure(meta);
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.h b/media/libmediaplayerservice/nuplayer/NuPlayer.h
index bb65162..e7c6a42 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.h
@@ -20,6 +20,9 @@
 
 #include <media/MediaPlayerInterface.h>
 #include <media/stagefright/foundation/AHandler.h>
+#include <media/stagefright/NativeWindowWrapper.h>
+#include <gui/SurfaceTextureClient.h>
+#include <surfaceflinger/Surface.h>
 
 namespace android {
 
@@ -38,6 +41,7 @@
             const char *url, const KeyedVector<String8, String8> *headers);
 
     void setVideoSurface(const sp<Surface> &surface);
+    void setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
     void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
     void start();
 
@@ -65,7 +69,7 @@
 
     enum {
         kWhatSetDataSource,
-        kWhatSetVideoSurface,
+        kWhatSetVideoNativeWindow,
         kWhatSetAudioSink,
         kWhatMoreDataQueued,
         kWhatStart,
@@ -81,7 +85,7 @@
 
     wp<NuPlayerDriver> mDriver;
     sp<Source> mSource;
-    sp<Surface> mSurface;
+    sp<NativeWindowWrapper> mNativeWindow;
     sp<MediaPlayerBase::AudioSink> mAudioSink;
     sp<Decoder> mVideoDecoder;
     sp<Decoder> mAudioDecoder;
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
index 761dfa4..517acc9 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
@@ -31,13 +31,15 @@
 #include <media/stagefright/MetaData.h>
 #include <media/stagefright/Utils.h>
 #include <surfaceflinger/Surface.h>
+#include <gui/ISurfaceTexture.h>
 
 namespace android {
 
 NuPlayer::Decoder::Decoder(
-        const sp<AMessage> &notify, const sp<Surface> &surface)
+        const sp<AMessage> &notify,
+        const sp<NativeWindowWrapper> &nativeWindow)
     : mNotify(notify),
-      mSurface(surface) {
+      mNativeWindow(nativeWindow) {
 }
 
 NuPlayer::Decoder::~Decoder() {
@@ -55,8 +57,8 @@
 
     sp<AMessage> format = makeFormat(meta);
 
-    if (mSurface != NULL) {
-        format->setObject("surface", mSurface);
+    if (mNativeWindow != NULL) {
+        format->setObject("native-window", mNativeWindow);
     }
 
     if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h
index 3874cfe..732f090 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h
@@ -28,7 +28,8 @@
 struct DecoderWrapper;
 
 struct NuPlayer::Decoder : public AHandler {
-    Decoder(const sp<AMessage> &notify, const sp<Surface> &surface = NULL);
+    Decoder(const sp<AMessage> &notify,
+            const sp<NativeWindowWrapper> &nativeWindow = NULL);
 
     void configure(const sp<MetaData> &meta);
 
@@ -47,7 +48,7 @@
     };
 
     sp<AMessage> mNotify;
-    sp<Surface> mSurface;
+    sp<NativeWindowWrapper> mNativeWindow;
 
     sp<ACodec> mCodec;
     sp<DecoderWrapper> mWrapper;
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
index ac19a2f..0eca958 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
@@ -86,6 +86,13 @@
     return OK;
 }
 
+status_t NuPlayerDriver::setVideoSurfaceTexture(
+        const sp<ISurfaceTexture> &surfaceTexture) {
+    mPlayer->setVideoSurfaceTexture(surfaceTexture);
+
+    return OK;
+}
+
 status_t NuPlayerDriver::prepare() {
     return OK;
 }
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h
index e3a5de4..67d0f3e 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h
@@ -36,6 +36,8 @@
     virtual status_t setDataSource(const sp<IStreamSource> &source);
 
     virtual status_t setVideoSurface(const sp<Surface> &surface);
+    virtual status_t setVideoSurfaceTexture(
+            const sp<ISurfaceTexture> &surfaceTexture);
     virtual status_t prepare();
     virtual status_t prepareAsync();
     virtual status_t start();
diff --git a/media/libstagefright/AACExtractor.cpp b/media/libstagefright/AACExtractor.cpp
new file mode 100644
index 0000000..4203b6e
--- /dev/null
+++ b/media/libstagefright/AACExtractor.cpp
@@ -0,0 +1,323 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "AACExtractor"
+#include <utils/Log.h>
+
+#include "include/AACExtractor.h"
+#include "include/avc_utils.h"
+
+#include <media/stagefright/foundation/ABuffer.h>
+#include <media/stagefright/DataSource.h>
+#include <media/stagefright/MediaBufferGroup.h>
+#include <media/stagefright/MediaDebug.h>
+#include <media/stagefright/MediaDefs.h>
+#include <media/stagefright/MediaErrors.h>
+#include <media/stagefright/MediaSource.h>
+#include <media/stagefright/MetaData.h>
+#include <utils/String8.h>
+
+namespace android {
+
+#define ADTS_HEADER_LENGTH 7
+
+class AACSource : public MediaSource {
+public:
+    AACSource(const sp<DataSource> &source,
+              const sp<MetaData> &meta,
+              const Vector<uint64_t> &offset_vector,
+              int64_t frame_duration_us);
+
+    virtual status_t start(MetaData *params = NULL);
+    virtual status_t stop();
+
+    virtual sp<MetaData> getFormat();
+
+    virtual status_t read(
+            MediaBuffer **buffer, const ReadOptions *options = NULL);
+
+protected:
+    virtual ~AACSource();
+
+private:
+    static const size_t kMaxFrameSize;
+    sp<DataSource> mDataSource;
+    sp<MetaData> mMeta;
+
+    off64_t mOffset;
+    int64_t mCurrentTimeUs;
+    bool mStarted;
+    MediaBufferGroup *mGroup;
+
+    Vector<uint64_t> mOffsetVector;
+    int64_t mFrameDurationUs;
+
+    AACSource(const AACSource &);
+    AACSource &operator=(const AACSource &);
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Returns the sample rate based on the sampling frequency index
+uint32_t get_sample_rate(const uint8_t sf_index)
+{
+    static const uint32_t sample_rates[] =
+    {
+        96000, 88200, 64000, 48000, 44100, 32000,
+        24000, 22050, 16000, 12000, 11025, 8000
+    };
+
+    if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
+        return sample_rates[sf_index];
+    }
+
+    return 0;
+}
+
+static size_t getFrameSize(const sp<DataSource> &source, off64_t offset) {
+    size_t frameSize = 0;
+
+    uint8_t syncword[2];
+    if (source->readAt(0, &syncword, 2) != 2) {
+        return 0;
+    }
+    if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
+        return 0;
+    }
+
+    uint8_t protectionAbsent;
+    if (source->readAt(offset + 1, &protectionAbsent, 1) < 1) {
+        return 0;
+    }
+    protectionAbsent &= 0x1;
+
+    uint8_t header[3];
+    if (source->readAt(offset + 3, &header, 3) < 3) {
+        return 0;
+    }
+
+    frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
+    frameSize += ADTS_HEADER_LENGTH + protectionAbsent ? 0 : 2;
+
+    return frameSize;
+}
+
+AACExtractor::AACExtractor(const sp<DataSource> &source)
+    : mDataSource(source),
+      mInitCheck(NO_INIT),
+      mFrameDurationUs(0) {
+    String8 mimeType;
+    float confidence;
+    if (!SniffAAC(mDataSource, &mimeType, &confidence, NULL)) {
+        return;
+    }
+
+    uint8_t profile, sf_index, channel, header[2];
+    if (mDataSource->readAt(2, &header, 2) < 2) {
+        return;
+    }
+
+    profile = (header[0] >> 6) & 0x3;
+    sf_index = (header[0] >> 2) & 0xf;
+    uint32_t sr = get_sample_rate(sf_index);
+    if (sr == 0) {
+        return;
+    }
+    channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
+
+    mMeta = MakeAACCodecSpecificData(profile, sf_index, channel);
+
+    off64_t offset = 0;
+    off64_t streamSize, numFrames = 0;
+    size_t frameSize = 0;
+    int64_t duration = 0;
+
+    if (mDataSource->getSize(&streamSize) == OK) {
+         while (offset < streamSize) {
+            if ((frameSize = getFrameSize(source, offset)) == 0) {
+                return;
+            }
+
+            mOffsetVector.push(offset);
+
+            offset += frameSize;
+            numFrames ++;
+        }
+
+        // Round up and get the duration
+        mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr;
+        duration = numFrames * mFrameDurationUs;
+        mMeta->setInt64(kKeyDuration, duration);
+    }
+
+    mInitCheck = OK;
+}
+
+AACExtractor::~AACExtractor() {
+}
+
+sp<MetaData> AACExtractor::getMetaData() {
+    sp<MetaData> meta = new MetaData;
+
+    if (mInitCheck != OK) {
+        return meta;
+    }
+
+    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
+
+    return meta;
+}
+
+size_t AACExtractor::countTracks() {
+    return mInitCheck == OK ? 1 : 0;
+}
+
+sp<MediaSource> AACExtractor::getTrack(size_t index) {
+    if (mInitCheck != OK || index != 0) {
+        return NULL;
+    }
+
+    return new AACSource(mDataSource, mMeta, mOffsetVector, mFrameDurationUs);
+}
+
+sp<MetaData> AACExtractor::getTrackMetaData(size_t index, uint32_t flags) {
+    if (mInitCheck != OK || index != 0) {
+        return NULL;
+    }
+
+    return mMeta;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+// 8192 = 2^13, 13bit AAC frame size (in bytes)
+const size_t AACSource::kMaxFrameSize = 8192;
+
+AACSource::AACSource(
+        const sp<DataSource> &source, const sp<MetaData> &meta,
+        const Vector<uint64_t> &offset_vector,
+        int64_t frame_duration_us)
+    : mDataSource(source),
+      mMeta(meta),
+      mOffset(0),
+      mCurrentTimeUs(0),
+      mStarted(false),
+      mGroup(NULL),
+      mOffsetVector(offset_vector),
+      mFrameDurationUs(frame_duration_us) {
+}
+
+AACSource::~AACSource() {
+    if (mStarted) {
+        stop();
+    }
+}
+
+status_t AACSource::start(MetaData *params) {
+    CHECK(!mStarted);
+
+    mOffset = 0;
+    mCurrentTimeUs = 0;
+    mGroup = new MediaBufferGroup;
+    mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
+    mStarted = true;
+
+    return OK;
+}
+
+status_t AACSource::stop() {
+    CHECK(mStarted);
+
+    delete mGroup;
+    mGroup = NULL;
+
+    mStarted = false;
+    return OK;
+}
+
+sp<MetaData> AACSource::getFormat() {
+    return mMeta;
+}
+
+status_t AACSource::read(
+        MediaBuffer **out, const ReadOptions *options) {
+    *out = NULL;
+
+    int64_t seekTimeUs;
+    ReadOptions::SeekMode mode;
+    if (options && options->getSeekTo(&seekTimeUs, &mode)) {
+        if (mFrameDurationUs > 0) {
+            int64_t seekFrame = seekTimeUs / mFrameDurationUs;
+            mCurrentTimeUs = seekFrame * mFrameDurationUs;
+
+            mOffset = mOffsetVector.itemAt(seekFrame);
+        }
+    }
+
+    size_t frameSize, frameSizeWithoutHeader;
+    if ((frameSize = getFrameSize(mDataSource, mOffset)) == 0) {
+        return ERROR_END_OF_STREAM;
+    }
+
+    MediaBuffer *buffer;
+    status_t err = mGroup->acquire_buffer(&buffer);
+    if (err != OK) {
+        return err;
+    }
+
+    frameSizeWithoutHeader = frameSize - ADTS_HEADER_LENGTH;
+    if (mDataSource->readAt(mOffset + ADTS_HEADER_LENGTH, buffer->data(),
+                frameSizeWithoutHeader) != (ssize_t)frameSizeWithoutHeader) {
+        buffer->release();
+        buffer = NULL;
+
+        return ERROR_IO;
+    }
+
+    buffer->set_range(0, frameSizeWithoutHeader);
+    buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
+    buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
+
+    mOffset += frameSize;
+    mCurrentTimeUs += mFrameDurationUs;
+
+    *out = buffer;
+    return OK;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+bool SniffAAC(
+        const sp<DataSource> &source, String8 *mimeType, float *confidence,
+        sp<AMessage> *) {
+    uint8_t header[2];
+
+    if (source->readAt(0, &header, 2) != 2) {
+        return false;
+    }
+
+    // ADTS syncword
+    if ((header[0] == 0xff) && ((header[1] & 0xf6) == 0xf0)) {
+        *mimeType = MEDIA_MIMETYPE_AUDIO_AAC_ADTS;
+        *confidence = 0.2;
+        return true;
+    }
+
+    return false;
+}
+
+}  // namespace android
diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index 505d9d4..b0ae3d8 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -11,9 +11,11 @@
 #include <media/stagefright/foundation/AMessage.h>
 
 #include <media/stagefright/MediaDefs.h>
+#include <media/stagefright/NativeWindowWrapper.h>
 #include <media/stagefright/OMXClient.h>
 
 #include <surfaceflinger/Surface.h>
+#include <gui/SurfaceTextureClient.h>
 
 #include <OMX_Component.h>
 
@@ -1633,8 +1635,11 @@
     mCodec->configureCodec(mime.c_str(), msg);
 
     sp<RefBase> obj;
-    if (msg->findObject("surface", &obj)) {
-        mCodec->mNativeWindow = static_cast<Surface *>(obj.get());
+    if (msg->findObject("native-window", &obj)) {
+        sp<NativeWindowWrapper> nativeWindow(
+                static_cast<NativeWindowWrapper *>(obj.get()));
+        CHECK(nativeWindow != NULL);
+        mCodec->mNativeWindow = nativeWindow->getNativeWindow();
     }
 
     CHECK_EQ((status_t)OK, mCodec->initNativeWindow());
diff --git a/media/libstagefright/Android.mk b/media/libstagefright/Android.mk
index 029b238..88069e9 100644
--- a/media/libstagefright/Android.mk
+++ b/media/libstagefright/Android.mk
@@ -5,6 +5,7 @@
 
 LOCAL_SRC_FILES:=                         \
         ACodec.cpp                        \
+        AACExtractor.cpp                  \
         AMRExtractor.cpp                  \
         AMRWriter.cpp                     \
         AudioPlayer.cpp                   \
@@ -57,7 +58,8 @@
         $(TOP)/frameworks/base/include/media/stagefright/openmax \
         $(TOP)/external/flac/include \
         $(TOP)/external/tremolo \
-        $(TOP)/frameworks/base/media/libstagefright/rtsp
+        $(TOP)/frameworks/base/media/libstagefright/rtsp \
+        $(TOP)/external/openssl/include \
 
 LOCAL_SHARED_LIBRARIES := \
         libbinder         \
@@ -71,7 +73,9 @@
         libstagefright_yuv \
         libcamera_client \
         libdrmframework  \
-        libcrypto
+        libcrypto        \
+        libssl           \
+        libgui
 
 LOCAL_STATIC_LIBRARIES := \
         libstagefright_color_conversion \
diff --git a/media/libstagefright/AudioSource.cpp b/media/libstagefright/AudioSource.cpp
index 7a1d73b..bbdec02 100644
--- a/media/libstagefright/AudioSource.cpp
+++ b/media/libstagefright/AudioSource.cpp
@@ -18,38 +18,54 @@
 #define LOG_TAG "AudioSource"
 #include <utils/Log.h>
 
-#include <media/stagefright/AudioSource.h>
-
 #include <media/AudioRecord.h>
-#include <media/stagefright/MediaBufferGroup.h>
-#include <media/stagefright/MediaDebug.h>
+#include <media/stagefright/AudioSource.h>
+#include <media/stagefright/MediaBuffer.h>
 #include <media/stagefright/MediaDefs.h>
 #include <media/stagefright/MetaData.h>
+#include <media/stagefright/foundation/ADebug.h>
 #include <cutils/properties.h>
 #include <stdlib.h>
 
 namespace android {
 
+static void AudioRecordCallbackFunction(int event, void *user, void *info) {
+    AudioSource *source = (AudioSource *) user;
+    switch (event) {
+        case AudioRecord::EVENT_MORE_DATA: {
+            source->dataCallbackTimestamp(*((AudioRecord::Buffer *) info), systemTime() / 1000);
+            break;
+        }
+        case AudioRecord::EVENT_OVERRUN: {
+            LOGW("AudioRecord reported overrun!");
+            break;
+        }
+        default:
+            // does nothing
+            break;
+    }
+}
+
 AudioSource::AudioSource(
         int inputSource, uint32_t sampleRate, uint32_t channels)
     : mStarted(false),
-      mCollectStats(false),
+      mSampleRate(sampleRate),
       mPrevSampleTimeUs(0),
-      mTotalLostFrames(0),
-      mPrevLostBytes(0),
-      mGroup(NULL) {
+      mNumFramesReceived(0),
+      mNumClientOwnedBuffers(0) {
 
     LOGV("sampleRate: %d, channels: %d", sampleRate, channels);
     CHECK(channels == 1 || channels == 2);
     uint32_t flags = AudioRecord::RECORD_AGC_ENABLE |
                      AudioRecord::RECORD_NS_ENABLE  |
                      AudioRecord::RECORD_IIR_ENABLE;
-
     mRecord = new AudioRecord(
                 inputSource, sampleRate, AudioSystem::PCM_16_BIT,
                 channels > 1? AudioSystem::CHANNEL_IN_STEREO: AudioSystem::CHANNEL_IN_MONO,
                 4 * kMaxBufferSize / sizeof(int16_t), /* Enable ping-pong buffers */
-                flags);
+                flags,
+                AudioRecordCallbackFunction,
+                this);
 
     mInitCheck = mRecord->initCheck();
 }
@@ -68,6 +84,7 @@
 }
 
 status_t AudioSource::start(MetaData *params) {
+    Mutex::Autolock autoLock(mLock);
     if (mStarted) {
         return UNKNOWN_ERROR;
     }
@@ -76,12 +93,6 @@
         return NO_INIT;
     }
 
-    char value[PROPERTY_VALUE_MAX];
-    if (property_get("media.stagefright.record-stats", value, NULL)
-        && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
-        mCollectStats = true;
-    }
-
     mTrackMaxAmplitude = false;
     mMaxAmplitude = 0;
     mInitialReadTimeUs = 0;
@@ -92,9 +103,6 @@
     }
     status_t err = mRecord->start();
     if (err == OK) {
-        mGroup = new MediaBufferGroup;
-        mGroup->add_buffer(new MediaBuffer(kMaxBufferSize));
-
         mStarted = true;
     } else {
         delete mRecord;
@@ -105,7 +113,25 @@
     return err;
 }
 
+void AudioSource::releaseQueuedFrames_l() {
+    LOGV("releaseQueuedFrames_l");
+    List<MediaBuffer *>::iterator it;
+    while (!mBuffersReceived.empty()) {
+        it = mBuffersReceived.begin();
+        (*it)->release();
+        mBuffersReceived.erase(it);
+    }
+}
+
+void AudioSource::waitOutstandingEncodingFrames_l() {
+    LOGV("waitOutstandingEncodingFrames_l: %lld", mNumClientOwnedBuffers);
+    while (mNumClientOwnedBuffers > 0) {
+        mFrameEncodingCompletionCondition.wait(mLock);
+    }
+}
+
 status_t AudioSource::stop() {
+    Mutex::Autolock autoLock(mLock);
     if (!mStarted) {
         return UNKNOWN_ERROR;
     }
@@ -114,29 +140,23 @@
         return NO_INIT;
     }
 
-    mRecord->stop();
-
-    delete mGroup;
-    mGroup = NULL;
-
     mStarted = false;
-
-    if (mCollectStats) {
-        LOGI("Total lost audio frames: %lld",
-            mTotalLostFrames + (mPrevLostBytes >> 1));
-    }
+    mRecord->stop();
+    waitOutstandingEncodingFrames_l();
+    releaseQueuedFrames_l();
 
     return OK;
 }
 
 sp<MetaData> AudioSource::getFormat() {
+    Mutex::Autolock autoLock(mLock);
     if (mInitCheck != OK) {
         return 0;
     }
 
     sp<MetaData> meta = new MetaData;
     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
-    meta->setInt32(kKeySampleRate, mRecord->getSampleRate());
+    meta->setInt32(kKeySampleRate, mSampleRate);
     meta->setInt32(kKeyChannelCount, mRecord->channelCount());
     meta->setInt32(kKeyMaxInputSize, kMaxBufferSize);
 
@@ -177,121 +197,131 @@
 
 status_t AudioSource::read(
         MediaBuffer **out, const ReadOptions *options) {
+    Mutex::Autolock autoLock(mLock);
+    *out = NULL;
 
     if (mInitCheck != OK) {
         return NO_INIT;
     }
 
-    int64_t readTimeUs = systemTime() / 1000;
-    *out = NULL;
-
-    MediaBuffer *buffer;
-    CHECK_EQ(mGroup->acquire_buffer(&buffer), OK);
-
-    int err = 0;
-    if (mStarted) {
-
-        uint32_t numFramesRecorded;
-        mRecord->getPosition(&numFramesRecorded);
-
-
-        if (numFramesRecorded == 0 && mPrevSampleTimeUs == 0) {
-            mInitialReadTimeUs = readTimeUs;
-            // Initial delay
-            if (mStartTimeUs > 0) {
-                mStartTimeUs = readTimeUs - mStartTimeUs;
-            } else {
-                // Assume latency is constant.
-                mStartTimeUs += mRecord->latency() * 1000;
-            }
-            mPrevSampleTimeUs = mStartTimeUs;
-        }
-
-        uint32_t sampleRate = mRecord->getSampleRate();
-
-        // Insert null frames when lost frames are detected.
-        int64_t timestampUs = mPrevSampleTimeUs;
-        uint32_t numLostBytes = mRecord->getInputFramesLost() << 1;
-        numLostBytes += mPrevLostBytes;
-#if 0
-        // Simulate lost frames
-        numLostBytes = ((rand() * 1.0 / RAND_MAX)) * 2 * kMaxBufferSize;
-        numLostBytes &= 0xFFFFFFFE; // Alignment requirement
-
-        // Reduce the chance to lose
-        if (rand() * 1.0 / RAND_MAX >= 0.05) {
-            numLostBytes = 0;
-        }
-#endif
-        if (numLostBytes > 0) {
-            if (numLostBytes > kMaxBufferSize) {
-                mPrevLostBytes = numLostBytes - kMaxBufferSize;
-                numLostBytes = kMaxBufferSize;
-            } else {
-                mPrevLostBytes = 0;
-            }
-
-            CHECK_EQ(numLostBytes & 1, 0);
-            timestampUs += ((1000000LL * (numLostBytes >> 1)) +
-                    (sampleRate >> 1)) / sampleRate;
-
-            if (mCollectStats) {
-                mTotalLostFrames += (numLostBytes >> 1);
-            }
-            memset(buffer->data(), 0, numLostBytes);
-            buffer->set_range(0, numLostBytes);
-            if (numFramesRecorded == 0) {
-                buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs);
-            }
-            buffer->meta_data()->setInt64(kKeyTime, mStartTimeUs + mPrevSampleTimeUs);
-            buffer->meta_data()->setInt64(kKeyDriftTime, readTimeUs - mInitialReadTimeUs);
-            mPrevSampleTimeUs = timestampUs;
-            *out = buffer;
-            return OK;
-        }
-
-        ssize_t n = mRecord->read(buffer->data(), buffer->size());
-        if (n <= 0) {
-            buffer->release();
-            LOGE("Read from AudioRecord returns %d", n);
-            return UNKNOWN_ERROR;
-        }
-
-        int64_t recordDurationUs = (1000000LL * n >> 1) / sampleRate;
-        timestampUs += recordDurationUs;
-
-        if (mPrevSampleTimeUs - mStartTimeUs < kAutoRampStartUs) {
-            // Mute the initial video recording signal
-            memset((uint8_t *) buffer->data(), 0, n);
-        } else if (mPrevSampleTimeUs - mStartTimeUs < kAutoRampStartUs + kAutoRampDurationUs) {
-            int32_t autoRampDurationFrames =
-                    (kAutoRampDurationUs * sampleRate + 500000LL) / 1000000LL;
-
-            int32_t autoRampStartFrames =
-                    (kAutoRampStartUs * sampleRate + 500000LL) / 1000000LL;
-
-            int32_t nFrames = numFramesRecorded - autoRampStartFrames;
-            rampVolume(nFrames, autoRampDurationFrames, (uint8_t *) buffer->data(), n);
-        }
-        if (mTrackMaxAmplitude) {
-            trackMaxAmplitude((int16_t *) buffer->data(), n >> 1);
-        }
-
-        if (numFramesRecorded == 0) {
-            buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs);
-        }
-
-        buffer->meta_data()->setInt64(kKeyTime, mStartTimeUs + mPrevSampleTimeUs);
-        buffer->meta_data()->setInt64(kKeyDriftTime, readTimeUs - mInitialReadTimeUs);
-        mPrevSampleTimeUs = timestampUs;
-        LOGV("initial delay: %lld, sample rate: %d, timestamp: %lld",
-                mStartTimeUs, sampleRate, timestampUs);
-
-        buffer->set_range(0, n);
-
-        *out = buffer;
+    while (mStarted && mBuffersReceived.empty()) {
+        mFrameAvailableCondition.wait(mLock);
+    }
+    if (!mStarted) {
         return OK;
     }
+    MediaBuffer *buffer = *mBuffersReceived.begin();
+    mBuffersReceived.erase(mBuffersReceived.begin());
+    ++mNumClientOwnedBuffers;
+    buffer->setObserver(this);
+    buffer->add_ref();
+
+    // Mute/suppress the recording sound
+    int64_t timeUs;
+    CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
+    int64_t elapsedTimeUs = timeUs - mStartTimeUs;
+    if (elapsedTimeUs < kAutoRampStartUs) {
+        memset((uint8_t *) buffer->data(), 0, buffer->range_length());
+    } else if (elapsedTimeUs < kAutoRampStartUs + kAutoRampDurationUs) {
+        int32_t autoRampDurationFrames =
+                    (kAutoRampDurationUs * mSampleRate + 500000LL) / 1000000LL;
+
+        int32_t autoRampStartFrames =
+                    (kAutoRampStartUs * mSampleRate + 500000LL) / 1000000LL;
+
+        int32_t nFrames = mNumFramesReceived - autoRampStartFrames;
+        rampVolume(nFrames, autoRampDurationFrames,
+                (uint8_t *) buffer->data(), buffer->range_length());
+    }
+
+    // Track the max recording signal amplitude.
+    if (mTrackMaxAmplitude) {
+        trackMaxAmplitude(
+            (int16_t *) buffer->data(), buffer->range_length() >> 1);
+    }
+
+    *out = buffer;
+    return OK;
+}
+
+void AudioSource::signalBufferReturned(MediaBuffer *buffer) {
+    LOGV("signalBufferReturned: %p", buffer->data());
+    Mutex::Autolock autoLock(mLock);
+    --mNumClientOwnedBuffers;
+    buffer->setObserver(0);
+    buffer->release();
+    mFrameEncodingCompletionCondition.signal();
+    return;
+}
+
+status_t AudioSource::dataCallbackTimestamp(
+        const AudioRecord::Buffer& audioBuffer, int64_t timeUs) {
+    LOGV("dataCallbackTimestamp: %lld us", timeUs);
+    Mutex::Autolock autoLock(mLock);
+    if (!mStarted) {
+        LOGW("Spurious callback from AudioRecord. Drop the audio data.");
+        return OK;
+    }
+
+    // Drop retrieved and previously lost audio data.
+    if (mNumFramesReceived == 0 && timeUs < mStartTimeUs) {
+        mRecord->getInputFramesLost();
+        LOGV("Drop audio data at %lld/%lld us", timeUs, mStartTimeUs);
+        return OK;
+    }
+
+    if (mNumFramesReceived == 0 && mPrevSampleTimeUs == 0) {
+        mInitialReadTimeUs = timeUs;
+        // Initial delay
+        if (mStartTimeUs > 0) {
+            mStartTimeUs = timeUs - mStartTimeUs;
+        } else {
+            // Assume latency is constant.
+            mStartTimeUs += mRecord->latency() * 1000;
+        }
+        mPrevSampleTimeUs = mStartTimeUs;
+    }
+
+    int64_t timestampUs = mPrevSampleTimeUs;
+
+    size_t numLostBytes = 0;
+    if (mNumFramesReceived > 0) {  // Ignore earlier frame lost
+        // getInputFramesLost() returns the number of lost frames.
+        // Convert number of frames lost to number of bytes lost.
+        numLostBytes = mRecord->getInputFramesLost() * mRecord->frameSize();
+    }
+
+    CHECK_EQ(numLostBytes & 1, 0u);
+    CHECK_EQ(audioBuffer.size & 1, 0u);
+    size_t bufferSize = numLostBytes + audioBuffer.size;
+    MediaBuffer *buffer = new MediaBuffer(bufferSize);
+    if (numLostBytes > 0) {
+        memset(buffer->data(), 0, numLostBytes);
+        memcpy((uint8_t *) buffer->data() + numLostBytes,
+                    audioBuffer.i16, audioBuffer.size);
+    } else {
+        if (audioBuffer.size == 0) {
+            LOGW("Nothing is available from AudioRecord callback buffer");
+            buffer->release();
+            return OK;
+        }
+        memcpy((uint8_t *) buffer->data(),
+                audioBuffer.i16, audioBuffer.size);
+    }
+
+    buffer->set_range(0, bufferSize);
+    timestampUs += ((1000000LL * (bufferSize >> 1)) +
+                    (mSampleRate >> 1)) / mSampleRate;
+
+    if (mNumFramesReceived == 0) {
+        buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs);
+    }
+    buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs);
+    buffer->meta_data()->setInt64(kKeyDriftTime, timeUs - mInitialReadTimeUs);
+    mPrevSampleTimeUs = timestampUs;
+    mNumFramesReceived += buffer->range_length() / sizeof(int16_t);
+    mBuffersReceived.push_back(buffer);
+    mFrameAvailableCondition.signal();
 
     return OK;
 }
diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp
index 8963951..4c744bd 100644
--- a/media/libstagefright/AwesomePlayer.cpp
+++ b/media/libstagefright/AwesomePlayer.cpp
@@ -28,6 +28,8 @@
 #include "include/MPEG2TSExtractor.h"
 
 #include <binder/IPCThreadState.h>
+#include <binder/IServiceManager.h>
+#include <media/IMediaPlayerService.h>
 #include <media/stagefright/foundation/hexdump.h>
 #include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/AudioPlayer.h>
@@ -41,13 +43,14 @@
 #include <media/stagefright/OMXCodec.h>
 
 #include <surfaceflinger/Surface.h>
+#include <gui/ISurfaceTexture.h>
+#include <gui/SurfaceTextureClient.h>
 
 #include <media/stagefright/foundation/ALooper.h>
 #include <media/stagefright/foundation/AMessage.h>
-#include "include/LiveSession.h"
 
 #define USE_SURFACE_ALLOC 1
-#define FRAME_DROP_FREQ 7
+#define FRAME_DROP_FREQ 0
 
 namespace android {
 
@@ -82,8 +85,8 @@
 
 struct AwesomeLocalRenderer : public AwesomeRenderer {
     AwesomeLocalRenderer(
-            const sp<Surface> &surface, const sp<MetaData> &meta)
-        : mTarget(new SoftwareRenderer(surface, meta)) {
+            const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta)
+        : mTarget(new SoftwareRenderer(nativeWindow, meta)) {
     }
 
     virtual void render(MediaBuffer *buffer) {
@@ -156,8 +159,17 @@
             const AwesomeNativeWindowRenderer &);
 };
 
-////////////////////////////////////////////////////////////////////////////////
+// To collect the decoder usage
+void addBatteryData(uint32_t params) {
+    sp<IBinder> binder =
+        defaultServiceManager()->getService(String16("media.player"));
+    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
+    CHECK(service.get() != NULL);
 
+    service->addBatteryData(params);
+}
+
+////////////////////////////////////////////////////////////////////////////////
 AwesomePlayer::AwesomePlayer()
     : mQueueStarted(false),
       mTimeSource(NULL),
@@ -233,19 +245,24 @@
 
     mUri = uri;
 
-    if (!strncmp("http://", uri, 7)) {
-        // Hack to support http live.
+    if (headers) {
+        mUriHeaders = *headers;
 
-        size_t len = strlen(uri);
-        if (!strcasecmp(&uri[len - 5], ".m3u8")
-                || strstr(&uri[7], "m3u8") != NULL) {
-            mUri = "httplive://";
-            mUri.append(&uri[7]);
+        ssize_t index = mUriHeaders.indexOfKey(String8("x-hide-urls-from-log"));
+        if (index >= 0) {
+            // Browser is in "incognito" mode, suppress logging URLs.
+
+            // This isn't something that should be passed to the server.
+            mUriHeaders.removeItemsAt(index);
+
+            mFlags |= INCOGNITO;
         }
     }
 
-    if (headers) {
-        mUriHeaders = *headers;
+    if (!(mFlags & INCOGNITO)) {
+        LOGI("setDataSource_l('%s')", mUri.string());
+    } else {
+        LOGI("setDataSource_l(URL suppressed)");
     }
 
     // The actual work will be done during preparation in the call to
@@ -376,14 +393,11 @@
 }
 
 void AwesomePlayer::reset() {
-    LOGI("reset");
-
     Mutex::Autolock autoLock(mLock);
     reset_l();
 }
 
 void AwesomePlayer::reset_l() {
-    LOGI("reset_l");
     mDisplayWidth = 0;
     mDisplayHeight = 0;
 
@@ -394,11 +408,25 @@
             mDrmManagerClient = NULL;
     }
 
+    if (mFlags & PLAYING) {
+        uint32_t params = IMediaPlayerService::kBatteryDataTrackDecoder;
+        if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) {
+            params |= IMediaPlayerService::kBatteryDataTrackAudio;
+        }
+        if (mVideoSource != NULL) {
+            params |= IMediaPlayerService::kBatteryDataTrackVideo;
+        }
+        addBatteryData(params);
+    }
+
     if (mFlags & PREPARING) {
         mFlags |= PREPARE_CANCELLED;
         if (mConnectingDataSource != NULL) {
             LOGI("interrupting the connection process");
             mConnectingDataSource->disconnect();
+        } else if (mConnectingRTSPController != NULL) {
+            LOGI("interrupting the connection process");
+            mConnectingRTSPController->disconnect();
         }
 
         if (mFlags & PREPARING_CONNECTED) {
@@ -408,10 +436,6 @@
         }
     }
 
-    if (mFlags & PREPARING) {
-        LOGI("waiting until preparation is completes.");
-    }
-
     while (mFlags & PREPARING) {
         mPreparedCondition.wait(mLock);
     }
@@ -435,8 +459,6 @@
     }
     mAudioSource.clear();
 
-    LOGI("audio source cleared");
-
     mTimeSource = NULL;
 
     delete mAudioPlayer;
@@ -454,11 +476,6 @@
         mRTSPController.clear();
     }
 
-    if (mLiveSession != NULL) {
-        mLiveSession->disconnect();
-        mLiveSession.clear();
-    }
-
     if (mVideoSource != NULL) {
         mVideoSource->stop();
 
@@ -473,15 +490,13 @@
         IPCThreadState::self()->flushCommands();
     }
 
-    LOGI("video source cleared");
-
     mDurationUs = -1;
     mFlags = 0;
     mExtractorFlags = 0;
     mTimeSourceDeltaUs = 0;
     mVideoTimeUs = 0;
 
-    mSeeking = false;
+    mSeeking = NO_SEEK;
     mSeekNotificationSent = false;
     mSeekTimeUs = 0;
 
@@ -491,8 +506,6 @@
     mFileSource.clear();
 
     mBitrate = -1;
-
-    LOGI("reset_l completed");
 }
 
 void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) {
@@ -665,35 +678,6 @@
     postBufferingEvent_l();
 }
 
-void AwesomePlayer::partial_reset_l() {
-    // Only reset the video renderer and shut down the video decoder.
-    // Then instantiate a new video decoder and resume video playback.
-
-    mVideoRenderer.clear();
-
-    if (mVideoBuffer) {
-        mVideoBuffer->release();
-        mVideoBuffer = NULL;
-    }
-
-    {
-        mVideoSource->stop();
-
-        // The following hack is necessary to ensure that the OMX
-        // component is completely released by the time we may try
-        // to instantiate it again.
-        wp<MediaSource> tmp = mVideoSource;
-        mVideoSource.clear();
-        while (tmp.promote() != NULL) {
-            usleep(1000);
-        }
-        IPCThreadState::self()->flushCommands();
-    }
-
-    CHECK_EQ((status_t)OK,
-             initVideoDecoder(OMXCodec::kIgnoreCodecSpecificData));
-}
-
 void AwesomePlayer::onStreamDone() {
     // Posted whenever any stream finishes playing.
 
@@ -703,21 +687,7 @@
     }
     mStreamDoneEventPending = false;
 
-    if (mStreamDoneStatus == INFO_DISCONTINUITY) {
-        // This special status is returned because an http live stream's
-        // video stream switched to a different bandwidth at this point
-        // and future data may have been encoded using different parameters.
-        // This requires us to shutdown the video decoder and reinstantiate
-        // a fresh one.
-
-        LOGV("INFO_DISCONTINUITY");
-
-        CHECK(mVideoSource != NULL);
-
-        partial_reset_l();
-        postVideoEvent_l();
-        return;
-    } else if (mStreamDoneStatus != ERROR_END_OF_STREAM) {
+    if (mStreamDoneStatus != ERROR_END_OF_STREAM) {
         LOGV("MEDIA_ERROR %d", mStreamDoneStatus);
 
         notifyListener_l(
@@ -794,25 +764,6 @@
                 mAudioPlayer = new AudioPlayer(mAudioSink, this);
                 mAudioPlayer->setSource(mAudioSource);
 
-                // We've already started the MediaSource in order to enable
-                // the prefetcher to read its data.
-                status_t err = mAudioPlayer->start(
-                        true /* sourceAlreadyStarted */);
-
-                if (err != OK) {
-                    delete mAudioPlayer;
-                    mAudioPlayer = NULL;
-
-                    mFlags &= ~(PLAYING | FIRST_FRAME);
-
-                    if (mDecryptHandle != NULL) {
-                        mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
-                                 Playback::STOP, 0);
-                    }
-
-                    return err;
-                }
-
                 mTimeSource = mAudioPlayer;
 
                 deferredAudioSeek = true;
@@ -820,8 +771,26 @@
                 mWatchForAudioSeekComplete = false;
                 mWatchForAudioEOS = true;
             }
-        } else {
-            mAudioPlayer->resume();
+        }
+
+        CHECK(!(mFlags & AUDIO_RUNNING));
+
+        if (mVideoSource == NULL) {
+            status_t err = startAudioPlayer_l();
+
+            if (err != OK) {
+                delete mAudioPlayer;
+                mAudioPlayer = NULL;
+
+                mFlags &= ~(PLAYING | FIRST_FRAME);
+
+                if (mDecryptHandle != NULL) {
+                    mDrmManagerClient->setPlaybackStatus(
+                            mDecryptHandle, Playback::STOP, 0);
+                }
+
+                return err;
+            }
         }
     }
 
@@ -850,6 +819,46 @@
         seekTo_l(0);
     }
 
+    uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted
+        | IMediaPlayerService::kBatteryDataTrackDecoder;
+    if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) {
+        params |= IMediaPlayerService::kBatteryDataTrackAudio;
+    }
+    if (mVideoSource != NULL) {
+        params |= IMediaPlayerService::kBatteryDataTrackVideo;
+    }
+    addBatteryData(params);
+
+    return OK;
+}
+
+status_t AwesomePlayer::startAudioPlayer_l() {
+    CHECK(!(mFlags & AUDIO_RUNNING));
+
+    if (mAudioSource == NULL || mAudioPlayer == NULL) {
+        return OK;
+    }
+
+    if (!(mFlags & AUDIOPLAYER_STARTED)) {
+        mFlags |= AUDIOPLAYER_STARTED;
+
+        // We've already started the MediaSource in order to enable
+        // the prefetcher to read its data.
+        status_t err = mAudioPlayer->start(
+                true /* sourceAlreadyStarted */);
+
+        if (err != OK) {
+            notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
+            return err;
+        }
+    } else {
+        mAudioPlayer->resume();
+    }
+
+    mFlags |= AUDIO_RUNNING;
+
+    mWatchForAudioEOS = true;
+
     return OK;
 }
 
@@ -898,7 +907,7 @@
 }
 
 void AwesomePlayer::initRenderer_l() {
-    if (mSurface == NULL) {
+    if (mNativeWindow == NULL) {
         return;
     }
 
@@ -929,13 +938,13 @@
         // directly to ANativeBuffers, so we must use a renderer that
         // just pushes those buffers to the ANativeWindow.
         mVideoRenderer =
-            new AwesomeNativeWindowRenderer(mSurface, rotationDegrees);
+            new AwesomeNativeWindowRenderer(mNativeWindow, rotationDegrees);
     } else {
         // Other decoders are instantiated locally and as a consequence
         // allocate their buffers in local address space.  This renderer
         // then performs a color conversion and copy to get the data
         // into the ANativeBuffer.
-        mVideoRenderer = new AwesomeLocalRenderer(mSurface, meta);
+        mVideoRenderer = new AwesomeLocalRenderer(mNativeWindow, meta);
     }
 }
 
@@ -954,7 +963,7 @@
 
     cancelPlayerEvents(true /* keepBufferingGoing */);
 
-    if (mAudioPlayer != NULL) {
+    if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) {
         if (at_eos) {
             // If we played the audio stream to completion we
             // want to make sure that all samples remaining in the audio
@@ -963,6 +972,8 @@
         } else {
             mAudioPlayer->pause();
         }
+
+        mFlags &= ~AUDIO_RUNNING;
     }
 
     mFlags &= ~PLAYING;
@@ -972,6 +983,16 @@
                 Playback::PAUSE, 0);
     }
 
+    uint32_t params = IMediaPlayerService::kBatteryDataTrackDecoder;
+    if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) {
+        params |= IMediaPlayerService::kBatteryDataTrackAudio;
+    }
+    if (mVideoSource != NULL) {
+        params |= IMediaPlayerService::kBatteryDataTrackVideo;
+    }
+
+    addBatteryData(params);
+
     return OK;
 }
 
@@ -983,6 +1004,17 @@
     Mutex::Autolock autoLock(mLock);
 
     mSurface = surface;
+    mNativeWindow = surface;
+}
+
+void AwesomePlayer::setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
+    Mutex::Autolock autoLock(mLock);
+
+    mSurface.clear();
+    if (surfaceTexture != NULL) {
+        mNativeWindow = new SurfaceTextureClient(surfaceTexture);
+    }
+
 }
 
 void AwesomePlayer::setAudioSink(
@@ -1020,7 +1052,7 @@
     if (mRTSPController != NULL) {
         *positionUs = mRTSPController->getNormalPlayTimeUs();
     }
-    else if (mSeeking) {
+    else if (mSeeking != NO_SEEK) {
         *positionUs = mSeekTimeUs;
     } else if (mVideoSource != NULL) {
         Mutex::Autolock autoLock(mMiscStateLock);
@@ -1064,7 +1096,7 @@
         play_l();
     }
 
-    mSeeking = true;
+    mSeeking = SEEK;
     mSeekNotificationSent = false;
     mSeekTimeUs = timeUs;
     mFlags &= ~(AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS);
@@ -1088,7 +1120,7 @@
 }
 
 void AwesomePlayer::seekAudioIfNecessary_l() {
-    if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) {
+    if (mSeeking != NO_SEEK && mVideoSource == NULL && mAudioPlayer != NULL) {
         mAudioPlayer->seekTo(mSeekTimeUs);
 
         mWatchForAudioSeekComplete = true;
@@ -1161,7 +1193,7 @@
             mClient.interface(), mVideoTrack->getFormat(),
             false, // createEncoder
             mVideoTrack,
-            NULL, flags, USE_SURFACE_ALLOC ? mSurface : NULL);
+            NULL, flags, USE_SURFACE_ALLOC ? mNativeWindow : NULL);
 
     if (mVideoSource != NULL) {
         int64_t durationUs;
@@ -1184,7 +1216,12 @@
 }
 
 void AwesomePlayer::finishSeekIfNecessary(int64_t videoTimeUs) {
-    if (!mSeeking || (mFlags & SEEK_PREVIEW)) {
+    if (mSeeking == SEEK_VIDEO_ONLY) {
+        mSeeking = NO_SEEK;
+        return;
+    }
+
+    if (mSeeking == NO_SEEK || (mFlags & SEEK_PREVIEW)) {
         return;
     }
 
@@ -1195,9 +1232,7 @@
         // requested seek time instead.
 
         mAudioPlayer->seekTo(videoTimeUs < 0 ? mSeekTimeUs : videoTimeUs);
-        mAudioPlayer->resume();
         mWatchForAudioSeekComplete = true;
-        mWatchForAudioEOS = true;
     } else if (!mSeekNotificationSent) {
         // If we're playing video only, report seek complete now,
         // otherwise audio player will notify us later.
@@ -1205,7 +1240,7 @@
     }
 
     mFlags |= FIRST_FRAME;
-    mSeeking = false;
+    mSeeking = NO_SEEK;
     mSeekNotificationSent = false;
 
     if (mDecryptHandle != NULL) {
@@ -1225,13 +1260,13 @@
     }
     mVideoEventPending = false;
 
-    if (mSeeking) {
+    if (mSeeking != NO_SEEK) {
         if (mVideoBuffer) {
             mVideoBuffer->release();
             mVideoBuffer = NULL;
         }
 
-        if (mCachedSource != NULL && mAudioSource != NULL
+        if (mSeeking == SEEK && mCachedSource != NULL && mAudioSource != NULL
                 && !(mFlags & SEEK_PREVIEW)) {
             // We're going to seek the video source first, followed by
             // the audio source.
@@ -1241,8 +1276,10 @@
             // locations, we'll "pause" the audio source, causing it to
             // stop reading input data until a subsequent seek.
 
-            if (mAudioPlayer != NULL) {
+            if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) {
                 mAudioPlayer->pause();
+
+                mFlags &= ~AUDIO_RUNNING;
             }
             mAudioSource->pause();
         }
@@ -1250,11 +1287,14 @@
 
     if (!mVideoBuffer) {
         MediaSource::ReadOptions options;
-        if (mSeeking) {
+        if (mSeeking != NO_SEEK) {
             LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6);
 
             options.setSeekTo(
-                    mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
+                    mSeekTimeUs,
+                    mSeeking == SEEK_VIDEO_ONLY
+                        ? MediaSource::ReadOptions::SEEK_NEXT_SYNC
+                        : MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
         }
         for (;;) {
             status_t err = mVideoSource->read(&mVideoBuffer, &options);
@@ -1278,7 +1318,7 @@
                 // So video playback is complete, but we may still have
                 // a seek request pending that needs to be applied
                 // to the audio track.
-                if (mSeeking) {
+                if (mSeeking != NO_SEEK) {
                     LOGV("video stream ended while seeking!");
                 }
                 finishSeekIfNecessary(-1);
@@ -1304,14 +1344,29 @@
     int64_t timeUs;
     CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
 
+    if (mSeeking == SEEK_VIDEO_ONLY) {
+        if (mSeekTimeUs > timeUs) {
+            LOGI("XXX mSeekTimeUs = %lld us, timeUs = %lld us",
+                 mSeekTimeUs, timeUs);
+        }
+    }
+
     {
         Mutex::Autolock autoLock(mMiscStateLock);
         mVideoTimeUs = timeUs;
     }
 
-    bool wasSeeking = mSeeking;
+    SeekType wasSeeking = mSeeking;
     finishSeekIfNecessary(timeUs);
 
+    if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) {
+        status_t err = startAudioPlayer_l();
+        if (err != OK) {
+            LOGE("Startung the audio player failed w/ err %d", err);
+            return;
+        }
+    }
+
     TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource;
 
     if (mFlags & FIRST_FRAME) {
@@ -1326,13 +1381,41 @@
         mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
     }
 
-    if (!wasSeeking) {
+    if (wasSeeking == SEEK_VIDEO_ONLY) {
+        int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
+
+        int64_t latenessUs = nowUs - timeUs;
+
+        if (latenessUs > 0) {
+            LOGI("after SEEK_VIDEO_ONLY we're late by %.2f secs", latenessUs / 1E6);
+        }
+    }
+
+    if (wasSeeking == NO_SEEK) {
         // Let's display the first frame after seeking right away.
 
         int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
 
         int64_t latenessUs = nowUs - timeUs;
 
+        if (latenessUs > 500000ll
+                && mRTSPController == NULL
+                && mAudioPlayer != NULL
+                && mAudioPlayer->getMediaTimeMapping(
+                    &realTimeUs, &mediaTimeUs)) {
+            LOGI("we're much too late (%.2f secs), video skipping ahead",
+                 latenessUs / 1E6);
+
+            mVideoBuffer->release();
+            mVideoBuffer = NULL;
+
+            mSeeking = SEEK_VIDEO_ONLY;
+            mSeekTimeUs = mediaTimeUs;
+
+            postVideoEvent_l();
+            return;
+        }
+
         if (latenessUs > 40000) {
             // We're more than 40ms late.
             LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6);
@@ -1370,7 +1453,7 @@
     mVideoBuffer->release();
     mVideoBuffer = NULL;
 
-    if (wasSeeking && (mFlags & SEEK_PREVIEW)) {
+    if (wasSeeking != NO_SEEK && (mFlags & SEEK_PREVIEW)) {
         mFlags &= ~SEEK_PREVIEW;
         return;
     }
@@ -1439,7 +1522,7 @@
             mSeekNotificationSent = true;
         }
 
-        mSeeking = false;
+        mSeeking = NO_SEEK;
     }
 
     status_t finalStatus;
@@ -1512,8 +1595,10 @@
 status_t AwesomePlayer::finishSetDataSource_l() {
     sp<DataSource> dataSource;
 
-    if (!strncasecmp("http://", mUri.string(), 7)) {
-        mConnectingDataSource = new NuHTTPDataSource;
+    if (!strncasecmp("http://", mUri.string(), 7)
+            || !strncasecmp("https://", mUri.string(), 8)) {
+        mConnectingDataSource = new NuHTTPDataSource(
+                (mFlags & INCOGNITO) ? NuHTTPDataSource::kFlagIncognito : 0);
 
         mLock.unlock();
         status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders);
@@ -1564,29 +1649,6 @@
             LOGI("Prepare cancelled while waiting for initial cache fill.");
             return UNKNOWN_ERROR;
         }
-    } else if (!strncasecmp(mUri.string(), "httplive://", 11)) {
-        String8 uri("http://");
-        uri.append(mUri.string() + 11);
-
-        if (mLooper == NULL) {
-            mLooper = new ALooper;
-            mLooper->setName("httplive");
-            mLooper->start();
-        }
-
-        mLiveSession = new LiveSession;
-        mLooper->registerHandler(mLiveSession);
-
-        mLiveSession->connect(uri.string());
-        dataSource = mLiveSession->getDataSource();
-
-        sp<MediaExtractor> extractor =
-            MediaExtractor::Create(dataSource, MEDIA_MIMETYPE_CONTAINER_MPEG2TS);
-
-        static_cast<MPEG2TSExtractor *>(extractor.get())
-            ->setLiveSession(mLiveSession);
-
-        return setDataSource_l(extractor);
     } else if (!strncasecmp("rtsp://", mUri.string(), 7)) {
         if (mLooper == NULL) {
             mLooper = new ALooper;
@@ -1594,7 +1656,13 @@
             mLooper->start();
         }
         mRTSPController = new ARTSPController(mLooper);
+        mConnectingRTSPController = mRTSPController;
+
+        mLock.unlock();
         status_t err = mRTSPController->connect(mUri.string());
+        mLock.lock();
+
+        mConnectingRTSPController.clear();
 
         LOGI("ARTSPController::connect returned %d", err);
 
diff --git a/media/libstagefright/CameraSource.cpp b/media/libstagefright/CameraSource.cpp
index 66e0657..8a24bc4 100644
--- a/media/libstagefright/CameraSource.cpp
+++ b/media/libstagefright/CameraSource.cpp
@@ -598,8 +598,7 @@
     }
 
     if (mNumGlitches > 0) {
-        LOGW("%d long delays between neighboring video frames during",
-                mNumGlitches);
+        LOGW("%d long delays between neighboring video frames", mNumGlitches);
     }
 
     CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
@@ -696,10 +695,9 @@
         int32_t msgType, const sp<IMemory> &data) {
     LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
     Mutex::Autolock autoLock(mLock);
-    if (!mStarted) {
+    if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
+        LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
         releaseOneRecordingFrame(data);
-        ++mNumFramesReceived;
-        ++mNumFramesDropped;
         return;
     }
 
diff --git a/media/libstagefright/CameraSourceTimeLapse.cpp b/media/libstagefright/CameraSourceTimeLapse.cpp
index e6fe618..3689557 100644
--- a/media/libstagefright/CameraSourceTimeLapse.cpp
+++ b/media/libstagefright/CameraSourceTimeLapse.cpp
@@ -277,7 +277,7 @@
         // this thread as read() will make a copy of this last frame and keep
         // returning it in the quick stop mode.
         Mutex::Autolock autoLock(mQuickStopLock);
-        CHECK_EQ(OK, mCamera->takePicture());
+        CHECK_EQ(OK, mCamera->takePicture(CAMERA_MSG_RAW_IMAGE));
         if (mQuickStop) {
             LOGV("threadTimeLapseEntry: Exiting due to mQuickStop = true");
             return;
diff --git a/media/libstagefright/DRMExtractor.cpp b/media/libstagefright/DRMExtractor.cpp
index 647cf43..2809df5 100644
--- a/media/libstagefright/DRMExtractor.cpp
+++ b/media/libstagefright/DRMExtractor.cpp
@@ -243,6 +243,7 @@
       mDrmManagerClient(NULL) {
     mOriginalExtractor = MediaExtractor::Create(source, mime);
     mOriginalExtractor->setDrmFlag(true);
+    mOriginalExtractor->getMetaData()->setInt32(kKeyIsDRM, 1);
 
     source->getDrmInfo(&mDecryptHandle, &mDrmManagerClient);
 }
diff --git a/media/libstagefright/DataSource.cpp b/media/libstagefright/DataSource.cpp
index e06fa81..3b38208 100644
--- a/media/libstagefright/DataSource.cpp
+++ b/media/libstagefright/DataSource.cpp
@@ -24,6 +24,7 @@
 #include "include/NuHTTPDataSource.h"
 #include "include/DRMExtractor.h"
 #include "include/FLACExtractor.h"
+#include "include/AACExtractor.h"
 
 #include "matroska/MatroskaExtractor.h"
 
@@ -109,6 +110,7 @@
     RegisterSniffer(SniffAMR);
     RegisterSniffer(SniffMPEG2TS);
     RegisterSniffer(SniffMP3);
+    RegisterSniffer(SniffAAC);
 
     char value[PROPERTY_VALUE_MAX];
     if (property_get("drm.service.enabled", value, NULL)
@@ -123,7 +125,8 @@
     sp<DataSource> source;
     if (!strncasecmp("file://", uri, 7)) {
         source = new FileSource(uri + 7);
-    } else if (!strncasecmp("http://", uri, 7)) {
+    } else if (!strncasecmp("http://", uri, 7)
+            || !strncasecmp("https://", uri, 8)) {
         sp<NuHTTPDataSource> httpSource = new NuHTTPDataSource;
         if (httpSource->connect(uri, headers) != OK) {
             return NULL;
diff --git a/media/libstagefright/HTTPStream.cpp b/media/libstagefright/HTTPStream.cpp
index 77a61a5..498c7b8 100644
--- a/media/libstagefright/HTTPStream.cpp
+++ b/media/libstagefright/HTTPStream.cpp
@@ -34,6 +34,8 @@
 
 #include <media/stagefright/foundation/ADebug.h>
 
+#include <openssl/ssl.h>
+
 namespace android {
 
 // static
@@ -41,11 +43,18 @@
 
 HTTPStream::HTTPStream()
     : mState(READY),
-      mSocket(-1) {
+      mSocket(-1),
+      mSSLContext(NULL),
+      mSSL(NULL) {
 }
 
 HTTPStream::~HTTPStream() {
     disconnect();
+
+    if (mSSLContext != NULL) {
+        SSL_CTX_free((SSL_CTX *)mSSLContext);
+        mSSLContext = NULL;
+    }
 }
 
 static bool MakeSocketBlocking(int s, bool blocking) {
@@ -198,7 +207,11 @@
     return MySendReceive(s, data, size, flags, false /* sendData */);
 }
 
-status_t HTTPStream::connect(const char *server, int port) {
+status_t HTTPStream::connect(const char *server, int port, bool https) {
+    if (port < 0) {
+        port = https ? 443 : 80;
+    }
+
     Mutex::Autolock autoLock(mLock);
 
     status_t err = OK;
@@ -249,6 +262,47 @@
         return res;
     }
 
+    if (https) {
+        CHECK(mSSL == NULL);
+
+        if (mSSLContext == NULL) {
+            SSL_library_init();
+
+            mSSLContext = SSL_CTX_new(TLSv1_client_method());
+
+            if (mSSLContext == NULL) {
+                LOGE("failed to create SSL context");
+                mState = READY;
+                return ERROR_IO;
+            }
+        }
+
+        mSSL = SSL_new((SSL_CTX *)mSSLContext);
+
+        if (mSSL == NULL) {
+            LOGE("failed to create SSL session");
+
+            mState = READY;
+            return ERROR_IO;
+        }
+
+        int res = SSL_set_fd((SSL *)mSSL, mSocket);
+
+        if (res == 1) {
+            res = SSL_connect((SSL *)mSSL);
+        }
+
+        if (res != 1) {
+            SSL_free((SSL *)mSSL);
+            mSSL = NULL;
+
+            LOGE("failed to connect over SSL");
+            mState = READY;
+
+            return ERROR_IO;
+        }
+    }
+
     mState = CONNECTED;
 
     return OK;
@@ -261,6 +315,13 @@
         return ERROR_NOT_CONNECTED;
     }
 
+    if (mSSL != NULL) {
+        SSL_shutdown((SSL *)mSSL);
+
+        SSL_free((SSL *)mSSL);
+        mSSL = NULL;
+    }
+
     CHECK(mSocket >= 0);
     close(mSocket);
     mSocket = -1;
@@ -276,7 +337,16 @@
     }
 
     while (size > 0) {
-        ssize_t n = MySend(mSocket, data, size, 0);
+        ssize_t n;
+        if (mSSL != NULL) {
+            n = SSL_write((SSL *)mSSL, data, size);
+
+            if (n < 0) {
+                n = -SSL_get_error((SSL *)mSSL, n);
+            }
+        } else {
+            n = MySend(mSocket, data, size, 0);
+        }
 
         if (n < 0) {
             disconnect();
@@ -317,7 +387,17 @@
 
     for (;;) {
         char c;
-        ssize_t n = MyReceive(mSocket, &c, 1, 0);
+        ssize_t n;
+        if (mSSL != NULL) {
+            n = SSL_read((SSL *)mSSL, &c, 1);
+
+            if (n < 0) {
+                n = -SSL_get_error((SSL *)mSSL, n);
+            }
+        } else {
+            n = MyReceive(mSocket, &c, 1, 0);
+        }
+
         if (n < 0) {
             disconnect();
 
@@ -437,7 +517,16 @@
 ssize_t HTTPStream::receive(void *data, size_t size) {
     size_t total = 0;
     while (total < size) {
-        ssize_t n = MyReceive(mSocket, (char *)data + total, size - total, 0);
+        ssize_t n;
+        if (mSSL != NULL) {
+            n = SSL_read((SSL *)mSSL, (char *)data + total, size - total);
+
+            if (n < 0) {
+                n = -SSL_get_error((SSL *)mSSL, n);
+            }
+        } else {
+            n = MyReceive(mSocket, (char *)data + total, size - total, 0);
+        }
 
         if (n < 0) {
             LOGE("recv failed, errno = %d (%s)", (int)n, strerror(-n));
diff --git a/media/libstagefright/MP3Extractor.cpp b/media/libstagefright/MP3Extractor.cpp
index 00a4dd5..eb4c68d 100644
--- a/media/libstagefright/MP3Extractor.cpp
+++ b/media/libstagefright/MP3Extractor.cpp
@@ -217,7 +217,7 @@
 
             *inout_pos += len;
 
-            LOGV("skipped ID3 tag, new starting offset is %ld (0x%08lx)",
+            LOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
                  *inout_pos, *inout_pos);
         }
 
@@ -241,7 +241,7 @@
     do {
         if (pos >= *inout_pos + kMaxBytesChecked) {
             // Don't scan forever.
-            LOGV("giving up at offset %ld", pos);
+            LOGV("giving up at offset %lld", pos);
             break;
         }
 
@@ -251,7 +251,15 @@
             } else {
                 memcpy(buf, tmp, remainingBytes);
                 bytesToRead = kMaxReadBytes - remainingBytes;
-                totalBytesRead = source->readAt(pos, buf + remainingBytes, bytesToRead);
+
+                /*
+                 * The next read position should start from the end of
+                 * the last buffer, and thus should include the remaining
+                 * bytes in the buffer.
+                 */
+                totalBytesRead = source->readAt(pos + remainingBytes,
+                                                buf + remainingBytes,
+                                                bytesToRead);
                 if (totalBytesRead <= 0) {
                     break;
                 }
@@ -283,7 +291,7 @@
             continue;
         }
 
-        LOGV("found possible 1st frame at %ld (header = 0x%08x)", pos, header);
+        LOGV("found possible 1st frame at %lld (header = 0x%08x)", pos, header);
 
         // We found what looks like a valid frame,
         // now find its successors.
@@ -314,7 +322,7 @@
                 break;
             }
 
-            LOGV("found subsequent frame #%d at %ld", j + 2, test_pos);
+            LOGV("found subsequent frame #%d at %lld", j + 2, test_pos);
 
             test_pos += test_frame_size;
         }
diff --git a/media/libstagefright/MPEG4Extractor.cpp b/media/libstagefright/MPEG4Extractor.cpp
index 108a1d1..7b96d01 100644
--- a/media/libstagefright/MPEG4Extractor.cpp
+++ b/media/libstagefright/MPEG4Extractor.cpp
@@ -1164,6 +1164,37 @@
             break;
         }
 
+        case FOURCC('d', '2', '6', '3'):
+        {
+            /*
+             * d263 contains a fixed 7 bytes part:
+             *   vendor - 4 bytes
+             *   version - 1 byte
+             *   level - 1 byte
+             *   profile - 1 byte
+             * optionally, "d263" box itself may contain a 16-byte
+             * bit rate box (bitr)
+             *   average bit rate - 4 bytes
+             *   max bit rate - 4 bytes
+             */
+            char buffer[23];
+            if (chunk_data_size != 7 &&
+                chunk_data_size != 23) {
+                LOGE("Incorrect D263 box size %lld", chunk_data_size);
+                return ERROR_MALFORMED;
+            }
+
+            if (mDataSource->readAt(
+                    data_offset, buffer, chunk_data_size) < chunk_data_size) {
+                return ERROR_IO;
+            }
+
+            mLastTrack->meta->setData(kKeyD263, kTypeD263, buffer, chunk_data_size);
+
+            *offset += chunk_size;
+            break;
+        }
+
         case FOURCC('m', 'e', 't', 'a'):
         {
             uint8_t buffer[4];
@@ -1565,6 +1596,14 @@
         return OK;
     }
 
+    if (objectTypeIndication  == 0x6b) {
+        // The media subtype is MP3 audio
+        // Our software MP3 audio decoder may not be able to handle
+        // packetized MP3 audio; for now, lets just return ERROR_UNSUPPORTED
+        LOGE("MP3 track in MP4/3GPP file is not supported");
+        return ERROR_UNSUPPORTED;
+    }
+
     const uint8_t *csd;
     size_t csd_size;
     if (esds.getCodecSpecificInfo(
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index b11789d..5d6ea7c 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -1281,7 +1281,21 @@
     initTrackingProgressStatus(params);
 
     sp<MetaData> meta = new MetaData;
+    if (mIsRealTimeRecording && mOwner->numTracks() > 1) {
+        /*
+         * This extra delay of accepting incoming audio/video signals
+         * helps to align a/v start time at the beginning of a recording
+         * session, and it also helps eliminate the "recording" sound for
+         * camcorder applications.
+         *
+         * Ideally, this platform-specific value should be defined
+         * in media_profiles.xml file
+         */
+        startTimeUs += 700000;
+    }
+
     meta->setInt64(kKeyTime, startTimeUs);
+
     status_t err = mSource->start(meta.get());
     if (err != OK) {
         mDone = mReachedEOS = true;
@@ -1944,7 +1958,11 @@
                      ((timestampUs * mTimeScale + 500000LL) / 1000000LL -
                      (lastTimestampUs * mTimeScale + 500000LL) / 1000000LL);
 
-            if (currDurationTicks != lastDurationTicks) {
+            // Force the first sample to have its own stts entry so that
+            // we can adjust its value later to maintain the A/V sync.
+            if (mNumSamples == 3 || currDurationTicks != lastDurationTicks) {
+                LOGV("%s lastDurationUs: %lld us, currDurationTicks: %lld us",
+                        mIsAudio? "Audio": "Video", lastDurationUs, currDurationTicks);
                 addOneSttsTableEntry(sampleCount, lastDurationUs);
                 sampleCount = 1;
             } else {
@@ -1957,6 +1975,8 @@
             }
             previousSampleSize = sampleSize;
         }
+        LOGV("%s timestampUs/lastTimestampUs: %lld/%lld",
+                mIsAudio? "Audio": "Video", timestampUs, lastTimestampUs);
         lastDurationUs = timestampUs - lastTimestampUs;
         lastDurationTicks = currDurationTicks;
         lastTimestampUs = timestampUs;
@@ -2028,7 +2048,16 @@
     } else {
         ++sampleCount;  // Count for the last sample
     }
-    addOneSttsTableEntry(sampleCount, lastDurationUs);
+
+    if (mNumSamples <= 2) {
+        addOneSttsTableEntry(1, lastDurationUs);
+        if (sampleCount - 1 > 0) {
+            addOneSttsTableEntry(sampleCount - 1, lastDurationUs);
+        }
+    } else {
+        addOneSttsTableEntry(sampleCount, lastDurationUs);
+    }
+
     mTrackDurationUs += lastDurationUs;
     mReachedEOS = true;
     LOGI("Received total/0-length (%d/%d) buffers and encoded %d frames. - %s",
@@ -2153,6 +2182,9 @@
     int32_t mvhdTimeScale = mOwner->getTimeScale();
     int64_t trakDurationUs = getDurationUs();
 
+    // Compensate for small start time difference from different media tracks
+    int64_t trackStartTimeOffsetUs = 0;
+
     mOwner->beginBox("trak");
 
       mOwner->beginBox("tkhd");
@@ -2191,26 +2223,8 @@
 
       int64_t moovStartTimeUs = mOwner->getStartTimestampUs();
       if (mStartTimestampUs != moovStartTimeUs) {
-        mOwner->beginBox("edts");
-          mOwner->beginBox("elst");
-            mOwner->writeInt32(0);           // version=0, flags=0: 32-bit time
-            mOwner->writeInt32(2);           // never ends with an empty list
-
-            // First elst entry: specify the starting time offset
-            int64_t offsetUs = mStartTimestampUs - moovStartTimeUs;
-            LOGV("OffsetUs: %lld", offsetUs);
-            int32_t seg = (offsetUs * mvhdTimeScale + 5E5) / 1E6;
-            mOwner->writeInt32(seg);         // in mvhd timecale
-            mOwner->writeInt32(-1);          // starting time offset
-            mOwner->writeInt32(1 << 16);     // rate = 1.0
-
-            // Second elst entry: specify the track duration
-            seg = (trakDurationUs * mvhdTimeScale + 5E5) / 1E6;
-            mOwner->writeInt32(seg);         // in mvhd timescale
-            mOwner->writeInt32(0);
-            mOwner->writeInt32(1 << 16);
-          mOwner->endBox();
-        mOwner->endBox();
+          CHECK(mStartTimestampUs > moovStartTimeUs);
+          trackStartTimeOffsetUs = mStartTimestampUs - moovStartTimeUs;
       }
 
       mOwner->beginBox("mdia");
@@ -2466,7 +2480,7 @@
           mOwner->beginBox("stts");
             mOwner->writeInt32(0);  // version=0, flags=0
             mOwner->writeInt32(mNumSttsTableEntries);
-            int64_t prevTimestampUs = 0;
+            int64_t prevTimestampUs = trackStartTimeOffsetUs;
             for (List<SttsTableEntry>::iterator it = mSttsTableEntries.begin();
                  it != mSttsTableEntries.end(); ++it) {
                 mOwner->writeInt32(it->sampleCount);
diff --git a/media/libstagefright/MediaDefs.cpp b/media/libstagefright/MediaDefs.cpp
index b50af89..0be7261 100644
--- a/media/libstagefright/MediaDefs.cpp
+++ b/media/libstagefright/MediaDefs.cpp
@@ -36,6 +36,7 @@
 const char *MEDIA_MIMETYPE_AUDIO_G711_MLAW = "audio/g711-mlaw";
 const char *MEDIA_MIMETYPE_AUDIO_RAW = "audio/raw";
 const char *MEDIA_MIMETYPE_AUDIO_FLAC = "audio/flac";
+const char *MEDIA_MIMETYPE_AUDIO_AAC_ADTS = "audio/aac-adts";
 
 const char *MEDIA_MIMETYPE_CONTAINER_MPEG4 = "video/mpeg4";
 const char *MEDIA_MIMETYPE_CONTAINER_WAV = "audio/wav";
diff --git a/media/libstagefright/MediaExtractor.cpp b/media/libstagefright/MediaExtractor.cpp
index 08ed206..23bad5b 100644
--- a/media/libstagefright/MediaExtractor.cpp
+++ b/media/libstagefright/MediaExtractor.cpp
@@ -27,6 +27,7 @@
 #include "include/DRMExtractor.h"
 #include "include/WVMExtractor.h"
 #include "include/FLACExtractor.h"
+#include "include/AACExtractor.h"
 
 #include "matroska/MatroskaExtractor.h"
 
@@ -66,41 +67,57 @@
              mime, confidence);
     }
 
-    if (!strncmp(mime, "drm", 3)) {
-        const char *originalMime = strrchr(mime, '+') + 1;
-
-        if (!strncmp(mime, "drm+es_based", 12)) {
+    bool isDrm = false;
+    // DRM MIME type syntax is "drm+type+original" where
+    // type is "es_based" or "container_based" and
+    // original is the content's cleartext MIME type
+    if (!strncmp(mime, "drm+", 4)) {
+        const char *originalMime = strchr(mime+4, '+');
+        if (originalMime == NULL) {
+            // second + not found
+            return NULL;
+        }
+        ++originalMime;
+        if (!strncmp(mime, "drm+es_based+", 13)) {
+            // DRMExtractor sets container metadata kKeyIsDRM to 1
             return new DRMExtractor(source, originalMime);
-        } else if (!strncmp(mime, "drm+container_based", 19)) {
+        } else if (!strncmp(mime, "drm+container_based+", 20)) {
             mime = originalMime;
+            isDrm = true;
         } else {
             return NULL;
         }
     }
 
+    MediaExtractor *ret = NULL;
     if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)
             || !strcasecmp(mime, "audio/mp4")) {
-        return new MPEG4Extractor(source);
+        ret = new MPEG4Extractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {
-        return new MP3Extractor(source, meta);
+        ret = new MP3Extractor(source, meta);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)
             || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
-        return new AMRExtractor(source);
+        ret = new AMRExtractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) {
-        return new FLACExtractor(source);
+        ret = new FLACExtractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) {
-        return new WAVExtractor(source);
+        ret = new WAVExtractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)) {
-        return new OggExtractor(source);
+        ret = new OggExtractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)) {
-        return new MatroskaExtractor(source);
+        ret = new MatroskaExtractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
-        return new MPEG2TSExtractor(source);
+        ret = new MPEG2TSExtractor(source);
     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WVM)) {
-        return new WVMExtractor(source);
+        ret = new WVMExtractor(source);
+    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC_ADTS)) {
+        ret = new AACExtractor(source);
+    }
+    if (ret != NULL && isDrm) {
+        ret->getMetaData()->setInt32(kKeyIsDRM, 1);
     }
 
-    return NULL;
+    return ret;
 }
 
 }  // namespace android
diff --git a/media/libstagefright/NuHTTPDataSource.cpp b/media/libstagefright/NuHTTPDataSource.cpp
index 0376e1c..dbbf3b4 100644
--- a/media/libstagefright/NuHTTPDataSource.cpp
+++ b/media/libstagefright/NuHTTPDataSource.cpp
@@ -24,22 +24,30 @@
 }
 
 static bool ParseURL(
-        const char *url, String8 *host, unsigned *port, String8 *path) {
+        const char *url, String8 *host, unsigned *port,
+        String8 *path, bool *https) {
     host->setTo("");
     *port = 0;
     path->setTo("");
 
-    if (strncasecmp("http://", url, 7)) {
+    size_t hostStart;
+    if (!strncasecmp("http://", url, 7)) {
+        hostStart = 7;
+        *https = false;
+    } else if (!strncasecmp("https://", url, 8)) {
+        hostStart = 8;
+        *https = true;
+    } else {
         return false;
     }
 
-    const char *slashPos = strchr(&url[7], '/');
+    const char *slashPos = strchr(&url[hostStart], '/');
 
     if (slashPos == NULL) {
-        host->setTo(&url[7]);
+        host->setTo(&url[hostStart]);
         path->setTo("/");
     } else {
-        host->setTo(&url[7], slashPos - &url[7]);
+        host->setTo(&url[hostStart], slashPos - &url[hostStart]);
         path->setTo(slashPos);
     }
 
@@ -57,15 +65,17 @@
         String8 tmp(host->string(), colonOffset);
         *host = tmp;
     } else {
-        *port = 80;
+        *port = (*https) ? 443 : 80;
     }
 
     return true;
 }
 
-NuHTTPDataSource::NuHTTPDataSource()
-    : mState(DISCONNECTED),
+NuHTTPDataSource::NuHTTPDataSource(uint32_t flags)
+    : mFlags(flags),
+      mState(DISCONNECTED),
       mPort(0),
+      mHTTPS(false),
       mOffset(0),
       mContentLength(0),
       mContentLengthValid(false),
@@ -111,11 +121,12 @@
 
     mUri = uri;
 
-    if (!ParseURL(uri, &host, &port, &path)) {
+    bool https;
+    if (!ParseURL(uri, &host, &port, &path, &https)) {
         return ERROR_MALFORMED;
     }
 
-    return connect(host, port, path, headers, offset);
+    return connect(host, port, path, https, headers, offset);
 }
 
 static bool IsRedirectStatusCode(int httpStatus) {
@@ -125,14 +136,19 @@
 
 status_t NuHTTPDataSource::connect(
         const char *host, unsigned port, const char *path,
+        bool https,
         const String8 &headers,
         off64_t offset) {
-    LOGI("connect to %s:%u%s @%lld", host, port, path, offset);
+    if (!(mFlags & kFlagIncognito)) {
+        LOGI("connect to %s:%u%s @%lld", host, port, path, offset);
+    } else {
+        LOGI("connect to <URL suppressed> @%lld", offset);
+    }
 
     bool needsToReconnect = true;
 
     if (mState == CONNECTED && host == mHost && port == mPort
-            && offset == mOffset) {
+            && https == mHTTPS && offset == mOffset) {
         if (mContentLengthValid && mOffset == mContentLength) {
             LOGI("Didn't have to reconnect, old one's still good.");
             needsToReconnect = false;
@@ -142,6 +158,7 @@
     mHost = host;
     mPort = port;
     mPath = path;
+    mHTTPS = https;
     mHeaders = headers;
 
     status_t err = OK;
@@ -150,7 +167,7 @@
 
     if (needsToReconnect) {
         mHTTP.disconnect();
-        err = mHTTP.connect(host, port);
+        err = mHTTP.connect(host, port, https);
     }
 
     if (err != OK) {
@@ -353,7 +370,7 @@
         String8 host = mHost;
         String8 path = mPath;
         String8 headers = mHeaders;
-        status_t err = connect(host, mPort, path, headers, offset);
+        status_t err = connect(host, mPort, path, mHTTPS, headers, offset);
 
         if (err != OK) {
             return err;
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index fccd68c..5d502e7 100644
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -3289,7 +3289,7 @@
 }
 
 status_t OMXCodec::stop() {
-    CODEC_LOGI("stop mState=%d", mState);
+    CODEC_LOGV("stop mState=%d", mState);
 
     Mutex::Autolock autoLock(mLock);
 
diff --git a/media/libstagefright/StagefrightMetadataRetriever.cpp b/media/libstagefright/StagefrightMetadataRetriever.cpp
index 600de7c..ea3b801 100644
--- a/media/libstagefright/StagefrightMetadataRetriever.cpp
+++ b/media/libstagefright/StagefrightMetadataRetriever.cpp
@@ -272,6 +272,12 @@
         return NULL;
     }
 
+    int32_t drm = 0;
+    if (mExtractor->getMetaData()->findInt32(kKeyIsDRM, &drm) && drm != 0) {
+        LOGE("frame grab not allowed.");
+        return NULL;
+    }
+
     size_t n = mExtractor->countTracks();
     size_t i;
     for (i = 0; i < n; ++i) {
diff --git a/media/libstagefright/avc_utils.cpp b/media/libstagefright/avc_utils.cpp
index fa12cf0..95cf2d3 100644
--- a/media/libstagefright/avc_utils.cpp
+++ b/media/libstagefright/avc_utils.cpp
@@ -329,5 +329,52 @@
     return foundIDR;
 }
 
+sp<MetaData> MakeAACCodecSpecificData(
+        unsigned profile, unsigned sampling_freq_index,
+        unsigned channel_configuration) {
+    sp<MetaData> meta = new MetaData;
+    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
+
+    CHECK_LE(sampling_freq_index, 11u);
+    static const int32_t kSamplingFreq[] = {
+        96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
+        16000, 12000, 11025, 8000
+    };
+    meta->setInt32(kKeySampleRate, kSamplingFreq[sampling_freq_index]);
+    meta->setInt32(kKeyChannelCount, channel_configuration);
+
+    static const uint8_t kStaticESDS[] = {
+        0x03, 22,
+        0x00, 0x00,     // ES_ID
+        0x00,           // streamDependenceFlag, URL_Flag, OCRstreamFlag
+
+        0x04, 17,
+        0x40,                       // Audio ISO/IEC 14496-3
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+
+        0x05, 2,
+        // AudioSpecificInfo follows
+
+        // oooo offf fccc c000
+        // o - audioObjectType
+        // f - samplingFreqIndex
+        // c - channelConfig
+    };
+    sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2);
+    memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS));
+
+    csd->data()[sizeof(kStaticESDS)] =
+        ((profile + 1) << 3) | (sampling_freq_index >> 1);
+
+    csd->data()[sizeof(kStaticESDS) + 1] =
+        ((sampling_freq_index << 7) & 0x80) | (channel_configuration << 3);
+
+    meta->setData(kKeyESDS, 0, csd->data(), csd->size());
+
+    return meta;
+}
+
 }  // namespace android
 
diff --git a/media/libstagefright/colorconversion/SoftwareRenderer.cpp b/media/libstagefright/colorconversion/SoftwareRenderer.cpp
index 70408d7..31afc43 100644
--- a/media/libstagefright/colorconversion/SoftwareRenderer.cpp
+++ b/media/libstagefright/colorconversion/SoftwareRenderer.cpp
@@ -26,14 +26,15 @@
 #include <surfaceflinger/Surface.h>
 #include <ui/android_native_buffer.h>
 #include <ui/GraphicBufferMapper.h>
+#include <gui/ISurfaceTexture.h>
 
 namespace android {
 
 SoftwareRenderer::SoftwareRenderer(
-        const sp<Surface> &surface, const sp<MetaData> &meta)
+        const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta)
     : mConverter(NULL),
       mYUVMode(None),
-      mSurface(surface) {
+      mNativeWindow(nativeWindow) {
     int32_t tmp;
     CHECK(meta->findInt32(kKeyColorFormat, &tmp));
     mColorFormat = (OMX_COLOR_FORMATTYPE)tmp;
@@ -65,22 +66,22 @@
             break;
     }
 
-    CHECK(mSurface.get() != NULL);
+    CHECK(mNativeWindow != NULL);
     CHECK(mWidth > 0);
     CHECK(mHeight > 0);
     CHECK(mConverter == NULL || mConverter->isValid());
 
     CHECK_EQ(0,
             native_window_set_usage(
-            mSurface.get(),
+            mNativeWindow.get(),
             GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN
             | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP));
 
-    CHECK_EQ(0, native_window_set_buffer_count(mSurface.get(), 2));
+    CHECK_EQ(0, native_window_set_buffer_count(mNativeWindow.get(), 2));
 
     // Width must be multiple of 32???
     CHECK_EQ(0, native_window_set_buffers_geometry(
-                mSurface.get(),
+                mNativeWindow.get(),
                 mCropRight - mCropLeft + 1,
                 mCropBottom - mCropTop + 1,
                 halFormat));
@@ -96,7 +97,7 @@
 
     if (transform) {
         CHECK_EQ(0, native_window_set_buffers_transform(
-                    mSurface.get(), transform));
+                    mNativeWindow.get(), transform));
     }
 }
 
@@ -109,12 +110,12 @@
         const void *data, size_t size, void *platformPrivate) {
     android_native_buffer_t *buf;
     int err;
-    if ((err = mSurface->dequeueBuffer(mSurface.get(), &buf)) != 0) {
+    if ((err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf)) != 0) {
         LOGW("Surface::dequeueBuffer returned error %d", err);
         return;
     }
 
-    CHECK_EQ(0, mSurface->lockBuffer(mSurface.get(), buf));
+    CHECK_EQ(0, mNativeWindow->lockBuffer(mNativeWindow.get(), buf));
 
     GraphicBufferMapper &mapper = GraphicBufferMapper::get();
 
@@ -140,7 +141,7 @@
 
     CHECK_EQ(0, mapper.unlock(buf->handle));
 
-    if ((err = mSurface->queueBuffer(mSurface.get(), buf)) != 0) {
+    if ((err = mNativeWindow->queueBuffer(mNativeWindow.get(), buf)) != 0) {
         LOGW("Surface::queueBuffer returned error %d", err);
     }
     buf = NULL;
diff --git a/media/libstagefright/foundation/ABitReader.cpp b/media/libstagefright/foundation/ABitReader.cpp
index 24c8df8..f07dd4f 100644
--- a/media/libstagefright/foundation/ABitReader.cpp
+++ b/media/libstagefright/foundation/ABitReader.cpp
@@ -90,9 +90,7 @@
 }
 
 const uint8_t *ABitReader::data() const {
-    CHECK_EQ(mNumBitsLeft % 8, 0u);
-
-    return mData - mNumBitsLeft / 8;
+    return mData - (mNumBitsLeft + 7) / 8;
 }
 
 }  // namespace android
diff --git a/media/libstagefright/httplive/LiveSession.cpp b/media/libstagefright/httplive/LiveSession.cpp
index 0bed3ca..f0cd6a0 100644
--- a/media/libstagefright/httplive/LiveSession.cpp
+++ b/media/libstagefright/httplive/LiveSession.cpp
@@ -41,9 +41,14 @@
 
 const int64_t LiveSession::kMaxPlaylistAgeUs = 15000000ll;
 
-LiveSession::LiveSession()
-    : mDataSource(new LiveDataSource),
-      mHTTPDataSource(new NuHTTPDataSource),
+LiveSession::LiveSession(uint32_t flags)
+    : mFlags(flags),
+      mDataSource(new LiveDataSource),
+      mHTTPDataSource(
+              new NuHTTPDataSource(
+                  (mFlags & kFlagIncognito)
+                    ? NuHTTPDataSource::kFlagIncognito
+                    : 0)),
       mPrevBandwidthIndex(-1),
       mLastPlaylistFetchTimeUs(-1),
       mSeqNumber(-1),
@@ -139,7 +144,11 @@
     AString url;
     CHECK(msg->findString("url", &url));
 
-    LOGI("onConnect '%s'", url.c_str());
+    if (!(mFlags & kFlagIncognito)) {
+        LOGI("onConnect '%s'", url.c_str());
+    } else {
+        LOGI("onConnect <URL suppressed>");
+    }
 
     mMasterURL = url;
 
@@ -168,18 +177,6 @@
         CHECK_GT(mBandwidthItems.size(), 0u);
 
         mBandwidthItems.sort(SortByBandwidth);
-
-        char value[PROPERTY_VALUE_MAX];
-        if (property_get("media.httplive.disable-nuplayer", value, NULL)
-                && (!strcasecmp(value, "true") || !strcmp(value, "1"))) {
-            // The "legacy" player cannot deal with audio format changes,
-            // some streams use different audio encoding parameters for
-            // their lowest bandwidth stream.
-            if (mBandwidthItems.size() > 1) {
-                // XXX Remove the lowest bitrate stream for now...
-                mBandwidthItems.removeAt(0);
-            }
-        }
     }
 
     postMonitorQueue();
@@ -201,7 +198,8 @@
 
     if (!strncasecmp(url, "file://", 7)) {
         source = new FileSource(url + 7);
-    } else if (strncasecmp(url, "http://", 7)) {
+    } else if (strncasecmp(url, "http://", 7)
+            && strncasecmp(url, "https://", 8)) {
         return ERROR_UNSUPPORTED;
     } else {
         {
diff --git a/media/libstagefright/include/AACExtractor.h b/media/libstagefright/include/AACExtractor.h
new file mode 100644
index 0000000..8e5657b
--- /dev/null
+++ b/media/libstagefright/include/AACExtractor.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef AAC_EXTRACTOR_H_
+
+#define AAC_EXTRACTOR_H_
+
+#include <media/stagefright/MediaExtractor.h>
+
+#include <utils/Vector.h>
+
+namespace android {
+
+struct AMessage;
+class String8;
+
+class AACExtractor : public MediaExtractor {
+public:
+    AACExtractor(const sp<DataSource> &source);
+
+    virtual size_t countTracks();
+    virtual sp<MediaSource> getTrack(size_t index);
+    virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
+
+    virtual sp<MetaData> getMetaData();
+
+protected:
+    virtual ~AACExtractor();
+
+private:
+    sp<DataSource> mDataSource;
+    sp<MetaData> mMeta;
+    status_t mInitCheck;
+
+    Vector<uint64_t> mOffsetVector;
+    int64_t mFrameDurationUs;
+
+    AACExtractor(const AACExtractor &);
+    AACExtractor &operator=(const AACExtractor &);
+};
+
+bool SniffAAC(
+        const sp<DataSource> &source, String8 *mimeType, float *confidence,
+        sp<AMessage> *);
+
+}  // namespace android
+
+#endif  // AAC_EXTRACTOR_H_
diff --git a/media/libstagefright/include/AMRExtractor.h b/media/libstagefright/include/AMRExtractor.h
index 589d837..4a1c827 100644
--- a/media/libstagefright/include/AMRExtractor.h
+++ b/media/libstagefright/include/AMRExtractor.h
@@ -18,6 +18,7 @@
 
 #define AMR_EXTRACTOR_H_
 
+#include <utils/Errors.h>
 #include <media/stagefright/MediaExtractor.h>
 
 namespace android {
diff --git a/media/libstagefright/include/AwesomePlayer.h b/media/libstagefright/include/AwesomePlayer.h
index 5120a12..4e6f75c 100644
--- a/media/libstagefright/include/AwesomePlayer.h
+++ b/media/libstagefright/include/AwesomePlayer.h
@@ -36,6 +36,7 @@
 struct MediaExtractor;
 struct MediaSource;
 struct NuCachedSource2;
+struct ISurfaceTexture;
 
 struct ALooper;
 struct ARTSPController;
@@ -43,8 +44,6 @@
 class DrmManagerClinet;
 class DecryptHandle;
 
-struct LiveSession;
-
 struct AwesomeRenderer : public RefBase {
     AwesomeRenderer() {}
 
@@ -82,6 +81,7 @@
     bool isPlaying() const;
 
     void setSurface(const sp<Surface> &surface);
+    void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
     void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
     status_t setLooping(bool shouldLoop);
 
@@ -121,6 +121,11 @@
         // We're triggering a single video event to display the first frame
         // after the seekpoint.
         SEEK_PREVIEW        = 4096,
+
+        AUDIO_RUNNING       = 8192,
+        AUDIOPLAYER_STARTED = 16384,
+
+        INCOGNITO           = 32768,
     };
 
     mutable Mutex mLock;
@@ -132,6 +137,7 @@
     wp<MediaPlayerBase> mListener;
 
     sp<Surface> mSurface;
+    sp<ANativeWindow> mNativeWindow;
     sp<MediaPlayerBase::AudioSink> mAudioSink;
 
     SystemTimeSource mSystemTimeSource;
@@ -162,7 +168,13 @@
     int64_t mTimeSourceDeltaUs;
     int64_t mVideoTimeUs;
 
-    bool mSeeking;
+    enum SeekType {
+        NO_SEEK,
+        SEEK,
+        SEEK_VIDEO_ONLY
+    };
+    SeekType mSeeking;
+
     bool mSeekNotificationSent;
     int64_t mSeekTimeUs;
 
@@ -202,8 +214,7 @@
 
     sp<ALooper> mLooper;
     sp<ARTSPController> mRTSPController;
-
-    sp<LiveSession> mLiveSession;
+    sp<ARTSPController> mConnectingRTSPController;
 
     DrmManagerClient *mDrmManagerClient;
     DecryptHandle *mDecryptHandle;
@@ -215,7 +226,6 @@
     status_t setDataSource_l(const sp<DataSource> &dataSource);
     status_t setDataSource_l(const sp<MediaExtractor> &extractor);
     void reset_l();
-    void partial_reset_l();
     status_t seekTo_l(int64_t timeUs);
     status_t pause_l(bool at_eos = false);
     void initRenderer_l();
@@ -256,6 +266,8 @@
     void finishSeekIfNecessary(int64_t videoTimeUs);
     void ensureCacheIsFetching_l();
 
+    status_t startAudioPlayer_l();
+
     AwesomePlayer(const AwesomePlayer &);
     AwesomePlayer &operator=(const AwesomePlayer &);
 };
diff --git a/media/libstagefright/include/HTTPStream.h b/media/libstagefright/include/HTTPStream.h
index 545cd0c..09e6a5f 100644
--- a/media/libstagefright/include/HTTPStream.h
+++ b/media/libstagefright/include/HTTPStream.h
@@ -32,7 +32,7 @@
     HTTPStream();
     ~HTTPStream();
 
-    status_t connect(const char *server, int port = 80);
+    status_t connect(const char *server, int port = -1, bool https = false);
     status_t disconnect();
 
     status_t send(const char *data, size_t size);
@@ -71,6 +71,9 @@
 
     KeyedVector<AString, AString> mHeaders;
 
+    void *mSSLContext;
+    void *mSSL;
+
     HTTPStream(const HTTPStream &);
     HTTPStream &operator=(const HTTPStream &);
 };
diff --git a/media/libstagefright/include/LiveSession.h b/media/libstagefright/include/LiveSession.h
index f1188c4..3fe5d4e 100644
--- a/media/libstagefright/include/LiveSession.h
+++ b/media/libstagefright/include/LiveSession.h
@@ -29,7 +29,11 @@
 struct NuHTTPDataSource;
 
 struct LiveSession : public AHandler {
-    LiveSession();
+    enum Flags {
+        // Don't log any URLs.
+        kFlagIncognito = 1,
+    };
+    LiveSession(uint32_t flags = 0);
 
     sp<DataSource> getDataSource();
 
@@ -67,6 +71,8 @@
         unsigned long mBandwidth;
     };
 
+    uint32_t mFlags;
+
     sp<LiveDataSource> mDataSource;
 
     sp<NuHTTPDataSource> mHTTPDataSource;
diff --git a/media/libstagefright/include/MP3Extractor.h b/media/libstagefright/include/MP3Extractor.h
index 728980e..ef71b8f 100644
--- a/media/libstagefright/include/MP3Extractor.h
+++ b/media/libstagefright/include/MP3Extractor.h
@@ -18,6 +18,7 @@
 
 #define MP3_EXTRACTOR_H_
 
+#include <utils/Errors.h>
 #include <media/stagefright/MediaExtractor.h>
 
 namespace android {
diff --git a/media/libstagefright/include/NuHTTPDataSource.h b/media/libstagefright/include/NuHTTPDataSource.h
index a99e84a..082b589 100644
--- a/media/libstagefright/include/NuHTTPDataSource.h
+++ b/media/libstagefright/include/NuHTTPDataSource.h
@@ -12,7 +12,11 @@
 namespace android {
 
 struct NuHTTPDataSource : public DataSource {
-    NuHTTPDataSource();
+    enum Flags {
+        // Don't log any URLs.
+        kFlagIncognito = 1
+    };
+    NuHTTPDataSource(uint32_t flags = 0);
 
     status_t connect(
             const char *uri,
@@ -52,11 +56,14 @@
 
     Mutex mLock;
 
+    uint32_t mFlags;
+
     State mState;
 
     String8 mHost;
     unsigned mPort;
     String8 mPath;
+    bool mHTTPS;
     String8 mHeaders;
     String8 mUri;
 
@@ -83,6 +90,7 @@
 
     status_t connect(
             const char *host, unsigned port, const char *path,
+            bool https,
             const String8 &headers,
             off64_t offset);
 
diff --git a/media/libstagefright/include/OggExtractor.h b/media/libstagefright/include/OggExtractor.h
index a41f681..e97c8cd 100644
--- a/media/libstagefright/include/OggExtractor.h
+++ b/media/libstagefright/include/OggExtractor.h
@@ -18,6 +18,7 @@
 
 #define OGG_EXTRACTOR_H_
 
+#include <utils/Errors.h>
 #include <media/stagefright/MediaExtractor.h>
 
 namespace android {
diff --git a/media/libstagefright/include/SoftwareRenderer.h b/media/libstagefright/include/SoftwareRenderer.h
index 90d3fe1..78037b9 100644
--- a/media/libstagefright/include/SoftwareRenderer.h
+++ b/media/libstagefright/include/SoftwareRenderer.h
@@ -20,16 +20,16 @@
 
 #include <media/stagefright/ColorConverter.h>
 #include <utils/RefBase.h>
+#include <ui/android_native_buffer.h>
 
 namespace android {
 
 struct MetaData;
-class Surface;
 
 class SoftwareRenderer {
 public:
     SoftwareRenderer(
-            const sp<Surface> &surface, const sp<MetaData> &meta);
+            const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta);
 
     ~SoftwareRenderer();
 
@@ -44,7 +44,7 @@
     OMX_COLOR_FORMATTYPE mColorFormat;
     ColorConverter *mConverter;
     YUVMode mYUVMode;
-    sp<Surface> mSurface;
+    sp<ANativeWindow> mNativeWindow;
     int32_t mWidth, mHeight;
     int32_t mCropLeft, mCropTop, mCropRight, mCropBottom;
 
diff --git a/media/libstagefright/include/WAVExtractor.h b/media/libstagefright/include/WAVExtractor.h
index 9de197f..ce1f33a 100644
--- a/media/libstagefright/include/WAVExtractor.h
+++ b/media/libstagefright/include/WAVExtractor.h
@@ -18,6 +18,7 @@
 
 #define WAV_EXTRACTOR_H_
 
+#include <utils/Errors.h>
 #include <media/stagefright/MediaExtractor.h>
 
 namespace android {
diff --git a/media/libstagefright/include/avc_utils.h b/media/libstagefright/include/avc_utils.h
index 3aeb07f..afff824 100644
--- a/media/libstagefright/include/avc_utils.h
+++ b/media/libstagefright/include/avc_utils.h
@@ -19,6 +19,7 @@
 #define AVC_UTILS_H_
 
 #include <media/stagefright/foundation/ABuffer.h>
+#include <utils/Errors.h>
 
 namespace android {
 
@@ -52,6 +53,10 @@
 
 const char *AVCProfileToString(uint8_t profile);
 
+sp<MetaData> MakeAACCodecSpecificData(
+        unsigned profile, unsigned sampling_freq_index,
+        unsigned channel_configuration);
+
 }  // namespace android
 
 #endif  // AVC_UTILS_H_
diff --git a/media/libstagefright/matroska/Android.mk b/media/libstagefright/matroska/Android.mk
index 33c9d97..1f1c68b 100644
--- a/media/libstagefright/matroska/Android.mk
+++ b/media/libstagefright/matroska/Android.mk
@@ -2,11 +2,11 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:=                 \
-        MatroskaExtractor.cpp     \
-        mkvparser.cpp             \
+        MatroskaExtractor.cpp
 
 LOCAL_C_INCLUDES:= \
-	$(JNI_H_INCLUDE) \
+        $(JNI_H_INCLUDE) \
+        $(TOP)/external/libvpx/mkvparser \
         $(TOP)/frameworks/base/include/media/stagefright/openmax \
 
 LOCAL_CFLAGS += -Wno-multichar
diff --git a/media/libstagefright/matroska/mkvparser.cpp b/media/libstagefright/matroska/mkvparser.cpp
deleted file mode 100644
index 7448d96..0000000
--- a/media/libstagefright/matroska/mkvparser.cpp
+++ /dev/null
@@ -1,4514 +0,0 @@
-// Copyright (c) 2010 The WebM project authors. All Rights Reserved.

-//

-// Use of this source code is governed by a BSD-style license

-// that can be found in the LICENSE file in the root of the source

-// tree. An additional intellectual property rights grant can be found

-// in the file PATENTS.  All contributing project authors may

-// be found in the AUTHORS file in the root of the source tree.

-

-#include "mkvparser.hpp"

-#include <cassert>

-#include <cstring>

-#include <new>

-//#include <windows.h>

-//#include "odbgstream.hpp"

-//using std::endl;

-

-mkvparser::IMkvReader::~IMkvReader()

-{

-}

-

-

-void mkvparser::GetVersion(int& major, int& minor, int& build, int& revision)

-{

-    major = 1;

-    minor = 0;

-    build = 0;

-    revision = 4;

-}

-

-

-long long mkvparser::ReadUInt(IMkvReader* pReader, long long pos, long& len)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(pos < available);

-    assert((available - pos) >= 1);  //assume here max u-int len is 8

-

-    unsigned char b;

-

-    hr = pReader->Read(pos, 1, &b);

-    if (hr < 0)

-        return hr;

-

-    assert(hr == 0L);

-

-    if (b & 0x80)       //1000 0000

-    {

-        len = 1;

-        b &= 0x7F;      //0111 1111

-    }

-    else if (b & 0x40)  //0100 0000

-    {

-        len = 2;

-        b &= 0x3F;      //0011 1111

-    }

-    else if (b & 0x20)  //0010 0000

-    {

-        len = 3;

-        b &= 0x1F;      //0001 1111

-    }

-    else if (b & 0x10)  //0001 0000

-    {

-        len = 4;

-        b &= 0x0F;      //0000 1111

-    }

-    else if (b & 0x08)  //0000 1000

-    {

-        len = 5;

-        b &= 0x07;      //0000 0111

-    }

-    else if (b & 0x04)  //0000 0100

-    {

-        len = 6;

-        b &= 0x03;      //0000 0011

-    }

-    else if (b & 0x02)  //0000 0010

-    {

-        len = 7;

-        b &= 0x01;      //0000 0001

-    }

-    else

-    {

-        assert(b & 0x01);  //0000 0001

-        len = 8;

-        b = 0;             //0000 0000

-    }

-

-    assert((available - pos) >= len);

-

-    long long result = b;

-    ++pos;

-    for (long i = 1; i < len; ++i)

-    {

-        hr = pReader->Read(pos, 1, &b);

-

-        if (hr < 0)

-            return hr;

-

-        assert(hr == 0L);

-

-        result <<= 8;

-        result |= b;

-

-        ++pos;

-    }

-

-    return result;

-}

-

-

-long long mkvparser::GetUIntLength(

-    IMkvReader* pReader,

-    long long pos,

-    long& len)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    if (pos >= available)

-        return pos;  //too few bytes available

-

-    unsigned char b;

-

-    hr = pReader->Read(pos, 1, &b);

-

-    if (hr < 0)

-        return hr;

-

-    assert(hr == 0L);

-

-    if (b == 0)  //we can't handle u-int values larger than 8 bytes

-        return E_FILE_FORMAT_INVALID;

-

-    unsigned char m = 0x80;

-    len = 1;

-

-    while (!(b & m))

-    {

-        m >>= 1;

-        ++len;

-    }

-

-    return 0;  //success

-}

-

-

-long long mkvparser::SyncReadUInt(

-    IMkvReader* pReader,

-    long long pos,

-    long long stop,

-    long& len)

-{

-    assert(pReader);

-

-    if (pos >= stop)

-        return E_FILE_FORMAT_INVALID;

-

-    unsigned char b;

-

-    long hr = pReader->Read(pos, 1, &b);

-

-    if (hr < 0)

-        return hr;

-

-    if (hr != 0L)

-        return E_BUFFER_NOT_FULL;

-

-    if (b == 0)  //we can't handle u-int values larger than 8 bytes

-        return E_FILE_FORMAT_INVALID;

-

-    unsigned char m = 0x80;

-    len = 1;

-

-    while (!(b & m))

-    {

-        m >>= 1;

-        ++len;

-    }

-

-    if ((pos + len) > stop)

-        return E_FILE_FORMAT_INVALID;

-

-    long long result = b & (~m);

-    ++pos;

-

-    for (int i = 1; i < len; ++i)

-    {

-        hr = pReader->Read(pos, 1, &b);

-

-        if (hr < 0)

-            return hr;

-

-        if (hr != 0L)

-            return E_BUFFER_NOT_FULL;

-

-        result <<= 8;

-        result |= b;

-

-        ++pos;

-    }

-

-    return result;

-}

-

-

-long long mkvparser::UnserializeUInt(

-    IMkvReader* pReader,

-    long long pos,

-    long long size)

-{

-    assert(pReader);

-    assert(pos >= 0);

-    assert(size > 0);

-    assert(size <= 8);

-

-    long long result = 0;

-

-    for (long long i = 0; i < size; ++i)

-    {

-        unsigned char b;

-

-        const long hr = pReader->Read(pos, 1, &b);

-

-        if (hr < 0)

-            return hr;

-        result <<= 8;

-        result |= b;

-

-        ++pos;

-    }

-

-    return result;

-}

-

-

-float mkvparser::Unserialize4Float(

-    IMkvReader* pReader,

-    long long pos)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-    assert((pos + 4) <= available);

-

-    float result;

-

-    unsigned char* const p = (unsigned char*)&result;

-    unsigned char* q = p + 4;

-

-    for (;;)

-    {

-        hr = pReader->Read(pos, 1, --q);

-        assert(hr == 0L);

-

-        if (q == p)

-            break;

-

-        ++pos;

-    }

-

-    return result;

-}

-

-

-double mkvparser::Unserialize8Double(

-    IMkvReader* pReader,

-    long long pos)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    double result;

-

-    unsigned char* const p = (unsigned char*)&result;

-    unsigned char* q = p + 8;

-

-    for (;;)

-    {

-        const long hr = pReader->Read(pos, 1, --q);

-        assert(hr == 0L);

-

-        if (q == p)

-            break;

-

-        ++pos;

-    }

-

-    return result;

-}

-

-signed char mkvparser::Unserialize1SInt(

-    IMkvReader* pReader,

-    long long pos)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr == 0);

-    assert(available <= total);

-    assert(pos < available);

-

-    signed char result;

-

-    hr = pReader->Read(pos, 1, (unsigned char*)&result);

-    assert(hr == 0);

-

-    return result;

-}

-

-short mkvparser::Unserialize2SInt(

-    IMkvReader* pReader,

-    long long pos)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-    assert((pos + 2) <= available);

-

-    short result;

-

-    unsigned char* const p = (unsigned char*)&result;

-    unsigned char* q = p + 2;

-

-    for (;;)

-    {

-        hr = pReader->Read(pos, 1, --q);

-        assert(hr == 0L);

-

-        if (q == p)

-            break;

-

-        ++pos;

-    }

-

-    return result;

-}

-

-

-bool mkvparser::Match(

-    IMkvReader* pReader,

-    long long& pos,

-    unsigned long id_,

-    long long& val)

-

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    long len;

-

-    const long long id = ReadUInt(pReader, pos, len);

-    assert(id >= 0);

-    assert(len > 0);

-    assert(len <= 8);

-    assert((pos + len) <= available);

-

-    if ((unsigned long)id != id_)

-        return false;

-

-    pos += len;  //consume id

-

-    const long long size = ReadUInt(pReader, pos, len);

-    assert(size >= 0);

-    assert(size <= 8);

-    assert(len > 0);

-    assert(len <= 8);

-    assert((pos + len) <= available);

-

-    pos += len;  //consume length of size of payload

-

-    val = UnserializeUInt(pReader, pos, size);

-    assert(val >= 0);

-

-    pos += size;  //consume size of payload

-

-    return true;

-}

-

-bool mkvparser::Match(

-    IMkvReader* pReader,

-    long long& pos,

-    unsigned long id_,

-    char*& val)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    long len;

-

-    const long long id = ReadUInt(pReader, pos, len);

-    assert(id >= 0);

-    assert(len > 0);

-    assert(len <= 8);

-    assert((pos + len) <= available);

-

-    if ((unsigned long)id != id_)

-        return false;

-

-    pos += len;  //consume id

-

-    const long long size_ = ReadUInt(pReader, pos, len);

-    assert(size_ >= 0);

-    assert(len > 0);

-    assert(len <= 8);

-    assert((pos + len) <= available);

-

-    pos += len;  //consume length of size of payload

-    assert((pos + size_) <= available);

-

-    const size_t size = static_cast<size_t>(size_);

-    val = new char[size+1];

-

-    for (size_t i = 0; i < size; ++i)

-    {

-        char c;

-

-        hr = pReader->Read(pos + i, 1, (unsigned char*)&c);

-        assert(hr == 0L);

-

-        val[i] = c;

-

-        if (c == '\0')

-            break;

-

-    }

-

-    val[size] = '\0';

-    pos += size_;  //consume size of payload

-

-    return true;

-}

-

-bool mkvparser::Match(

-    IMkvReader* pReader,

-    long long& pos,

-    unsigned long id_,

-    unsigned char*& buf,

-    size_t& buflen)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    long len;

-    const long long id = ReadUInt(pReader, pos, len);

-    assert(id >= 0);

-    assert(len > 0);

-    assert(len <= 8);

-    assert((pos + len) <= available);

-

-    if ((unsigned long)id != id_)

-        return false;

-

-    pos += len;  //consume id

-

-    const long long size_ = ReadUInt(pReader, pos, len);

-    assert(size_ >= 0);

-    assert(len > 0);

-    assert(len <= 8);

-    assert((pos + len) <= available);

-

-    pos += len;  //consume length of size of payload

-    assert((pos + size_) <= available);

-

-    const long buflen_ = static_cast<long>(size_);

-

-    buf = new (std::nothrow) unsigned char[buflen_];

-    assert(buf);  //TODO

-

-    hr = pReader->Read(pos, buflen_, buf);

-    assert(hr == 0L);

-

-    buflen = buflen_;

-

-    pos += size_;  //consume size of payload

-    return true;

-}

-

-

-bool mkvparser::Match(

-    IMkvReader* pReader,

-    long long& pos,

-    unsigned long id_,

-    double& val)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-    long idlen;

-    const long long id = ReadUInt(pReader, pos, idlen);

-    assert(id >= 0);  //TODO

-

-    if ((unsigned long)id != id_)

-        return false;

-

-    long sizelen;

-    const long long size = ReadUInt(pReader, pos + idlen, sizelen);

-

-    switch (size)

-    {

-        case 4:

-        case 8:

-            break;

-        default:

-            return false;

-    }

-

-    pos += idlen + sizelen;  //consume id and size fields

-    assert((pos + size) <= available);

-

-    if (size == 4)

-        val = Unserialize4Float(pReader, pos);

-    else

-    {

-        assert(size == 8);

-        val = Unserialize8Double(pReader, pos);

-    }

-

-    pos += size;  //consume size of payload

-

-    return true;

-}

-

-

-bool mkvparser::Match(

-    IMkvReader* pReader,

-    long long& pos,

-    unsigned long id_,

-    short& val)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    long len;

-    const long long id = ReadUInt(pReader, pos, len);

-    assert(id >= 0);

-    assert((pos + len) <= available);

-

-    if ((unsigned long)id != id_)

-        return false;

-

-    pos += len;  //consume id

-

-    const long long size = ReadUInt(pReader, pos, len);

-    assert(size <= 2);

-    assert((pos + len) <= available);

-

-    pos += len;  //consume length of size of payload

-    assert((pos + size) <= available);

-

-    //TODO:

-    // Generalize this to work for any size signed int

-    if (size == 1)

-        val = Unserialize1SInt(pReader, pos);

-    else

-        val = Unserialize2SInt(pReader, pos);

-

-    pos += size;  //consume size of payload

-

-    return true;

-}

-

-

-namespace mkvparser

-{

-

-EBMLHeader::EBMLHeader():

-    m_docType(NULL)

-{

-}

-

-EBMLHeader::~EBMLHeader()

-{

-    delete[] m_docType;

-}

-

-long long EBMLHeader::Parse(

-    IMkvReader* pReader,

-    long long& pos)

-{

-    assert(pReader);

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-

-    if (hr < 0)

-        return hr;

-

-    pos = 0;

-    long long end = (1024 < available)? 1024: available;

-

-    for (;;)

-    {

-        unsigned char b = 0;

-

-        while (pos < end)

-        {

-            hr = pReader->Read(pos, 1, &b);

-

-            if (hr < 0)

-                return hr;

-

-            if (b == 0x1A)

-                break;

-

-            ++pos;

-        }

-

-        if (b != 0x1A)

-        {

-            if ((pos >= 1024) ||

-                (available >= total) ||

-                ((total - available) < 5))

-                  return -1;

-

-            return available + 5;  //5 = 4-byte ID + 1st byte of size

-        }

-

-        if ((total - pos) < 5)

-            return E_FILE_FORMAT_INVALID;

-

-        if ((available - pos) < 5)

-            return pos + 5;  //try again later

-

-        long len;

-

-        const long long result = ReadUInt(pReader, pos, len);

-

-        if (result < 0)  //error

-            return result;

-

-        if (result == 0x0A45DFA3)  //ReadId masks-off length indicator bits

-        {

-            assert(len == 4);

-            pos += len;

-            break;

-        }

-

-        ++pos;  //throw away just the 0x1A byte, and try again

-    }

-

-    long len;

-    long long result = GetUIntLength(pReader, pos, len);

-

-    if (result < 0)  //error

-        return result;

-

-    if (result > 0)  //need more data

-        return result;

-

-    assert(len > 0);

-    assert(len <= 8);

-

-    if ((total -  pos) < len)

-        return E_FILE_FORMAT_INVALID;

-    if ((available - pos) < len)

-        return pos + len;  //try again later

-

-    result = ReadUInt(pReader, pos, len);

-

-    if (result < 0)  //error

-        return result;

-

-    pos += len;  //consume u-int

-

-    if ((total - pos) < result)

-        return E_FILE_FORMAT_INVALID;

-

-    if ((available - pos) < result)

-        return pos + result;

-

-    end = pos + result;

-

-    m_version = 1;

-    m_readVersion = 1;

-    m_maxIdLength = 4;

-    m_maxSizeLength = 8;

-    m_docTypeVersion = 1;

-    m_docTypeReadVersion = 1;

-

-    while (pos < end)

-    {

-        if (Match(pReader, pos, 0x0286, m_version))

-            ;

-        else if (Match(pReader, pos, 0x02F7, m_readVersion))

-            ;

-        else if (Match(pReader, pos, 0x02F2, m_maxIdLength))

-            ;

-        else if (Match(pReader, pos, 0x02F3, m_maxSizeLength))

-            ;

-        else if (Match(pReader, pos, 0x0282, m_docType))

-            ;

-        else if (Match(pReader, pos, 0x0287, m_docTypeVersion))

-            ;

-        else if (Match(pReader, pos, 0x0285, m_docTypeReadVersion))

-            ;

-        else

-        {

-            result = ReadUInt(pReader, pos, len);

-            assert(result > 0);

-            assert(len > 0);

-            assert(len <= 8);

-

-            pos += len;

-            assert(pos < end);

-

-            result = ReadUInt(pReader, pos, len);

-            assert(result >= 0);

-            assert(len > 0);

-            assert(len <= 8);

-

-            pos += len + result;

-            assert(pos <= end);

-        }

-    }

-

-    assert(pos == end);

-

-    return 0;

-}

-

-

-Segment::Segment(

-    IMkvReader* pReader,

-    long long start,

-    long long size) :

-    m_pReader(pReader),

-    m_start(start),

-    m_size(size),

-    m_pos(start),

-    m_pInfo(NULL),

-    m_pTracks(NULL),

-    m_pCues(NULL),

-    m_clusters(NULL),

-    m_clusterCount(0),

-    m_clusterPreloadCount(0),

-    m_clusterSize(0)

-{

-}

-

-

-Segment::~Segment()

-{

-    const long count = m_clusterCount + m_clusterPreloadCount;

-

-    Cluster** i = m_clusters;

-    Cluster** j = m_clusters + count;

-

-    while (i != j)

-    {

-        Cluster* const p = *i++;

-        assert(p);

-

-        delete p;

-    }

-

-    delete[] m_clusters;

-

-    delete m_pTracks;

-    delete m_pInfo;

-    delete m_pCues;

-}

-

-

-long long Segment::CreateInstance(

-    IMkvReader* pReader,

-    long long pos,

-    Segment*& pSegment)

-{

-    assert(pReader);

-    assert(pos >= 0);

-

-    pSegment = NULL;

-

-    long long total, available;

-

-    long hr = pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    //I would assume that in practice this loop would execute

-    //exactly once, but we allow for other elements (e.g. Void)

-    //to immediately follow the EBML header.  This is fine for

-    //the source filter case (since the entire file is available),

-    //but in the splitter case over a network we should probably

-    //just give up early.  We could for example decide only to

-    //execute this loop a maximum of, say, 10 times.

-

-    while (pos < total)

-    {

-        //Read ID

-

-        long len;

-        long long result = GetUIntLength(pReader, pos, len);

-

-        if (result)  //error, or too few available bytes

-            return result;

-

-        if ((pos + len) > total)

-            return E_FILE_FORMAT_INVALID;

-

-        if ((pos + len) > available)

-            return pos + len;

-

-        //TODO: if we liberalize the behavior of ReadUInt, we can

-        //probably eliminate having to use GetUIntLength here.

-        const long long id = ReadUInt(pReader, pos, len);

-

-        if (id < 0)  //error

-            return id;

-

-        pos += len;  //consume ID

-

-        //Read Size

-

-        result = GetUIntLength(pReader, pos, len);

-

-        if (result)  //error, or too few available bytes

-            return result;

-

-        if ((pos + len) > total)

-            return E_FILE_FORMAT_INVALID;

-

-        if ((pos + len) > available)

-            return pos + len;

-

-        //TODO: if we liberalize the behavior of ReadUInt, we can

-        //probably eliminate having to use GetUIntLength here.

-        const long long size = ReadUInt(pReader, pos, len);

-

-        if (size < 0)

-            return size;

-

-        pos += len;  //consume length of size of element

-

-        //Pos now points to start of payload

-

-        if ((pos + size) > total)

-            return E_FILE_FORMAT_INVALID;

-

-        if (id == 0x08538067)  //Segment ID

-        {

-            pSegment = new  Segment(pReader, pos, size);

-            assert(pSegment);  //TODO

-

-            return 0;    //success

-        }

-

-        pos += size;  //consume payload

-    }

-

-    assert(pos == total);

-

-    pSegment = new Segment(pReader, pos, 0);

-    assert(pSegment);  //TODO

-

-    return 0;  //success (sort of)

-}

-

-

-long long Segment::ParseHeaders()

-{

-    //Outermost (level 0) segment object has been constructed,

-    //and pos designates start of payload.  We need to find the

-    //inner (level 1) elements.

-    long long total, available;

-

-    long hr = m_pReader->Length(&total, &available);

-    assert(hr >= 0);

-    assert(available <= total);

-

-    const long long stop = m_start + m_size;

-    assert(stop <= total);

-    assert(m_pos <= stop);

-

-    bool bQuit = false;

-

-    while ((m_pos < stop) && !bQuit)

-    {

-        long long pos = m_pos;

-

-        long len;

-        long long result = GetUIntLength(m_pReader, pos, len);

-

-        if (result)  //error, or too few available bytes

-            return result;

-

-        if ((pos + len) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        if ((pos + len) > available)

-            return pos + len;

-

-        const long long idpos = pos;

-        const long long id = ReadUInt(m_pReader, idpos, len);

-

-        if (id < 0)  //error

-            return id;

-

-        pos += len;  //consume ID

-

-        //Read Size

-        result = GetUIntLength(m_pReader, pos, len);

-

-        if (result)  //error, or too few available bytes

-            return result;

-

-        if ((pos + len) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        if ((pos + len) > available)

-            return pos + len;

-

-        const long long size = ReadUInt(m_pReader, pos, len);

-

-        if (size < 0)

-            return size;

-

-        pos += len;  //consume length of size of element

-

-        //Pos now points to start of payload

-

-        if ((pos + size) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        //We read EBML elements either in total or nothing at all.

-

-        if ((pos + size) > available)

-            return pos + size;

-

-        if (id == 0x0549A966)  //Segment Info ID

-        {

-            assert(m_pInfo == NULL);

-

-            m_pInfo = new SegmentInfo(this, pos, size);

-            assert(m_pInfo);  //TODO

-        }

-        else if (id == 0x0654AE6B)  //Tracks ID

-        {

-            assert(m_pTracks == NULL);

-

-            m_pTracks = new Tracks(this, pos, size);

-            assert(m_pTracks);  //TODO

-        }

-        else if (id == 0x0C53BB6B)  //Cues ID

-        {

-            if (m_pCues == NULL)

-            {

-                m_pCues = new Cues(this, pos, size);

-                assert(m_pCues);  //TODO

-            }

-        }

-        else if (id == 0x014D9B74)  //SeekHead ID

-        {

-            ParseSeekHead(pos, size);

-        }

-        else if (id == 0x0F43B675)  //Cluster ID

-        {

-            bQuit = true;

-        }

-

-        if (!bQuit)

-            m_pos = pos + size;  //consume payload

-    }

-

-    assert(m_pos <= stop);

-

-    if (m_pInfo == NULL)  //TODO: liberalize this behavior

-        return E_FILE_FORMAT_INVALID;

-

-    if (m_pTracks == NULL)

-        return E_FILE_FORMAT_INVALID;

-

-    return 0;  //success

-}

-

-

-#if 0

-long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const

-{

-    pCluster = NULL;

-    pos_ = -1;

-

-    const long long stop = m_start + m_size;

-    assert(m_pos <= stop);

-

-    long long pos = m_pos;

-    long long off = -1;

-

-    while (pos < stop)

-    {

-        long len;

-        const long long idpos = pos;

-

-        const long long id = SyncReadUInt(m_pReader, pos, stop, len);

-

-        if (id < 0)  //error

-            return static_cast<long>(id);

-

-        if (id == 0)

-            return E_FILE_FORMAT_INVALID;

-

-        pos += len;  //consume id

-        assert(pos < stop);

-

-        const long long size = SyncReadUInt(m_pReader, pos, stop, len);

-

-        if (size < 0)  //error

-            return static_cast<long>(size);

-

-        pos += len;  //consume size

-        assert(pos <= stop);

-

-        if (size == 0)  //weird

-            continue;

-

-        //pos now points to start of payload

-

-        pos += size;  //consume payload

-        assert(pos <= stop);

-

-        if (id == 0x0F43B675)  //Cluster ID

-        {

-            off = idpos - m_start;  // >= 0 means we found a cluster

-            break;

-        }

-    }

-

-    assert(pos <= stop);

-

-    //Indicate to caller how much of file has been consumed. This is

-    //used later in AddCluster to adjust the current parse position

-    //(the value cached in the segment object itself) to the

-    //file position value just past the cluster we parsed.

-

-    if (off < 0)  //we did not found any more clusters

-    {

-        pos_ = stop;  //pos_ >= 0 here means EOF (cluster is NULL)

-        return 0;     //TODO: confirm this return value

-    }

-

-    //We found a cluster.  Now read something, to ensure that it is

-    //fully loaded in the network cache.

-

-    if (pos >= stop)  //we parsed the entire segment

-    {

-        //We did find a cluster, but it was very last element in the segment.

-        //Our preference is that the loop above runs 1 1/2 times:

-        //the first pass finds the cluster, and the second pass

-        //finds the element the follows the cluster.  In this case, however,

-        //we reached the end of the file without finding another element,

-        //so we didn't actually read anything yet associated with "end of the

-        //cluster".  And we must perform an actual read, in order

-        //to guarantee that all of the data that belongs to this

-        //cluster has been loaded into the network cache.  So instead

-        //of reading the next element that follows the cluster, we

-        //read the last byte of the cluster (which is also the last

-        //byte in the file).

-

-        //Read the last byte of the file. (Reading 0 bytes at pos

-        //might work too -- it would depend on how the reader is

-        //implemented.  Here we take the more conservative approach,

-        //since this makes fewer assumptions about the network

-        //reader abstraction.)

-

-        unsigned char b;

-

-        const int result = m_pReader->Read(pos - 1, 1, &b);

-        assert(result == 0);

-

-        pos_ = stop;

-    }

-    else

-    {

-        long len;

-        const long long idpos = pos;

-

-        const long long id = SyncReadUInt(m_pReader, pos, stop, len);

-

-        if (id < 0)  //error

-            return static_cast<long>(id);

-

-        if (id == 0)

-            return E_BUFFER_NOT_FULL;

-

-        pos += len;  //consume id

-        assert(pos < stop);

-

-        const long long size = SyncReadUInt(m_pReader, pos, stop, len);

-

-        if (size < 0)  //error

-            return static_cast<long>(size);

-

-        pos_ = idpos;

-    }

-

-    //We found a cluster, and it has been completely loaded into the

-    //network cache.  (We can guarantee this because we actually read

-    //the EBML tag that follows the cluster, or, if we reached EOF,

-    //because we actually read the last byte of the cluster).

-

-    Segment* const this_ = const_cast<Segment*>(this);

-

-    pCluster = Cluster::Parse(this_, m_clusterCount, off);

-    assert(pCluster);

-    assert(pCluster->m_index == m_clusterCount);

-

-    return 0;

-}

-

-

-bool Segment::AddCluster(Cluster* pCluster, long long pos)

-{

-    assert(pos >= m_start);

-

-    const long long stop = m_start + m_size;

-    assert(pos <= stop);

-

-    if (pCluster)

-    {

-        AppendCluster(pCluster);

-        assert(m_clusters);

-        assert(m_clusterSize > pCluster->m_index);

-        assert(m_clusters[pCluster->m_index] == pCluster);

-    }

-

-    m_pos = pos;  //m_pos >= stop is now we know we have all clusters

-

-    return (pos >= stop);

-}

-#endif

-

-

-long Segment::LoadCluster()

-{

-    const long long stop = m_start + m_size;

-

-    while (m_pos < stop)

-    {

-        long long pos = m_pos;

-

-        long len;

-

-        long long result = GetUIntLength(m_pReader, pos, len);

-

-        if (result < 0)  //error

-            return static_cast<long>(result);

-

-        if ((pos + len) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        const long long idpos = pos;

-        const long long id = ReadUInt(m_pReader, idpos, len);

-

-        if (id < 0)  //error

-            return static_cast<long>(id);

-

-        pos += len;  //consume ID

-

-        //Read Size

-        result = GetUIntLength(m_pReader, pos, len);

-

-        if (result < 0)  //error

-            return static_cast<long>(result);

-

-        if ((pos + len) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        const long long size = ReadUInt(m_pReader, pos, len);

-

-        if (size < 0)  //error

-            return static_cast<long>(size);

-

-        pos += len;  //consume length of size of element

-

-        if (size == 0)  //weird

-        {

-            m_pos = pos;

-            continue;

-        }

-

-        //Pos now points to start of payload

-

-        if ((pos + size) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        if (id == 0x0C53BB6B)  //Cues ID

-        {

-            if (m_pCues == NULL)

-            {

-                m_pCues = new Cues(this, pos, size);

-                assert(m_pCues);  //TODO

-            }

-

-            m_pos = pos + size;  //consume payload

-            continue;

-        }

-

-        if (id != 0x0F43B675)  //Cluster ID

-        {

-            m_pos = pos + size;  //consume payload

-            continue;

-        }

-

-        const long idx = m_clusterCount;

-        const long long idoff = idpos - m_start;

-

-        if (m_clusterPreloadCount > 0)

-        {

-            assert(idx < m_clusterSize);

-

-            Cluster* const pCluster = m_clusters[idx];

-            assert(pCluster);

-            assert(pCluster->m_index < 0);

-

-            const long long off_ = pCluster->m_pos;

-            assert(off_);

-

-            const long long off = off_ * ((off_ >= 0) ? 1 : -1);

-            assert(idoff <= off);

-

-            if (idoff == off)  //cluster has been preloaded already

-            {

-                pCluster->m_index = idx;

-                ++m_clusterCount;

-                --m_clusterPreloadCount;

-

-                m_pos = pos + size;  //consume payload

-                break;

-            }

-        }

-

-        Cluster* const pCluster = Cluster::Parse(this, idx, idoff);

-        assert(pCluster);

-        assert(pCluster->m_index == idx);

-

-        AppendCluster(pCluster);

-        assert(m_clusters);

-        assert(idx < m_clusterSize);

-        assert(m_clusters[idx] == pCluster);

-

-        m_pos = pos + size;  //consume payload

-        break;

-    }

-

-    assert(m_pos <= stop);

-    return 0;

-}

-

-

-void Segment::AppendCluster(Cluster* pCluster)

-{

-    assert(pCluster);

-    assert(pCluster->m_index >= 0);

-

-    const long count = m_clusterCount + m_clusterPreloadCount;

-

-    long& size = m_clusterSize;

-    assert(size >= count);

-

-    const long idx = pCluster->m_index;

-    assert(idx == m_clusterCount);

-

-    if (count >= size)

-    {

-        long n;

-

-        if (size > 0)

-            n = 2 * size;

-        else if (m_pInfo == 0)

-            n = 2048;

-        else

-        {

-            const long long ns = m_pInfo->GetDuration();

-

-            if (ns <= 0)

-                n = 2048;

-            else

-            {

-                const long long sec = (ns + 999999999LL) / 1000000000LL;

-                n = static_cast<long>(sec);

-            }

-        }

-

-        Cluster** const qq = new Cluster*[n];

-        Cluster** q = qq;

-

-        Cluster** p = m_clusters;

-        Cluster** const pp = p + count;

-

-        while (p != pp)

-            *q++ = *p++;

-

-        delete[] m_clusters;

-

-        m_clusters = qq;

-        size = n;

-    }

-

-    if (m_clusterPreloadCount > 0)

-    {

-        assert(m_clusters);

-

-        Cluster** const p = m_clusters + m_clusterCount;

-        assert(*p);

-        assert((*p)->m_index < 0);

-

-        Cluster** q = p + m_clusterPreloadCount;

-        assert(q < (m_clusters + size));

-

-        for (;;)

-        {

-            Cluster** const qq = q - 1;

-            assert((*qq)->m_index < 0);

-

-            *q = *qq;

-            q = qq;

-

-            if (q == p)

-                break;

-        }

-    }

-

-    m_clusters[idx] = pCluster;

-    ++m_clusterCount;

-}

-

-

-void Segment::PreloadCluster(Cluster* pCluster, ptrdiff_t idx)

-{

-    assert(pCluster);

-    assert(pCluster->m_index < 0);

-    assert(idx >= m_clusterCount);

-

-    const long count = m_clusterCount + m_clusterPreloadCount;

-

-    long& size = m_clusterSize;

-    assert(size >= count);

-

-    if (count >= size)

-    {

-        long n;

-

-        if (size > 0)

-            n = 2 * size;

-        else if (m_pInfo == 0)

-            n = 2048;

-        else

-        {

-            const long long ns = m_pInfo->GetDuration();

-

-            if (ns <= 0)

-                n = 2048;

-            else

-            {

-                const long long sec = (ns + 999999999LL) / 1000000000LL;

-                n = static_cast<long>(sec);

-            }

-        }

-

-        Cluster** const qq = new Cluster*[n];

-        Cluster** q = qq;

-

-        Cluster** p = m_clusters;

-        Cluster** const pp = p + count;

-

-        while (p != pp)

-            *q++ = *p++;

-

-        delete[] m_clusters;

-

-        m_clusters = qq;

-        size = n;

-    }

-

-    assert(m_clusters);

-

-    Cluster** const p = m_clusters + idx;

-

-    Cluster** q = m_clusters + count;

-    assert(q >= p);

-    assert(q < (m_clusters + size));

-

-    while (q > p)

-    {

-        Cluster** const qq = q - 1;

-        assert((*qq)->m_index < 0);

-

-        *q = *qq;

-        q = qq;

-    }

-

-    m_clusters[idx] = pCluster;

-    ++m_clusterPreloadCount;

-}

-

-

-long Segment::Load()

-{

-    assert(m_clusters == NULL);

-    assert(m_clusterSize == 0);

-    assert(m_clusterCount == 0);

-

-    //Outermost (level 0) segment object has been constructed,

-    //and pos designates start of payload.  We need to find the

-    //inner (level 1) elements.

-    const long long stop = m_start + m_size;

-

-#ifdef _DEBUG  //TODO: this is really Microsoft-specific

-    {

-        long long total, available;

-

-        long hr = m_pReader->Length(&total, &available);

-        assert(hr >= 0);

-        assert(available >= total);

-        assert(stop <= total);

-    }

-#endif

-

-    while (m_pos < stop)

-    {

-        long long pos = m_pos;

-

-        long len;

-

-        long long result = GetUIntLength(m_pReader, pos, len);

-

-        if (result < 0)  //error

-            return static_cast<long>(result);

-

-        if ((pos + len) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        const long long idpos = pos;

-        const long long id = ReadUInt(m_pReader, idpos, len);

-

-        if (id < 0)  //error

-            return static_cast<long>(id);

-

-        pos += len;  //consume ID

-

-        //Read Size

-        result = GetUIntLength(m_pReader, pos, len);

-

-        if (result < 0)  //error

-            return static_cast<long>(result);

-

-        if ((pos + len) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        const long long size = ReadUInt(m_pReader, pos, len);

-

-        if (size < 0)  //error

-            return static_cast<long>(size);

-

-        pos += len;  //consume length of size of element

-

-        //Pos now points to start of payload

-

-        if ((pos + size) > stop)

-            return E_FILE_FORMAT_INVALID;

-

-        if (id == 0x0F43B675)  //Cluster ID

-        {

-            const long idx = m_clusterCount;

-            const long long off = idpos - m_start;

-

-            Cluster* const pCluster = Cluster::Parse(this, idx, off);

-            assert(pCluster);

-            assert(pCluster->m_index == idx);

-

-            AppendCluster(pCluster);

-            assert(m_clusters);

-            assert(m_clusterSize > idx);

-            assert(m_clusters[idx] == pCluster);

-        }

-        else if (id == 0x0C53BB6B)  //Cues ID

-        {

-            assert(m_pCues == NULL);

-

-            m_pCues = new Cues(this, pos, size);

-            assert(m_pCues);  //TODO

-        }

-        else if (id == 0x0549A966)  //SegmentInfo ID

-        {

-            assert(m_pInfo == NULL);

-

-            m_pInfo = new  SegmentInfo(this, pos, size);

-            assert(m_pInfo);

-        }

-        else if (id == 0x0654AE6B)  //Tracks ID

-        {

-            assert(m_pTracks == NULL);

-

-            m_pTracks = new Tracks(this, pos, size);

-            assert(m_pTracks);  //TODO

-        }

-

-        m_pos = pos + size;  //consume payload

-    }

-

-    assert(m_pos >= stop);

-

-    if (m_pInfo == NULL)

-        return E_FILE_FORMAT_INVALID;  //TODO: ignore this case?

-

-    if (m_pTracks == NULL)

-        return E_FILE_FORMAT_INVALID;

-

-    if (m_clusters == NULL)  //TODO: ignore this case?

-        return E_FILE_FORMAT_INVALID;

-

-    //TODO: decide whether we require Cues element

-    //if (m_pCues == NULL)

-    //   return E_FILE_FORMAT_INVALID;

-

-    return 0;

-}

-

-

-void Segment::ParseSeekHead(long long start, long long size_)

-{

-    long long pos = start;

-    const long long stop = start + size_;

-

-    while (pos < stop)

-    {

-        long len;

-

-        const long long id = ReadUInt(m_pReader, pos, len);

-        assert(id >= 0);  //TODO

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume ID

-

-        const long long size = ReadUInt(m_pReader, pos, len);

-        assert(size >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume Size field

-        assert((pos + size) <= stop);

-

-        if (id == 0x0DBB)  //SeekEntry ID

-            ParseSeekEntry(pos, size);

-

-        pos += size;  //consume payload

-        assert(pos <= stop);

-    }

-

-    assert(pos == stop);

-}

-

-

-void Segment::ParseCues(long long off)

-{

-    if (m_pCues)

-        return;

-

-    //odbgstream os;

-    //os << "Segment::ParseCues (begin)" << endl;

-

-    long long pos = m_start + off;

-    const long long stop = m_start + m_size;

-

-    long len;

-

-    long long result = GetUIntLength(m_pReader, pos, len);

-    assert(result == 0);

-    assert((pos + len) <= stop);

-

-    const long long idpos = pos;

-

-    const long long id = ReadUInt(m_pReader, idpos, len);

-    assert(id == 0x0C53BB6B);  //Cues ID

-

-    pos += len;  //consume ID

-    assert(pos < stop);

-

-    //Read Size

-

-    result = GetUIntLength(m_pReader, pos, len);

-    assert(result == 0);

-    assert((pos + len) <= stop);

-

-    const long long size = ReadUInt(m_pReader, pos, len);

-    assert(size >= 0);

-

-    pos += len;  //consume length of size of element

-    assert((pos + size) <= stop);

-

-    //Pos now points to start of payload

-

-    m_pCues = new Cues(this, pos, size);

-    assert(m_pCues);  //TODO

-

-    //os << "Segment::ParseCues (end)" << endl;

-}

-

-

-void Segment::ParseSeekEntry(

-   long long start,

-   long long size_)

-{

-    long long pos = start;

-

-    const long long stop = start + size_;

-

-    long len;

-

-    const long long seekIdId = ReadUInt(m_pReader, pos, len);

-    //seekIdId;

-    assert(seekIdId == 0x13AB);  //SeekID ID

-    assert((pos + len) <= stop);

-

-    pos += len;  //consume id

-

-    const long long seekIdSize = ReadUInt(m_pReader, pos, len);

-    assert(seekIdSize >= 0);

-    assert((pos + len) <= stop);

-

-    pos += len;  //consume size

-

-    const long long seekId = ReadUInt(m_pReader, pos, len);  //payload

-    assert(seekId >= 0);

-    assert(len == seekIdSize);

-    assert((pos + len) <= stop);

-

-    pos += seekIdSize;  //consume payload

-

-    const long long seekPosId = ReadUInt(m_pReader, pos, len);

-    //seekPosId;

-    assert(seekPosId == 0x13AC);  //SeekPos ID

-    assert((pos + len) <= stop);

-

-    pos += len;  //consume id

-

-    const long long seekPosSize = ReadUInt(m_pReader, pos, len);

-    assert(seekPosSize >= 0);

-    assert((pos + len) <= stop);

-

-    pos += len;  //consume size

-    assert((pos + seekPosSize) <= stop);

-

-    const long long seekOff = UnserializeUInt(m_pReader, pos, seekPosSize);

-    assert(seekOff >= 0);

-    assert(seekOff < m_size);

-

-    pos += seekPosSize;  //consume payload

-    assert(pos == stop);

-

-    const long long seekPos = m_start + seekOff;

-    assert(seekPos < (m_start + m_size));

-

-    if (seekId == 0x0C53BB6B)  //Cues ID

-        ParseCues(seekOff);

-}

-

-

-Cues::Cues(Segment* pSegment, long long start_, long long size_) :

-    m_pSegment(pSegment),

-    m_start(start_),

-    m_size(size_),

-    m_cue_points(NULL),

-    m_count(0),

-    m_preload_count(0),

-    m_pos(start_)

-{

-}

-

-

-Cues::~Cues()

-{

-    const size_t n = m_count + m_preload_count;

-

-    CuePoint** p = m_cue_points;

-    CuePoint** const q = p + n;

-

-    while (p != q)

-    {

-        CuePoint* const pCP = *p++;

-        assert(pCP);

-

-        delete pCP;

-    }

-

-    delete[] m_cue_points;

-}

-

-

-void Cues::Init() const

-{

-    if (m_cue_points)

-        return;

-

-    assert(m_count == 0);

-    assert(m_preload_count == 0);

-

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    const long long stop = m_start + m_size;

-    long long pos = m_start;

-

-    size_t cue_points_size = 0;

-

-    while (pos < stop)

-    {

-        const long long idpos = pos;

-

-        long len;

-

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);  //TODO

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume ID

-

-        const long long size = ReadUInt(pReader, pos, len);

-        assert(size >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume Size field

-        assert((pos + size) <= stop);

-

-        if (id == 0x3B)  //CuePoint ID

-            PreloadCuePoint(cue_points_size, idpos);

-

-        pos += size;  //consume payload

-        assert(pos <= stop);

-    }

-}

-

-

-void Cues::PreloadCuePoint(

-    size_t& cue_points_size,

-    long long pos) const

-{

-    assert(m_count == 0);

-

-    if (m_preload_count >= cue_points_size)

-    {

-        size_t n;

-

-        if (cue_points_size > 0)

-            n = static_cast<size_t>(2 * cue_points_size);

-        else

-        {

-            const SegmentInfo* const pInfo = m_pSegment->GetInfo();

-

-            if (pInfo == NULL)

-                n = 2048;

-            else

-            {

-                const long long ns = pInfo->GetDuration();

-

-                if (ns <= 0)

-                    n = 2048;

-                else

-                {

-                    const long long sec = (ns + 999999999LL) / 1000000000LL;

-                    n = static_cast<size_t>(sec);

-                }

-            }

-        }

-

-        CuePoint** const qq = new CuePoint*[n];

-        CuePoint** q = qq;  //beginning of target

-

-        CuePoint** p = m_cue_points;                //beginning of source

-        CuePoint** const pp = p + m_preload_count;  //end of source

-

-        while (p != pp)

-            *q++ = *p++;

-

-        delete[] m_cue_points;

-

-        m_cue_points = qq;

-        cue_points_size = n;

-    }

-

-    CuePoint* const pCP = new CuePoint(m_preload_count, pos);

-    m_cue_points[m_preload_count++] = pCP;

-}

-

-

-bool Cues::LoadCuePoint() const

-{

-    //odbgstream os;

-    //os << "Cues::LoadCuePoint" << endl;

-

-    const long long stop = m_start + m_size;

-

-    if (m_pos >= stop)

-        return false;  //nothing else to do

-

-    Init();

-

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    while (m_pos < stop)

-    {

-        const long long idpos = m_pos;

-

-        long len;

-

-        const long long id = ReadUInt(pReader, m_pos, len);

-        assert(id >= 0);  //TODO

-        assert((m_pos + len) <= stop);

-

-        m_pos += len;  //consume ID

-

-        const long long size = ReadUInt(pReader, m_pos, len);

-        assert(size >= 0);

-        assert((m_pos + len) <= stop);

-

-        m_pos += len;  //consume Size field

-        assert((m_pos + size) <= stop);

-

-        if (id != 0x3B)  //CuePoint ID

-        {

-            m_pos += size;  //consume payload

-            assert(m_pos <= stop);

-

-            continue;

-        }

-

-        assert(m_preload_count > 0);

-

-        CuePoint* const pCP = m_cue_points[m_count];

-        assert(pCP);

-        assert((pCP->GetTimeCode() >= 0) || (-pCP->GetTimeCode() == idpos));

-

-        pCP->Load(pReader);

-        ++m_count;

-        --m_preload_count;

-

-        m_pos += size;  //consume payload

-        assert(m_pos <= stop);

-

-        break;

-    }

-

-    return (m_pos < stop);

-}

-

-

-bool Cues::Find(

-    long long time_ns,

-    const Track* pTrack,

-    const CuePoint*& pCP,

-    const CuePoint::TrackPosition*& pTP) const

-{

-    assert(time_ns >= 0);

-    assert(pTrack);

-

-    LoadCuePoint();

-

-    assert(m_cue_points);

-    assert(m_count > 0);

-

-    CuePoint** const ii = m_cue_points;

-    CuePoint** i = ii;

-

-    CuePoint** const jj = ii + m_count + m_preload_count;

-    CuePoint** j = jj;

-

-    pCP = *i;

-    assert(pCP);

-

-    if (time_ns <= pCP->GetTime(m_pSegment))

-    {

-        pTP = pCP->Find(pTrack);

-        return (pTP != NULL);

-    }

-

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    while (i < j)

-    {

-        //INVARIANT:

-        //[ii, i) <= time_ns

-        //[i, j)  ?

-        //[j, jj) > time_ns

-

-        CuePoint** const k = i + (j - i) / 2;

-        assert(k < jj);

-

-        CuePoint* const pCP = *k;

-        assert(pCP);

-

-        pCP->Load(pReader);

-

-        const long long t = pCP->GetTime(m_pSegment);

-

-        if (t <= time_ns)

-            i = k + 1;

-        else

-            j = k;

-

-        assert(i <= j);

-    }

-

-    assert(i == j);

-    assert(i <= jj);

-    assert(i > ii);

-

-    pCP = *--i;

-    assert(pCP);

-    assert(pCP->GetTime(m_pSegment) <= time_ns);

-

-    //TODO: here and elsewhere, it's probably not correct to search

-    //for the cue point with this time, and then search for a matching

-    //track.  In principle, the matching track could be on some earlier

-    //cue point, and with our current algorithm, we'd miss it.  To make

-    //this bullet-proof, we'd need to create a secondary structure,

-    //with a list of cue points that apply to a track, and then search

-    //that track-based structure for a matching cue point.

-

-    pTP = pCP->Find(pTrack);

-    return (pTP != NULL);

-}

-

-

-#if 0

-bool Cues::FindNext(

-    long long time_ns,

-    const Track* pTrack,

-    const CuePoint*& pCP,

-    const CuePoint::TrackPosition*& pTP) const

-{

-    pCP = 0;

-    pTP = 0;

-

-    if (m_count == 0)

-        return false;

-

-    assert(m_cue_points);

-

-    const CuePoint* const* const ii = m_cue_points;

-    const CuePoint* const* i = ii;

-

-    const CuePoint* const* const jj = ii + m_count;

-    const CuePoint* const* j = jj;

-

-    while (i < j)

-    {

-        //INVARIANT:

-        //[ii, i) <= time_ns

-        //[i, j)  ?

-        //[j, jj) > time_ns

-

-        const CuePoint* const* const k = i + (j - i) / 2;

-        assert(k < jj);

-

-        pCP = *k;

-        assert(pCP);

-

-        const long long t = pCP->GetTime(m_pSegment);

-

-        if (t <= time_ns)

-            i = k + 1;

-        else

-            j = k;

-

-        assert(i <= j);

-    }

-

-    assert(i == j);

-    assert(i <= jj);

-

-    if (i >= jj)  //time_ns is greater than max cue point

-        return false;

-

-    pCP = *i;

-    assert(pCP);

-    assert(pCP->GetTime(m_pSegment) > time_ns);

-

-    pTP = pCP->Find(pTrack);

-    return (pTP != NULL);

-}

-#endif

-

-

-const CuePoint* Cues::GetFirst() const

-{

-    LoadCuePoint();  //init cues

-

-    const size_t count = m_count + m_preload_count;

-

-    if (count == 0)  //weird

-        return NULL;

-

-    CuePoint* const* const pp = m_cue_points;

-    assert(pp);

-

-    CuePoint* const pCP = pp[0];

-    assert(pCP);

-    assert(pCP->GetTimeCode() >= 0);

-

-    return pCP;

-}

-

-

-const CuePoint* Cues::GetLast() const

-{

-    LoadCuePoint();  //init cues

-

-    const size_t count = m_count + m_preload_count;

-

-    if (count == 0)  //weird

-        return NULL;

-

-    const size_t index = count - 1;

-

-    CuePoint* const* const pp = m_cue_points;

-    assert(pp);

-

-    CuePoint* const pCP = pp[index];

-    assert(pCP);

-

-    pCP->Load(m_pSegment->m_pReader);

-    assert(pCP->GetTimeCode() >= 0);

-

-    return pCP;

-}

-

-

-const CuePoint* Cues::GetNext(const CuePoint* pCurr) const

-{

-    if (pCurr == NULL)

-        return NULL;

-

-    assert(pCurr->GetTimeCode() >= 0);

-    assert(m_cue_points);

-    assert(m_count >= 1);

-

-    const size_t count = m_count + m_preload_count;

-

-    size_t index = pCurr->m_index;

-    assert(index < count);

-

-    CuePoint* const* const pp = m_cue_points;

-    assert(pp);

-    assert(pp[index] == pCurr);

-

-    ++index;

-

-    if (index >= count)

-        return NULL;

-

-    CuePoint* const pNext = pp[index];

-    assert(pNext);

-

-    pNext->Load(m_pSegment->m_pReader);

-

-    return pNext;

-}

-

-

-const BlockEntry* Cues::GetBlock(

-    const CuePoint* pCP,

-    const CuePoint::TrackPosition* pTP) const

-{

-    if (pCP == NULL)

-        return NULL;

-

-    if (pTP == NULL)

-        return NULL;

-

-    return m_pSegment->GetBlock(*pCP, *pTP);

-}

-

-

-const BlockEntry* Segment::GetBlock(

-    const CuePoint& cp,

-    const CuePoint::TrackPosition& tp)

-{

-    Cluster** const ii = m_clusters;

-    Cluster** i = ii;

-

-    const long count = m_clusterCount + m_clusterPreloadCount;

-

-    Cluster** const jj = ii + count;

-    Cluster** j = jj;

-

-    while (i < j)

-    {

-        //INVARIANT:

-        //[ii, i) < pTP->m_pos

-        //[i, j) ?

-        //[j, jj)  > pTP->m_pos

-

-        Cluster** const k = i + (j - i) / 2;

-        assert(k < jj);

-

-        Cluster* const pCluster = *k;

-        assert(pCluster);

-

-        const long long pos_ = pCluster->m_pos;

-        assert(pos_);

-

-        const long long pos = pos_ * ((pos_ < 0) ? -1 : 1);

-

-        if (pos < tp.m_pos)

-            i = k + 1;

-        else if (pos > tp.m_pos)

-            j = k;

-        else

-            return pCluster->GetEntry(cp, tp);

-    }

-

-    assert(i == j);

-

-    Cluster* const pCluster = Cluster::Parse(this, -1, tp.m_pos);

-    const ptrdiff_t idx = i - m_clusters;

-

-    PreloadCluster(pCluster, idx);

-    assert(m_clusters);

-    assert(m_clusterPreloadCount > 0);

-    assert(m_clusters[idx] == pCluster);

-

-    return pCluster->GetEntry(cp, tp);

-}

-

-

-

-CuePoint::CuePoint(size_t idx, long long pos) :

-    m_index(idx),

-    m_timecode(-1 * pos),

-    m_track_positions(NULL),

-    m_track_positions_count(0)

-{

-    assert(pos > 0);

-}

-

-

-CuePoint::~CuePoint()

-{

-    delete[] m_track_positions;

-}

-

-

-void CuePoint::Load(IMkvReader* pReader)

-{

-    //odbgstream os;

-    //os << "CuePoint::Load(begin): timecode=" << m_timecode << endl;

-

-    if (m_timecode >= 0)  //already loaded

-        return;

-

-    assert(m_track_positions == NULL);

-    assert(m_track_positions_count == 0);

-

-    long long pos_ = -m_timecode;

-

-    long long stop;

-

-    {

-        long len;

-

-        const long long id = ReadUInt(pReader, pos_, len);

-        assert(id == 0x3B);  //CuePoint ID

-        //assert((pos + len) <= stop);

-

-        pos_ += len;  //consume ID

-

-        const long long size = ReadUInt(pReader, pos_, len);

-        assert(size >= 0);

-        //assert((pos + len) <= stop);

-

-        pos_ += len;  //consume Size field

-        //assert((pos + size) <= stop);

-

-        //pos_ now points to start of payload

-

-        stop = pos_ + size;

-    }

-

-    long long pos = pos_;

-

-    //First count number of track positions

-

-    while (pos < stop)

-    {

-        long len;

-

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);  //TODO

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume ID

-

-        const long long size = ReadUInt(pReader, pos, len);

-        assert(size >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume Size field

-        assert((pos + size) <= stop);

-

-        if (id == 0x33)  //CueTime ID

-            m_timecode = UnserializeUInt(pReader, pos, size);

-

-        else if (id == 0x37) //CueTrackPosition(s) ID

-            ++m_track_positions_count;

-

-        pos += size;  //consume payload

-        assert(pos <= stop);

-    }

-

-    assert(m_timecode >= 0);

-    assert(m_track_positions_count > 0);

-

-    //os << "CuePoint::Load(cont'd): idpos=" << idpos

-    //   << " timecode=" << m_timecode

-    //   << endl;

-

-    m_track_positions = new TrackPosition[m_track_positions_count];

-

-    //Now parse track positions

-

-    TrackPosition* p = m_track_positions;

-    pos = pos_;

-

-    while (pos < stop)

-    {

-        long len;

-

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);  //TODO

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume ID

-

-        const long long size = ReadUInt(pReader, pos, len);

-        assert(size >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume Size field

-        assert((pos + size) <= stop);

-

-        if (id == 0x37) //CueTrackPosition(s) ID

-        {

-            TrackPosition& tp = *p++;

-            tp.Parse(pReader, pos, size);

-        }

-

-        pos += size;  //consume payload

-        assert(pos <= stop);

-    }

-

-    assert(size_t(p - m_track_positions) == m_track_positions_count);

-}

-

-

-

-void CuePoint::TrackPosition::Parse(

-    IMkvReader* pReader,

-    long long start_,

-    long long size_)

-{

-    const long long stop = start_ + size_;

-    long long pos = start_;

-

-    m_track = -1;

-    m_pos = -1;

-    m_block = 1;  //default

-

-    while (pos < stop)

-    {

-        long len;

-

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);  //TODO

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume ID

-

-        const long long size = ReadUInt(pReader, pos, len);

-        assert(size >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume Size field

-        assert((pos + size) <= stop);

-

-        if (id == 0x77)  //CueTrack ID

-            m_track = UnserializeUInt(pReader, pos, size);

-

-        else if (id == 0x71)  //CueClusterPos ID

-            m_pos = UnserializeUInt(pReader, pos, size);

-

-        else if (id == 0x1378)  //CueBlockNumber

-            m_block = UnserializeUInt(pReader, pos, size);

-

-        pos += size;  //consume payload

-        assert(pos <= stop);

-    }

-

-    assert(m_pos >= 0);

-    //assert(m_track > 0);

-    //assert(m_block > 0);

-}

-

-

-const CuePoint::TrackPosition* CuePoint::Find(const Track* pTrack) const

-{

-    assert(pTrack);

-

-    const long long n = pTrack->GetNumber();

-

-    const TrackPosition* i = m_track_positions;

-    const TrackPosition* const j = i + m_track_positions_count;

-

-    while (i != j)

-    {

-        const TrackPosition& p = *i++;

-

-        if (p.m_track == n)

-            return &p;

-    }

-

-    return NULL;  //no matching track number found

-}

-

-

-long long CuePoint::GetTimeCode() const

-{

-    return m_timecode;

-}

-

-long long CuePoint::GetTime(Segment* pSegment) const

-{

-    assert(pSegment);

-    assert(m_timecode >= 0);

-

-    const SegmentInfo* const pInfo = pSegment->GetInfo();

-    assert(pInfo);

-

-    const long long scale = pInfo->GetTimeCodeScale();

-    assert(scale >= 1);

-

-    const long long time = scale * m_timecode;

-

-    return time;

-}

-

-

-long long Segment::Unparsed() const

-{

-    const long long stop = m_start + m_size;

-

-    const long long result = stop - m_pos;

-    assert(result >= 0);

-

-    return result;

-}

-

-

-Cluster* Segment::GetFirst()

-{

-    if ((m_clusters == NULL) || (m_clusterCount <= 0))

-       return &m_eos;

-

-    Cluster* const pCluster = m_clusters[0];

-    assert(pCluster);

-

-    return pCluster;

-}

-

-

-Cluster* Segment::GetLast()

-{

-    if ((m_clusters == NULL) || (m_clusterCount <= 0))

-        return &m_eos;

-

-    const long idx = m_clusterCount - 1;

-

-    Cluster* const pCluster = m_clusters[idx];

-    assert(pCluster);

-

-    return pCluster;

-}

-

-

-unsigned long Segment::GetCount() const

-{

-    return m_clusterCount;

-}

-

-

-Cluster* Segment::GetNext(const Cluster* pCurr)

-{

-    assert(pCurr);

-    assert(pCurr != &m_eos);

-    assert(m_clusters);

-

-    long idx =  pCurr->m_index;

-

-    if (idx >= 0)

-    {

-        assert(m_clusterCount > 0);

-        assert(idx < m_clusterCount);

-        assert(pCurr == m_clusters[idx]);

-

-        ++idx;

-

-        if (idx >= m_clusterCount)

-            return &m_eos;  //caller will LoadCluster as desired

-

-        Cluster* const pNext = m_clusters[idx];

-        assert(pNext);

-        assert(pNext->m_index >= 0);

-        assert(pNext->m_index == idx);

-

-        return pNext;

-    }

-

-    assert(m_clusterPreloadCount > 0);

-

-    const long long off_ = pCurr->m_pos;

-    const long long off = off_ * ((off_ < 0) ? -1 : 1);

-

-    long long pos = m_start + off;

-    const long long stop = m_start + m_size;  //end of segment

-

-    {

-        long len;

-

-        long long result = GetUIntLength(m_pReader, pos, len);

-        assert(result == 0);  //TODO

-        assert((pos + len) <= stop);  //TODO

-

-        const long long id = ReadUInt(m_pReader, pos, len);

-        assert(id == 0x0F43B675);  //Cluster ID   //TODO

-

-        pos += len;  //consume ID

-

-        //Read Size

-        result = GetUIntLength(m_pReader, pos, len);

-        assert(result == 0);  //TODO

-        assert((pos + len) <= stop);  //TODO

-

-        const long long size = ReadUInt(m_pReader, pos, len);

-        assert(size > 0);  //TODO

-        assert((pCurr->m_size <= 0) || (pCurr->m_size == size));

-

-        pos += len;  //consume length of size of element

-        assert((pos + size) <= stop);  //TODO

-

-        //Pos now points to start of payload

-

-        pos += size;  //consume payload

-    }

-

-    long long off_next = 0;

-

-    while (pos < stop)

-    {

-        long len;

-

-        long long result = GetUIntLength(m_pReader, pos, len);

-        assert(result == 0);  //TODO

-        assert((pos + len) <= stop);  //TODO

-

-        const long long idpos = pos;  //pos of next (potential) cluster

-

-        const long long id = ReadUInt(m_pReader, idpos, len);

-        assert(id > 0);  //TODO

-

-        pos += len;  //consume ID

-

-        //Read Size

-        result = GetUIntLength(m_pReader, pos, len);

-        assert(result == 0);  //TODO

-        assert((pos + len) <= stop);  //TODO

-

-        const long long size = ReadUInt(m_pReader, pos, len);

-        assert(size >= 0);  //TODO

-

-        pos += len;  //consume length of size of element

-        assert((pos + size) <= stop);  //TODO

-

-        //Pos now points to start of payload

-

-        if (size == 0)  //weird

-            continue;

-

-        if (id == 0x0F43B675)  //Cluster ID

-        {

-            off_next = idpos - m_start;

-            break;

-        }

-

-        pos += size;  //consume payload

-    }

-

-    if (off_next <= 0)

-        return 0;

-

-    Cluster** const ii = m_clusters + m_clusterCount;

-    Cluster** i = ii;

-

-    Cluster** const jj = ii + m_clusterPreloadCount;

-    Cluster** j = jj;

-

-    while (i < j)

-    {

-        //INVARIANT:

-        //[0, i) < pos_next

-        //[i, j) ?

-        //[j, jj)  > pos_next

-

-        Cluster** const k = i + (j - i) / 2;

-        assert(k < jj);

-

-        Cluster* const pNext = *k;

-        assert(pNext);

-        assert(pNext->m_index < 0);

-

-        const long long pos_ = pNext->m_pos;

-        assert(pos_);

-

-        pos = pos_ * ((pos_ < 0) ? -1 : 1);

-

-        if (pos < off_next)

-            i = k + 1;

-        else if (pos > off_next)

-            j = k;

-        else

-            return pNext;

-    }

-

-    assert(i == j);

-

-    Cluster* const pNext = Cluster::Parse(this, -1, off_next);

-    const ptrdiff_t idx_next = i - m_clusters;  //insertion position

-

-    PreloadCluster(pNext, idx_next);

-    assert(m_clusters);

-    assert(idx_next < m_clusterSize);

-    assert(m_clusters[idx_next] == pNext);

-

-    return pNext;

-}

-

-

-Cluster* Segment::FindCluster(long long time_ns)

-{

-    if ((m_clusters == NULL) || (m_clusterCount <= 0))

-        return &m_eos;

-

-    {

-        Cluster* const pCluster = m_clusters[0];

-        assert(pCluster);

-        assert(pCluster->m_index == 0);

-

-        if (time_ns <= pCluster->GetTime())

-            return pCluster;

-    }

-

-    //Binary search of cluster array

-

-    long i = 0;

-    long j = m_clusterCount;

-

-    while (i < j)

-    {

-        //INVARIANT:

-        //[0, i) <= time_ns

-        //[i, j) ?

-        //[j, m_clusterCount)  > time_ns

-

-        const long k = i + (j - i) / 2;

-        assert(k < m_clusterCount);

-

-        Cluster* const pCluster = m_clusters[k];

-        assert(pCluster);

-        assert(pCluster->m_index == k);

-

-        const long long t = pCluster->GetTime();

-

-        if (t <= time_ns)

-            i = k + 1;

-        else

-            j = k;

-

-        assert(i <= j);

-    }

-

-    assert(i == j);

-    assert(i > 0);

-    assert(i <= m_clusterCount);

-

-    const long k = i - 1;

-

-    Cluster* const pCluster = m_clusters[k];

-    assert(pCluster);

-    assert(pCluster->m_index == k);

-    assert(pCluster->GetTime() <= time_ns);

-

-    return pCluster;

-}

-

-

-const BlockEntry* Segment::Seek(

-    long long time_ns,

-    const Track* pTrack)

-{

-    assert(pTrack);

-

-    if ((m_clusters == NULL) || (m_clusterCount <= 0))

-        return pTrack->GetEOS();

-

-    Cluster** const i = m_clusters;

-    assert(i);

-

-    {

-        Cluster* const pCluster = *i;

-        assert(pCluster);

-        assert(pCluster->m_index == 0);  //m_clusterCount > 0

-        assert(pCluster->m_pSegment == this);

-

-        if (time_ns <= pCluster->GetTime())

-            return pCluster->GetEntry(pTrack);

-    }

-

-    Cluster** const j = i + m_clusterCount;

-

-    if (pTrack->GetType() == 2)  //audio

-    {

-        //TODO: we could decide to use cues for this, as we do for video.

-        //But we only use it for video because looking around for a keyframe

-        //can get expensive.  Audio doesn't require anything special so a

-        //straight cluster search is good enough (we assume).

-

-        Cluster** lo = i;

-        Cluster** hi = j;

-

-        while (lo < hi)

-        {

-            //INVARIANT:

-            //[i, lo) <= time_ns

-            //[lo, hi) ?

-            //[hi, j)  > time_ns

-

-            Cluster** const mid = lo + (hi - lo) / 2;

-            assert(mid < hi);

-

-            Cluster* const pCluster = *mid;

-            assert(pCluster);

-            assert(pCluster->m_index == long(mid - m_clusters));

-            assert(pCluster->m_pSegment == this);

-

-            const long long t = pCluster->GetTime();

-

-            if (t <= time_ns)

-                lo = mid + 1;

-            else

-                hi = mid;

-

-            assert(lo <= hi);

-        }

-

-        assert(lo == hi);

-        assert(lo > i);

-        assert(lo <= j);

-

-        Cluster* const pCluster = *--lo;

-        assert(pCluster);

-        assert(pCluster->GetTime() <= time_ns);

-

-        return pCluster->GetEntry(pTrack);

-    }

-

-    assert(pTrack->GetType() == 1);  //video

-

-    Cluster** lo = i;

-    Cluster** hi = j;

-

-    while (lo < hi)

-    {

-        //INVARIANT:

-        //[i, lo) <= time_ns

-        //[lo, hi) ?

-        //[hi, j)  > time_ns

-

-        Cluster** const mid = lo + (hi - lo) / 2;

-        assert(mid < hi);

-

-        Cluster* const pCluster = *mid;

-        assert(pCluster);

-

-        const long long t = pCluster->GetTime();

-

-        if (t <= time_ns)

-            lo = mid + 1;

-        else

-            hi = mid;

-

-        assert(lo <= hi);

-    }

-

-    assert(lo == hi);

-    assert(lo > i);

-    assert(lo <= j);

-

-    Cluster* pCluster = *--lo;

-    assert(pCluster);

-    assert(pCluster->GetTime() <= time_ns);

-

-    {

-        const BlockEntry* const pBlockEntry = pCluster->GetEntry(pTrack);

-        assert(pBlockEntry);

-

-        if (!pBlockEntry->EOS())  //found a keyframe

-        {

-            const Block* const pBlock = pBlockEntry->GetBlock();

-            assert(pBlock);

-

-            //TODO: this isn't necessarily the keyframe we want,

-            //since there might another keyframe on this same

-            //cluster with a greater timecode that but that is

-            //still less than the requested time.  For now we

-            //simply return the first keyframe we find.

-

-            if (pBlock->GetTime(pCluster) <= time_ns)

-                return pBlockEntry;

-        }

-    }

-

-    const VideoTrack* const pVideo = static_cast<const VideoTrack*>(pTrack);

-

-    while (lo != i)

-    {

-        pCluster = *--lo;

-        assert(pCluster);

-        assert(pCluster->GetTime() <= time_ns);

-

-        const BlockEntry* const pBlockEntry = pCluster->GetMaxKey(pVideo);

-        assert(pBlockEntry);

-

-        if (!pBlockEntry->EOS())

-            return pBlockEntry;

-    }

-

-    //weird: we're on the first cluster, but no keyframe found

-    //should never happen but we must return something anyway

-

-    return pTrack->GetEOS();

-}

-

-

-#if 0

-bool Segment::SearchCues(

-    long long time_ns,

-    Track* pTrack,

-    Cluster*& pCluster,

-    const BlockEntry*& pBlockEntry,

-    const CuePoint*& pCP,

-    const CuePoint::TrackPosition*& pTP)

-{

-    if (pTrack->GetType() != 1)  //not video

-        return false;  //TODO: for now, just handle video stream

-

-    if (m_pCues == NULL)

-        return false;

-

-    if (!m_pCues->Find(time_ns, pTrack, pCP, pTP))

-        return false;  //weird

-

-    assert(pCP);

-    assert(pTP);

-    assert(pTP->m_track == pTrack->GetNumber());

-

-    //We have the cue point and track position we want,

-    //so we now need to search for the cluster having

-    //the indicated position.

-

-    return GetCluster(pCP, pTP, pCluster, pBlockEntry);

-}

-#endif

-

-

-Tracks* Segment::GetTracks() const

-{

-    return m_pTracks;

-}

-

-

-const SegmentInfo* Segment::GetInfo() const

-{

-    return m_pInfo;

-}

-

-

-const Cues* Segment::GetCues() const

-{

-    return m_pCues;

-}

-

-

-long long Segment::GetDuration() const

-{

-    assert(m_pInfo);

-    return m_pInfo->GetDuration();

-}

-

-

-SegmentInfo::SegmentInfo(Segment* pSegment, long long start, long long size_) :

-    m_pSegment(pSegment),

-    m_start(start),

-    m_size(size_),

-    m_pMuxingAppAsUTF8(NULL),

-    m_pWritingAppAsUTF8(NULL),

-    m_pTitleAsUTF8(NULL)

-{

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    long long pos = start;

-    const long long stop = start + size_;

-

-    m_timecodeScale = 1000000;

-    m_duration = -1;

-

-    while (pos < stop)

-    {

-        if (Match(pReader, pos, 0x0AD7B1, m_timecodeScale))

-            assert(m_timecodeScale > 0);

-

-        else if (Match(pReader, pos, 0x0489, m_duration))

-            assert(m_duration >= 0);

-

-        else if (Match(pReader, pos, 0x0D80, m_pMuxingAppAsUTF8))   //[4D][80]

-            assert(m_pMuxingAppAsUTF8);

-

-        else if (Match(pReader, pos, 0x1741, m_pWritingAppAsUTF8))  //[57][41]

-            assert(m_pWritingAppAsUTF8);

-

-        else if (Match(pReader, pos, 0x3BA9, m_pTitleAsUTF8))       //[7B][A9]

-            assert(m_pTitleAsUTF8);

-

-        else

-        {

-            long len;

-

-            const long long id = ReadUInt(pReader, pos, len);

-            //id;

-            assert(id >= 0);

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume id

-            assert((stop - pos) > 0);

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);

-            assert((pos + len) <= stop);

-

-            pos += len + size;  //consume size and payload

-            assert(pos <= stop);

-        }

-    }

-

-    assert(pos == stop);

-}

-

-SegmentInfo::~SegmentInfo()

-{

-    if (m_pMuxingAppAsUTF8)

-    {

-        delete[] m_pMuxingAppAsUTF8;

-        m_pMuxingAppAsUTF8 = NULL;

-    }

-

-    if (m_pWritingAppAsUTF8)

-    {

-        delete[] m_pWritingAppAsUTF8;

-        m_pWritingAppAsUTF8 = NULL;

-    }

-

-    if (m_pTitleAsUTF8)

-    {

-        delete[] m_pTitleAsUTF8;

-        m_pTitleAsUTF8 = NULL;

-    }

-}

-

-long long SegmentInfo::GetTimeCodeScale() const

-{

-    return m_timecodeScale;

-}

-

-

-long long SegmentInfo::GetDuration() const

-{

-    if (m_duration < 0)

-        return -1;

-

-    assert(m_timecodeScale >= 1);

-

-    const double dd = double(m_duration) * double(m_timecodeScale);

-    const long long d = static_cast<long long>(dd);

-

-    return d;

-}

-

-const char* SegmentInfo::GetMuxingAppAsUTF8() const

-{

-    return m_pMuxingAppAsUTF8;

-}

-

-

-const char* SegmentInfo::GetWritingAppAsUTF8() const

-{

-    return m_pWritingAppAsUTF8;

-}

-

-const char* SegmentInfo::GetTitleAsUTF8() const

-{

-    return m_pTitleAsUTF8;

-}

-

-Track::Track(Segment* pSegment, const Info& i) :

-    m_pSegment(pSegment),

-    m_info(i)

-{

-}

-

-Track::~Track()

-{

-    Info& info = const_cast<Info&>(m_info);

-    info.Clear();

-}

-

-Track::Info::Info():

-    type(-1),

-    number(-1),

-    uid(-1),

-    nameAsUTF8(NULL),

-    codecId(NULL),

-    codecPrivate(NULL),

-    codecPrivateSize(0),

-    codecNameAsUTF8(NULL)

-{

-}

-

-

-void Track::Info::Clear()

-{

-    delete[] nameAsUTF8;

-    nameAsUTF8 = NULL;

-

-    delete[] codecId;

-    codecId = NULL;

-

-    delete[] codecPrivate;

-    codecPrivate = NULL;

-

-    codecPrivateSize = 0;

-

-    delete[] codecNameAsUTF8;

-    codecNameAsUTF8 = NULL;

-}

-

-const BlockEntry* Track::GetEOS() const

-{

-    return &m_eos;

-}

-

-long long Track::GetType() const

-{

-    return m_info.type;

-}

-

-long long Track::GetNumber() const

-{

-    return m_info.number;

-}

-

-const char* Track::GetNameAsUTF8() const

-{

-    return m_info.nameAsUTF8;

-}

-

-const char* Track::GetCodecNameAsUTF8() const

-{

-    return m_info.codecNameAsUTF8;

-}

-

-

-const char* Track::GetCodecId() const

-{

-    return m_info.codecId;

-}

-

-const unsigned char* Track::GetCodecPrivate(size_t& size) const

-{

-    size = m_info.codecPrivateSize;

-    return m_info.codecPrivate;

-}

-

-

-long Track::GetFirst(const BlockEntry*& pBlockEntry) const

-{

-    Cluster* pCluster = m_pSegment->GetFirst();

-

-    //If Segment::GetFirst returns NULL, then this must be a network

-    //download, and we haven't loaded any clusters yet.  In this case,

-    //returning NULL from Track::GetFirst means the same thing.

-

-    for (int i = 0; i < 100; ++i)  //arbitrary upper bound

-    {

-        if (pCluster == NULL)

-        {

-            pBlockEntry = GetEOS();

-            return 1;

-        }

-

-        if (pCluster->EOS())

-        {

-            if (m_pSegment->Unparsed() <= 0)  //all clusters have been loaded

-            {

-                pBlockEntry = GetEOS();

-                return 1;

-            }

-

-            pBlockEntry = 0;

-            return E_BUFFER_NOT_FULL;

-        }

-

-        pBlockEntry = pCluster->GetFirst();

-

-        while (pBlockEntry)

-        {

-            const Block* const pBlock = pBlockEntry->GetBlock();

-            assert(pBlock);

-

-            if (pBlock->GetTrackNumber() == m_info.number)

-                return 0;

-

-            pBlockEntry = pCluster->GetNext(pBlockEntry);

-        }

-

-        pCluster = m_pSegment->GetNext(pCluster);

-    }

-

-    //NOTE: if we get here, it means that we didn't find a block with

-    //a matching track number.  We interpret that as an error (which

-    //might be too conservative).

-

-    pBlockEntry = GetEOS();  //so we can return a non-NULL value

-    return 1;

-}

-

-

-long Track::GetNext(

-    const BlockEntry* pCurrEntry,

-    const BlockEntry*& pNextEntry) const

-{

-    assert(pCurrEntry);

-    assert(!pCurrEntry->EOS());  //?

-

-    const Block* const pCurrBlock = pCurrEntry->GetBlock();

-    assert(pCurrBlock->GetTrackNumber() == m_info.number);

-

-    Cluster* pCluster = pCurrEntry->GetCluster();

-    assert(pCluster);

-    assert(!pCluster->EOS());

-

-    pNextEntry = pCluster->GetNext(pCurrEntry);

-

-    for (int i = 0; i < 100; ++i)  //arbitrary upper bound to search

-    {

-        while (pNextEntry)

-        {

-            const Block* const pNextBlock = pNextEntry->GetBlock();

-            assert(pNextBlock);

-

-            if (pNextBlock->GetTrackNumber() == m_info.number)

-                return 0;

-

-            pNextEntry = pCluster->GetNext(pNextEntry);

-        }

-

-        pCluster = m_pSegment->GetNext(pCluster);

-

-        if (pCluster == NULL)

-        {

-            pNextEntry = GetEOS();

-            return 1;

-        }

-

-        if (pCluster->EOS())

-        {

-            if (m_pSegment->Unparsed() <= 0)   //all clusters have been loaded

-            {

-                pNextEntry = GetEOS();

-                return 1;

-            }

-

-            //TODO: there is a potential O(n^2) problem here: we tell the

-            //caller to (pre)load another cluster, which he does, but then he

-            //calls GetNext again, which repeats the same search.  This is

-            //a pathological case, since the only way it can happen is if

-            //there exists a long sequence of clusters none of which contain a

-            // block from this track.  One way around this problem is for the

-            //caller to be smarter when he loads another cluster: don't call

-            //us back until you have a cluster that contains a block from this

-            //track. (Of course, that's not cheap either, since our caller

-            //would have to scan the each cluster as it's loaded, so that

-            //would just push back the problem.)

-

-            pNextEntry = NULL;

-            return E_BUFFER_NOT_FULL;

-        }

-

-        pNextEntry = pCluster->GetFirst();

-    }

-

-    //NOTE: if we get here, it means that we didn't find a block with

-    //a matching track number after lots of searching, so we give

-    //up trying.

-

-    pNextEntry = GetEOS();  //so we can return a non-NULL value

-    return 1;

-}

-

-

-Track::EOSBlock::EOSBlock()

-{

-}

-

-

-bool Track::EOSBlock::EOS() const

-{

-    return true;

-}

-

-

-Cluster* Track::EOSBlock::GetCluster() const

-{

-    return NULL;

-}

-

-

-size_t Track::EOSBlock::GetIndex() const

-{

-    return 0;

-}

-

-

-const Block* Track::EOSBlock::GetBlock() const

-{

-    return NULL;

-}

-

-

-bool Track::EOSBlock::IsBFrame() const

-{

-    return false;

-}

-

-

-VideoTrack::VideoTrack(Segment* pSegment, const Info& i) :

-    Track(pSegment, i),

-    m_width(-1),

-    m_height(-1),

-    m_rate(-1)

-{

-    assert(i.type == 1);

-    assert(i.number > 0);

-

-    IMkvReader* const pReader = pSegment->m_pReader;

-

-    const Settings& s = i.settings;

-    assert(s.start >= 0);

-    assert(s.size >= 0);

-

-    long long pos = s.start;

-    assert(pos >= 0);

-

-    const long long stop = pos + s.size;

-

-    while (pos < stop)

-    {

-#ifdef _DEBUG

-        long len;

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);  //TODO: handle error case

-        assert((pos + len) <= stop);

-#endif

-        if (Match(pReader, pos, 0x30, m_width))

-            ;

-        else if (Match(pReader, pos, 0x3A, m_height))

-            ;

-        else if (Match(pReader, pos, 0x0383E3, m_rate))

-            ;

-        else

-        {

-            long len;

-            const long long id = ReadUInt(pReader, pos, len);

-            assert(id >= 0);  //TODO: handle error case

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume id

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);  //TODO: handle error case

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume length of size

-            assert((pos + size) <= stop);

-

-            //pos now designates start of payload

-

-            pos += size;  //consume payload

-            assert(pos <= stop);

-        }

-    }

-

-    return;

-}

-

-

-bool VideoTrack::VetEntry(const BlockEntry* pBlockEntry) const

-{

-    assert(pBlockEntry);

-

-    const Block* const pBlock = pBlockEntry->GetBlock();

-    assert(pBlock);

-    assert(pBlock->GetTrackNumber() == m_info.number);

-

-    return pBlock->IsKey();

-}

-

-

-long long VideoTrack::GetWidth() const

-{

-    return m_width;

-}

-

-

-long long VideoTrack::GetHeight() const

-{

-    return m_height;

-}

-

-

-double VideoTrack::GetFrameRate() const

-{

-    return m_rate;

-}

-

-

-AudioTrack::AudioTrack(Segment* pSegment, const Info& i) :

-    Track(pSegment, i),

-    m_rate(0.0),

-    m_channels(0),

-    m_bitDepth(-1)

-{

-    assert(i.type == 2);

-    assert(i.number > 0);

-

-    IMkvReader* const pReader = pSegment->m_pReader;

-

-    const Settings& s = i.settings;

-    assert(s.start >= 0);

-    assert(s.size >= 0);

-

-    long long pos = s.start;

-    assert(pos >= 0);

-

-    const long long stop = pos + s.size;

-

-    while (pos < stop)

-    {

-#ifdef _DEBUG

-        long len;

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);  //TODO: handle error case

-        assert((pos + len) <= stop);

-#endif

-        if (Match(pReader, pos, 0x35, m_rate))

-            ;

-        else if (Match(pReader, pos, 0x1F, m_channels))

-            ;

-        else if (Match(pReader, pos, 0x2264, m_bitDepth))

-            ;

-        else

-        {

-            long len;

-            const long long id = ReadUInt(pReader, pos, len);

-            assert(id >= 0);  //TODO: handle error case

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume id

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);  //TODO: handle error case

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume length of size

-            assert((pos + size) <= stop);

-

-            //pos now designates start of payload

-

-            pos += size;  //consume payload

-            assert(pos <= stop);

-        }

-    }

-

-    return;

-}

-

-

-bool AudioTrack::VetEntry(const BlockEntry* pBlockEntry) const

-{

-    assert(pBlockEntry);

-

-    const Block* const pBlock = pBlockEntry->GetBlock();

-    assert(pBlock);

-    assert(pBlock->GetTrackNumber() == m_info.number);

-

-    return true;

-}

-

-

-double AudioTrack::GetSamplingRate() const

-{

-    return m_rate;

-}

-

-

-long long AudioTrack::GetChannels() const

-{

-    return m_channels;

-}

-

-long long AudioTrack::GetBitDepth() const

-{

-    return m_bitDepth;

-}

-

-Tracks::Tracks(Segment* pSegment, long long start, long long size_) :

-    m_pSegment(pSegment),

-    m_start(start),

-    m_size(size_),

-    m_trackEntries(NULL),

-    m_trackEntriesEnd(NULL)

-{

-    long long stop = m_start + m_size;

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    long long pos1 = m_start;

-    int count = 0;

-

-    while (pos1 < stop)

-    {

-        long len;

-        const long long id = ReadUInt(pReader, pos1, len);

-        assert(id >= 0);

-        assert((pos1 + len) <= stop);

-

-        pos1 += len;  //consume id

-

-        const long long size = ReadUInt(pReader, pos1, len);

-        assert(size >= 0);

-        assert((pos1 + len) <= stop);

-

-        pos1 += len;  //consume length of size

-

-        //pos now desinates start of element

-        if (id == 0x2E)  //TrackEntry ID

-            ++count;

-

-        pos1 += size;  //consume payload

-        assert(pos1 <= stop);

-    }

-

-    if (count <= 0)

-        return;

-

-    m_trackEntries = new Track*[count];

-    m_trackEntriesEnd = m_trackEntries;

-

-    long long pos = m_start;

-

-    while (pos < stop)

-    {

-        long len;

-        const long long id = ReadUInt(pReader, pos, len);

-        assert(id >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume id

-

-        const long long size1 = ReadUInt(pReader, pos, len);

-        assert(size1 >= 0);

-        assert((pos + len) <= stop);

-

-        pos += len;  //consume length of size

-

-        //pos now desinates start of element

-

-        if (id == 0x2E)  //TrackEntry ID

-            ParseTrackEntry(pos, size1, *m_trackEntriesEnd++);

-

-        pos += size1;  //consume payload

-        assert(pos <= stop);

-    }

-}

-

-

-unsigned long Tracks::GetTracksCount() const

-{

-    const ptrdiff_t result = m_trackEntriesEnd - m_trackEntries;

-    assert(result >= 0);

-

-    return static_cast<unsigned long>(result);

-}

-

-

-void Tracks::ParseTrackEntry(

-    long long start,

-    long long size,

-    Track*& pTrack)

-{

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    long long pos = start;

-    const long long stop = start + size;

-

-    Track::Info i;

-

-    Track::Settings videoSettings;

-    videoSettings.start = -1;

-

-    Track::Settings audioSettings;

-    audioSettings.start = -1;

-

-    while (pos < stop)

-    {

-#ifdef _DEBUG

-        long len;

-        const long long id = ReadUInt(pReader, pos, len);

-        len;

-        id;

-#endif

-        if (Match(pReader, pos, 0x57, i.number))

-            assert(i.number > 0);

-        else if (Match(pReader, pos, 0x33C5, i.uid))

-            ;

-        else if (Match(pReader, pos, 0x03, i.type))

-            ;

-        else if (Match(pReader, pos, 0x136E, i.nameAsUTF8))

-            assert(i.nameAsUTF8);

-        else if (Match(pReader, pos, 0x06, i.codecId))

-            ;

-        else if (Match(pReader,

-                       pos,

-                       0x23A2,

-                       i.codecPrivate,

-                       i.codecPrivateSize))

-            ;

-        else if (Match(pReader, pos, 0x058688, i.codecNameAsUTF8))

-            assert(i.codecNameAsUTF8);

-        else

-        {

-            long len;

-

-            const long long id = ReadUInt(pReader, pos, len);

-            assert(id >= 0);  //TODO: handle error case

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume id

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);  //TODO: handle error case

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume length of size

-            const long long start = pos;

-

-            pos += size;  //consume payload

-            assert(pos <= stop);

-

-            if (id == 0x60)

-            {

-                videoSettings.start = start;

-                videoSettings.size = size;

-            }

-            else if (id == 0x61)

-            {

-                audioSettings.start = start;

-                audioSettings.size = size;

-            }

-        }

-    }

-

-    assert(pos == stop);

-    //TODO: propertly vet info.number, to ensure both its existence,

-    //and that it is unique among all tracks.

-    assert(i.number > 0);

-

-    //TODO: vet settings, to ensure that video settings (0x60)

-    //were specified when type = 1, and that audio settings (0x61)

-    //were specified when type = 2.

-    if (i.type == 1)  //video

-    {

-        assert(audioSettings.start < 0);

-        assert(videoSettings.start >= 0);

-

-        i.settings = videoSettings;

-

-        VideoTrack* const t = new VideoTrack(m_pSegment, i);

-        assert(t);  //TODO

-        pTrack = t;

-    }

-    else if (i.type == 2)  //audio

-    {

-        assert(videoSettings.start < 0);

-        assert(audioSettings.start >= 0);

-

-        i.settings = audioSettings;

-

-        AudioTrack* const t = new  AudioTrack(m_pSegment, i);

-        assert(t);  //TODO

-        pTrack = t;

-    }

-    else

-    {

-        // for now we do not support other track types yet.

-        // TODO: support other track types

-        i.Clear();

-

-        pTrack = NULL;

-    }

-

-    return;

-}

-

-

-Tracks::~Tracks()

-{

-    Track** i = m_trackEntries;

-    Track** const j = m_trackEntriesEnd;

-

-    while (i != j)

-    {

-        Track* const pTrack = *i++;

-        delete pTrack;

-    }

-

-    delete[] m_trackEntries;

-}

-

-

-Track* Tracks::GetTrackByNumber(unsigned long tn_) const

-{

-    const long long tn = tn_;

-

-    Track** i = m_trackEntries;

-    Track** const j = m_trackEntriesEnd;

-

-    while (i != j)

-    {

-        Track* const pTrack = *i++;

-

-        if (pTrack == NULL)

-            continue;

-

-        if (tn == pTrack->GetNumber())

-            return pTrack;

-    }

-

-    return NULL;  //not found

-}

-

-

-Track* Tracks::GetTrackByIndex(unsigned long idx) const

-{

-    const ptrdiff_t count = m_trackEntriesEnd - m_trackEntries;

-

-    if (idx >= static_cast<unsigned long>(count))

-         return NULL;

-

-    return m_trackEntries[idx];

-}

-

-

-void Cluster::Load()

-{

-    assert(m_pSegment);

-    assert(m_pos);

-    assert(m_size);

-

-    if (m_pos > 0)  //loaded

-    {

-        assert(m_size > 0);

-        assert(m_timecode >= 0);

-        return;

-    }

-

-    assert(m_pos < 0);  //not loaded yet

-    assert(m_size < 0);

-    assert(m_timecode < 0);

-

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    m_pos *= -1;                                  //relative to segment

-    long long pos = m_pSegment->m_start + m_pos;  //absolute

-

-    long len;

-

-    const long long id_ = ReadUInt(pReader, pos, len);

-    assert(id_ >= 0);

-    assert(id_ == 0x0F43B675);  //Cluster ID

-

-    pos += len;  //consume id

-

-    const long long size_ = ReadUInt(pReader, pos, len);

-    assert(size_ >= 0);

-

-    pos += len;  //consume size

-

-    m_size = size_;

-    const long long stop = pos + size_;

-

-    long long timecode = -1;

-

-    while (pos < stop)

-    {

-        if (Match(pReader, pos, 0x67, timecode))

-            break;

-        else

-        {

-            const long long id = ReadUInt(pReader, pos, len);

-            assert(id >= 0);  //TODO

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume id

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);  //TODO

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume size

-

-            if (id == 0x20)  //BlockGroup ID

-                break;

-

-            if (id == 0x23)  //SimpleBlock ID

-                break;

-

-            pos += size;  //consume payload

-            assert(pos <= stop);

-        }

-    }

-

-    assert(pos <= stop);

-    assert(timecode >= 0);

-

-    m_timecode = timecode;

-}

-

-

-Cluster* Cluster::Parse(

-    Segment* pSegment,

-    long idx,

-    long long off)

-{

-    assert(pSegment);

-    assert(off >= 0);

-    assert(off < pSegment->m_size);

-

-    Cluster* const pCluster = new Cluster(pSegment, idx, -off);

-    assert(pCluster);

-

-    return pCluster;

-}

-

-

-Cluster::Cluster() :

-    m_pSegment(NULL),

-    m_index(0),

-    m_pos(0),

-    m_size(0),

-    m_timecode(0),

-    m_entries(NULL),

-    m_entriesCount(0)

-{

-}

-

-

-Cluster::Cluster(

-    Segment* pSegment,

-    long idx,

-    long long off) :

-    m_pSegment(pSegment),

-    m_index(idx),

-    m_pos(off),

-    m_size(-1),

-    m_timecode(-1),

-    m_entries(NULL),

-    m_entriesCount(0)

-{

-}

-

-

-Cluster::~Cluster()

-{

-    BlockEntry** i = m_entries;

-    BlockEntry** const j = m_entries + m_entriesCount;

-

-    while (i != j)

-    {

-         BlockEntry* p = *i++;

-         assert(p);

-

-         delete p;

-    }

-

-    delete[] m_entries;

-}

-

-

-bool Cluster::EOS() const

-{

-    return (m_pSegment == NULL);

-}

-

-

-void Cluster::LoadBlockEntries()

-{

-    if (m_entries)

-        return;

-

-    assert(m_pSegment);

-    assert(m_pos);

-    assert(m_size);

-    assert(m_entriesCount == 0);

-

-    IMkvReader* const pReader = m_pSegment->m_pReader;

-

-    if (m_pos < 0)

-        m_pos *= -1;  //relative to segment

-

-    long long pos = m_pSegment->m_start + m_pos;  //absolute

-

-    {

-        long len;

-

-        const long long id = ReadUInt(pReader, pos, len);

-        id;

-        assert(id >= 0);

-        assert(id == 0x0F43B675);  //Cluster ID

-

-        pos += len;  //consume id

-

-        const long long size = ReadUInt(pReader, pos, len);

-        assert(size > 0);

-

-        pos += len;  //consume size

-

-        //pos now points to start of payload

-

-        if (m_size >= 0)

-            assert(size == m_size);

-        else

-            m_size = size;

-    }

-

-    const long long stop = pos + m_size;

-    long long timecode = -1;  //of cluster itself

-

-    //First count the number of entries

-

-    long long idx = pos;  //points to start of payload

-    m_entriesCount = 0;

-

-    while (idx < stop)

-    {

-        if (Match(pReader, idx, 0x67, timecode))

-        {

-            if (m_timecode >= 0)

-                assert(timecode == m_timecode);

-            else

-                m_timecode = timecode;

-        }

-        else

-        {

-            long len;

-

-            const long long id = ReadUInt(pReader, idx, len);

-            assert(id >= 0);  //TODO

-            assert((idx + len) <= stop);

-

-            idx += len;  //consume id

-

-            const long long size = ReadUInt(pReader, idx, len);

-            assert(size >= 0);  //TODO

-            assert((idx + len) <= stop);

-

-            idx += len;  //consume size

-

-            if (id == 0x20)  //BlockGroup ID

-                ++m_entriesCount;

-            else if (id == 0x23)  //SimpleBlock ID

-                ++m_entriesCount;

-

-            idx += size;  //consume payload

-            assert(idx <= stop);

-        }

-    }

-

-    assert(idx == stop);

-    assert(m_timecode >= 0);

-

-    if (m_entriesCount == 0)  //TODO: handle empty clusters

-        return;

-

-    m_entries = new BlockEntry*[m_entriesCount];

-    size_t index = 0;

-

-    while (pos < stop)

-    {

-        if (Match(pReader, pos, 0x67, timecode))

-            assert(timecode == m_timecode);

-        else

-        {

-            long len;

-            const long long id = ReadUInt(pReader, pos, len);

-            assert(id >= 0);  //TODO

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume id

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);  //TODO

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume size

-

-            if (id == 0x20)  //BlockGroup ID

-                ParseBlockGroup(pos, size, index++);

-            else if (id == 0x23)  //SimpleBlock ID

-                ParseSimpleBlock(pos, size, index++);

-

-            pos += size;  //consume payload

-            assert(pos <= stop);

-        }

-    }

-

-    assert(pos == stop);

-    assert(timecode >= 0);

-    assert(index == m_entriesCount);

-}

-

-

-

-long long Cluster::GetTimeCode()

-{

-    Load();

-    return m_timecode;

-}

-

-

-long long Cluster::GetTime()

-{

-    const long long tc = GetTimeCode();

-    assert(tc >= 0);

-

-    const SegmentInfo* const pInfo = m_pSegment->GetInfo();

-    assert(pInfo);

-

-    const long long scale = pInfo->GetTimeCodeScale();

-    assert(scale >= 1);

-

-    const long long t = m_timecode * scale;

-

-    return t;

-}

-

-

-long long Cluster::GetFirstTime()

-{

-    const BlockEntry* const pEntry = GetFirst();

-

-    if (pEntry == NULL)  //empty cluster

-        return GetTime();

-

-    const Block* const pBlock = pEntry->GetBlock();

-    assert(pBlock);

-

-    return pBlock->GetTime(this);

-}

-

-

-long long Cluster::GetLastTime()

-{

-    const BlockEntry* const pEntry = GetLast();

-

-    if (pEntry == NULL)  //empty cluster

-        return GetTime();

-

-    const Block* const pBlock = pEntry->GetBlock();

-    assert(pBlock);

-

-    return pBlock->GetTime(this);

-}

-

-

-void Cluster::ParseBlockGroup(long long start, long long size, size_t index)

-{

-    assert(m_entries);

-    assert(m_entriesCount);

-    assert(index < m_entriesCount);

-

-    BlockGroup* const pGroup =

-        new (std::nothrow) BlockGroup(this, index, start, size);

-    assert(pGroup);  //TODO

-

-    m_entries[index] = pGroup;

-}

-

-

-

-void Cluster::ParseSimpleBlock(long long start, long long size, size_t index)

-{

-    assert(m_entries);

-    assert(m_entriesCount);

-    assert(index < m_entriesCount);

-

-    SimpleBlock* const pSimpleBlock =

-        new (std::nothrow) SimpleBlock(this, index, start, size);

-    assert(pSimpleBlock);  //TODO

-

-    m_entries[index] = pSimpleBlock;

-}

-

-

-const BlockEntry* Cluster::GetFirst()

-{

-    LoadBlockEntries();

-    //assert(m_entries);

-    //assert(m_entriesCount >= 1);

-

-    if ((m_entries == NULL) || (m_entriesCount == 0))

-        return NULL;

-

-    const BlockEntry* const pFirst = m_entries[0];

-    assert(pFirst);

-

-    return pFirst;

-}

-

-

-const BlockEntry* Cluster::GetLast()

-{

-    LoadBlockEntries();

-    //assert(m_entries);

-    //assert(m_entriesCount >= 1);

-

-    if ((m_entries == NULL) || (m_entriesCount == 0))

-        return NULL;

-

-    const size_t idx = m_entriesCount - 1;

-

-    const BlockEntry* const pLast = m_entries[idx];

-    assert(pLast);

-

-    return pLast;

-}

-

-

-const BlockEntry* Cluster::GetNext(const BlockEntry* pEntry) const

-{

-    assert(pEntry);

-    assert(m_entries);

-    assert(m_entriesCount);

-

-    size_t idx = pEntry->GetIndex();

-    assert(idx < m_entriesCount);

-    assert(m_entries[idx] == pEntry);

-

-    ++idx;

-

-    if (idx >= m_entriesCount)

-      return NULL;

-

-    return m_entries[idx];

-}

-

-

-const BlockEntry* Cluster::GetEntry(const Track* pTrack)

-{

-    assert(pTrack);

-

-    if (m_pSegment == NULL)  //EOS

-        return pTrack->GetEOS();

-

-    LoadBlockEntries();

-

-    if ((m_entries == NULL) || (m_entriesCount == 0))

-        return NULL;

-

-    BlockEntry** i = m_entries;

-    assert(i);

-

-    BlockEntry** const j = i + m_entriesCount;

-

-    while (i != j)

-    {

-        const BlockEntry* const pEntry = *i++;

-        assert(pEntry);

-        assert(!pEntry->EOS());

-

-        const Block* const pBlock = pEntry->GetBlock();

-        assert(pBlock);

-

-        if (pBlock->GetTrackNumber() != pTrack->GetNumber())

-            continue;

-

-        if (pTrack->VetEntry(pEntry))

-            return pEntry;

-    }

-

-    return pTrack->GetEOS();  //no satisfactory block found

-}

-

-

-const BlockEntry*

-Cluster::GetEntry(

-    const CuePoint& cp,

-    const CuePoint::TrackPosition& tp)

-{

-    assert(m_pSegment);

-

-    LoadBlockEntries();

-

-    if (m_entries == NULL)

-        return NULL;

-

-    const long long count = m_entriesCount;

-

-    if (count <= 0)

-        return NULL;

-

-    const long long tc = cp.GetTimeCode();

-

-    if ((tp.m_block > 0) && (tp.m_block <= count))

-    {

-        const size_t block = static_cast<size_t>(tp.m_block);

-        const size_t index = block - 1;

-

-        const BlockEntry* const pEntry = m_entries[index];

-        assert(pEntry);

-        assert(!pEntry->EOS());

-

-        const Block* const pBlock = pEntry->GetBlock();

-        assert(pBlock);

-

-        if ((pBlock->GetTrackNumber() == tp.m_track) &&

-            (pBlock->GetTimeCode(this) == tc))

-        {

-            return pEntry;

-        }

-    }

-

-    const BlockEntry* const* i = m_entries;

-    const BlockEntry* const* const j = i + count;

-

-    while (i != j)

-    {

-        const BlockEntry* const pEntry = *i++;

-        assert(pEntry);

-        assert(!pEntry->EOS());

-

-        const Block* const pBlock = pEntry->GetBlock();

-        assert(pBlock);

-

-        if (pBlock->GetTrackNumber() != tp.m_track)

-            continue;

-

-        const long long tc_ = pBlock->GetTimeCode(this);

-

-        if (tc_ < tc)

-            continue;

-

-        if (tc_ > tc)

-            return NULL;

-

-        const Tracks* const pTracks = m_pSegment->GetTracks();

-        assert(pTracks);

-

-        const long tn = static_cast<long>(tp.m_track);

-        const Track* const pTrack = pTracks->GetTrackByNumber(tn);

-

-        if (pTrack == NULL)

-            return NULL;

-

-        const long long type = pTrack->GetType();

-

-        if (type == 2)  //audio

-            return pEntry;

-

-        if (type != 1)  //not video

-            return NULL;

-

-        if (!pBlock->IsKey())

-            return NULL;

-

-        return pEntry;

-    }

-

-    return NULL;

-}

-

-

-const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack)

-{

-    assert(pTrack);

-

-    if (m_pSegment == NULL)  //EOS

-        return pTrack->GetEOS();

-

-    LoadBlockEntries();

-    //assert(m_entries);

-

-    BlockEntry** i = m_entries + m_entriesCount;

-    BlockEntry** const j = m_entries;

-

-    while (i != j)

-    {

-        const BlockEntry* const pEntry = *--i;

-        assert(pEntry);

-        assert(!pEntry->EOS());

-

-        const Block* const pBlock = pEntry->GetBlock();

-        assert(pBlock);

-

-        if (pBlock->GetTrackNumber() != pTrack->GetNumber())

-            continue;

-

-        if (pBlock->IsKey())

-            return pEntry;

-    }

-

-    return pTrack->GetEOS();  //no satisfactory block found

-}

-

-

-

-BlockEntry::BlockEntry()

-{

-}

-

-

-BlockEntry::~BlockEntry()

-{

-}

-

-

-SimpleBlock::SimpleBlock(

-    Cluster* pCluster,

-    size_t idx,

-    long long start,

-    long long size) :

-    m_pCluster(pCluster),

-    m_index(idx),

-    m_block(start, size, pCluster->m_pSegment->m_pReader)

-{

-}

-

-

-bool SimpleBlock::EOS() const

-{

-    return false;

-}

-

-

-Cluster* SimpleBlock::GetCluster() const

-{

-    return m_pCluster;

-}

-

-

-size_t SimpleBlock::GetIndex() const

-{

-    return m_index;

-}

-

-

-const Block* SimpleBlock::GetBlock() const

-{

-    return &m_block;

-}

-

-

-bool SimpleBlock::IsBFrame() const

-{

-    return false;

-}

-

-

-BlockGroup::BlockGroup(

-    Cluster* pCluster,

-    size_t idx,

-    long long start,

-    long long size_) :

-    m_pCluster(pCluster),

-    m_index(idx),

-    m_prevTimeCode(0),

-    m_nextTimeCode(0),

-    m_pBlock(NULL)  //TODO: accept multiple blocks within a block group

-{

-    IMkvReader* const pReader = m_pCluster->m_pSegment->m_pReader;

-

-    long long pos = start;

-    const long long stop = start + size_;

-

-    bool bSimpleBlock = false;

-    bool bReferenceBlock = false;

-

-    while (pos < stop)

-    {

-        short t;

-

-        if (Match(pReader, pos, 0x7B, t))

-        {

-            if (t < 0)

-                m_prevTimeCode = t;

-            else if (t > 0)

-                m_nextTimeCode = t;

-            else

-                assert(false);

-

-            bReferenceBlock = true;

-        }

-        else

-        {

-            long len;

-            const long long id = ReadUInt(pReader, pos, len);

-            assert(id >= 0);  //TODO

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume ID

-

-            const long long size = ReadUInt(pReader, pos, len);

-            assert(size >= 0);  //TODO

-            assert((pos + len) <= stop);

-

-            pos += len;  //consume size

-

-            switch (id)

-            {

-                case 0x23:  //SimpleBlock ID

-                    bSimpleBlock = true;

-                    //YES, FALL THROUGH TO NEXT CASE

-

-                case 0x21:  //Block ID

-                    ParseBlock(pos, size);

-                    break;

-

-                default:

-                    break;

-            }

-

-            pos += size;  //consume payload

-            assert(pos <= stop);

-        }

-    }

-

-    assert(pos == stop);

-    assert(m_pBlock);

-

-    if (!bSimpleBlock)

-        m_pBlock->SetKey(!bReferenceBlock);

-}

-

-

-BlockGroup::~BlockGroup()

-{

-    delete m_pBlock;

-}

-

-

-void BlockGroup::ParseBlock(long long start, long long size)

-{

-    IMkvReader* const pReader = m_pCluster->m_pSegment->m_pReader;

-

-    Block* const pBlock = new Block(start, size, pReader);

-    assert(pBlock);  //TODO

-

-    //TODO: the Matroska spec says you have multiple blocks within the

-    //same block group, with blocks ranked by priority (the flag bits).

-

-    assert(m_pBlock == NULL);

-    m_pBlock = pBlock;

-}

-

-

-bool BlockGroup::EOS() const

-{

-    return false;

-}

-

-

-Cluster* BlockGroup::GetCluster() const

-{

-    return m_pCluster;

-}

-

-

-size_t BlockGroup::GetIndex() const

-{

-    return m_index;

-}

-

-

-const Block* BlockGroup::GetBlock() const

-{

-    return m_pBlock;

-}

-

-

-short BlockGroup::GetPrevTimeCode() const

-{

-    return m_prevTimeCode;

-}

-

-

-short BlockGroup::GetNextTimeCode() const

-{

-    return m_nextTimeCode;

-}

-

-

-bool BlockGroup::IsBFrame() const

-{

-    return (m_nextTimeCode > 0);

-}

-

-

-

-Block::Block(long long start, long long size_, IMkvReader* pReader) :

-    m_start(start),

-    m_size(size_)

-{

-    long long pos = start;

-    const long long stop = start + size_;

-

-    long len;

-

-    m_track = ReadUInt(pReader, pos, len);

-    assert(m_track > 0);

-    assert((pos + len) <= stop);

-

-    pos += len;  //consume track number

-    assert((stop - pos) >= 2);

-

-    m_timecode = Unserialize2SInt(pReader, pos);

-

-    pos += 2;

-    assert((stop - pos) >= 1);

-

-    const long hr = pReader->Read(pos, 1, &m_flags);

-    assert(hr == 0L);

-

-    ++pos;

-    assert(pos <= stop);

-

-    m_frameOff = pos;

-

-    const long long frame_size = stop - pos;

-

-    assert(frame_size <= 2147483647L);

-

-    m_frameSize = static_cast<long>(frame_size);

-}

-

-

-long long Block::GetTimeCode(Cluster* pCluster) const

-{

-    assert(pCluster);

-

-    const long long tc0 = pCluster->GetTimeCode();

-    assert(tc0 >= 0);

-

-    const long long tc = tc0 + static_cast<long long>(m_timecode);

-    assert(tc >= 0);

-

-    return tc;  //unscaled timecode units

-}

-

-

-long long Block::GetTime(Cluster* pCluster) const

-{

-    assert(pCluster);

-

-    const long long tc = GetTimeCode(pCluster);

-

-    const Segment* const pSegment = pCluster->m_pSegment;

-    const SegmentInfo* const pInfo = pSegment->GetInfo();

-    assert(pInfo);

-

-    const long long scale = pInfo->GetTimeCodeScale();

-    assert(scale >= 1);

-

-    const long long ns = tc * scale;

-

-    return ns;

-}

-

-

-long long Block::GetTrackNumber() const

-{

-    return m_track;

-}

-

-

-bool Block::IsKey() const

-{

-    return ((m_flags & static_cast<unsigned char>(1 << 7)) != 0);

-}

-

-unsigned char Block::Flags() const {

-    return m_flags;

-}

-

-void Block::SetKey(bool bKey)

-{

-    if (bKey)

-        m_flags |= static_cast<unsigned char>(1 << 7);

-    else

-        m_flags &= 0x7F;

-}

-

-

-long long Block::GetOffset() const

-{

-  return m_frameOff;

-}

-

-

-long Block::GetSize() const

-{

-    return m_frameSize;

-}

-

-

-long Block::Read(IMkvReader* pReader, unsigned char* buf) const

-{

-

-    assert(pReader);

-    assert(buf);

-

-    const long hr = pReader->Read(m_frameOff, m_frameSize, buf);

-

-    return hr;

-}

-

-

-}  //end namespace mkvparser

diff --git a/media/libstagefright/matroska/mkvparser.hpp b/media/libstagefright/matroska/mkvparser.hpp
deleted file mode 100644
index f7d8948..0000000
--- a/media/libstagefright/matroska/mkvparser.hpp
+++ /dev/null
@@ -1,556 +0,0 @@
-// Copyright (c) 2010 The WebM project authors. All Rights Reserved.

-//

-// Use of this source code is governed by a BSD-style license

-// that can be found in the LICENSE file in the root of the source

-// tree. An additional intellectual property rights grant can be found

-// in the file PATENTS.  All contributing project authors may

-// be found in the AUTHORS file in the root of the source tree.

-

-#ifndef MKVPARSER_HPP

-#define MKVPARSER_HPP

-

-#include <cstdlib>

-#include <cstdio>

-

-namespace mkvparser

-{

-

-const int E_FILE_FORMAT_INVALID = -2;

-const int E_BUFFER_NOT_FULL = -3;

-

-class IMkvReader

-{

-public:

-    virtual int Read(long long pos, long len, unsigned char* buf) = 0;

-    virtual int Length(long long* total, long long* available) = 0;

-protected:

-    virtual ~IMkvReader();

-};

-

-long long GetUIntLength(IMkvReader*, long long, long&);

-long long ReadUInt(IMkvReader*, long long, long&);

-long long SyncReadUInt(IMkvReader*, long long pos, long long stop, long&);

-long long UnserializeUInt(IMkvReader*, long long pos, long long size);

-float Unserialize4Float(IMkvReader*, long long);

-double Unserialize8Double(IMkvReader*, long long);

-short Unserialize2SInt(IMkvReader*, long long);

-signed char Unserialize1SInt(IMkvReader*, long long);

-bool Match(IMkvReader*, long long&, unsigned long, long long&);

-bool Match(IMkvReader*, long long&, unsigned long, char*&);

-bool Match(IMkvReader*, long long&, unsigned long,unsigned char*&, size_t&);

-bool Match(IMkvReader*, long long&, unsigned long, double&);

-bool Match(IMkvReader*, long long&, unsigned long, short&);

-

-void GetVersion(int& major, int& minor, int& build, int& revision);

-

-struct EBMLHeader

-{

-    EBMLHeader();

-    ~EBMLHeader();

-    long long m_version;

-    long long m_readVersion;

-    long long m_maxIdLength;

-    long long m_maxSizeLength;

-    char* m_docType;

-    long long m_docTypeVersion;

-    long long m_docTypeReadVersion;

-

-    long long Parse(IMkvReader*, long long&);

-};

-

-

-class Segment;

-class Track;

-class Cluster;

-

-class Block

-{

-    Block(const Block&);

-    Block& operator=(const Block&);

-

-public:

-    const long long m_start;

-    const long long m_size;

-

-    Block(long long start, long long size, IMkvReader*);

-

-    long long GetTrackNumber() const;

-    long long GetTimeCode(Cluster*) const;  //absolute, but not scaled

-    long long GetTime(Cluster*) const;      //absolute, and scaled (ns units)

-    bool IsKey() const;

-    void SetKey(bool);

-

-    unsigned char Flags() const;

-

-    long long GetOffset() const;

-    long GetSize() const;

-    long Read(IMkvReader*, unsigned char*) const;

-

-private:

-    long long m_track;   //Track::Number()

-    short m_timecode;  //relative to cluster

-    unsigned char m_flags;

-    long long m_frameOff;

-    long m_frameSize;

-

-};

-

-

-class BlockEntry

-{

-    BlockEntry(const BlockEntry&);

-    BlockEntry& operator=(const BlockEntry&);

-

-public:

-    virtual ~BlockEntry();

-    virtual bool EOS() const = 0;

-    virtual Cluster* GetCluster() const = 0;

-    virtual size_t GetIndex() const = 0;

-    virtual const Block* GetBlock() const = 0;

-    virtual bool IsBFrame() const = 0;

-

-protected:

-    BlockEntry();

-

-};

-

-

-class SimpleBlock : public BlockEntry

-{

-    SimpleBlock(const SimpleBlock&);

-    SimpleBlock& operator=(const SimpleBlock&);

-

-public:

-    SimpleBlock(Cluster*, size_t, long long start, long long size);

-

-    bool EOS() const;

-    Cluster* GetCluster() const;

-    size_t GetIndex() const;

-    const Block* GetBlock() const;

-    bool IsBFrame() const;

-

-protected:

-    Cluster* const m_pCluster;

-    const size_t m_index;

-    Block m_block;

-

-};

-

-

-class BlockGroup : public BlockEntry

-{

-    BlockGroup(const BlockGroup&);

-    BlockGroup& operator=(const BlockGroup&);

-

-public:

-    BlockGroup(Cluster*, size_t, long long, long long);

-    ~BlockGroup();

-

-    bool EOS() const;

-    Cluster* GetCluster() const;

-    size_t GetIndex() const;

-    const Block* GetBlock() const;

-    bool IsBFrame() const;

-

-    short GetPrevTimeCode() const;  //relative to block's time

-    short GetNextTimeCode() const;  //as above

-

-protected:

-    Cluster* const m_pCluster;

-    const size_t m_index;

-

-private:

-    BlockGroup(Cluster*, size_t, unsigned long);

-    void ParseBlock(long long start, long long size);

-

-    short m_prevTimeCode;

-    short m_nextTimeCode;

-

-    //TODO: the Matroska spec says you can have multiple blocks within the

-    //same block group, with blocks ranked by priority (the flag bits).

-    //For now we just cache a single block.

-#if 0

-    typedef std::deque<Block*> blocks_t;

-    blocks_t m_blocks;  //In practice should contain only a single element.

-#else

-    Block* m_pBlock;

-#endif

-

-};

-

-

-class Track

-{

-    Track(const Track&);

-    Track& operator=(const Track&);

-

-public:

-    Segment* const m_pSegment;

-    virtual ~Track();

-

-    long long GetType() const;

-    long long GetNumber() const;

-    const char* GetNameAsUTF8() const;

-    const char* GetCodecNameAsUTF8() const;

-    const char* GetCodecId() const;

-    const unsigned char* GetCodecPrivate(size_t&) const;

-

-    const BlockEntry* GetEOS() const;

-

-    struct Settings

-    {

-        long long start;

-        long long size;

-    };

-

-    struct Info

-    {

-        long long type;

-        long long number;

-        long long uid;

-        char* nameAsUTF8;

-        char* codecId;

-        unsigned char* codecPrivate;

-        size_t codecPrivateSize;

-        char* codecNameAsUTF8;

-        Settings settings;

-        Info();

-        void Clear();

-    };

-

-    long GetFirst(const BlockEntry*&) const;

-    long GetNext(const BlockEntry* pCurr, const BlockEntry*& pNext) const;

-    virtual bool VetEntry(const BlockEntry*) const = 0;

-

-protected:

-    Track(Segment*, const Info&);

-    const Info m_info;

-

-    class EOSBlock : public BlockEntry

-    {

-    public:

-        EOSBlock();

-

-        bool EOS() const;

-        Cluster* GetCluster() const;

-        size_t GetIndex() const;

-        const Block* GetBlock() const;

-        bool IsBFrame() const;

-    };

-

-    EOSBlock m_eos;

-

-};

-

-

-class VideoTrack : public Track

-{

-    VideoTrack(const VideoTrack&);

-    VideoTrack& operator=(const VideoTrack&);

-

-public:

-    VideoTrack(Segment*, const Info&);

-    long long GetWidth() const;

-    long long GetHeight() const;

-    double GetFrameRate() const;

-

-    bool VetEntry(const BlockEntry*) const;

-

-private:

-    long long m_width;

-    long long m_height;

-    double m_rate;

-

-};

-

-

-class AudioTrack : public Track

-{

-    AudioTrack(const AudioTrack&);

-    AudioTrack& operator=(const AudioTrack&);

-

-public:

-    AudioTrack(Segment*, const Info&);

-    double GetSamplingRate() const;

-    long long GetChannels() const;

-    long long GetBitDepth() const;

-    bool VetEntry(const BlockEntry*) const;

-

-private:

-    double m_rate;

-    long long m_channels;

-    long long m_bitDepth;

-};

-

-

-class Tracks

-{

-    Tracks(const Tracks&);

-    Tracks& operator=(const Tracks&);

-

-public:

-    Segment* const m_pSegment;

-    const long long m_start;

-    const long long m_size;

-

-    Tracks(Segment*, long long start, long long size);

-    virtual ~Tracks();

-

-    Track* GetTrackByNumber(unsigned long tn) const;

-    Track* GetTrackByIndex(unsigned long idx) const;

-

-private:

-    Track** m_trackEntries;

-    Track** m_trackEntriesEnd;

-

-    void ParseTrackEntry(long long, long long, Track*&);

-

-public:

-    unsigned long GetTracksCount() const;

-};

-

-

-class SegmentInfo

-{

-    SegmentInfo(const SegmentInfo&);

-    SegmentInfo& operator=(const SegmentInfo&);

-

-public:

-    Segment* const m_pSegment;

-    const long long m_start;

-    const long long m_size;

-

-    SegmentInfo(Segment*, long long start, long long size);

-    ~SegmentInfo();

-    long long GetTimeCodeScale() const;

-    long long GetDuration() const;  //scaled

-    const char* GetMuxingAppAsUTF8() const;

-    const char* GetWritingAppAsUTF8() const;

-    const char* GetTitleAsUTF8() const;

-

-private:

-    long long m_timecodeScale;

-    double m_duration;

-    char* m_pMuxingAppAsUTF8;

-    char* m_pWritingAppAsUTF8;

-    char* m_pTitleAsUTF8;

-};

-

-class Cues;

-class CuePoint

-{

-    friend class Cues;

-

-    CuePoint(size_t, long long);

-    ~CuePoint();

-

-    CuePoint(const CuePoint&);

-    CuePoint& operator=(const CuePoint&);

-

-public:

-    void Load(IMkvReader*);

-

-    long long GetTimeCode() const;      //absolute but unscaled

-    long long GetTime(Segment*) const;  //absolute and scaled (ns units)

-

-    struct TrackPosition

-    {

-        long long m_track;

-        long long m_pos;  //of cluster

-        long long m_block;

-        //codec_state  //defaults to 0

-        //reference = clusters containing req'd referenced blocks

-        //  reftime = timecode of the referenced block

-

-        void Parse(IMkvReader*, long long, long long);

-    };

-

-    const TrackPosition* Find(const Track*) const;

-

-private:

-    const size_t m_index;

-    long long m_timecode;

-    TrackPosition* m_track_positions;

-    size_t m_track_positions_count;

-

-};

-

-

-class Cues

-{

-    friend class Segment;

-

-    Cues(Segment*, long long start, long long size);

-    ~Cues();

-

-    Cues(const Cues&);

-    Cues& operator=(const Cues&);

-

-public:

-    Segment* const m_pSegment;

-    const long long m_start;

-    const long long m_size;

-

-    bool Find(  //lower bound of time_ns

-        long long time_ns,

-        const Track*,

-        const CuePoint*&,

-        const CuePoint::TrackPosition*&) const;

-

-#if 0

-    bool FindNext(  //upper_bound of time_ns

-        long long time_ns,

-        const Track*,

-        const CuePoint*&,

-        const CuePoint::TrackPosition*&) const;

-#endif

-

-    const CuePoint* GetFirst() const;

-    const CuePoint* GetLast() const;

-

-    const CuePoint* GetNext(const CuePoint*) const;

-

-    const BlockEntry* GetBlock(

-                        const CuePoint*,

-                        const CuePoint::TrackPosition*) const;

-

-private:

-    void Init() const;

-    bool LoadCuePoint() const;

-    void PreloadCuePoint(size_t&, long long) const;

-

-    mutable CuePoint** m_cue_points;

-    mutable size_t m_count;

-    mutable size_t m_preload_count;

-    mutable long long m_pos;

-

-};

-

-

-class Cluster

-{

-    Cluster(const Cluster&);

-    Cluster& operator=(const Cluster&);

-

-public:

-    Segment* const m_pSegment;

-

-public:

-    static Cluster* Parse(Segment*, long, long long off);

-

-    Cluster();  //EndOfStream

-    ~Cluster();

-

-    bool EOS() const;

-

-    long long GetTimeCode();   //absolute, but not scaled

-    long long GetTime();       //absolute, and scaled (nanosecond units)

-    long long GetFirstTime();  //time (ns) of first (earliest) block

-    long long GetLastTime();   //time (ns) of last (latest) block

-

-    const BlockEntry* GetFirst();

-    const BlockEntry* GetLast();

-    const BlockEntry* GetNext(const BlockEntry*) const;

-    const BlockEntry* GetEntry(const Track*);

-    const BlockEntry* GetEntry(

-        const CuePoint&,

-        const CuePoint::TrackPosition&);

-    const BlockEntry* GetMaxKey(const VideoTrack*);

-

-protected:

-    Cluster(Segment*, long, long long off);

-

-public:

-    //TODO: these should all be private, with public selector functions

-    long m_index;

-    long long m_pos;

-    long long m_size;

-

-private:

-    long long m_timecode;

-    BlockEntry** m_entries;

-    size_t m_entriesCount;

-

-    void Load();

-    void LoadBlockEntries();

-    void ParseBlockGroup(long long, long long, size_t);

-    void ParseSimpleBlock(long long, long long, size_t);

-

-};

-

-

-class Segment

-{

-    friend class Cues;

-

-    Segment(const Segment&);

-    Segment& operator=(const Segment&);

-

-private:

-    Segment(IMkvReader*, long long pos, long long size);

-

-public:

-    IMkvReader* const m_pReader;

-    const long long m_start;  //posn of segment payload

-    const long long m_size;   //size of segment payload

-    Cluster m_eos;  //TODO: make private?

-

-    static long long CreateInstance(IMkvReader*, long long, Segment*&);

-    ~Segment();

-

-    long Load();  //loads headers and all clusters

-

-    //for incremental loading (splitter)

-    long long Unparsed() const;

-    long long ParseHeaders();  //stops when first cluster is found

-    long LoadCluster();        //loads one cluster

-

-#if 0

-    //This pair parses one cluster, but only changes the state of the

-    //segment object when the cluster is actually added to the index.

-    long ParseCluster(Cluster*&, long long& newpos) const;

-    bool AddCluster(Cluster*, long long);

-#endif

-

-    Tracks* GetTracks() const;

-    const SegmentInfo* GetInfo() const;

-    const Cues* GetCues() const;

-

-    long long GetDuration() const;

-

-    unsigned long GetCount() const;

-    Cluster* GetFirst();

-    Cluster* GetLast();

-    Cluster* GetNext(const Cluster*);

-

-    Cluster* FindCluster(long long time_nanoseconds);

-    const BlockEntry* Seek(long long time_nanoseconds, const Track*);

-

-private:

-

-    long long m_pos;  //absolute file posn; what has been consumed so far

-    SegmentInfo* m_pInfo;

-    Tracks* m_pTracks;

-    Cues* m_pCues;

-    Cluster** m_clusters;

-    long m_clusterCount;         //number of entries for which m_index >= 0

-    long m_clusterPreloadCount;  //number of entries for which m_index < 0

-    long m_clusterSize;          //array size

-

-    void AppendCluster(Cluster*);

-    void PreloadCluster(Cluster*, ptrdiff_t);

-

-    void ParseSeekHead(long long pos, long long size);

-    void ParseSeekEntry(long long pos, long long size);

-    void ParseCues(long long);

-

-    const BlockEntry* GetBlock(

-        const CuePoint&,

-        const CuePoint::TrackPosition&);

-

-};

-

-

-}  //end namespace mkvparser

-

-#endif  //MKVPARSER_HPP

diff --git a/media/libstagefright/mpeg2ts/ATSParser.cpp b/media/libstagefright/mpeg2ts/ATSParser.cpp
index 4335b99..6056739 100644
--- a/media/libstagefright/mpeg2ts/ATSParser.cpp
+++ b/media/libstagefright/mpeg2ts/ATSParser.cpp
@@ -371,21 +371,6 @@
     mBuffer->setRange(0, 0);
 
     switch (type) {
-        case DISCONTINUITY_HTTPLIVE:
-        {
-            mQueue.clear(true);
-
-            if (mStreamType == 0x1b) {
-                // Don't signal discontinuities on audio streams.
-                if (mSource != NULL) {
-                    mSource->queueDiscontinuity(type);
-                } else {
-                    deferDiscontinuity(type);
-                }
-            }
-            break;
-        }
-
         case DISCONTINUITY_SEEK:
         case DISCONTINUITY_FORMATCHANGE:
         {
diff --git a/media/libstagefright/mpeg2ts/ATSParser.h b/media/libstagefright/mpeg2ts/ATSParser.h
index ec3be84..455f9d5 100644
--- a/media/libstagefright/mpeg2ts/ATSParser.h
+++ b/media/libstagefright/mpeg2ts/ATSParser.h
@@ -34,7 +34,6 @@
 struct ATSParser : public RefBase {
     enum DiscontinuityType {
         DISCONTINUITY_NONE,
-        DISCONTINUITY_HTTPLIVE,
         DISCONTINUITY_SEEK,
         DISCONTINUITY_FORMATCHANGE
     };
diff --git a/media/libstagefright/mpeg2ts/ESQueue.cpp b/media/libstagefright/mpeg2ts/ESQueue.cpp
index 73efdfe..dcaf9f7 100644
--- a/media/libstagefright/mpeg2ts/ESQueue.cpp
+++ b/media/libstagefright/mpeg2ts/ESQueue.cpp
@@ -341,54 +341,6 @@
     return timeUs;
 }
 
-// static
-sp<MetaData> ElementaryStreamQueue::MakeAACCodecSpecificData(
-        unsigned profile, unsigned sampling_freq_index,
-        unsigned channel_configuration) {
-    sp<MetaData> meta = new MetaData;
-    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
-
-    CHECK_LE(sampling_freq_index, 11u);
-    static const int32_t kSamplingFreq[] = {
-        96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
-        16000, 12000, 11025, 8000
-    };
-    meta->setInt32(kKeySampleRate, kSamplingFreq[sampling_freq_index]);
-    meta->setInt32(kKeyChannelCount, channel_configuration);
-
-    static const uint8_t kStaticESDS[] = {
-        0x03, 22,
-        0x00, 0x00,     // ES_ID
-        0x00,           // streamDependenceFlag, URL_Flag, OCRstreamFlag
-
-        0x04, 17,
-        0x40,                       // Audio ISO/IEC 14496-3
-        0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00,
-
-        0x05, 2,
-        // AudioSpecificInfo follows
-
-        // oooo offf fccc c000
-        // o - audioObjectType
-        // f - samplingFreqIndex
-        // c - channelConfig
-    };
-    sp<ABuffer> csd = new ABuffer(sizeof(kStaticESDS) + 2);
-    memcpy(csd->data(), kStaticESDS, sizeof(kStaticESDS));
-
-    csd->data()[sizeof(kStaticESDS)] =
-        ((profile + 1) << 3) | (sampling_freq_index >> 1);
-
-    csd->data()[sizeof(kStaticESDS) + 1] =
-        ((sampling_freq_index << 7) & 0x80) | (channel_configuration << 3);
-
-    meta->setData(kKeyESDS, 0, csd->data(), csd->size());
-
-    return meta;
-}
-
 struct NALPosition {
     size_t nalOffset;
     size_t nalSize;
diff --git a/media/libstagefright/mpeg2ts/ESQueue.h b/media/libstagefright/mpeg2ts/ESQueue.h
index 5b7957e..153cfe6 100644
--- a/media/libstagefright/mpeg2ts/ESQueue.h
+++ b/media/libstagefright/mpeg2ts/ESQueue.h
@@ -19,6 +19,7 @@
 #define ES_QUEUE_H_
 
 #include <media/stagefright/foundation/ABase.h>
+#include <utils/Errors.h>
 #include <utils/List.h>
 #include <utils/RefBase.h>
 
@@ -61,10 +62,6 @@
     // returns its timestamp in us (or -1 if no time information).
     int64_t fetchTimestamp(size_t size);
 
-    static sp<MetaData> MakeAACCodecSpecificData(
-            unsigned profile, unsigned sampling_freq_index,
-            unsigned channel_configuration);
-
     DISALLOW_EVIL_CONSTRUCTORS(ElementaryStreamQueue);
 };
 
diff --git a/media/libstagefright/mpeg2ts/MPEG2TSExtractor.cpp b/media/libstagefright/mpeg2ts/MPEG2TSExtractor.cpp
index a1f0796..dfec47f 100644
--- a/media/libstagefright/mpeg2ts/MPEG2TSExtractor.cpp
+++ b/media/libstagefright/mpeg2ts/MPEG2TSExtractor.cpp
@@ -202,20 +202,13 @@
     LOGI("haveAudio=%d, haveVideo=%d", haveAudio, haveVideo);
 }
 
-static bool isDiscontinuity(const uint8_t *data, ssize_t size) {
-    return size == 188 && data[0] == 0x00;
-}
-
 status_t MPEG2TSExtractor::feedMore() {
     Mutex::Autolock autoLock(mLock);
 
     uint8_t packet[kTSPacketSize];
     ssize_t n = mDataSource->readAt(mOffset, packet, kTSPacketSize);
 
-    if (isDiscontinuity(packet, n)) {
-        LOGI("XXX discontinuity detected");
-        mParser->signalDiscontinuity(ATSParser::DISCONTINUITY_HTTPLIVE);
-    } else if (n < (ssize_t)kTSPacketSize) {
+    if (n < (ssize_t)kTSPacketSize) {
         return (n < 0) ? (status_t)n : ERROR_END_OF_STREAM;
     } else {
         mParser->feedTSPacket(packet, kTSPacketSize);
diff --git a/media/libstagefright/rtsp/AMPEG4AudioAssembler.cpp b/media/libstagefright/rtsp/AMPEG4AudioAssembler.cpp
index 8bfe285..11d9c22 100644
--- a/media/libstagefright/rtsp/AMPEG4AudioAssembler.cpp
+++ b/media/libstagefright/rtsp/AMPEG4AudioAssembler.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+//#define LOG_NDEBUG 0
+#define LOG_TAG "AMPEG4AudioAssembler"
+
 #include "AMPEG4AudioAssembler.h"
 
 #include "ARTPSource.h"
@@ -139,7 +142,10 @@
     return OK;
 }
 
-static status_t parseAudioSpecificConfig(ABitReader *bits) {
+static status_t parseAudioSpecificConfig(ABitReader *bits, sp<ABuffer> *asc) {
+    const uint8_t *dataStart = bits->data();
+    size_t totalNumBits = bits->numBitsLeft();
+
     unsigned audioObjectType;
     CHECK_EQ(parseAudioObjectType(bits, &audioObjectType), (status_t)OK);
 
@@ -185,13 +191,13 @@
         }
     }
 
-#if 0
-    // This is not supported here as the upper layers did not explicitly
-    // signal the length of AudioSpecificConfig.
-
     if (extensionAudioObjectType != 5 && bits->numBitsLeft() >= 16) {
+        size_t numBitsLeftAtStart = bits->numBitsLeft();
+
         unsigned syncExtensionType = bits->getBits(11);
         if (syncExtensionType == 0x2b7) {
+            LOGI("found syncExtension");
+
             CHECK_EQ(parseAudioObjectType(bits, &extensionAudioObjectType),
                      (status_t)OK);
 
@@ -203,9 +209,45 @@
                     /* unsigned extensionSamplingFrequency = */bits->getBits(24);
                 }
             }
+
+            size_t numBitsInExtension =
+                numBitsLeftAtStart - bits->numBitsLeft();
+
+            if (numBitsInExtension & 7) {
+                // Apparently an extension is always considered an even
+                // multiple of 8 bits long.
+
+                LOGI("Skipping %d bits after sync extension",
+                     8 - (numBitsInExtension & 7));
+
+                bits->skipBits(8 - (numBitsInExtension & 7));
+            }
+        } else {
+            bits->putBits(syncExtensionType, 11);
         }
     }
-#endif
+
+    if (asc != NULL) {
+        size_t bitpos = totalNumBits & 7;
+
+        ABitReader bs(dataStart, (totalNumBits + 7) / 8);
+
+        totalNumBits -= bits->numBitsLeft();
+
+        size_t numBytes = (totalNumBits + 7) / 8;
+
+        *asc = new ABuffer(numBytes);
+
+        if (bitpos & 7) {
+            bs.skipBits(8 - (bitpos & 7));
+        }
+
+        uint8_t *dstPtr = (*asc)->data();
+        while (numBytes > 0) {
+            *dstPtr++ = bs.getBits(8);
+            --numBytes;
+        }
+    }
 
     return OK;
 }
@@ -214,6 +256,7 @@
         ABitReader *bits,
         unsigned *numSubFrames,
         unsigned *frameLengthType,
+        ssize_t *fixedFrameLength,
         bool *otherDataPresent,
         unsigned *otherDataLenBits) {
     unsigned audioMuxVersion = bits->getBits(1);
@@ -242,12 +285,14 @@
 
     if (audioMuxVersion == 0) {
         // AudioSpecificConfig
-        CHECK_EQ(parseAudioSpecificConfig(bits), (status_t)OK);
+        CHECK_EQ(parseAudioSpecificConfig(bits, NULL /* asc */), (status_t)OK);
     } else {
         TRESPASS();  // XXX to be implemented
     }
 
     *frameLengthType = bits->getBits(3);
+    *fixedFrameLength = -1;
+
     switch (*frameLengthType) {
         case 0:
         {
@@ -260,7 +305,14 @@
 
         case 1:
         {
-            /* unsigned frameLength = */bits->getBits(9);
+            *fixedFrameLength = bits->getBits(9);
+            break;
+        }
+
+        case 2:
+        {
+            // reserved
+            TRESPASS();
             break;
         }
 
@@ -338,9 +390,21 @@
                 break;
             }
 
-            default:
-                TRESPASS();  // XXX to be implemented
+            case 2:
+            {
+                // reserved
+
+                TRESPASS();
                 break;
+            }
+
+            default:
+            {
+                CHECK_GE(mFixedFrameLength, 0);
+
+                payloadLength = mFixedFrameLength;
+                break;
+            }
         }
 
         CHECK_LE(offset + payloadLength, buffer->size());
@@ -393,6 +457,7 @@
     ABitReader bits(config->data(), config->size());
     status_t err = parseStreamMuxConfig(
             &bits, &mNumSubFrames, &mFrameLengthType,
+            &mFixedFrameLength,
             &mOtherDataPresent, &mOtherDataLenBits);
 
     CHECK_EQ(err, (status_t)NO_ERROR);
diff --git a/media/libstagefright/rtsp/AMPEG4AudioAssembler.h b/media/libstagefright/rtsp/AMPEG4AudioAssembler.h
index 9cef94c..1361cd2 100644
--- a/media/libstagefright/rtsp/AMPEG4AudioAssembler.h
+++ b/media/libstagefright/rtsp/AMPEG4AudioAssembler.h
@@ -46,6 +46,7 @@
     bool mMuxConfigPresent;
     unsigned mNumSubFrames;
     unsigned mFrameLengthType;
+    ssize_t mFixedFrameLength;
     bool mOtherDataPresent;
     unsigned mOtherDataLenBits;
 
diff --git a/media/libstagefright/rtsp/AMPEG4ElementaryAssembler.cpp b/media/libstagefright/rtsp/AMPEG4ElementaryAssembler.cpp
index 13988cd..9f6bd29 100644
--- a/media/libstagefright/rtsp/AMPEG4ElementaryAssembler.cpp
+++ b/media/libstagefright/rtsp/AMPEG4ElementaryAssembler.cpp
@@ -104,7 +104,7 @@
       mNextExpectedSeqNoValid(false),
       mNextExpectedSeqNo(0),
       mAccessUnitDamaged(false) {
-    mIsGeneric = desc.startsWith("mpeg4-generic/");
+    mIsGeneric = !strncasecmp(desc.c_str(),"mpeg4-generic/", 14);
 
     if (mIsGeneric) {
         AString value;
diff --git a/media/libstagefright/rtsp/APacketSource.cpp b/media/libstagefright/rtsp/APacketSource.cpp
index f0b858d..6819fef 100644
--- a/media/libstagefright/rtsp/APacketSource.cpp
+++ b/media/libstagefright/rtsp/APacketSource.cpp
@@ -20,6 +20,7 @@
 
 #include "APacketSource.h"
 
+#include "ARawAudioAssembler.h"
 #include "ASessionDescription.h"
 
 #include "avc_utils.h"
@@ -637,7 +638,7 @@
 
         mFormat->setInt32(kKeyWidth, width);
         mFormat->setInt32(kKeyHeight, height);
-    } else if (!strncmp(desc.c_str(), "mpeg4-generic/", 14)) {
+    } else if (!strncasecmp(desc.c_str(), "mpeg4-generic/", 14)) {
         AString val;
         if (!GetAttribute(params.c_str(), "mode", &val)
                 || (strcasecmp(val.c_str(), "AAC-lbr")
@@ -661,6 +662,8 @@
         mFormat->setData(
                 kKeyESDS, 0,
                 codecSpecificData->data(), codecSpecificData->size());
+    } else if (ARawAudioAssembler::Supports(desc.c_str())) {
+        ARawAudioAssembler::MakeFormat(desc.c_str(), mFormat);
     } else {
         mInitCheck = ERROR_UNSUPPORTED;
     }
diff --git a/media/libstagefright/rtsp/ARTPConnection.cpp b/media/libstagefright/rtsp/ARTPConnection.cpp
index 601f569..47de4e0 100644
--- a/media/libstagefright/rtsp/ARTPConnection.cpp
+++ b/media/libstagefright/rtsp/ARTPConnection.cpp
@@ -123,7 +123,7 @@
         struct sockaddr_in addr;
         memset(addr.sin_zero, 0, sizeof(addr.sin_zero));
         addr.sin_family = AF_INET;
-        addr.sin_addr.s_addr = INADDR_ANY;
+        addr.sin_addr.s_addr = htonl(INADDR_ANY);
         addr.sin_port = htons(port);
 
         if (bind(*rtpSocket,
@@ -340,6 +340,8 @@
 }
 
 status_t ARTPConnection::receive(StreamInfo *s, bool receiveRTP) {
+    LOGV("receiving %s", receiveRTP ? "RTP" : "RTCP");
+
     CHECK(!s->mIsInjected);
 
     sp<ABuffer> buffer = new ABuffer(65536);
diff --git a/media/libstagefright/rtsp/ARTPSource.cpp b/media/libstagefright/rtsp/ARTPSource.cpp
index 893a387..3aa07ce 100644
--- a/media/libstagefright/rtsp/ARTPSource.cpp
+++ b/media/libstagefright/rtsp/ARTPSource.cpp
@@ -25,6 +25,7 @@
 #include "AH263Assembler.h"
 #include "AMPEG4AudioAssembler.h"
 #include "AMPEG4ElementaryAssembler.h"
+#include "ARawAudioAssembler.h"
 #include "ASessionDescription.h"
 
 #include <media/stagefright/foundation/ABuffer.h>
@@ -67,9 +68,11 @@
     } else  if (!strncmp(desc.c_str(), "AMR-WB/", 7)) {
         mAssembler = new AAMRAssembler(notify, true /* isWide */, params);
     } else if (!strncmp(desc.c_str(), "MP4V-ES/", 8)
-            || !strncmp(desc.c_str(), "mpeg4-generic/", 14)) {
+            || !strncasecmp(desc.c_str(), "mpeg4-generic/", 14)) {
         mAssembler = new AMPEG4ElementaryAssembler(notify, desc, params);
         mIssueFIRRequests = true;
+    } else if (ARawAudioAssembler::Supports(desc.c_str())) {
+        mAssembler = new ARawAudioAssembler(notify, desc.c_str(), params);
     } else {
         TRESPASS();
     }
diff --git a/media/libstagefright/rtsp/ARTSPConnection.cpp b/media/libstagefright/rtsp/ARTSPConnection.cpp
index e936923..0740515 100644
--- a/media/libstagefright/rtsp/ARTSPConnection.cpp
+++ b/media/libstagefright/rtsp/ARTSPConnection.cpp
@@ -545,6 +545,10 @@
     return buffer;
 }
 
+static bool IsRTSPVersion(const AString &s) {
+    return s == "RTSP/1.0";
+}
+
 bool ARTSPConnection::receiveRTSPReponse() {
     AString statusLine;
 
@@ -584,13 +588,27 @@
         return false;
     }
 
-    AString statusCodeStr(
-            response->mStatusLine, space1 + 1, space2 - space1 - 1);
+    bool isRequest = false;
 
-    if (!ParseSingleUnsignedLong(
-                statusCodeStr.c_str(), &response->mStatusCode)
-            || response->mStatusCode < 100 || response->mStatusCode > 999) {
-        return false;
+    if (!IsRTSPVersion(AString(response->mStatusLine, 0, space1))) {
+        CHECK(IsRTSPVersion(
+                    AString(
+                        response->mStatusLine,
+                        space2 + 1,
+                        response->mStatusLine.size() - space2 - 1)));
+
+        isRequest = true;
+
+        response->mStatusCode = 0;
+    } else {
+        AString statusCodeStr(
+                response->mStatusLine, space1 + 1, space2 - space1 - 1);
+
+        if (!ParseSingleUnsignedLong(
+                    statusCodeStr.c_str(), &response->mStatusCode)
+                || response->mStatusCode < 100 || response->mStatusCode > 999) {
+            return false;
+        }
     }
 
     AString line;
@@ -680,7 +698,63 @@
         }
     }
 
-    return notifyResponseListener(response);
+    return isRequest
+        ? handleServerRequest(response)
+        : notifyResponseListener(response);
+}
+
+bool ARTSPConnection::handleServerRequest(const sp<ARTSPResponse> &request) {
+    // Implementation of server->client requests is optional for all methods
+    // but we do need to respond, even if it's just to say that we don't
+    // support the method.
+
+    ssize_t space1 = request->mStatusLine.find(" ");
+    CHECK_GE(space1, 0);
+
+    AString response;
+    response.append("RTSP/1.0 501 Not Implemented\r\n");
+
+    ssize_t i = request->mHeaders.indexOfKey("cseq");
+
+    if (i >= 0) {
+        AString value = request->mHeaders.valueAt(i);
+
+        unsigned long cseq;
+        if (!ParseSingleUnsignedLong(value.c_str(), &cseq)) {
+            return false;
+        }
+
+        response.append("CSeq: ");
+        response.append(cseq);
+        response.append("\r\n");
+    }
+
+    response.append("\r\n");
+
+    size_t numBytesSent = 0;
+    while (numBytesSent < response.size()) {
+        ssize_t n =
+            send(mSocket, response.c_str() + numBytesSent,
+                 response.size() - numBytesSent, 0);
+
+        if (n == 0) {
+            // Server closed the connection.
+            LOGE("Server unexpectedly closed the connection.");
+
+            return false;
+        } else if (n < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+
+            LOGE("Error sending rtsp response.");
+            return false;
+        }
+
+        numBytesSent += (size_t)n;
+    }
+
+    return true;
 }
 
 // static
diff --git a/media/libstagefright/rtsp/ARTSPConnection.h b/media/libstagefright/rtsp/ARTSPConnection.h
index 19be2a6..0fecf3c6 100644
--- a/media/libstagefright/rtsp/ARTSPConnection.h
+++ b/media/libstagefright/rtsp/ARTSPConnection.h
@@ -109,6 +109,8 @@
     status_t findPendingRequest(
             const sp<ARTSPResponse> &response, ssize_t *index) const;
 
+    bool handleServerRequest(const sp<ARTSPResponse> &request);
+
     static bool ParseSingleUnsignedLong(
             const char *from, unsigned long *x);
 
diff --git a/media/libstagefright/rtsp/ARTSPController.cpp b/media/libstagefright/rtsp/ARTSPController.cpp
index a7563ff..1328d2e 100644
--- a/media/libstagefright/rtsp/ARTSPController.cpp
+++ b/media/libstagefright/rtsp/ARTSPController.cpp
@@ -69,7 +69,14 @@
 void ARTSPController::disconnect() {
     Mutex::Autolock autoLock(mLock);
 
-    if (mState != CONNECTED) {
+    if (mState == CONNECTING) {
+        mState = DISCONNECTED;
+        mConnectionResult = ERROR_IO;
+        mCondition.broadcast();
+
+        mHandler.clear();
+        return;
+    } else if (mState != CONNECTED) {
         return;
     }
 
diff --git a/media/libstagefright/rtsp/ARawAudioAssembler.cpp b/media/libstagefright/rtsp/ARawAudioAssembler.cpp
new file mode 100644
index 0000000..dd47ea3
--- /dev/null
+++ b/media/libstagefright/rtsp/ARawAudioAssembler.cpp
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "ARawAudioAssembler"
+#include <utils/Log.h>
+
+#include "ARawAudioAssembler.h"
+
+#include "ARTPSource.h"
+#include "ASessionDescription.h"
+
+#include <media/stagefright/foundation/ABuffer.h>
+#include <media/stagefright/foundation/ADebug.h>
+#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/hexdump.h>
+#include <media/stagefright/MediaDefs.h>
+#include <media/stagefright/MetaData.h>
+#include <media/stagefright/Utils.h>
+
+namespace android {
+
+ARawAudioAssembler::ARawAudioAssembler(
+        const sp<AMessage> &notify, const char *desc, const AString &params)
+    : mNotifyMsg(notify),
+      mNextExpectedSeqNoValid(false),
+      mNextExpectedSeqNo(0) {
+}
+
+ARawAudioAssembler::~ARawAudioAssembler() {
+}
+
+ARTPAssembler::AssemblyStatus ARawAudioAssembler::assembleMore(
+        const sp<ARTPSource> &source) {
+    return addPacket(source);
+}
+
+ARTPAssembler::AssemblyStatus ARawAudioAssembler::addPacket(
+        const sp<ARTPSource> &source) {
+    List<sp<ABuffer> > *queue = source->queue();
+
+    if (queue->empty()) {
+        return NOT_ENOUGH_DATA;
+    }
+
+    if (mNextExpectedSeqNoValid) {
+        List<sp<ABuffer> >::iterator it = queue->begin();
+        while (it != queue->end()) {
+            if ((uint32_t)(*it)->int32Data() >= mNextExpectedSeqNo) {
+                break;
+            }
+
+            it = queue->erase(it);
+        }
+
+        if (queue->empty()) {
+            return NOT_ENOUGH_DATA;
+        }
+    }
+
+    sp<ABuffer> buffer = *queue->begin();
+
+    if (!mNextExpectedSeqNoValid) {
+        mNextExpectedSeqNoValid = true;
+        mNextExpectedSeqNo = (uint32_t)buffer->int32Data();
+    } else if ((uint32_t)buffer->int32Data() != mNextExpectedSeqNo) {
+        LOGV("Not the sequence number I expected");
+
+        return WRONG_SEQUENCE_NUMBER;
+    }
+
+    // hexdump(buffer->data(), buffer->size());
+
+    if (buffer->size() < 1) {
+        queue->erase(queue->begin());
+        ++mNextExpectedSeqNo;
+
+        LOGV("raw audio packet too short.");
+
+        return MALFORMED_PACKET;
+    }
+
+    sp<AMessage> msg = mNotifyMsg->dup();
+    msg->setObject("access-unit", buffer);
+    msg->post();
+
+    queue->erase(queue->begin());
+    ++mNextExpectedSeqNo;
+
+    return OK;
+}
+
+void ARawAudioAssembler::packetLost() {
+    CHECK(mNextExpectedSeqNoValid);
+    ++mNextExpectedSeqNo;
+}
+
+void ARawAudioAssembler::onByeReceived() {
+    sp<AMessage> msg = mNotifyMsg->dup();
+    msg->setInt32("eos", true);
+    msg->post();
+}
+
+// static
+bool ARawAudioAssembler::Supports(const char *desc) {
+    return !strncmp(desc, "PCMU/", 5)
+        || !strncmp(desc, "PCMA/", 5);
+}
+
+// static
+void ARawAudioAssembler::MakeFormat(
+        const char *desc, const sp<MetaData> &format) {
+    if (!strncmp(desc, "PCMU/", 5)) {
+        format->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_G711_MLAW);
+    } else if (!strncmp(desc, "PCMA/", 5)) {
+        format->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_G711_ALAW);
+    } else {
+        TRESPASS();
+    }
+
+    int32_t sampleRate, numChannels;
+    ASessionDescription::ParseFormatDesc(
+            desc, &sampleRate, &numChannels);
+
+    format->setInt32(kKeySampleRate, sampleRate);
+    format->setInt32(kKeyChannelCount, numChannels);
+}
+
+}  // namespace android
+
diff --git a/media/libstagefright/rtsp/ARawAudioAssembler.h b/media/libstagefright/rtsp/ARawAudioAssembler.h
new file mode 100644
index 0000000..ed7af08
--- /dev/null
+++ b/media/libstagefright/rtsp/ARawAudioAssembler.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef A_RAW_AUDIO_ASSEMBLER_H_
+
+#define A_RAW_AUDIO_ASSEMBLER_H_
+
+#include "ARTPAssembler.h"
+
+namespace android {
+
+struct AMessage;
+struct AString;
+struct MetaData;
+
+struct ARawAudioAssembler : public ARTPAssembler {
+    ARawAudioAssembler(
+            const sp<AMessage> &notify,
+            const char *desc, const AString &params);
+
+    static bool Supports(const char *desc);
+
+    static void MakeFormat(
+            const char *desc, const sp<MetaData> &format);
+
+protected:
+    virtual ~ARawAudioAssembler();
+
+    virtual AssemblyStatus assembleMore(const sp<ARTPSource> &source);
+    virtual void onByeReceived();
+    virtual void packetLost();
+
+private:
+    bool mIsWide;
+
+    sp<AMessage> mNotifyMsg;
+    bool mNextExpectedSeqNoValid;
+    uint32_t mNextExpectedSeqNo;
+
+    AssemblyStatus addPacket(const sp<ARTPSource> &source);
+
+    DISALLOW_EVIL_CONSTRUCTORS(ARawAudioAssembler);
+};
+
+}  // namespace android
+
+#endif  // A_RAW_AUDIO_ASSEMBLER_H_
diff --git a/media/libstagefright/rtsp/ASessionDescription.cpp b/media/libstagefright/rtsp/ASessionDescription.cpp
index 3e710dc..f03f7a2 100644
--- a/media/libstagefright/rtsp/ASessionDescription.cpp
+++ b/media/libstagefright/rtsp/ASessionDescription.cpp
@@ -71,6 +71,11 @@
             line.setTo(desc, i, eolPos - i);
         }
 
+        if (line.empty()) {
+            i = eolPos + 1;
+            continue;
+        }
+
         if (line.size() < 2 || line.c_str()[1] != '=') {
             return false;
         }
diff --git a/media/libstagefright/rtsp/Android.mk b/media/libstagefright/rtsp/Android.mk
index fb42de8..8530ff3 100644
--- a/media/libstagefright/rtsp/Android.mk
+++ b/media/libstagefright/rtsp/Android.mk
@@ -9,6 +9,7 @@
         AMPEG4AudioAssembler.cpp    \
         AMPEG4ElementaryAssembler.cpp \
         APacketSource.cpp           \
+        ARawAudioAssembler.cpp      \
         ARTPAssembler.cpp           \
         ARTPConnection.cpp          \
         ARTPSource.cpp              \
diff --git a/media/libstagefright/rtsp/MyHandler.h b/media/libstagefright/rtsp/MyHandler.h
index ba7c1b2..d15d9c5 100644
--- a/media/libstagefright/rtsp/MyHandler.h
+++ b/media/libstagefright/rtsp/MyHandler.h
@@ -38,6 +38,7 @@
 
 #include <arpa/inet.h>
 #include <sys/socket.h>
+#include <netdb.h>
 
 // If no access units are received within 5 secs, assume that the rtp
 // stream has ended and signal end of stream.
@@ -121,9 +122,10 @@
         // want to transmit user/pass in cleartext.
         AString host, path, user, pass;
         unsigned port;
-        if (ARTSPConnection::ParseURL(
-                    mSessionURL.c_str(), &host, &port, &path, &user, &pass)
-                && user.size() > 0) {
+        CHECK(ARTSPConnection::ParseURL(
+                    mSessionURL.c_str(), &host, &port, &path, &user, &pass));
+
+        if (user.size() > 0) {
             mSessionURL.clear();
             mSessionURL.append("rtsp://");
             mSessionURL.append(host);
@@ -133,6 +135,8 @@
 
             LOGI("rewritten session url: '%s'", mSessionURL.c_str());
         }
+
+        mSessionHost = host;
     }
 
     void connect(const sp<AMessage> &doneMsg) {
@@ -248,34 +252,64 @@
     // In case we're behind NAT, fire off two UDP packets to the remote
     // rtp/rtcp ports to poke a hole into the firewall for future incoming
     // packets. We're going to send an RR/SDES RTCP packet to both of them.
-    void pokeAHole(int rtpSocket, int rtcpSocket, const AString &transport) {
+    bool pokeAHole(int rtpSocket, int rtcpSocket, const AString &transport) {
+        struct sockaddr_in addr;
+        memset(addr.sin_zero, 0, sizeof(addr.sin_zero));
+        addr.sin_family = AF_INET;
+
         AString source;
         AString server_port;
         if (!GetAttribute(transport.c_str(),
                           "source",
-                          &source)
-                || !GetAttribute(transport.c_str(),
+                          &source)) {
+            LOGW("Missing 'source' field in Transport response. Using "
+                 "RTSP endpoint address.");
+
+            struct hostent *ent = gethostbyname(mSessionHost.c_str());
+            if (ent == NULL) {
+                LOGE("Failed to look up address of session host '%s'",
+                     mSessionHost.c_str());
+
+                return false;
+            }
+
+            addr.sin_addr.s_addr = *(in_addr_t *)ent->h_addr;
+        } else {
+            addr.sin_addr.s_addr = inet_addr(source.c_str());
+        }
+
+        if (!GetAttribute(transport.c_str(),
                                  "server_port",
                                  &server_port)) {
-            return;
+            LOGI("Missing 'server_port' field in Transport response.");
+            return false;
         }
 
         int rtpPort, rtcpPort;
         if (sscanf(server_port.c_str(), "%d-%d", &rtpPort, &rtcpPort) != 2
                 || rtpPort <= 0 || rtpPort > 65535
                 || rtcpPort <=0 || rtcpPort > 65535
-                || rtcpPort != rtpPort + 1
-                || (rtpPort & 1) != 0) {
-            return;
+                || rtcpPort != rtpPort + 1) {
+            LOGE("Server picked invalid RTP/RTCP port pair %s,"
+                 " RTP port must be even, RTCP port must be one higher.",
+                 server_port.c_str());
+
+            return false;
         }
 
-        struct sockaddr_in addr;
-        memset(addr.sin_zero, 0, sizeof(addr.sin_zero));
-        addr.sin_family = AF_INET;
-        addr.sin_addr.s_addr = inet_addr(source.c_str());
+        if (rtpPort & 1) {
+            LOGW("Server picked an odd RTP port, it should've picked an "
+                 "even one, we'll let it pass for now, but this may break "
+                 "in the future.");
+        }
 
         if (addr.sin_addr.s_addr == INADDR_NONE) {
-            return;
+            return true;
+        }
+
+        if (IN_LOOPBACK(ntohl(addr.sin_addr.s_addr))) {
+            // No firewalls to traverse on the loopback interface.
+            return true;
         }
 
         // Make up an RR/SDES RTCP packet.
@@ -289,16 +323,26 @@
         ssize_t n = sendto(
                 rtpSocket, buf->data(), buf->size(), 0,
                 (const sockaddr *)&addr, sizeof(addr));
-        CHECK_EQ(n, (ssize_t)buf->size());
+
+        if (n < (ssize_t)buf->size()) {
+            LOGE("failed to poke a hole for RTP packets");
+            return false;
+        }
 
         addr.sin_port = htons(rtcpPort);
 
         n = sendto(
                 rtcpSocket, buf->data(), buf->size(), 0,
                 (const sockaddr *)&addr, sizeof(addr));
-        CHECK_EQ(n, (ssize_t)buf->size());
+
+        if (n < (ssize_t)buf->size()) {
+            LOGE("failed to poke a hole for RTCP packets");
+            return false;
+        }
 
         LOGV("successfully poked holes.");
+
+        return true;
     }
 
     virtual void onMessageReceived(const sp<AMessage> &msg) {
@@ -381,6 +425,7 @@
                                 response->mContent->size());
 
                         if (!mSessionDesc->isValid()) {
+                            LOGE("Failed to parse session description.");
                             result = ERROR_MALFORMED;
                         } else {
                             ssize_t i = response->mHeaders.indexOfKey("content-base");
@@ -395,6 +440,25 @@
                                 }
                             }
 
+                            if (!mBaseURL.startsWith("rtsp://")) {
+                                // Some misbehaving servers specify a relative
+                                // URL in one of the locations above, combine
+                                // it with the absolute session URL to get
+                                // something usable...
+
+                                LOGW("Server specified a non-absolute base URL"
+                                     ", combining it with the session URL to "
+                                     "get something usable...");
+
+                                AString tmp;
+                                CHECK(MakeURL(
+                                            mSessionURL.c_str(),
+                                            mBaseURL.c_str(),
+                                            &tmp));
+
+                                mBaseURL = tmp;
+                            }
+
                             CHECK_GT(mSessionDesc->countTracks(), 1u);
                             setupTrack(1);
                         }
@@ -455,9 +519,12 @@
                         if (!track->mUsingInterleavedTCP) {
                             AString transport = response->mHeaders.valueAt(i);
 
-                            pokeAHole(track->mRTPSocket,
-                                      track->mRTCPSocket,
-                                      transport);
+                            // We are going to continue even if we were
+                            // unable to poke a hole into the firewall...
+                            pokeAHole(
+                                    track->mRTPSocket,
+                                    track->mRTCPSocket,
+                                    transport);
                         }
 
                         mRTPConn->addStream(
@@ -853,17 +920,16 @@
             case 'tiou':
             {
                 if (!mReceivedFirstRTCPPacket) {
-                    if (mTryFakeRTCP) {
-                        LOGW("Never received any data, disconnecting.");
-                        (new AMessage('abor', id()))->post();
-                    } else if (mTryTCPInterleaving && mReceivedFirstRTPPacket) {
+                    if (mReceivedFirstRTPPacket && !mTryFakeRTCP) {
                         LOGW("We received RTP packets but no RTCP packets, "
                              "using fake timestamps.");
 
                         mTryFakeRTCP = true;
 
                         mReceivedFirstRTCPPacket = true;
-                    } else {
+
+                        fakeTimestamps();
+                    } else if (!mReceivedFirstRTPPacket && !mTryTCPInterleaving) {
                         LOGW("Never received any data, switching transports.");
 
                         mTryTCPInterleaving = true;
@@ -871,6 +937,9 @@
                         sp<AMessage> msg = new AMessage('abor', id());
                         msg->setInt32("reconnect", true);
                         msg->post();
+                    } else {
+                        LOGW("Never received any data, disconnecting.");
+                        (new AMessage('abor', id()))->post();
                     }
                 }
                 break;
@@ -1016,6 +1085,7 @@
     sp<ASessionDescription> mSessionDesc;
     AString mOriginalSessionURL;  // This one still has user:pass@
     AString mSessionURL;
+    AString mSessionHost;
     AString mBaseURL;
     AString mSessionID;
     bool mSetupTracksSuccessful;
@@ -1158,6 +1228,12 @@
         return true;
     }
 
+    void fakeTimestamps() {
+        for (size_t i = 0; i < mTracks.size(); ++i) {
+            onTimeUpdate(i, 0, 0ll);
+        }
+    }
+
     void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime) {
         LOGV("onTimeUpdate track %d, rtpTime = 0x%08x, ntpTime = 0x%016llx",
              trackIndex, rtpTime, ntpTime);
diff --git a/media/mtp/MtpDevice.cpp b/media/mtp/MtpDevice.cpp
index fb1b073..4ea8849 100644
--- a/media/mtp/MtpDevice.cpp
+++ b/media/mtp/MtpDevice.cpp
@@ -38,6 +38,7 @@
 
 namespace android {
 
+#if 0
 static bool isMtpDevice(uint16_t vendor, uint16_t product) {
     // Sandisk Sansa Fuze
     if (vendor == 0x0781 && product == 0x74c2)
@@ -47,6 +48,7 @@
         return true;
     return false;
 }
+#endif
 
 MtpDevice* MtpDevice::open(const char* deviceName, int fd) {
     struct usb_device *device = usb_device_new(deviceName, fd);
@@ -91,7 +93,9 @@
                 LOGD("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
                 free(manufacturerName);
                 free(productName);
-            } else {
+            }
+#if 0
+             else {
                 // look for special cased devices based on vendor/product ID
                 // we are doing this mainly for testing purposes
                 uint16_t vendor = usb_device_get_vendor_id(device);
@@ -119,7 +123,7 @@
                     printf("no MTP string\n");
                 }
             }
-
+#endif
             // if we got here, then we have a likely MTP or PTP device
 
             // interface should be followed by three endpoints
diff --git a/media/mtp/MtpPacket.cpp b/media/mtp/MtpPacket.cpp
index d3f2cb4..baf99e5 100644
--- a/media/mtp/MtpPacket.cpp
+++ b/media/mtp/MtpPacket.cpp
@@ -153,12 +153,13 @@
 
 #ifdef MTP_HOST
 int MtpPacket::transfer(struct usb_request* request) {
-    if (usb_request_queue(request)) {
-        LOGE("usb_endpoint_queue failed, errno: %d", errno);
-        return -1;
-    }
-    request = usb_request_wait(request->dev);
-    return (request ? request->actual_length : -1);
+    int result = usb_device_bulk_transfer(request->dev,
+                            request->endpoint,
+                            request->buffer,
+                            request->buffer_length,
+                            0);
+    request->actual_length = result;
+    return result;
 }
 #endif
 
diff --git a/media/mtp/MtpServer.cpp b/media/mtp/MtpServer.cpp
index 853a5af..37e02a3 100644
--- a/media/mtp/MtpServer.cpp
+++ b/media/mtp/MtpServer.cpp
@@ -84,6 +84,8 @@
 static const MtpEventCode kSupportedEventCodes[] = {
     MTP_EVENT_OBJECT_ADDED,
     MTP_EVENT_OBJECT_REMOVED,
+    MTP_EVENT_STORE_ADDED,
+    MTP_EVENT_STORE_REMOVED,
 };
 
 MtpServer::MtpServer(int fd, MtpDatabase* database,
@@ -104,11 +106,23 @@
 MtpServer::~MtpServer() {
 }
 
-void MtpServer::addStorage(const char* filePath, uint64_t reserveSpace) {
-    int index = mStorages.size() + 1;
-    index |= index << 16;   // set high and low part to our index
-    MtpStorage* storage = new MtpStorage(index, filePath, reserveSpace);
-    addStorage(storage);
+void MtpServer::addStorage(MtpStorage* storage) {
+    Mutex::Autolock autoLock(mMutex);
+
+    mStorages.push(storage);
+    sendStoreAdded(storage->getStorageID());
+}
+
+void MtpServer::removeStorage(MtpStorage* storage) {
+    Mutex::Autolock autoLock(mMutex);
+
+    for (int i = 0; i < mStorages.size(); i++) {
+        if (mStorages[i] == storage) {
+            mStorages.removeAt(i);
+            sendStoreRemoved(storage->getStorageID());
+            break;
+        }
+    }
 }
 
 MtpStorage* MtpServer::getStorage(MtpStorageID id) {
@@ -122,6 +136,12 @@
     return NULL;
 }
 
+bool MtpServer::hasStorage(MtpStorageID id) {
+    if (id == 0 || id == 0xFFFFFFFF)
+        return mStorages.size() > 0;
+    return (getStorage(id) != NULL);
+}
+
 void MtpServer::run() {
     int fd = mFD;
 
@@ -203,28 +223,38 @@
 }
 
 void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
-    if (mSessionOpen) {
-        LOGV("sendObjectAdded %d\n", handle);
-        mEvent.setEventCode(MTP_EVENT_OBJECT_ADDED);
-        mEvent.setTransactionID(mRequest.getTransactionID());
-        mEvent.setParameter(1, handle);
-        int ret = mEvent.write(mFD);
-        LOGV("mEvent.write returned %d\n", ret);
-    }
+    LOGV("sendObjectAdded %d\n", handle);
+    sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
 }
 
 void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
+    LOGV("sendObjectRemoved %d\n", handle);
+    sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
+}
+
+void MtpServer::sendStoreAdded(MtpStorageID id) {
+    LOGV("sendStoreAdded %08X\n", id);
+    sendEvent(MTP_EVENT_STORE_ADDED, id);
+}
+
+void MtpServer::sendStoreRemoved(MtpStorageID id) {
+    LOGV("sendStoreRemoved %08X\n", id);
+    sendEvent(MTP_EVENT_STORE_REMOVED, id);
+}
+
+void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
     if (mSessionOpen) {
-        LOGV("sendObjectRemoved %d\n", handle);
-        mEvent.setEventCode(MTP_EVENT_OBJECT_REMOVED);
+        mEvent.setEventCode(code);
         mEvent.setTransactionID(mRequest.getTransactionID());
-        mEvent.setParameter(1, handle);
+        mEvent.setParameter(1, param1);
         int ret = mEvent.write(mFD);
         LOGV("mEvent.write returned %d\n", ret);
     }
 }
 
 bool MtpServer::handleRequest() {
+    Mutex::Autolock autoLock(mMutex);
+
     MtpOperationCode operation = mRequest.getOperationCode();
     MtpResponseCode response;
 
@@ -439,6 +469,9 @@
     MtpObjectFormat format = mRequest.getParameter(2);      // 0 for all formats
     MtpObjectHandle parent = mRequest.getParameter(3);      // 0xFFFFFFFF for objects with no parent
                                                             // 0x00000000 for all objects?
+
+    if (!hasStorage(storageID))
+        return MTP_RESPONSE_INVALID_STORAGE_ID;
     if (parent == 0xFFFFFFFF)
         parent = 0;
 
@@ -455,6 +488,8 @@
     MtpObjectFormat format = mRequest.getParameter(2);      // 0 for all formats
     MtpObjectHandle parent = mRequest.getParameter(3);      // 0xFFFFFFFF for objects with no parent
                                                             // 0x00000000 for all objects?
+    if (!hasStorage(storageID))
+        return MTP_RESPONSE_INVALID_STORAGE_ID;
     if (parent == 0xFFFFFFFF)
         parent = 0;
 
@@ -471,7 +506,9 @@
 MtpResponseCode MtpServer::doGetObjectReferences() {
     if (!mSessionOpen)
         return MTP_RESPONSE_SESSION_NOT_OPEN;
-    MtpStorageID handle = mRequest.getParameter(1);
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
+    MtpObjectHandle handle = mRequest.getParameter(1);
 
     // FIXME - check for invalid object handle
     MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
@@ -487,7 +524,10 @@
 MtpResponseCode MtpServer::doSetObjectReferences() {
     if (!mSessionOpen)
         return MTP_RESPONSE_SESSION_NOT_OPEN;
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpStorageID handle = mRequest.getParameter(1);
+
     MtpObjectHandleList* references = mData.getAUInt32();
     MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
     delete references;
@@ -495,6 +535,8 @@
 }
 
 MtpResponseCode MtpServer::doGetObjectPropValue() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpObjectHandle handle = mRequest.getParameter(1);
     MtpObjectProperty property = mRequest.getParameter(2);
     LOGV("GetObjectPropValue %d %s\n", handle,
@@ -504,6 +546,8 @@
 }
 
 MtpResponseCode MtpServer::doSetObjectPropValue() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpObjectHandle handle = mRequest.getParameter(1);
     MtpObjectProperty property = mRequest.getParameter(2);
     LOGV("SetObjectPropValue %d %s\n", handle,
@@ -537,6 +581,8 @@
 }
 
 MtpResponseCode MtpServer::doGetObjectPropList() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
 
     MtpObjectHandle handle = mRequest.getParameter(1);
     // use uint32_t so we can support 0xFFFFFFFF
@@ -552,11 +598,15 @@
 }
 
 MtpResponseCode MtpServer::doGetObjectInfo() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpObjectHandle handle = mRequest.getParameter(1);
     return mDatabase->getObjectInfo(handle, mData);
 }
 
 MtpResponseCode MtpServer::doGetObject() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpObjectHandle handle = mRequest.getParameter(1);
     MtpString pathBuf;
     int64_t fileLength;
@@ -592,6 +642,8 @@
 }
 
 MtpResponseCode MtpServer::doGetPartialObject() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpObjectHandle handle = mRequest.getParameter(1);
     uint32_t offset = mRequest.getParameter(2);
     uint32_t length = mRequest.getParameter(3);
@@ -688,6 +740,7 @@
     if (mSendObjectFileSize > storage->getFreeSpace())
         return MTP_RESPONSE_STORAGE_FULL;
 
+LOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
     MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
             format, parent, storageID, mSendObjectFileSize, modifiedTime);
     if (handle == kInvalidObjectHandle) {
@@ -719,6 +772,8 @@
 }
 
 MtpResponseCode MtpServer::doSendObject() {
+    if (!hasStorage())
+        return MTP_RESPONSE_GENERAL_ERROR;
     MtpResponseCode result = MTP_RESPONSE_OK;
     mode_t mask;
     int ret;
@@ -835,6 +890,8 @@
 }
 
 MtpResponseCode MtpServer::doDeleteObject() {
+    if (!hasStorage())
+        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
     MtpObjectHandle handle = mRequest.getParameter(1);
     MtpObjectFormat format = mRequest.getParameter(2);
     // FIXME - support deleting all objects if handle is 0xFFFFFFFF
diff --git a/media/mtp/MtpServer.h b/media/mtp/MtpServer.h
index 605d5a2..1efa715 100644
--- a/media/mtp/MtpServer.h
+++ b/media/mtp/MtpServer.h
@@ -22,9 +22,10 @@
 #include "MtpResponsePacket.h"
 #include "MtpEventPacket.h"
 #include "mtp.h"
-
 #include "MtpUtils.h"
 
+#include <utils/threads.h>
+
 namespace android {
 
 class MtpDatabase;
@@ -62,20 +63,29 @@
     MtpString           mSendObjectFilePath;
     size_t              mSendObjectFileSize;
 
+    Mutex               mMutex;
+
 public:
                         MtpServer(int fd, MtpDatabase* database,
                                     int fileGroup, int filePerm, int directoryPerm);
     virtual             ~MtpServer();
 
-    void                addStorage(const char* filePath, uint64_t reserveSpace);
-    inline void         addStorage(MtpStorage* storage) { mStorages.push(storage); }
-    MtpStorage*         getStorage(MtpStorageID id);
+    void                addStorage(MtpStorage* storage);
+    void                removeStorage(MtpStorage* storage);
+
     void                run();
 
     void                sendObjectAdded(MtpObjectHandle handle);
     void                sendObjectRemoved(MtpObjectHandle handle);
 
 private:
+    MtpStorage*         getStorage(MtpStorageID id);
+    inline bool         hasStorage() { return mStorages.size() > 0; }
+    bool                hasStorage(MtpStorageID id);
+    void                sendStoreAdded(MtpStorageID id);
+    void                sendStoreRemoved(MtpStorageID id);
+    void                sendEvent(MtpEventCode code, uint32_t param1);
+
     bool                handleRequest();
 
     MtpResponseCode     doGetDeviceInfo();
diff --git a/media/mtp/MtpStorage.cpp b/media/mtp/MtpStorage.cpp
index 2fbbc51..6cb88b3 100644
--- a/media/mtp/MtpStorage.cpp
+++ b/media/mtp/MtpStorage.cpp
@@ -59,7 +59,7 @@
 uint64_t MtpStorage::getMaxCapacity() {
     if (mMaxCapacity == 0) {
         struct statfs   stat;
-        if (statfs(mFilePath, &stat))
+        if (statfs(getPath(), &stat))
             return -1;
         mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize;
     }
@@ -68,7 +68,7 @@
 
 uint64_t MtpStorage::getFreeSpace() {
     struct statfs   stat;
-    if (statfs(mFilePath, &stat))
+    if (statfs(getPath(), &stat))
         return -1;
     uint64_t freeSpace = (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
     return (freeSpace > mReserveSpace ? freeSpace - mReserveSpace : 0);
diff --git a/media/mtp/MtpStorage.h b/media/mtp/MtpStorage.h
index ace720b..858c9d3 100644
--- a/media/mtp/MtpStorage.h
+++ b/media/mtp/MtpStorage.h
@@ -17,6 +17,7 @@
 #ifndef _MTP_STORAGE_H
 #define _MTP_STORAGE_H
 
+#include "MtpTypes.h"
 #include "mtp.h"
 
 namespace android {
@@ -27,7 +28,7 @@
 
 private:
     MtpStorageID            mStorageID;
-    const char*             mFilePath;
+    MtpString               mFilePath;
     uint64_t                mMaxCapacity;
     // amount of free space to leave unallocated
     uint64_t                mReserveSpace;
@@ -44,7 +45,7 @@
     uint64_t                getMaxCapacity();
     uint64_t                getFreeSpace();
     const char*             getDescription() const;
-    inline const char*      getPath() const { return mFilePath; }
+    inline const char*      getPath() const { return (const char *)mFilePath; }
 };
 
 }; // namespace android
diff --git a/media/mtp/mtp.h b/media/mtp/mtp.h
index 8bc2e22..6fedc16 100644
--- a/media/mtp/mtp.h
+++ b/media/mtp/mtp.h
@@ -22,6 +22,8 @@
 
 #define MTP_STANDARD_VERSION            100
 
+#define MTP_FIRST_STORAGE_ID            0x00010001
+
 // Container Types
 #define MTP_CONTAINER_TYPE_UNDEFINED    0
 #define MTP_CONTAINER_TYPE_COMMAND      1
diff --git a/services/audioflinger/AudioPolicyManagerBase.cpp b/services/audioflinger/AudioPolicyManagerBase.cpp
index 58e9b08..bfc80db 100644
--- a/services/audioflinger/AudioPolicyManagerBase.cpp
+++ b/services/audioflinger/AudioPolicyManagerBase.cpp
@@ -610,7 +610,7 @@
         // store time at which the stream was stopped - see isStreamActive()
         outputDesc->mStopTime[stream] = systemTime();
 
-        setOutputDevice(output, getNewDevice(output));
+        setOutputDevice(output, getNewDevice(output), false, outputDesc->mLatency*2);
 
 #ifdef WITH_A2DP
         if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
@@ -1622,12 +1622,6 @@
             if (device) break;
             device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
             if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
-            if (device) break;
 #ifdef WITH_A2DP
             // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
             if (!isInCall()) {
@@ -1637,6 +1631,12 @@
                 if (device) break;
             }
 #endif
+            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
+            if (device) break;
+            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
+            if (device) break;
+            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
+            if (device) break;
             device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
             if (device == 0) {
                 LOGE("getDeviceForStrategy() earpiece device not found");
@@ -1644,12 +1644,6 @@
             break;
 
         case AudioSystem::FORCE_SPEAKER:
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
-            if (device) break;
 #ifdef WITH_A2DP
             // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
             // A2DP speaker when forcing to speaker output
@@ -1658,6 +1652,12 @@
                 if (device) break;
             }
 #endif
+            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
+            if (device) break;
+            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
+            if (device) break;
+            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
+            if (device) break;
             device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
             if (device == 0) {
                 LOGE("getDeviceForStrategy() speaker device not found");
@@ -1686,20 +1686,9 @@
         if (device2 == 0) {
             device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
         }
-        if (device2 == 0) {
-            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
-        }
-        if (device2 == 0) {
-            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
-        }
-        if (device2 == 0) {
-            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
-        }
 #ifdef WITH_A2DP
-        if (mA2dpOutput != 0) {
-            if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
-                break;
-            }
+        if ((mA2dpOutput != 0) &&
+                (strategy != STRATEGY_SONIFICATION || a2dpUsedForSonification())) {
             if (device2 == 0) {
                 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
             }
@@ -1712,6 +1701,15 @@
         }
 #endif
         if (device2 == 0) {
+            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
+        }
+        if (device2 == 0) {
+            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
+        }
+        if (device2 == 0) {
+            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
+        }
+        if (device2 == 0) {
             device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
         }
 
@@ -1891,7 +1889,15 @@
         mStreams[i].mVolDbAtt[StreamDescriptor::VOLMAX] = 0.0f;
     }
 
-    // TODO add modifications for music to have finer steps below knee1 and above knee2
+    // Modification for music: more attenuation for lower volumes, finer steps at high volumes
+    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLMIN] = 1;
+    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLMIN] = -58.0f;
+    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLKNEE1] = 20;
+    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLKNEE1] = -40.0f;
+    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLKNEE2] = 60;
+    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLKNEE2] = -17.0f;
+    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLMAX] = 100;
+    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLMAX] = 0.0f;
 }
 
 float AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device)
@@ -1915,9 +1921,7 @@
         (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
         AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
         AudioSystem::DEVICE_OUT_WIRED_HEADSET |
-        AudioSystem::DEVICE_OUT_WIRED_HEADPHONE |
-        AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET |
-        AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)) &&
+        AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) &&
         ((getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) ||
          (stream == AudioSystem::SYSTEM)) &&
         streamDesc.mCanBeMuted) {
diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp
index 3d8ca7a..a09e16b 100644
--- a/services/camera/libcameraservice/CameraService.cpp
+++ b/services/camera/libcameraservice/CameraService.cpp
@@ -727,17 +727,30 @@
 }
 
 // take a picture - image is returned in callback
-status_t CameraService::Client::takePicture() {
-    LOG1("takePicture (pid %d)", getCallingPid());
+status_t CameraService::Client::takePicture(int msgType) {
+    LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
 
     Mutex::Autolock lock(mLock);
     status_t result = checkPidAndHardware();
     if (result != NO_ERROR) return result;
 
-    enableMsgType(CAMERA_MSG_SHUTTER |
-                  CAMERA_MSG_POSTVIEW_FRAME |
-                  CAMERA_MSG_RAW_IMAGE |
-                  CAMERA_MSG_COMPRESSED_IMAGE);
+    if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
+        (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
+        LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
+                " cannot be both enabled");
+        return BAD_VALUE;
+    }
+
+    // We only accept picture related message types
+    // and ignore other types of messages for takePicture().
+    int picMsgType = msgType
+                        & (CAMERA_MSG_SHUTTER |
+                           CAMERA_MSG_POSTVIEW_FRAME |
+                           CAMERA_MSG_RAW_IMAGE |
+                           CAMERA_MSG_RAW_IMAGE_NOTIFY |
+                           CAMERA_MSG_COMPRESSED_IMAGE);
+
+    enableMsgType(picMsgType);
 
     return mHardware->takePicture();
 }
diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h
index ccb9cf7..1c43b00 100644
--- a/services/camera/libcameraservice/CameraService.h
+++ b/services/camera/libcameraservice/CameraService.h
@@ -108,7 +108,7 @@
         virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
         virtual status_t        autoFocus();
         virtual status_t        cancelAutoFocus();
-        virtual status_t        takePicture();
+        virtual status_t        takePicture(int msgType);
         virtual status_t        setParameters(const String8& params);
         virtual String8         getParameters() const;
         virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);