Bug 918372 - Allow debugging early Android WebRTC functionality. r=blassey a=lsblakk
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Wed, 25 Sep 2013 08:08:28 +0200
changeset 155557 bc7b5e1c60a2543f79e415fb6a77eb108a69b8ce
parent 155556 c8abbbef2a6785e10bbba79f0a9e36b7f49734da
child 155558 891eaf3a213e096f86bc2c49602f3d1aee8b6185
push id4327
push usergpascutto@mozilla.com
push dateMon, 30 Sep 2013 12:10:44 +0000
treeherdermozilla-aurora@891eaf3a213e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey, lsblakk
bugs918372
milestone26.0a2
Bug 918372 - Allow debugging early Android WebRTC functionality. r=blassey a=lsblakk
media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc
--- a/media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc
+++ b/media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc
@@ -43,175 +43,194 @@ VideoCaptureModule* VideoCaptureImpl::Cr
 
   if (!implementation || implementation->Init(id, deviceUniqueIdUTF8) != 0) {
     delete implementation;
     implementation = NULL;
   }
   return implementation;
 }
 
+#ifdef DEBUG
 // Android logging, uncomment to print trace to
 // logcat instead of trace file/callback
-// #include <android/log.h>
+#include <android/log.h>
 // #undef WEBRTC_TRACE
 // #define WEBRTC_TRACE(a,b,c,...)
 // __android_log_print(ANDROID_LOG_DEBUG, "*WEBRTCN*", __VA_ARGS__)
+// Some functions are called before before the WebRTC logging can be brought up,
+// log those to the Android log.
+#define EARLY_WEBRTC_TRACE(a,b,c,...) __android_log_print(ANDROID_LOG_DEBUG, "*WEBRTC-VCA", __VA_ARGS__)
+#else
+#define EARLY_WEBRTC_TRACE(a,b,c,...)
+#endif
 
 JavaVM* VideoCaptureAndroid::g_jvm = NULL;
 //VideoCaptureAndroid.java
 jclass VideoCaptureAndroid::g_javaCmClass = NULL;
 //VideoCaptureDeviceInfoAndroid.java
 jclass VideoCaptureAndroid::g_javaCmDevInfoClass = NULL;
 //static instance of VideoCaptureDeviceInfoAndroid.java
 jobject VideoCaptureAndroid::g_javaCmDevInfoObject = NULL;
 
 /*
  * Register references to Java Capture class.
  */
 int32_t VideoCaptureAndroid::SetAndroidObjects(void* javaVM,
                                                void* javaContext) {
 
   MOZ_ASSERT(javaVM != nullptr || g_javaCmDevInfoClass != nullptr);
+  EARLY_WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+               "%s: running", __FUNCTION__);
 
   g_jvm = static_cast<JavaVM*> (javaVM);
 
   if (javaVM) {
     // Already done? Exit early.
     if (g_javaCmClass != NULL
         && g_javaCmDevInfoClass != NULL
         && g_javaCmDevInfoObject != NULL) {
+        EARLY_WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+                     "%s: early exit", __FUNCTION__);
         return 0;
     }
 
     JNIEnv* env = NULL;
     if (g_jvm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
-      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: could not get Java environment", __FUNCTION__);
       return -1;
     }
     // get java capture class type (note path to class packet)
     g_javaCmClass = jsjni_GetGlobalClassRef(AndroidJavaCaptureClass);
     if (!g_javaCmClass) {
-      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: could not find java class", __FUNCTION__);
       return -1;
     }
     JNINativeMethod nativeFunctions =
         { "ProvideCameraFrame", "([BIIJ)V",
           (void*) &VideoCaptureAndroid::ProvideCameraFrame };
     if (env->RegisterNatives(g_javaCmClass, &nativeFunctions, 1) == 0) {
-      WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, -1,
                    "%s: Registered native functions", __FUNCTION__);
     }
     else {
-      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: Failed to register native functions",
                    __FUNCTION__);
       return -1;
     }
 
     // get java capture class type (note path to class packet)
     g_javaCmDevInfoClass = jsjni_GetGlobalClassRef(
                  AndroidJavaCaptureDeviceInfoClass);
     if (!g_javaCmDevInfoClass) {
-      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: could not find java class", __FUNCTION__);
       return -1;
     }
 
-    WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, -1,
+    EARLY_WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, -1,
                  "VideoCaptureDeviceInfoAndroid get method id");
 
     // get the method ID for the Android Java CaptureClass static
     //CreateVideoCaptureAndroid factory method.
     jmethodID cid = env->GetStaticMethodID(
         g_javaCmDevInfoClass,
         "CreateVideoCaptureDeviceInfoAndroid",
         "(ILandroid/content/Context;)"
         "Lorg/webrtc/videoengine/VideoCaptureDeviceInfoAndroid;");
     if (cid == NULL) {
-      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: could not get java"
                    "VideoCaptureDeviceInfoAndroid constructor ID",
                    __FUNCTION__);
       return -1;
     }
 
