Merge "Software AVC encoder is now OMX-based"
diff --git a/include/media/AudioRecord.h b/include/media/AudioRecord.h
index 70f79c6..ef77692 100644
--- a/include/media/AudioRecord.h
+++ b/include/media/AudioRecord.h
@@ -27,6 +27,7 @@
 #include <utils/Errors.h>
 #include <binder/IInterface.h>
 #include <binder/IMemory.h>
+#include <cutils/sched_policy.h>
 #include <utils/threads.h>
 
 #include <system/audio.h>
@@ -393,7 +394,7 @@
     audio_io_handle_t       mInput;
     int                     mSessionId;
     int                     mPreviousPriority;          // before start()
-    int                     mPreviousSchedulingGroup;
+    SchedPolicy             mPreviousSchedulingGroup;
 };
 
 }; // namespace android
diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h
index 98b1f3e..639d6d2 100644
--- a/include/media/AudioTrack.h
+++ b/include/media/AudioTrack.h
@@ -28,6 +28,7 @@
 #include <utils/Errors.h>
 #include <binder/IInterface.h>
 #include <binder/IMemory.h>
+#include <cutils/sched_policy.h>
 #include <utils/threads.h>
 
 namespace android {
@@ -536,7 +537,7 @@
     status_t                mRestoreStatus;
     bool                    mIsTimed;
     int                     mPreviousPriority;          // before start()
-    int                     mPreviousSchedulingGroup;
+    SchedPolicy             mPreviousSchedulingGroup;
 };
 
 class TimedAudioTrack : public AudioTrack
diff --git a/include/media/stagefright/OMXCodec.h b/include/media/stagefright/OMXCodec.h
index bf054ac..055da5d 100644
--- a/include/media/stagefright/OMXCodec.h
+++ b/include/media/stagefright/OMXCodec.h
@@ -24,6 +24,8 @@
 #include <media/stagefright/MediaSource.h>
 #include <utils/threads.h>
 
+#include <OMX_Audio.h>
+
 namespace android {
 
 struct MediaCodecList;
@@ -390,6 +392,8 @@
         bool isEncoder,
         CodecCapabilities *caps);
 
+status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]);
+
 }  // namespace android
 
 #endif  // OMX_CODEC_H_
diff --git a/libvideoeditor/lvpp/NativeWindowRenderer.cpp b/libvideoeditor/lvpp/NativeWindowRenderer.cpp
index 0f3ea3c..b2c2675 100755
--- a/libvideoeditor/lvpp/NativeWindowRenderer.cpp
+++ b/libvideoeditor/lvpp/NativeWindowRenderer.cpp
@@ -571,10 +571,8 @@
     : mRenderer(renderer)
     , mTextureId(textureId) {
     mST = new SurfaceTexture(mTextureId);
-    uint32_t outWidth, outHeight, outTransform;
-    mST->connect(NATIVE_WINDOW_API_MEDIA, &outWidth, &outHeight, &outTransform);
-
     mSTC = new SurfaceTextureClient(mST);
+    native_window_connect(mSTC.get(), NATIVE_WINDOW_API_MEDIA);
 }
 
 RenderInput::~RenderInput() {
diff --git a/media/libmedia/AudioRecord.cpp b/media/libmedia/AudioRecord.cpp
index 950f5c6..c21979b 100644
--- a/media/libmedia/AudioRecord.cpp
+++ b/media/libmedia/AudioRecord.cpp
@@ -79,7 +79,7 @@
 
 AudioRecord::AudioRecord()
     : mStatus(NO_INIT), mSessionId(0),
-      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
+      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
 {
 }
 
@@ -95,7 +95,7 @@
         int notificationFrames,
         int sessionId)
     : mStatus(NO_INIT), mSessionId(0),
