omap4xxx: camera: cancel and flush for SW JPEG encoder

Fixes b/5378650

1. Add method to externally cancel Encoder class while in the middle of
   encoding.
2. Add queue in AppCallbackNotifier to track all the encoding sessions. When
   stopping AppCallbackNotifier if any encoding sessions are still ongoing,
   cancel and wait for the threads to return. Previously, it was possible for
   Encoder threads to still be running when the preview and images buffers were
   freed.

Change-Id: Ib123d1644dfa7058a6f50f0001b4d05359853827
Signed-off-by: Tyler Luu <tluu@ti.com>
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/camera/Encoder_libjpeg.cpp b/camera/Encoder_libjpeg.cpp
index 23058d3..152e3cc 100644
--- a/camera/Encoder_libjpeg.cpp
+++ b/camera/Encoder_libjpeg.cpp
@@ -367,7 +367,7 @@
     row_src = src;
     row_uv = src + out_width * out_height * bpp;
 
-    while (cinfo.next_scanline < cinfo.image_height) {
+    while ((cinfo.next_scanline < cinfo.image_height) && !mCancelEncoding) {
         JSAMPROW row[1];    /* pointer to JSAMPLE row[s] */
 
         // convert input yuv format to yuv444
@@ -388,7 +388,10 @@
         }
     }
 
-    jpeg_finish_compress(&cinfo);
+    // no need to finish encoding routine if we are prematurely stopping
+    // we will end up crashing in dest_mgr since data is incomplete
+    if (!mCancelEncoding)
+        jpeg_finish_compress(&cinfo);
     jpeg_destroy_compress(&cinfo);
 
     if (resize_src) free(resize_src);