Merge "stagefright: allow controlling experiments from property" into mnc-dev
diff --git a/include/media/stagefright/foundation/ADebug.h b/include/media/stagefright/foundation/ADebug.h
index 24df85a..65f415a 100644
--- a/include/media/stagefright/foundation/ADebug.h
+++ b/include/media/stagefright/foundation/ADebug.h
@@ -117,7 +117,7 @@
 
     };
 
-    // parse the property or string to get the debug level for a component name
+    // parse the property or string to get a long-type level for a component name
     // string format is:
     // <level>[:<glob>][,<level>[:<glob>]...]
     // - <level> is 0-5 corresponding to ADebug::Level
@@ -125,10 +125,14 @@
     //   matches all components
     // - string is read left-to-right, and the last matching level is returned, or
     //   the def if no terms matched
+    static long GetLevelFromSettingsString(
+            const char *name, const char *value, long def);
+    static long GetLevelFromProperty(
+            const char *name, const char *value, long def);
+
+    // same for ADebug::Level - performs clamping to valid debug ranges
     static Level GetDebugLevelFromProperty(
             const char *name, const char *propertyName, Level def = kDebugNone);
-    static Level GetDebugLevelFromString(
-            const char *name, const char *value, Level def = kDebugNone);
 
     // remove redundant segments of a codec name, and return a newly allocated
     // string suitable for debugging
diff --git a/media/libstagefright/foundation/ADebug.cpp b/media/libstagefright/foundation/ADebug.cpp
index 0d1cea4..24fa561 100644
--- a/media/libstagefright/foundation/ADebug.cpp
+++ b/media/libstagefright/foundation/ADebug.cpp
@@ -32,11 +32,10 @@
 namespace android {
 
 //static
-ADebug::Level ADebug::GetDebugLevelFromString(
-        const char *name, const char *value, ADebug::Level def) {
+long ADebug::GetLevelFromSettingsString(
+        const char *name, const char *value, long def) {
     // split on ,
     const char *next = value, *current;
-    const unsigned long maxLevel = (unsigned long)kDebugMax;
     while (next != NULL) {
         current = next;
         next = strchr(current, ',');
@@ -52,8 +51,8 @@
 
         // get level
         char *end;
-        errno = 0;  // strtoul does not clear errno, but it can be set for any return value
-        unsigned long level = strtoul(current, &end, 10);
+        errno = 0;  // strtol does not clear errno, but it can be set for any return value
+        long level = strtol(current, &end, 10);
         while (isspace(*end)) {
             ++end;
         }
@@ -77,8 +76,18 @@
             }
         }
 
-        // update debug level
-        def = (Level)min(level, maxLevel);
+        // update value
+        def = level;
+    }
+    return def;
+}
+
+//static
+long ADebug::GetLevelFromProperty(
+        const char *name, const char *propertyName, long def) {
+    char value[PROPERTY_VALUE_MAX];
+    if (property_get(propertyName, value, NULL)) {
+        def = GetLevelFromSettingsString(name, value, def);
     }
     return def;
 }
@@ -86,11 +95,8 @@
 //static
 ADebug::Level ADebug::GetDebugLevelFromProperty(
         const char *name, const char *propertyName, ADebug::Level def) {
-    char value[PROPERTY_VALUE_MAX];
-    if (property_get(propertyName, value, NULL)) {
-        return GetDebugLevelFromString(name, value, def);
-    }
-    return def;
+    long level = GetLevelFromProperty(name, propertyName, (long)def);
+    return (Level)min(max(level, (long)kDebugNone), (long)kDebugMax);
 }
 
 //static
@@ -118,6 +124,15 @@
 bool ADebug::getExperimentFlag(
         bool allow, const char *name, uint64_t modulo,
         uint64_t limit, uint64_t plus, uint64_t timeDivisor) {
+    // see if this experiment should be disabled/enabled based on properties.
+    // default to 2 to allow 0/1 specification
+    const int undefined = 2;
+    long level = GetLevelFromProperty(name, "debug.stagefright.experiments", undefined);
+    if (level != undefined) {
+        ALOGI("experiment '%s': %s from property", name, level ? "ENABLED" : "disabled");
+        return level != 0;
+    }
+
     static volatile int32_t haveSerial = 0;
     static uint64_t serialNum;
     if (!android_atomic_acquire_load(&haveSerial)) {
@@ -138,11 +153,10 @@
                 num = num * 256 + c;
             }
         }
-        ALOGI("got serial");
         serialNum = num;
         android_atomic_release_store(1, &haveSerial);
     }
-    ALOGI("serial: %llu, time: %llu", (long long)serialNum, (long long)time(NULL));
+    ALOGD("serial: %llu, time: %lld", (long long unsigned)serialNum, (long long)time(NULL));
     // MINOR: use modulo for counter and time, so that their sum does not
     // roll over, and mess up the correlation between related experiments.
     // e.g. keep (a mod 2N) = 0 impl (a mod N) = 0
diff --git a/media/libstagefright/tests/Utils_test.cpp b/media/libstagefright/tests/Utils_test.cpp
index c1e663c..d736501 100644
--- a/media/libstagefright/tests/Utils_test.cpp
+++ b/media/libstagefright/tests/Utils_test.cpp
@@ -109,21 +109,21 @@
 
 TEST_F(UtilsTest, TestDebug) {
 #define LVL(x) (ADebug::Level)(x)
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "", LVL(5)), LVL(5));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "   \t  \n ", LVL(2)), LVL(2));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "3", LVL(5)), LVL(3));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "3:*deo", LVL(5)), LVL(3));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString(
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "", LVL(5)), LVL(5));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "   \t  \n ", LVL(2)), LVL(2));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3", LVL(5)), LVL(3));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3:*deo", LVL(5)), LVL(3));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString(
             "video", "\t\n 3 \t\n:\t\n video \t\n", LVL(5)), LVL(3));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "3:*deo,2:vid*", LVL(5)), LVL(2));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString(
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3:*deo,2:vid*", LVL(5)), LVL(2));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString(
             "avideo", "\t\n 3 \t\n:\t\n avideo \t\n,\t\n 2 \t\n:\t\n video \t\n", LVL(5)), LVL(3));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString(
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString(
             "audio.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(2));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString(
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString(
             "video.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("video", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
-    ASSERT_EQ(ADebug::GetDebugLevelFromString("omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(4));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3));
+    ASSERT_EQ(ADebug::GetLevelFromSettingsString("omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(4));
 #undef LVL
 }