-      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
+      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
 {
     mStatus = set(inputSource, sampleRate, format, channelMask,
             frameCount, flags, cbf, user, notificationFrames, sessionId);
@@ -342,7 +342,7 @@
                 mCondition.signal();
             } else {
                 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
-                mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
+                get_sched_policy(0, &mPreviousSchedulingGroup);
                 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
             }
         } else {
@@ -374,7 +374,7 @@
             t->requestExit();
         } else {
             setpriority(PRIO_PROCESS, 0, mPreviousPriority);
-            androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
+            set_sched_policy(0, mPreviousSchedulingGroup);
         }
     }
 
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index 5d5a312..b1be8b1 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -84,7 +84,7 @@
     : mStatus(NO_INIT),
       mIsTimed(false),
       mPreviousPriority(ANDROID_PRIORITY_NORMAL),
-      mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
+      mPreviousSchedulingGroup(SP_DEFAULT)
 {
 }
 
@@ -102,7 +102,7 @@
     : mStatus(NO_INIT),
       mIsTimed(false),
       mPreviousPriority(ANDROID_PRIORITY_NORMAL),
-      mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
+      mPreviousSchedulingGroup(SP_DEFAULT)
 {
     mStatus = set(streamType, sampleRate, format, channelMask,
             frameCount, flags, cbf, user, notificationFrames,
@@ -123,7 +123,7 @@
         int sessionId)
     : mStatus(NO_INIT),
       mIsTimed(false),
-      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
+      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
 {
     mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
             frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
@@ -144,7 +144,7 @@
     : mStatus(NO_INIT),
       mIsTimed(false),
       mPreviousPriority(ANDROID_PRIORITY_NORMAL),
-      mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
+      mPreviousSchedulingGroup(SP_DEFAULT)
 {
     mStatus = set(streamType, sampleRate, format, channelMask,
             0 /*frameCount*/, flags, cbf, user, notificationFrames,
@@ -378,7 +378,7 @@
             t->resume();
         } else {
             mPreviousPriority = getpriority(PRIO_PROCESS, 0);
-            mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
+            get_sched_policy(0, &mPreviousSchedulingGroup);
             androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
         }
 
@@ -403,7 +403,7 @@
                 t->pause();
             } else {
                 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
-                androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
+                set_sched_policy(0, mPreviousSchedulingGroup);
             }
         }
     }
@@ -436,7 +436,7 @@
             t->pause();
         } else {
             setpriority(PRIO_PROCESS, 0, mPreviousPriority);
-            androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
+            set_sched_policy(0, mPreviousSchedulingGroup);
         }
     }
 
diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index e6e0413..5ac34c9 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -1182,13 +1182,8 @@
     pcmParams.nSamplingRate = sampleRate;
     pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
 
-    if (numChannels == 1) {
-        pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
-    } else {
-        CHECK_EQ(numChannels, 2);
-
-        pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
-        pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
+    if (getOMXChannelMapping(numChannels, pcmParams.eChannelMapping) != OK) {
+        return OMX_ErrorNone;
     }
 
     return mOMX->setParameter(
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index 1c4b47e..1d6f927 100755
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -3310,14 +3310,8 @@
     pcmParams.nSamplingRate = sampleRate;
     pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
 
-    if (numChannels == 1) {
-        pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
-    } else {
-        CHECK_EQ(numChannels, 2);
-
-        pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
-        pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
-    }
+    CHECK_EQ(getOMXChannelMapping(
+                numChannels, pcmParams.eChannelMapping), (status_t)OK);
 
     err = mOMX->setParameter(
             mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
@@ -4602,4 +4596,67 @@
     header->pBuffer = (OMX_U8 *)info->mData;
 }
 
+// These are supposed be equivalent to the logic in
+// "audio_channel_out_mask_from_count".
+status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]) {
+    switch (numChannels) {
+        case 1:
+            map[0] = OMX_AUDIO_ChannelCF;
+            break;
+        case 2:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            break;
+        case 3:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            map[2] = OMX_AUDIO_ChannelCF;
+            break;
+        case 4:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            map[2] = OMX_AUDIO_ChannelLR;
+            map[3] = OMX_AUDIO_ChannelRR;
+            break;
+        case 5:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            map[2] = OMX_AUDIO_ChannelCF;
+            map[3] = OMX_AUDIO_ChannelLR;
+            map[4] = OMX_AUDIO_ChannelRR;
+            break;
+        case 6:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            map[2] = OMX_AUDIO_ChannelCF;
+            map[3] = OMX_AUDIO_ChannelLFE;
+            map[4] = OMX_AUDIO_ChannelLR;
+            map[5] = OMX_AUDIO_ChannelRR;
+            break;
+        case 7:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            map[2] = OMX_AUDIO_ChannelCF;
+            map[3] = OMX_AUDIO_ChannelLFE;
+            map[4] = OMX_AUDIO_ChannelLR;
+            map[5] = OMX_AUDIO_ChannelRR;
+            map[6] = OMX_AUDIO_ChannelCS;
+            break;
+        case 8:
+            map[0] = OMX_AUDIO_ChannelLF;
+            map[1] = OMX_AUDIO_ChannelRF;
+            map[2] = OMX_AUDIO_ChannelCF;
+            map[3] = OMX_AUDIO_ChannelLFE;
+            map[4] = OMX_AUDIO_ChannelLR;
+            map[5] = OMX_AUDIO_ChannelRR;
+            map[6] = OMX_AUDIO_ChannelLS;
+            map[7] = OMX_AUDIO_ChannelRS;
+            break;
+        default:
+            return -EINVAL;
+    }
+
+    return OK;
+}
+
 }  // namespace android
