1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 /**
19  * This class simulates a hardware JPEG compressor.  It receives image buffers
20  * in RGBA_8888 format, processes them in a worker thread, and then pushes them
21  * out to their destination stream.
22  */
23 
24 #ifndef ANDROID_SERVERS_CAMERA_JPEGCOMPRESSOR_H
25 #define ANDROID_SERVERS_CAMERA_JPEGCOMPRESSOR_H
26 
27 #include "utils/Thread.h"
28 #include "utils/Mutex.h"
29 #include "utils/Timers.h"
30 #include "utils/Vector.h"
31 //#include "Base.h"
32 #include <stdio.h>
33 #include <gui/CpuConsumer.h>
34 
35 extern "C" {
36 #include <jpeglib.h>
37 }
38 
39 
40 namespace android {
41 namespace camera2 {
42 
43 class JpegCompressor: private Thread, public virtual RefBase {
44   public:
45 
46     JpegCompressor();
47     ~JpegCompressor();
48 
49     // Start compressing COMPRESSED format buffers; JpegCompressor takes
50     // ownership of the Buffers vector.
51     status_t start(const Vector<CpuConsumer::LockedBuffer*>& buffers,
52             nsecs_t captureTime);
53 
54     status_t cancel();
55 
56     bool isBusy();
57     bool isStreamInUse(uint32_t id);
58 
59     bool waitForDone(nsecs_t timeout);
60 
61     // TODO: Measure this
62     static const size_t kMaxJpegSize = 300000;
63 
64   private:
65     Mutex mBusyMutex;
66     Mutex mMutex;
67     bool mIsBusy;
68     Condition mDone;
69     nsecs_t mCaptureTime;
70 
71     Vector<CpuConsumer::LockedBuffer*> mBuffers;
72     CpuConsumer::LockedBuffer *mJpegBuffer;
73     CpuConsumer::LockedBuffer *mAuxBuffer;
74 
75     jpeg_compress_struct mCInfo;
76 
77     struct JpegError : public jpeg_error_mgr {
78         JpegCompressor *parent;
79     };
80     j_common_ptr mJpegErrorInfo;
81 
82     struct JpegDestination : public jpeg_destination_mgr {
83         JpegCompressor *parent;
84     };
85 
86     static void jpegErrorHandler(j_common_ptr cinfo);
87 
88     static void jpegInitDestination(j_compress_ptr cinfo);
89     static boolean jpegEmptyOutputBuffer(j_compress_ptr cinfo);
90     static void jpegTermDestination(j_compress_ptr cinfo);
91 
92     bool checkError(const char *msg);
93     void cleanUp();
94 
95     /**
96      * Inherited Thread virtual overrides
97      */
98   private:
99     virtual status_t readyToRun();
100     virtual bool threadLoop();
101 };
102 
103 }; // namespace camera2
104 }; // namespace android
105 
106 #endif
107