forked from google/grafika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraCaptureActivity.java
774 lines (681 loc) · 31.1 KB
/
CameraCaptureActivity.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
* Copyright 2013 Google Inc. All rights reserved.
*
* 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.grafika;
import android.opengl.EGL14;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.widget.Toast;
import com.android.grafika.gles.FullFrameRect;
import com.android.grafika.gles.Texture2dProgram;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
/**
* Shows the camera preview on screen while simultaneously recording it to a .mp4 file.
* <p>
* Every time we receive a frame from the camera, we need to:
* <ul>
* <li>Render the frame to the SurfaceView, on GLSurfaceView's renderer thread.
* <li>Render the frame to the mediacodec's input surface, on the encoder thread, if
* recording is enabled.
* </ul>
* <p>
* At any given time there are four things in motion:
* <ol>
* <li>The UI thread, embodied by this Activity. We must respect -- or work around -- the
* app lifecycle changes. In particular, we need to release and reacquire the Camera
* so that, if the user switches away from us, we're not preventing another app from
* using the camera.
* <li>The Camera, which will busily generate preview frames once we hand it a
* SurfaceTexture. We'll get notifications on the main UI thread unless we define a
* Looper on the thread where the SurfaceTexture is created (the GLSurfaceView renderer
* thread).
* <li>The video encoder thread, embodied by TextureMovieEncoder. This needs to share
* the Camera preview external texture with the GLSurfaceView renderer, which means the
* EGLContext in this thread must be created with a reference to the renderer thread's
* context in hand.
* <li>The GLSurfaceView renderer thread, embodied by CameraSurfaceRenderer. The thread
* is created for us by GLSurfaceView. We don't get callbacks for pause/resume or
* thread startup/shutdown, though we could generate messages from the Activity for most
* of these things. The EGLContext created on this thread must be shared with the
* video encoder, and must be used to create a SurfaceTexture that is used by the
* Camera. As the creator of the SurfaceTexture, it must also be the one to call
* updateTexImage(). The renderer thread is thus at the center of a multi-thread nexus,
* which is a bit awkward since it's the thread we have the least control over.
* </ol>
* <p>
* GLSurfaceView is fairly painful here. Ideally we'd create the video encoder, create
* an EGLContext for it, and pass that into GLSurfaceView to share. The API doesn't allow
* this, so we have to do it the other way around. When GLSurfaceView gets torn down
* (say, because we rotated the device), the EGLContext gets tossed, which means that when
* it comes back we have to re-create the EGLContext used by the video encoder. (And, no,
* the "preserve EGLContext on pause" feature doesn't help.)
* <p>
* We could simplify this quite a bit by using TextureView instead of GLSurfaceView, but that
* comes with a performance hit. We could also have the renderer thread drive the video
* encoder directly, allowing them to work from a single EGLContext, but it's useful to
* decouple the operations, and it's generally unwise to perform disk I/O on the thread that
* renders your UI.
* <p>
* We want to access Camera from the UI thread (setup, teardown) and the renderer thread
* (configure SurfaceTexture, start preview), but the API says you can only access the object
* from a single thread. So we need to pick one thread to own it, and the other thread has to
* access it remotely. Some things are simpler if we let the renderer thread manage it,
* but we'd really like to be sure that Camera is released before we leave onPause(), which
* means we need to make a synchronous call from the UI thread into the renderer thread, which
* we don't really have full control over. It's less scary to have the UI thread own Camera
* and have the renderer call back into the UI thread through the standard Handler mechanism.
* <p>
* (The <a href="http://developer.android.com/training/camera/cameradirect.html#TaskOpenCamera">
* camera docs</a> recommend accessing the camera from a non-UI thread to avoid bogging the
* UI thread down. Since the GLSurfaceView-managed renderer thread isn't a great choice,
* we might want to create a dedicated camera thread. Not doing that here.)
* <p>
* With three threads working simultaneously (plus Camera causing periodic events as frames
* arrive) we have to be very careful when communicating state changes. In general we want
* to send a message to the thread, rather than directly accessing state in the object.
* <p>
*
* <p>
* To exercise the API a bit, the video encoder is required to survive Activity restarts. In the
* current implementation it stops recording but doesn't stop time from advancing, so you'll
* see a pause in the video. (We could adjust the timer to make it seamless, or output a
* "paused" message and hold on that in the recording, or leave the Camera running so it
* continues to generate preview frames while the Activity is paused.) The video encoder object
* is managed as a static property of the Activity.
*/
public class CameraCaptureActivity extends Activity
implements SurfaceTexture.OnFrameAvailableListener, OnItemSelectedListener {
private static final String TAG = MainActivity.TAG;
private static final boolean VERBOSE = false;
// Camera filters; must match up with cameraFilterNames in strings.xml
static final int FILTER_NONE = 0;
static final int FILTER_BLACK_WHITE = 1;
static final int FILTER_BLUR = 2;
static final int FILTER_SHARPEN = 3;
static final int FILTER_EDGE_DETECT = 4;
static final int FILTER_EMBOSS = 5;
private GLSurfaceView mGLView;
private CameraSurfaceRenderer mRenderer;
private Camera mCamera;
private CameraHandler mCameraHandler;
private boolean mRecordingEnabled; // controls button state
private int mCameraPreviewWidth, mCameraPreviewHeight;
// this is static so it survives activity restarts
private static TextureMovieEncoder sVideoEncoder = new TextureMovieEncoder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_capture);
File outputFile = new File(getFilesDir(), "camera-test.mp4");
TextView fileText = (TextView) findViewById(R.id.cameraOutputFile_text);
fileText.setText(outputFile.toString());
Spinner spinner = (Spinner) findViewById(R.id.cameraFilter_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.cameraFilterNames, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
// Define a handler that receives camera-control messages from other threads. All calls
// to Camera must be made on the same thread. Note we create this before the renderer
// thread, so we know the fully-constructed object will be visible.
mCameraHandler = new CameraHandler(this);
mRecordingEnabled = sVideoEncoder.isRecording();
// Configure the GLSurfaceView. This will start the Renderer thread, with an
// appropriate EGL context.
mGLView = (GLSurfaceView) findViewById(R.id.cameraPreview_surfaceView);
mGLView.setEGLContextClientVersion(2); // select GLES 2.0
mRenderer = new CameraSurfaceRenderer(mCameraHandler, sVideoEncoder, outputFile);
mGLView.setRenderer(mRenderer);
mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
Log.d(TAG, "onCreate complete: " + this);
}
@Override
protected void onResume() {
Log.d(TAG, "onResume -- acquiring camera");
super.onResume();
updateControls();
if (PermissionHelper.hasCameraPermission(this)) {
if (mCamera == null) {
openCamera(1280, 720); // updates mCameraPreviewWidth/Height
}
} else {
PermissionHelper.requestCameraPermission(this, false);
}
mGLView.onResume();
mGLView.queueEvent(new Runnable() {
@Override public void run() {
mRenderer.setCameraPreviewSize(mCameraPreviewWidth, mCameraPreviewHeight);
}
});
Log.d(TAG, "onResume complete: " + this);
}
@Override
protected void onPause() {
Log.d(TAG, "onPause -- releasing camera");
super.onPause();
releaseCamera();
mGLView.queueEvent(new Runnable() {
@Override public void run() {
// Tell the renderer that it's about to be paused so it can clean up.
mRenderer.notifyPausing();
}
});
mGLView.onPause();
Log.d(TAG, "onPause complete");
}
@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
mCameraHandler.invalidateHandler(); // paranoia
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (!PermissionHelper.hasCameraPermission(this)) {
Toast.makeText(this,
"Camera permission is needed to run this application", Toast.LENGTH_LONG).show();
PermissionHelper.launchPermissionSettings(this);
finish();
} else {
openCamera(1280, 720); // updates mCameraPreviewWidth/Height
}
}
// spinner selected
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
final int filterNum = spinner.getSelectedItemPosition();
Log.d(TAG, "onItemSelected: " + filterNum);
mGLView.queueEvent(new Runnable() {
@Override public void run() {
// notify the renderer that we want to change the encoder's state
mRenderer.changeFilterMode(filterNum);
}
});
}
@Override public void onNothingSelected(AdapterView<?> parent) {}
/**
* Opens a camera, and attempts to establish preview mode at the specified width and height.
* <p>
* Sets mCameraPreviewWidth and mCameraPreviewHeight to the actual width/height of the preview.
*/
private void openCamera(int desiredWidth, int desiredHeight) {
if (mCamera != null) {
throw new RuntimeException("camera already initialized");
}
Camera.CameraInfo info = new Camera.CameraInfo();
// Try to find a front-facing camera (e.g. for videoconferencing).
int numCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numCameras; i++) {
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mCamera = Camera.open(i);
break;
}
}
if (mCamera == null) {
Log.d(TAG, "No front-facing camera found; opening default");
mCamera = Camera.open(); // opens first back-facing camera
}
if (mCamera == null) {
throw new RuntimeException("Unable to open camera");
}
Camera.Parameters parms = mCamera.getParameters();
CameraUtils.choosePreviewSize(parms, desiredWidth, desiredHeight);
// Give the camera a hint that we're recording video. This can have a big
// impact on frame rate.
parms.setRecordingHint(true);
// leave the frame rate set to default
mCamera.setParameters(parms);
int[] fpsRange = new int[2];
Camera.Size mCameraPreviewSize = parms.getPreviewSize();
parms.getPreviewFpsRange(fpsRange);
String previewFacts = mCameraPreviewSize.width + "x" + mCameraPreviewSize.height;
if (fpsRange[0] == fpsRange[1]) {
previewFacts += " @" + (fpsRange[0] / 1000.0) + "fps";
} else {
previewFacts += " @[" + (fpsRange[0] / 1000.0) +
" - " + (fpsRange[1] / 1000.0) + "] fps";
}
TextView text = (TextView) findViewById(R.id.cameraParams_text);
text.setText(previewFacts);
mCameraPreviewWidth = mCameraPreviewSize.width;
mCameraPreviewHeight = mCameraPreviewSize.height;
AspectFrameLayout layout = (AspectFrameLayout) findViewById(R.id.cameraPreview_afl);
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0) {
mCamera.setDisplayOrientation(90);
layout.setAspectRatio((double) mCameraPreviewHeight / mCameraPreviewWidth);
} else if(display.getRotation() == Surface.ROTATION_270) {
layout.setAspectRatio((double) mCameraPreviewHeight/ mCameraPreviewWidth);
mCamera.setDisplayOrientation(180);
} else {
// Set the preview aspect ratio.
layout.setAspectRatio((double) mCameraPreviewWidth / mCameraPreviewHeight);
}
}
/**
* Stops camera preview, and releases the camera to the system.
*/
private void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
Log.d(TAG, "releaseCamera -- done");
}
}
/**
* onClick handler for "record" button.
*/
public void clickToggleRecording(@SuppressWarnings("unused") View unused) {
mRecordingEnabled = !mRecordingEnabled;
mGLView.queueEvent(new Runnable() {
@Override public void run() {
// notify the renderer that we want to change the encoder's state
mRenderer.changeRecordingState(mRecordingEnabled);
}
});
updateControls();
}
// /**
// * onClick handler for "rebind" checkbox.
// */
// public void clickRebindCheckbox(View unused) {
// CheckBox cb = (CheckBox) findViewById(R.id.rebindHack_checkbox);
// TextureRender.sWorkAroundContextProblem = cb.isChecked();
// }
/**
* Updates the on-screen controls to reflect the current state of the app.
*/
private void updateControls() {
Button toggleRelease = (Button) findViewById(R.id.toggleRecording_button);
int id = mRecordingEnabled ?
R.string.toggleRecordingOff : R.string.toggleRecordingOn;
toggleRelease.setText(id);
//CheckBox cb = (CheckBox) findViewById(R.id.rebindHack_checkbox);
//cb.setChecked(TextureRender.sWorkAroundContextProblem);
}
/**
* Connects the SurfaceTexture to the Camera preview output, and starts the preview.
*/
private void handleSetSurfaceTexture(SurfaceTexture st) {
st.setOnFrameAvailableListener(this);
try {
mCamera.setPreviewTexture(st);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
mCamera.startPreview();
}
@Override
public void onFrameAvailable(SurfaceTexture st) {
// The SurfaceTexture uses this to signal the availability of a new frame. The
// thread that "owns" the external texture associated with the SurfaceTexture (which,
// by virtue of the context being shared, *should* be either one) needs to call
// updateTexImage() to latch the buffer.
//
// Once the buffer is latched, the GLSurfaceView thread can signal the encoder thread.
// This feels backward -- we want recording to be prioritized over rendering -- but
// since recording is only enabled some of the time it's easier to do it this way.
//
// Since GLSurfaceView doesn't establish a Looper, this will *probably* execute on
// the main UI thread. Fortunately, requestRender() can be called from any thread,
// so it doesn't really matter.
if (VERBOSE) Log.d(TAG, "ST onFrameAvailable");
mGLView.requestRender();
}
/**
* Handles camera operation requests from other threads. Necessary because the Camera
* must only be accessed from one thread.
* <p>
* The object is created on the UI thread, and all handlers run there. Messages are
* sent from other threads, using sendMessage().
*/
static class CameraHandler extends Handler {
public static final int MSG_SET_SURFACE_TEXTURE = 0;
// Weak reference to the Activity; only access this from the UI thread.
private WeakReference<CameraCaptureActivity> mWeakActivity;
public CameraHandler(CameraCaptureActivity activity) {
mWeakActivity = new WeakReference<CameraCaptureActivity>(activity);
}
/**
* Drop the reference to the activity. Useful as a paranoid measure to ensure that
* attempts to access a stale Activity through a handler are caught.
*/
public void invalidateHandler() {
mWeakActivity.clear();
}
@Override // runs on UI thread
public void handleMessage(Message inputMessage) {
int what = inputMessage.what;
Log.d(TAG, "CameraHandler [" + this + "]: what=" + what);
CameraCaptureActivity activity = mWeakActivity.get();
if (activity == null) {
Log.w(TAG, "CameraHandler.handleMessage: activity is null");
return;
}
switch (what) {
case MSG_SET_SURFACE_TEXTURE:
activity.handleSetSurfaceTexture((SurfaceTexture) inputMessage.obj);
break;
default:
throw new RuntimeException("unknown msg " + what);
}
}
}
}
/**
* Renderer object for our GLSurfaceView.
* <p>
* Do not call any methods here directly from another thread -- use the
* GLSurfaceView#queueEvent() call.
*/
class CameraSurfaceRenderer implements GLSurfaceView.Renderer {
private static final String TAG = MainActivity.TAG;
private static final boolean VERBOSE = false;
private static final int RECORDING_OFF = 0;
private static final int RECORDING_ON = 1;
private static final int RECORDING_RESUMED = 2;
private CameraCaptureActivity.CameraHandler mCameraHandler;
private TextureMovieEncoder mVideoEncoder;
private File mOutputFile;
private FullFrameRect mFullScreen;
private final float[] mSTMatrix = new float[16];
private int mTextureId;
private SurfaceTexture mSurfaceTexture;
private boolean mRecordingEnabled;
private int mRecordingStatus;
private int mFrameCount;
// width/height of the incoming camera preview frames
private boolean mIncomingSizeUpdated;
private int mIncomingWidth;
private int mIncomingHeight;
private int mCurrentFilter;
private int mNewFilter;
/**
* Constructs CameraSurfaceRenderer.
* <p>
* @param cameraHandler Handler for communicating with UI thread
* @param movieEncoder video encoder object
* @param outputFile output file for encoded video; forwarded to movieEncoder
*/
public CameraSurfaceRenderer(CameraCaptureActivity.CameraHandler cameraHandler,
TextureMovieEncoder movieEncoder, File outputFile) {
mCameraHandler = cameraHandler;
mVideoEncoder = movieEncoder;
mOutputFile = outputFile;
mTextureId = -1;
mRecordingStatus = -1;
mRecordingEnabled = false;
mFrameCount = -1;
mIncomingSizeUpdated = false;
mIncomingWidth = mIncomingHeight = -1;
// We could preserve the old filter mode, but currently not bothering.
mCurrentFilter = -1;
mNewFilter = CameraCaptureActivity.FILTER_NONE;
}
/**
* Notifies the renderer thread that the activity is pausing.
* <p>
* For best results, call this *after* disabling Camera preview.
*/
public void notifyPausing() {
if (mSurfaceTexture != null) {
Log.d(TAG, "renderer pausing -- releasing SurfaceTexture");
mSurfaceTexture.release();
mSurfaceTexture = null;
}
if (mFullScreen != null) {
mFullScreen.release(false); // assume the GLSurfaceView EGL context is about
mFullScreen = null; // to be destroyed
}
mIncomingWidth = mIncomingHeight = -1;
}
/**
* Notifies the renderer that we want to stop or start recording.
*/
public void changeRecordingState(boolean isRecording) {
Log.d(TAG, "changeRecordingState: was " + mRecordingEnabled + " now " + isRecording);
mRecordingEnabled = isRecording;
}
/**
* Changes the filter that we're applying to the camera preview.
*/
public void changeFilterMode(int filter) {
mNewFilter = filter;
}
/**
* Updates the filter program.
*/
public void updateFilter() {
Texture2dProgram.ProgramType programType;
float[] kernel = null;
float colorAdj = 0.0f;
Log.d(TAG, "Updating filter to " + mNewFilter);
switch (mNewFilter) {
case CameraCaptureActivity.FILTER_NONE:
programType = Texture2dProgram.ProgramType.TEXTURE_EXT;
break;
case CameraCaptureActivity.FILTER_BLACK_WHITE:
// (In a previous version the TEXTURE_EXT_BW variant was enabled by a flag called
// ROSE_COLORED_GLASSES, because the shader set the red channel to the B&W color
// and green/blue to zero.)
programType = Texture2dProgram.ProgramType.TEXTURE_EXT_BW;
break;
case CameraCaptureActivity.FILTER_BLUR:
programType = Texture2dProgram.ProgramType.TEXTURE_EXT_FILT;
kernel = new float[] {
1f/16f, 2f/16f, 1f/16f,
2f/16f, 4f/16f, 2f/16f,
1f/16f, 2f/16f, 1f/16f };
break;
case CameraCaptureActivity.FILTER_SHARPEN:
programType = Texture2dProgram.ProgramType.TEXTURE_EXT_FILT;
kernel = new float[] {
0f, -1f, 0f,
-1f, 5f, -1f,
0f, -1f, 0f };
break;
case CameraCaptureActivity.FILTER_EDGE_DETECT:
programType = Texture2dProgram.ProgramType.TEXTURE_EXT_FILT;
kernel = new float[] {
-1f, -1f, -1f,
-1f, 8f, -1f,
-1f, -1f, -1f };
break;
case CameraCaptureActivity.FILTER_EMBOSS:
programType = Texture2dProgram.ProgramType.TEXTURE_EXT_FILT;
kernel = new float[] {
2f, 0f, 0f,
0f, -1f, 0f,
0f, 0f, -1f };
colorAdj = 0.5f;
break;
default:
throw new RuntimeException("Unknown filter mode " + mNewFilter);
}
// Do we need a whole new program? (We want to avoid doing this if we don't have
// too -- compiling a program could be expensive.)
if (programType != mFullScreen.getProgram().getProgramType()) {
mFullScreen.changeProgram(new Texture2dProgram(programType));
// If we created a new program, we need to initialize the texture width/height.
mIncomingSizeUpdated = true;
}
// Update the filter kernel (if any).
if (kernel != null) {
mFullScreen.getProgram().setKernel(kernel, colorAdj);
}
mCurrentFilter = mNewFilter;
}
/**
* Records the size of the incoming camera preview frames.
* <p>
* It's not clear whether this is guaranteed to execute before or after onSurfaceCreated(),
* so we assume it could go either way. (Fortunately they both run on the same thread,
* so we at least know that they won't execute concurrently.)
*/
public void setCameraPreviewSize(int width, int height) {
Log.d(TAG, "setCameraPreviewSize");
mIncomingWidth = width;
mIncomingHeight = height;
mIncomingSizeUpdated = true;
}
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
Log.d(TAG, "onSurfaceCreated");
// We're starting up or coming back. Either way we've got a new EGLContext that will
// need to be shared with the video encoder, so figure out if a recording is already
// in progress.
mRecordingEnabled = mVideoEncoder.isRecording();
if (mRecordingEnabled) {
mRecordingStatus = RECORDING_RESUMED;
} else {
mRecordingStatus = RECORDING_OFF;
}
// Set up the texture blitter that will be used for on-screen display. This
// is *not* applied to the recording, because that uses a separate shader.
mFullScreen = new FullFrameRect(
new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));
mTextureId = mFullScreen.createTextureObject();
// Create a SurfaceTexture, with an external texture, in this EGL context. We don't
// have a Looper in this thread -- GLSurfaceView doesn't create one -- so the frame
// available messages will arrive on the main thread.
mSurfaceTexture = new SurfaceTexture(mTextureId);
// Tell the UI thread to enable the camera preview.
mCameraHandler.sendMessage(mCameraHandler.obtainMessage(
CameraCaptureActivity.CameraHandler.MSG_SET_SURFACE_TEXTURE, mSurfaceTexture));
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
Log.d(TAG, "onSurfaceChanged " + width + "x" + height);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onDrawFrame(GL10 unused) {
if (VERBOSE) Log.d(TAG, "onDrawFrame tex=" + mTextureId);
boolean showBox = false;
// Latch the latest frame. If there isn't anything new, we'll just re-use whatever
// was there before.
mSurfaceTexture.updateTexImage();
// If the recording state is changing, take care of it here. Ideally we wouldn't
// be doing all this in onDrawFrame(), but the EGLContext sharing with GLSurfaceView
// makes it hard to do elsewhere.
if (mRecordingEnabled) {
switch (mRecordingStatus) {
case RECORDING_OFF:
Log.d(TAG, "START recording");
// start recording
mVideoEncoder.startRecording(new TextureMovieEncoder.EncoderConfig(
mOutputFile, 640, 480, 1000000, EGL14.eglGetCurrentContext()));
mRecordingStatus = RECORDING_ON;
break;
case RECORDING_RESUMED:
Log.d(TAG, "RESUME recording");
mVideoEncoder.updateSharedContext(EGL14.eglGetCurrentContext());
mRecordingStatus = RECORDING_ON;
break;
case RECORDING_ON:
// yay
break;
default:
throw new RuntimeException("unknown status " + mRecordingStatus);
}
} else {
switch (mRecordingStatus) {
case RECORDING_ON:
case RECORDING_RESUMED:
// stop recording
Log.d(TAG, "STOP recording");
mVideoEncoder.stopRecording();
mRecordingStatus = RECORDING_OFF;
break;
case RECORDING_OFF:
// yay
break;
default:
throw new RuntimeException("unknown status " + mRecordingStatus);
}
}
// Set the video encoder's texture name. We only need to do this once, but in the
// current implementation it has to happen after the video encoder is started, so
// we just do it here.
//
// TODO: be less lame.
mVideoEncoder.setTextureId(mTextureId);
// Tell the video encoder thread that a new frame is available.
// This will be ignored if we're not actually recording.
mVideoEncoder.frameAvailable(mSurfaceTexture);
if (mIncomingWidth <= 0 || mIncomingHeight <= 0) {
// Texture size isn't set yet. This is only used for the filters, but to be
// safe we can just skip drawing while we wait for the various races to resolve.
// (This seems to happen if you toggle the screen off/on with power button.)
Log.i(TAG, "Drawing before incoming texture size set; skipping");
return;
}
// Update the filter, if necessary.
if (mCurrentFilter != mNewFilter) {
updateFilter();
}
if (mIncomingSizeUpdated) {
mFullScreen.getProgram().setTexSize(mIncomingWidth, mIncomingHeight);
mIncomingSizeUpdated = false;
}
// Draw the video frame.
mSurfaceTexture.getTransformMatrix(mSTMatrix);
mFullScreen.drawFrame(mTextureId, mSTMatrix);
// Draw a flashing box if we're recording. This only appears on screen.
showBox = (mRecordingStatus == RECORDING_ON);
if (showBox && (++mFrameCount & 0x04) == 0) {
drawBox();
}
}
/**
* Draws a red box in the corner.
*/
private void drawBox() {
GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
GLES20.glScissor(0, 0, 100, 100);
GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}
}