diff --git a/services/audioflinger/Android.mk b/services/audioflinger/Android.mk
index 4fbc67d..48b35cf 100644
--- a/services/audioflinger/Android.mk
+++ b/services/audioflinger/Android.mk
@@ -3,6 +3,17 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
+    ISchedulingPolicyService.cpp \
+    SchedulingPolicyService.cpp
+
+# FIXME Move this library to frameworks/native
+LOCAL_MODULE := libscheduling_policy
+
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
     AudioBufferProviderSource.cpp   \
     AudioStreamOutSink.cpp          \
     AudioStreamInSource.cpp         \
@@ -56,6 +67,7 @@
     libpowermanager
 
 LOCAL_STATIC_LIBRARIES := \
+    libscheduling_policy \
     libnbaio \
     libcpustats \
     libmedia_helper
@@ -68,6 +80,6 @@
 
 LOCAL_CFLAGS += -DSTATE_QUEUE_INSTANTIATIONS='"StateQueueInstantiations.cpp"'
 
-LOCAL_CFLAGS += -UHAVE_REQUEST_PRIORITY -UFAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE -USOAKER
+LOCAL_CFLAGS += -DHAVE_REQUEST_PRIORITY -UFAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE -USOAKER
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index de60942..6eeda9a 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -80,6 +80,10 @@
 #include "MonoPipeReader.h"
 #include "SourceAudioBufferProvider.h"
 
+#ifdef HAVE_REQUEST_PRIORITY
+#include "SchedulingPolicyService.h"
+#endif
+
 #ifdef SOAKER
 #include "Soaker.h"
 #endif