-    WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, -1,
+    EARLY_WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, -1,
                  "%s: construct static java device object", __FUNCTION__);
 
     // construct the object by calling the static constructor object
     jobject javaCameraDeviceInfoObjLocal =
         env->CallStaticObjectMethod(g_javaCmDevInfoClass,
                                     cid, (int) -1,
                                     javaContext);
     if (!javaCameraDeviceInfoObjLocal) {
-      WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, -1,
                    "%s: could not create Java Capture Device info object",
                    __FUNCTION__);
       return -1;
     }
     // create a reference to the object (to tell JNI that
     // we are referencing it after this function has returned)
     g_javaCmDevInfoObject = env->NewGlobalRef(javaCameraDeviceInfoObjLocal);
     if (!g_javaCmDevInfoObject) {
-      WEBRTC_TRACE(webrtc::kTraceError,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError,
                    webrtc::kTraceAudioDevice,
                    -1,
                    "%s: could not create Java"
                    "cameradevinceinfo object reference",
                    __FUNCTION__);
       return -1;
     }
     // Delete local object ref, we only use the global ref
     env->DeleteLocalRef(javaCameraDeviceInfoObjLocal);
+
+    EARLY_WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+                 "%s: success", __FUNCTION__);
     return 0;
   }
   else {
-    WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+    EARLY_WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
                  "%s: JVM is NULL, assuming deinit", __FUNCTION__);
     if (!g_jvm) {
-      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: SetAndroidObjects not called with a valid JVM.",
                    __FUNCTION__);
       return -1;
     }
     JNIEnv* env = NULL;
     bool attached = false;
     if (g_jvm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
       // try to attach the thread and get the env
       // Attach this thread to JVM
       jint res = g_jvm->AttachCurrentThread(&env, NULL);
       if ((res < 0) || !env) {
-        WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture,
+        EARLY_WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture,
                      -1, "%s: Could not attach thread to JVM (%d, %p)",
                      __FUNCTION__, res, env);
         return -1;
       }
       attached = true;
     }
     env->DeleteGlobalRef(g_javaCmDevInfoObject);
     env->DeleteGlobalRef(g_javaCmDevInfoClass);
     env->DeleteGlobalRef(g_javaCmClass);
     if (attached && g_jvm->DetachCurrentThread() < 0) {
-      WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, -1,
+      EARLY_WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, -1,
                    "%s: Could not detach thread from JVM", __FUNCTION__);
       return -1;
     }
     return 0;
     env = (JNIEnv *) NULL;
   }
   return 0;
 }
 
 int32_t VideoCaptureAndroid::AttachAndUseAndroidDeviceInfoObjects(
     JNIEnv*& env,
     jclass& javaCmDevInfoClass,
     jobject& javaCmDevInfoObject,
     bool& attached) {
+
+    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
+                 "%s: AttachAndUseAndroidDeviceInfoObj.",
+                 __FUNCTION__);
+
   // get the JNI env for this thread
   if (!g_jvm) {
     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                  "%s: SetAndroidObjects not called with a valid JVM.",
                  __FUNCTION__);
     return -1;
   }
   attached = false;
@@ -221,31 +240,39 @@ int32_t VideoCaptureAndroid::AttachAndUs
     jint res = g_jvm->AttachCurrentThread(&env, NULL);
     if ((res < 0) || !env) {
       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, -1,
                    "%s: Could not attach thread to JVM (%d, %p)",
                    __FUNCTION__, res, env);
       return -1;
     }
     attached = true;
+    WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+                 "%s: attach success", __FUNCTION__);
+  } else {
+    WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+                 "%s: did not attach because JVM Env present", __FUNCTION__);
   }
   MOZ_ASSERT(g_javaCmDevInfoClass != nullptr);
   MOZ_ASSERT(g_javaCmDevInfoObject != nullptr);
   javaCmDevInfoClass = g_javaCmDevInfoClass;
   javaCmDevInfoObject = g_javaCmDevInfoObject;
   return 0;
 
 }
 
 int32_t VideoCaptureAndroid::ReleaseAndroidDeviceInfoObjects(
     bool attached) {
   if (attached && g_jvm->DetachCurrentThread() < 0) {
     WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, -1,
                  "%s: Could not detach thread from JVM", __FUNCTION__);
     return -1;
+  } else if (!attached) {
+      WEBRTC_TRACE(webrtc::kTraceStateInfo, webrtc::kTraceVideoCapture, -1,
+                   "%s: not attached, no detach", __FUNCTION__);
   }
   return 0;
 }
 
 /*
  * JNI callback from Java class. Called
  * when the camera has a new frame to deliver
  * Class:     org_webrtc_capturemodule_VideoCaptureAndroid