Camera: Maintain reference to shared camera library for the Snapshot Thread

A reference to libqcamera.so is maintained for the duration of the Snapshot thread as LINK_jpeg_encoder_join
should be executed only for the lifetime of this object.

Change-Id: I398c7b6b84f109b1b36976a6bc0f31000b4408a1
diff --git a/QualcommCameraHardware.cpp b/QualcommCameraHardware.cpp
index 4e1c4cc..1722089 100644
--- a/QualcommCameraHardware.cpp
+++ b/QualcommCameraHardware.cpp
@@ -3170,6 +3170,18 @@
 void QualcommCameraHardware::runSnapshotThread(void *data)
 {
     LOGV("runSnapshotThread E");
+#if DLOPEN_LIBMMCAMERA
+    // We need to maintain a reference to libqcamera.so for the duration of the
+    // Snapshot thread, because we do not know when it will exit relative to the
+    // lifetime of this object.  We do not want to dlclose() libqcamera while
+    // LINK_cam_frame is still running.
+    void *libhandle = ::dlopen("liboemcamera.so", RTLD_NOW);
+    LOGV("SNAPSHOT: loading libqcamera at %p", libhandle);
+    if (!libhandle) {
+        LOGE("FATAL ERROR: could not dlopen liboemcamera.so: %s", dlerror());
+    }
+#endif
+
     if(mSnapshotFormat == PICTURE_FORMAT_JPEG){
         if (native_start_snapshot(mCameraControlFd))
             receiveRawPicture();
@@ -3188,7 +3200,6 @@
     mInSnapshotModeWaitLock.unlock();
 
     mSnapshotFormat = 0;
-
     mJpegThreadWaitLock.lock();
     while (mJpegThreadRunning) {
         LOGV("runSnapshotThread: waiting for jpeg thread to complete.");
@@ -3197,13 +3208,24 @@
     }
     mJpegThreadWaitLock.unlock();
     //clear the resources
-    LINK_jpeg_encoder_join();
+#if DLOPEN_LIBMMCAMERA
+    if(libhandle)
+#endif
+    {
+        LINK_jpeg_encoder_join();
+    }
     deinitRaw();
 
     mSnapshotThreadWaitLock.lock();
     mSnapshotThreadRunning = false;
     mSnapshotThreadWait.signal();
     mSnapshotThreadWaitLock.unlock();
+#if DLOPEN_LIBMMCAMERA
+    if (libhandle) {
+        ::dlclose(libhandle);
+        LOGV("SNAPSHOT: dlclose(libqcamera)");
+    }
+#endif
 
     LOGV("runSnapshotThread X");
 }