diff --git a/services/audioflinger/ISchedulingPolicyService.cpp b/services/audioflinger/ISchedulingPolicyService.cpp
new file mode 100644
index 0000000..909b77e
--- /dev/null
+++ b/services/audioflinger/ISchedulingPolicyService.cpp
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2012 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_TAG "SchedulingPolicyService"
+//#define LOG_NDEBUG 0
+
+#include <binder/Parcel.h>
+#include "ISchedulingPolicyService.h"
+
+namespace android {
+
+// Keep in sync with frameworks/base/core/java/android/os/ISchedulingPolicyService.aidl
+enum {
+    REQUEST_PRIORITY_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
+};
+
+// ----------------------------------------------------------------------
+
+class BpSchedulingPolicyService : public BpInterface<ISchedulingPolicyService>
+{
+public:
+    BpSchedulingPolicyService(const sp<IBinder>& impl)
+        : BpInterface<ISchedulingPolicyService>(impl)
+    {
+    }
+
+    virtual int requestPriority(int32_t pid, int32_t tid, int32_t prio)
+    {
+        Parcel data, reply;
+        data.writeInterfaceToken(ISchedulingPolicyService::getInterfaceDescriptor());
+        data.writeInt32(pid);
+        data.writeInt32(tid);
+        data.writeInt32(prio);
+        remote()->transact(REQUEST_PRIORITY_TRANSACTION, data, &reply);
+        // fail on exception
+        if (reply.readExceptionCode() != 0) return -1;
+        return reply.readInt32();
+    }
+};
+
+IMPLEMENT_META_INTERFACE(SchedulingPolicyService, "android.os.ISchedulingPolicyService");
+
+// ----------------------------------------------------------------------
+
+status_t BnSchedulingPolicyService::onTransact(
+    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
+{
+    switch (code) {
+    case REQUEST_PRIORITY_TRANSACTION:
+        // Not reached
+        return NO_ERROR;
+        break;
+    default:
+        return BBinder::onTransact(code, data, reply, flags);
+    }
+}
+
+}   // namespace android
diff --git a/services/audioflinger/ISchedulingPolicyService.h b/services/audioflinger/ISchedulingPolicyService.h
new file mode 100644
index 0000000..a38e67e
--- /dev/null
+++ b/services/audioflinger/ISchedulingPolicyService.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2012 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 ANDROID_ISCHEDULING_POLICY_SERVICE_H
+#define ANDROID_ISCHEDULING_POLICY_SERVICE_H
+
+#include <binder/IInterface.h>
+
+namespace android {
+
+class ISchedulingPolicyService : public IInterface
+{
+public:
+    DECLARE_META_INTERFACE(SchedulingPolicyService);
+
+    virtual int         requestPriority(/*pid_t*/int32_t pid, /*pid_t*/int32_t tid,
+                                                int32_t prio) = 0;
+
+};
+
+class BnSchedulingPolicyService : public BnInterface<ISchedulingPolicyService>
+{
+public:
+    virtual status_t    onTransact( uint32_t code,
+                                    const Parcel& data,
+                                    Parcel* reply,
+                                    uint32_t flags = 0);
+};
+
+}   // namespace android
+
+#endif  // ANDROID_ISCHEDULING_POLICY_SERVICE_H
diff --git a/services/audioflinger/SchedulingPolicyService.cpp b/services/audioflinger/SchedulingPolicyService.cpp
new file mode 100644
index 0000000..59cc99a
--- /dev/null
+++ b/services/audioflinger/SchedulingPolicyService.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#include <binder/IServiceManager.h>
+#include <utils/Mutex.h>
+#include "ISchedulingPolicyService.h"
+#include "SchedulingPolicyService.h"
+
+namespace android {
+
+static sp<ISchedulingPolicyService> sSchedulingPolicyService;
+static const String16 _scheduling_policy("scheduling_policy");
+static Mutex sMutex;
+
+int requestPriority(pid_t pid, pid_t tid, int32_t prio)
+{
+    // FIXME merge duplicated code related to service lookup, caching, and error recovery
+    sp<ISchedulingPolicyService> sps;
+    for (;;) {
+        sMutex.lock();
+        sps = sSchedulingPolicyService;
+        sMutex.unlock();
+        if (sps != 0) {
+            break;
+        }
+        sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
+        if (binder != 0) {
+            sps = interface_cast<ISchedulingPolicyService>(binder);
+            sMutex.lock();
+            sSchedulingPolicyService = sps;
+            sMutex.unlock();
+            break;
+        }
+        sleep(1);
+    }
+    return sps->requestPriority(pid, tid, prio);
+}
+
+}   // namespace android
diff --git a/services/audioflinger/SchedulingPolicyService.h b/services/audioflinger/SchedulingPolicyService.h
new file mode 100644
index 0000000..7ac8454
--- /dev/null
+++ b/services/audioflinger/SchedulingPolicyService.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2012 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 _ANDROID_SCHEDULING_POLICY_SERVICE_H
+#define _ANDROID_SCHEDULING_POLICY_SERVICE_H
+
+namespace android {
+
+// Request elevated priority for thread tid, whose thread group leader must be pid.
+// The priority parameter is currently restricted to either 1 or 2.
+int requestPriority(pid_t pid, pid_t tid, int32_t prio);
+
+}   // namespace android
+
+#endif  // _ANDROID_SCHEDULING_POLICY_SERVICE_H