1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mediaframeworktest.functional;
import com.android.mediaframeworktest.MediaFrameworkTest;
import com.android.mediaframeworktest.MediaNames;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.ConditionVariable;
import android.os.Environment;
import android.os.Looper;
import android.test.ActivityInstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import android.view.SurfaceHolder;
import java.io.*;
/**
* Junit / Instrumentation test case for the camera api
*/
public class CameraTest extends ActivityInstrumentationTestCase<MediaFrameworkTest> {
private String TAG = "CameraTest";
private boolean rawPreviewCallbackResult = false;
private boolean shutterCallbackResult = false;
private boolean rawPictureCallbackResult = false;
private boolean jpegPictureCallbackResult = false;
private static int WAIT_FOR_COMMAND_TO_COMPLETE = 10000; // Milliseconds.
private static final int CAMERA_ID = 0;
private RawPreviewCallback mRawPreviewCallback = new RawPreviewCallback();
private TestShutterCallback mShutterCallback = new TestShutterCallback();
private RawPictureCallback mRawPictureCallback = new RawPictureCallback();
private JpegPictureCallback mJpegPictureCallback = new JpegPictureCallback();
private boolean mInitialized = false;
private Looper mLooper = null;
private final ConditionVariable mPreviewDone = new ConditionVariable();
private final ConditionVariable mSnapshotDone = new ConditionVariable();
Camera mCamera;
Context mContext;
public CameraTest() {
super("com.android.mediaframeworktest", MediaFrameworkTest.class);
}
protected void setUp() throws Exception {
super.setUp();
}
/*
* Initializes the message looper so that the Camera object can
* receive the callback messages.
*/
private void initializeMessageLooper() {
final ConditionVariable startDone = new ConditionVariable();
Log.v(TAG, "start looper");
new Thread() {
@Override
public void run() {
// Set up a looper to be used by camera.
Looper.prepare();
Log.v(TAG, "start loopRun");
// Save the looper so that we can terminate this thread
// after we are done with it.
mLooper = Looper.myLooper();
mCamera = Camera.open(CAMERA_ID);
startDone.open();
Looper.loop(); // Blocks forever until Looper.quit() is called.
Log.v(TAG, "initializeMessageLooper: quit.");
}
}.start();
if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
fail("initializeMessageLooper: start timeout");
}
}
/*
* Terminates the message looper thread.
*/
private void terminateMessageLooper() throws Exception {
mLooper.quit();
// Looper.quit() is asynchronous. The looper may still has some
// preview callbacks in the queue after quit is called. The preview
// callback still uses the camera object (setHasPreviewCallback).
// After camera is released, RuntimeException will be thrown from
// the method. So we need to join the looper thread here.
mLooper.getThread().join();
mCamera.release();
}
//Implement the previewCallback
private final class RawPreviewCallback implements PreviewCallback {
public void onPreviewFrame(byte [] rawData, Camera camera) {
Log.v(TAG, "Preview callback start");
int rawDataLength = 0;
if (rawData != null) {
rawDataLength = rawData.length;
}
if (rawDataLength > 0) {
rawPreviewCallbackResult = true;
} else {
rawPreviewCallbackResult = false;
}
mPreviewDone.open();
Log.v(TAG, "Preview callback stop");
}
};
//Implement the shutterCallback
private final class TestShutterCallback implements ShutterCallback {
public void onShutter() {
shutterCallbackResult = true;
Log.v(TAG, "onShutter called");
}
};
//Implement the RawPictureCallback
private final class RawPictureCallback implements PictureCallback {
public void onPictureTaken(byte [] rawData, Camera camera) {
// no support for raw data - success if we get the callback
rawPictureCallbackResult = true;
Log.v(TAG, "RawPictureCallback callback");
}
};
//Implement the JpegPictureCallback
private final class JpegPictureCallback implements PictureCallback {
public void onPictureTaken(byte [] rawData, Camera camera) {
try {
if (rawData != null) {
int rawDataLength = rawData.length;
File rawoutput = new File(
Environment.getExternalStorageDirectory().toString(), "/test.bmp");
FileOutputStream outstream = new FileOutputStream(rawoutput);
outstream.write(rawData);
Log.v(TAG, "JpegPictureCallback rawDataLength = " + rawDataLength);
jpegPictureCallbackResult = true;
} else {
jpegPictureCallbackResult = false;
}
mSnapshotDone.open();
Log.v(TAG, "Jpeg Picture callback");
} catch (Exception e) {
Log.v(TAG, e.toString());
}
}
};
private void waitForPreviewDone() {
if (!mPreviewDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
Log.v(TAG, "waitForPreviewDone: timeout");
}
mPreviewDone.close();
}
private void waitForSnapshotDone() {
if (!mSnapshotDone.block(MediaNames.WAIT_SNAPSHOT_TIME)) {
// timeout could be expected or unexpected. The caller will decide.
Log.v(TAG, "waitForSnapshotDone: timeout");
}
mSnapshotDone.close();
}
private void checkTakePicture() {
SurfaceHolder mSurfaceHolder;
try {
mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
mCamera.setPreviewDisplay(mSurfaceHolder);
Log.v(TAG, "Start preview");
mCamera.startPreview();
waitForPreviewDone();
mCamera.setPreviewCallback(null);
mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback);
waitForSnapshotDone();
} catch (Exception e) {
Log.v(TAG, e.toString());
}
}
private void checkPreviewCallback() {
SurfaceHolder mSurfaceHolder;
try {
mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
mCamera.setPreviewDisplay(mSurfaceHolder);
Log.v(TAG, "start preview");
mCamera.startPreview();
waitForPreviewDone();
mCamera.setPreviewCallback(null);
} catch (Exception e) {
Log.v(TAG, e.toString());
}
}
/*
* TODO(yslau): Need to setup the golden rawData and compare the
* the new captured rawData with the golden one.
*
* Test case 1: Take a picture and verify all the callback
* functions are called properly.
*/
@LargeTest
public void testTakePicture() throws Exception {
initializeMessageLooper();
mCamera.setPreviewCallback(mRawPreviewCallback);
checkTakePicture();
terminateMessageLooper();
assertTrue("shutterCallbackResult", shutterCallbackResult);
assertTrue("rawPictureCallbackResult", rawPictureCallbackResult);
assertTrue("jpegPictureCallbackResult", jpegPictureCallbackResult);
}
/*
* Test case 2: Set the preview and
* verify the RawPreviewCallback is called
*/
@LargeTest
public void testCheckPreview() throws Exception {
initializeMessageLooper();
mCamera.setPreviewCallback(mRawPreviewCallback);
checkPreviewCallback();
terminateMessageLooper();
assertTrue("RawPreviewCallbackResult", rawPreviewCallbackResult);
}
}
|