/
MainActivity.java
353 lines (322 loc) · 9.99 KB
/
MainActivity.java
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* UVCCamera
* library and sample to access to UVC web camera on non-rooted Android device
*
* Copyright (c) 2014-2017 saki t_saki@serenegiant.com
*
* 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.
*
* All files in the folder are under this Apache License, Version 2.0.
* Files in the libjpeg-turbo, libusb, libuvc, rapidjson folder
* may have a different license, see the respective files.
*/
package com.serenegiant.usbcameratest6;
import java.io.File;
import android.graphics.SurfaceTexture;
import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.serenegiant.common.BaseActivity;
import com.serenegiant.encoder.MediaMuxerWrapper;
import com.serenegiant.usb.CameraDialog;
import com.serenegiant.usb.USBMonitor;
import com.serenegiant.usb.USBMonitor.OnDeviceConnectListener;
import com.serenegiant.usb.USBMonitor.UsbControlBlock;
import com.serenegiant.usb.UVCCamera;
import com.serenegiant.usbcameracommon.UVCCameraHandlerMultiSurface;
import com.serenegiant.widget.CameraViewInterface;
import com.serenegiant.widget.UVCCameraTextureView;
public final class MainActivity extends BaseActivity implements CameraDialog.CameraDialogParent {
private static final boolean DEBUG = true; // TODO set false on release
private static final String TAG = "MainActivity";
private final Object mSync = new Object();
/**
* for accessing USB
*/
private USBMonitor mUSBMonitor;
/**
* Handler to execute camera releated methods sequentially on private thread
*/
private UVCCameraHandlerMultiSurface mCameraHandler;
/**
* for camera preview display
*/
private CameraViewInterface mUVCCameraViewL;
private CameraViewInterface mUVCCameraViewR;
/**
* for open&start / stop&close camera preview
*/
private ToggleButton mCameraButton;
/**
* button for start/stop recording
*/
private ImageButton mCaptureButton;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (DEBUG) Log.v(TAG, "onCreate:");
setContentView(R.layout.activity_main);
mCameraButton = (ToggleButton)findViewById(R.id.camera_button);
mCameraButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
mCaptureButton = (ImageButton)findViewById(R.id.capture_button);
mCaptureButton.setOnClickListener(mOnClickListener);
mCaptureButton.setVisibility(View.INVISIBLE);
mUVCCameraViewL = (CameraViewInterface)findViewById(R.id.camera_view_L);
mUVCCameraViewL.setAspectRatio(UVCCamera.DEFAULT_PREVIEW_WIDTH / (float)UVCCamera.DEFAULT_PREVIEW_HEIGHT);
mUVCCameraViewL.setCallback(mCallback);
((View)mUVCCameraViewL).setOnLongClickListener(mOnLongClickListener);
mUVCCameraViewR = (CameraViewInterface)findViewById(R.id.camera_view_R);
mUVCCameraViewR.setAspectRatio(UVCCamera.DEFAULT_PREVIEW_WIDTH / (float)UVCCamera.DEFAULT_PREVIEW_HEIGHT);
mUVCCameraViewR.setCallback(mCallback);
((View)mUVCCameraViewL).setOnLongClickListener(mOnLongClickListener);
synchronized (mSync) {
mUSBMonitor = new USBMonitor(this, mOnDeviceConnectListener);
mCameraHandler = UVCCameraHandlerMultiSurface.createHandler(this, mUVCCameraViewL, 1,
UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT);
}
}
@Override
protected void onStart() {
super.onStart();
if (DEBUG) Log.v(TAG, "onStart:");
synchronized (mSync) {
mUSBMonitor.register();
}
if (mUVCCameraViewL != null) {
mUVCCameraViewL.onResume();
}
if (mUVCCameraViewR != null) {
mUVCCameraViewR.onResume();
}
}
@Override
protected void onStop() {
if (DEBUG) Log.v(TAG, "onStop:");
synchronized (mSync) {
// mCameraHandler.stopRecording();
// mCameraHandler.stopPreview();
mCameraHandler.close(); // #close include #stopRecording and #stopPreview
mUSBMonitor.unregister();
}
if (mUVCCameraViewL != null) {
mUVCCameraViewL.onPause();
}
if (mUVCCameraViewR != null) {
mUVCCameraViewR.onPause();
}
setCameraButton(false);
super.onStop();
}
@Override
public void onDestroy() {
if (DEBUG) Log.v(TAG, "onDestroy:");
synchronized (mSync) {
if (mCameraHandler != null) {
mCameraHandler.release();
mCameraHandler = null;
}
if (mUSBMonitor != null) {
mUSBMonitor.destroy();
mUSBMonitor = null;
}
}
mUVCCameraViewL = null;
mUVCCameraViewR = null;
mCameraButton = null;
mCaptureButton = null;
super.onDestroy();
}
/**
* event handler when click camera / capture button
*/
private final OnClickListener mOnClickListener = new OnClickListener() {
@Override
public void onClick(final View view) {
switch (view.getId()) {
case R.id.capture_button:
synchronized (mSync) {
if ((mCameraHandler != null) && mCameraHandler.isOpened()) {
if (checkPermissionWriteExternalStorage() && checkPermissionAudio()) {
if (!mCameraHandler.isRecording()) {
mCaptureButton.setColorFilter(0xffff0000); // turn red
mCameraHandler.startRecording();
} else {
mCaptureButton.setColorFilter(0); // return to default color
mCameraHandler.stopRecording();
}
}
}
}
break;
}
}
};
private final CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener
= new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.camera_button:
synchronized (mSync) {
if (isChecked && (mCameraHandler != null) && !mCameraHandler.isOpened()) {
CameraDialog.showDialog(MainActivity.this);
} else {
mCameraHandler.close();
setCameraButton(false);
}
}
break;
}
}
};
/**
* capture still image when you long click on preview image(not on buttons)
*/
private final OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
@Override
public boolean onLongClick(final View view) {
switch (view.getId()) {
case R.id.camera_view_L:
case R.id.camera_view_R:
synchronized (mSync) {
if ((mCameraHandler != null) && mCameraHandler.isOpened()) {
if (checkPermissionWriteExternalStorage()) {
final File outputFile = MediaMuxerWrapper.getCaptureFile(Environment.DIRECTORY_DCIM, ".png");
mCameraHandler.captureStill(outputFile.toString());
}
return true;
}
}
}
return false;
}
};
private void setCameraButton(final boolean isOn) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCameraButton != null) {
try {
mCameraButton.setOnCheckedChangeListener(null);
mCameraButton.setChecked(isOn);
} finally {
mCameraButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
}
if (!isOn && (mCaptureButton != null)) {
mCaptureButton.setVisibility(View.INVISIBLE);
}
}
}, 0);
}
private void startPreview() {
synchronized (mSync) {
if (mCameraHandler != null) {
mCameraHandler.startPreview();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mCaptureButton.setVisibility(View.VISIBLE);
}
});
}
private final OnDeviceConnectListener mOnDeviceConnectListener = new OnDeviceConnectListener() {
@Override
public void onAttach(final UsbDevice device) {
Toast.makeText(MainActivity.this, "USB_DEVICE_ATTACHED", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnect(final UsbDevice device, final UsbControlBlock ctrlBlock, final boolean createNew) {
if (DEBUG) Log.v(TAG, "onConnect:");
synchronized (mSync) {
if (mCameraHandler != null) {
mCameraHandler.open(ctrlBlock);
startPreview();
}
}
}
@Override
public void onDisconnect(final UsbDevice device, final UsbControlBlock ctrlBlock) {
if (DEBUG) Log.v(TAG, "onDisconnect:");
synchronized (mSync) {
if (mCameraHandler != null) {
queueEvent(new Runnable() {
@Override
public void run() {
synchronized (mSync) {
if (mCameraHandler != null) {
mCameraHandler.close();
}
}
}
}, 0);
}
}
setCameraButton(false);
}
@Override
public void onDettach(final UsbDevice device) {
Toast.makeText(MainActivity.this, "USB_DEVICE_DETACHED", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(final UsbDevice device) {
setCameraButton(false);
}
};
/**
* to access from CameraDialog
* @return
*/
@Override
public USBMonitor getUSBMonitor() {
synchronized (mSync) {
return mUSBMonitor;
}
}
@Override
public void onDialogResult(boolean canceled) {
if (canceled) {
setCameraButton(false);
}
}
private final CameraViewInterface.Callback
mCallback = new CameraViewInterface.Callback() {
@Override
public void onSurfaceCreated(final CameraViewInterface view, final Surface surface) {
mCameraHandler.addSurface(surface.hashCode(), surface, false);
}
@Override
public void onSurfaceChanged(final CameraViewInterface view, final Surface surface, final int width, final int height) {
}
@Override
public void onSurfaceDestroy(final CameraViewInterface view, final Surface surface) {
synchronized (mSync) {
if (mCameraHandler != null) {
mCameraHandler.removeSurface(surface.hashCode());
}
}
}
};
}