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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
|
/*
* Copyright (C) 2011 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.camera;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.filterfw.GraphEnvironment;
import android.filterfw.core.Filter;
import android.filterfw.core.GLEnvironment;
import android.filterfw.core.GraphRunner;
import android.filterfw.core.GraphRunner.OnRunnerDoneListener;
import android.filterfw.geometry.Point;
import android.filterfw.geometry.Quad;
import android.filterpacks.videoproc.BackDropperFilter;
import android.filterpacks.videoproc.BackDropperFilter.LearningDoneListener;
import android.filterpacks.videosink.MediaEncoderFilter.OnRecordingDoneListener;
import android.filterpacks.videosrc.SurfaceTextureSource.SurfaceTextureSourceListener;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.media.CamcorderProfile;
import android.os.ConditionVariable;
import android.os.Handler;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import android.os.SystemProperties;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.File;
import java.lang.Runnable;
import java.io.FileDescriptor;
/**
* Encapsulates the mobile filter framework components needed to record video with
* effects applied. Modeled after MediaRecorder.
*/
public class EffectsRecorder {
public static final int EFFECT_NONE = 0;
public static final int EFFECT_GOOFY_FACE = 1;
public static final int EFFECT_BACKDROPPER = 2;
public static final int EFFECT_GF_SQUEEZE = 0;
public static final int EFFECT_GF_BIG_EYES = 1;
public static final int EFFECT_GF_BIG_MOUTH = 2;
public static final int EFFECT_GF_SMALL_MOUTH = 3;
public static final int EFFECT_GF_BIG_NOSE = 4;
public static final int EFFECT_GF_SMALL_EYES = 5;
public static final int NUM_OF_GF_EFFECTS = EFFECT_GF_SMALL_EYES + 1;
public static final int EFFECT_MSG_STARTED_LEARNING = 0;
public static final int EFFECT_MSG_DONE_LEARNING = 1;
public static final int EFFECT_MSG_SWITCHING_EFFECT = 2;
public static final int EFFECT_MSG_EFFECTS_STOPPED = 3;
public static final int EFFECT_MSG_RECORDING_DONE = 4;
private Context mContext;
private Handler mHandler;
private boolean mReleased;
private Camera mCameraDevice;
private CamcorderProfile mProfile;
private double mCaptureRate = 0;
private SurfaceHolder mPreviewSurfaceHolder;
private int mPreviewWidth;
private int mPreviewHeight;
private MediaRecorder.OnInfoListener mInfoListener;
private MediaRecorder.OnErrorListener mErrorListener;
private String mOutputFile;
private FileDescriptor mFd;
private int mOrientationHint = 0;
private long mMaxFileSize = 0;
private int mCameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;
private int mEffect = EFFECT_NONE;
private int mCurrentEffect = EFFECT_NONE;
private EffectsListener mEffectsListener;
private Object mEffectParameter;
private GraphEnvironment mGraphEnv;
private int mGraphId;
private GraphRunner mRunner = null;
private GraphRunner mOldRunner = null;
private SurfaceTexture mTextureSource;
private static final String mVideoRecordSound = "/system/media/audio/ui/VideoRecord.ogg";
private SoundPlayer mRecordSound;
private static final int STATE_CONFIGURE = 0;
private static final int STATE_WAITING_FOR_SURFACE = 1;
private static final int STATE_PREVIEW = 2;
private static final int STATE_RECORD = 3;
private static final int STATE_RELEASED = 4;
private int mState = STATE_CONFIGURE;
private boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
private static final String TAG = "effectsrecorder";
/** Determine if a given effect is supported at runtime
* Some effects require libraries not available on all devices
*/
public static boolean isEffectSupported(int effectId) {
switch (effectId) {
case EFFECT_GOOFY_FACE:
return Filter.isAvailable("com.google.android.filterpacks.facedetect.GoofyRenderFilter");
case EFFECT_BACKDROPPER:
return Filter.isAvailable("android.filterpacks.videoproc.BackDropperFilter");
default:
return false;
}
}
public EffectsRecorder(Context context) {
if (mLogVerbose) Log.v(TAG, "EffectsRecorder created (" + this + ")");
mContext = context;
mHandler = new Handler(Looper.getMainLooper());
// Construct sound player; use enforced sound output if necessary
File recordSoundFile = new File(mVideoRecordSound);
try {
ParcelFileDescriptor recordSoundParcel =
ParcelFileDescriptor.open(recordSoundFile,
ParcelFileDescriptor.MODE_READ_ONLY);
AssetFileDescriptor recordSoundAsset =
new AssetFileDescriptor(recordSoundParcel, 0,
AssetFileDescriptor.UNKNOWN_LENGTH);
if (SystemProperties.get("ro.camera.sound.forced", "0").equals("0")) {
if (mLogVerbose) Log.v(TAG, "Standard recording sound");
mRecordSound = new SoundPlayer(recordSoundAsset, false);
} else {
if (mLogVerbose) Log.v(TAG, "Forced recording sound");
mRecordSound = new SoundPlayer(recordSoundAsset, true);
}
} catch (java.io.FileNotFoundException e) {
Log.e(TAG, "System video record sound not found");
mRecordSound = null;
}
}
public void setCamera(Camera cameraDevice) {
switch (mState) {
case STATE_PREVIEW:
throw new RuntimeException("setCamera cannot be called while previewing!");
case STATE_RECORD:
throw new RuntimeException("setCamera cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setCamera called on an already released recorder!");
default:
break;
}
mCameraDevice = cameraDevice;
}
public void setProfile(CamcorderProfile profile) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setProfile cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setProfile called on an already released recorder!");
default:
break;
}
mProfile = profile;
}
public void setOutputFile(String outputFile) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setOutputFile cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setOutputFile called on an already released recorder!");
default:
break;
}
mOutputFile = outputFile;
mFd = null;
}
public void setOutputFile(FileDescriptor fd) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setOutputFile cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setOutputFile called on an already released recorder!");
default:
break;
}
mOutputFile = null;
mFd = fd;
}
/**
* Sets the maximum filesize (in bytes) of the recording session.
* This will be passed on to the MediaEncoderFilter and then to the
* MediaRecorder ultimately. If zero or negative, the MediaRecorder will
* disable the limit
*/
public synchronized void setMaxFileSize(long maxFileSize) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setMaxFileSize cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setMaxFileSize called on an already released recorder!");
default:
break;
}
mMaxFileSize = maxFileSize;
}
public void setCaptureRate(double fps) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setCaptureRate cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setCaptureRate called on an already released recorder!");
default:
break;
}
if (mLogVerbose) Log.v(TAG, "Setting time lapse capture rate to " + fps + " fps");
mCaptureRate = fps;
}
public void setPreviewDisplay(SurfaceHolder previewSurfaceHolder,
int previewWidth,
int previewHeight) {
if (mLogVerbose) Log.v(TAG, "setPreviewDisplay (" + this + ")");
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setPreviewDisplay cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setPreviewDisplay called on an already released recorder!");
default:
break;
}
mPreviewSurfaceHolder = previewSurfaceHolder;
mPreviewWidth = previewWidth;
mPreviewHeight = previewHeight;
switch (mState) {
case STATE_WAITING_FOR_SURFACE:
startPreview();
break;
case STATE_PREVIEW:
initializeEffect(true);
break;
}
}
public void setEffect(int effect, Object effectParameter) {
if (mLogVerbose) Log.v(TAG,
"setEffect: effect ID " + effect +
", parameter " + effectParameter.toString() );
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setEffect cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setEffect called on an already released recorder!");
default:
break;
}
mEffect = effect;
mEffectParameter = effectParameter;
if (mState == STATE_PREVIEW) {
initializeEffect(false);
}
}
public interface EffectsListener {
public void onEffectsUpdate(int effectId, int effectMsg);
public void onEffectsError(Exception exception, String filePath);
}
public void setEffectsListener(EffectsListener listener) {
mEffectsListener = listener;
}
private void setFaceDetectOrientation(int degrees) {
if (mCurrentEffect == EFFECT_GOOFY_FACE) {
Filter rotateFilter = mRunner.getGraph().getFilter("rotate");
Filter metaRotateFilter = mRunner.getGraph().getFilter("metarotate");
rotateFilter.setInputValue("rotation", degrees);
int reverseDegrees = (360 - degrees) % 360;
metaRotateFilter.setInputValue("rotation", reverseDegrees);
}
}
private void setRecordingOrientation() {
if ( mState <= STATE_PREVIEW && mRunner != null ) {
Point bl = new Point(0, 0);
Point br = new Point(1, 0);
Point tl = new Point(0, 1);
Point tr = new Point(1, 1);
Quad recordingRegion;
if (mCameraFacing == Camera.CameraInfo.CAMERA_FACING_BACK) {
// The back camera is not mirrored, so use a identity transform
recordingRegion = new Quad(bl, br, tl, tr);
} else {
// Recording region needs to be tweaked for front cameras, since they
// mirror their preview
if (mOrientationHint == 0 || mOrientationHint == 180) {
// Horizontal flip in landscape
recordingRegion = new Quad(br, bl, tr, tl);
} else {
// Horizontal flip in portrait
recordingRegion = new Quad(tl, tr, bl, br);
}
}
Filter recorder = mRunner.getGraph().getFilter("recorder");
recorder.setInputValue("inputRegion", recordingRegion);
}
}
public void setOrientationHint(int degrees) {
switch (mState) {
case STATE_RELEASED:
throw new RuntimeException(
"setOrientationHint called on an already released recorder!");
default:
break;
}
if (mLogVerbose) Log.v(TAG, "Setting orientation hint to: " + degrees);
mOrientationHint = degrees;
setFaceDetectOrientation(degrees);
setRecordingOrientation();
}
public void setCameraFacing(int facing) {
switch (mState) {
case STATE_RELEASED:
throw new RuntimeException(
"setCameraFacing called on alrady released recorder!");
default:
break;
}
mCameraFacing = facing;
setRecordingOrientation();
}
public void setOnInfoListener(MediaRecorder.OnInfoListener infoListener) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setInfoListener cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setInfoListener called on an already released recorder!");
default:
break;
}
mInfoListener = infoListener;
}
public void setOnErrorListener(MediaRecorder.OnErrorListener errorListener) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setErrorListener cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setErrorListener called on an already released recorder!");
default:
break;
}
mErrorListener = errorListener;
}
private void initializeFilterFramework() {
mGraphEnv = new GraphEnvironment();
mGraphEnv.createGLEnvironment();
if (mLogVerbose) {
Log.v(TAG, "Effects framework initializing. Recording size "
+ mProfile.videoFrameWidth + ", " + mProfile.videoFrameHeight);
}
mGraphEnv.addReferences(
"textureSourceCallback", mSourceReadyCallback,
"recordingWidth", mProfile.videoFrameWidth,
"recordingHeight", mProfile.videoFrameHeight,
"recordingProfile", mProfile,
"learningDoneListener", mLearningDoneListener,
"recordingDoneListener", mRecordingDoneListener);
mRunner = null;
mGraphId = -1;
mCurrentEffect = EFFECT_NONE;
}
private synchronized void initializeEffect(boolean forceReset) {
if (forceReset ||
mCurrentEffect != mEffect ||
mCurrentEffect == EFFECT_BACKDROPPER) {
if (mLogVerbose) {
Log.v(TAG, "Effect initializing. Preview size "
+ mPreviewWidth + ", " + mPreviewHeight);
}
mGraphEnv.addReferences(
"previewSurface", mPreviewSurfaceHolder.getSurface(),
"previewWidth", mPreviewWidth,
"previewHeight", mPreviewHeight,
"orientation", mOrientationHint);
if (mState == STATE_PREVIEW) {
// Switching effects while running. Inform video camera.
sendMessage(mCurrentEffect, EFFECT_MSG_SWITCHING_EFFECT);
}
switch (mEffect) {
case EFFECT_GOOFY_FACE:
mGraphId = mGraphEnv.loadGraph(mContext, R.raw.goofy_face);
break;
case EFFECT_BACKDROPPER:
sendMessage(EFFECT_BACKDROPPER, EFFECT_MSG_STARTED_LEARNING);
mGraphId = mGraphEnv.loadGraph(mContext, R.raw.backdropper);
break;
default:
throw new RuntimeException("Unknown effect ID" + mEffect + "!");
}
mCurrentEffect = mEffect;
mOldRunner = mRunner;
mRunner = mGraphEnv.getRunner(mGraphId, GraphEnvironment.MODE_ASYNCHRONOUS);
mRunner.setDoneCallback(mRunnerDoneCallback);
if (mLogVerbose) {
Log.v(TAG, "New runner: " + mRunner
+ ". Old runner: " + mOldRunner);
}
if (mState == STATE_PREVIEW) {
// Switching effects while running. Stop existing runner.
// The stop callback will take care of starting new runner.
mCameraDevice.stopPreview();
try {
mCameraDevice.setPreviewTexture(null);
} catch(IOException e) {
throw new RuntimeException("Unable to connect camera to effect input", e);
}
mOldRunner.stop();
}
}
switch (mCurrentEffect) {
case EFFECT_GOOFY_FACE:
tryEnableVideoStabilization(true);
Filter goofyFilter = mRunner.getGraph().getFilter("goofyrenderer");
goofyFilter.setInputValue("currentEffect",
((Integer)mEffectParameter).intValue());
break;
case EFFECT_BACKDROPPER:
tryEnableVideoStabilization(false);
Filter backgroundSrc = mRunner.getGraph().getFilter("background");
backgroundSrc.setInputValue("sourceUrl",
(String)mEffectParameter);
break;
default:
break;
}
setFaceDetectOrientation(mOrientationHint);
setRecordingOrientation();
}
public synchronized void startPreview() {
if (mLogVerbose) Log.v(TAG, "Starting preview (" + this + ")");
switch (mState) {
case STATE_PREVIEW:
// Already running preview
Log.w(TAG, "startPreview called when already running preview");
return;
case STATE_RECORD:
throw new RuntimeException("Cannot start preview when already recording!");
case STATE_RELEASED:
throw new RuntimeException("setEffect called on an already released recorder!");
default:
break;
}
if (mEffect == EFFECT_NONE) {
throw new RuntimeException("No effect selected!");
}
if (mEffectParameter == null) {
throw new RuntimeException("No effect parameter provided!");
}
if (mProfile == null) {
throw new RuntimeException("No recording profile provided!");
}
if (mPreviewSurfaceHolder == null) {
if (mLogVerbose) Log.v(TAG, "Passed a null surface holder; waiting for valid one");
mState = STATE_WAITING_FOR_SURFACE;
return;
}
if (mCameraDevice == null) {
throw new RuntimeException("No camera to record from!");
}
if (mLogVerbose) Log.v(TAG, "Initializing filter graph");
initializeFilterFramework();
initializeEffect(true);
if (mLogVerbose) Log.v(TAG, "Starting filter graph");
mRunner.run();
// Rest of preview startup handled in mSourceReadyCallback
}
private SurfaceTextureSourceListener mSourceReadyCallback =
new SurfaceTextureSourceListener() {
public void onSurfaceTextureSourceReady(SurfaceTexture source) {
if (mLogVerbose) Log.v(TAG, "SurfaceTexture ready callback received");
synchronized(EffectsRecorder.this) {
mTextureSource = source;
// When shutting down a graph, we receive a null SurfaceTexture to
// indicate that. Don't want to connect up the camera in that case.
if (source == null) return;
if (mState == STATE_RELEASED) return;
// Lock AE/AWB to reduce transition flicker
tryEnable3ALocks(true);
mCameraDevice.stopPreview();
if (mLogVerbose) Log.v(TAG, "Runner active, connecting effects preview");
try {
mCameraDevice.setPreviewTexture(mTextureSource);
} catch(IOException e) {
throw new RuntimeException("Unable to connect camera to effect input", e);
}
mCameraDevice.startPreview();
// Unlock AE/AWB after preview started
tryEnable3ALocks(false);
mState = STATE_PREVIEW;
if (mLogVerbose) Log.v(TAG, "Start preview/effect switch complete");
}
}
};
private LearningDoneListener mLearningDoneListener =
new LearningDoneListener() {
public void onLearningDone(BackDropperFilter filter) {
if (mLogVerbose) Log.v(TAG, "Learning done callback triggered");
// Called in a processing thread, so have to post message back to UI
// thread
sendMessage(EFFECT_BACKDROPPER, EFFECT_MSG_DONE_LEARNING);
enable3ALocks(true);
}
};
// A callback to finalize the media after the recording is done.
private OnRecordingDoneListener mRecordingDoneListener =
new OnRecordingDoneListener() {
// Forward the callback to the VideoCamera object (as an asynchronous event).
public void onRecordingDone() {
if (mLogVerbose) Log.v(TAG, "Recording done callback triggered");
sendMessage(EFFECT_NONE, EFFECT_MSG_RECORDING_DONE);
}
};
public synchronized void startRecording() {
if (mLogVerbose) Log.v(TAG, "Starting recording (" + this + ")");
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("Already recording, cannot begin anew!");
case STATE_RELEASED:
throw new RuntimeException("startRecording called on an already released recorder!");
default:
break;
}
if ((mOutputFile == null) && (mFd == null)) {
throw new RuntimeException("No output file name or descriptor provided!");
}
if (mState == STATE_CONFIGURE) {
startPreview();
}
Filter recorder = mRunner.getGraph().getFilter("recorder");
if (mFd != null) {
recorder.setInputValue("outputFileDescriptor", mFd);
} else {
recorder.setInputValue("outputFile", mOutputFile);
}
// It is ok to set the audiosource without checking for timelapse here
// since that check will be done in the MediaEncoderFilter itself
recorder.setInputValue("audioSource", MediaRecorder.AudioSource.CAMCORDER);
recorder.setInputValue("recordingProfile", mProfile);
recorder.setInputValue("orientationHint", mOrientationHint);
// Important to set the timelapseinterval to 0 if the capture rate is not >0
// since the recorder does not get created every time the recording starts.
// The recorder infers whether the capture is timelapsed based on the value of
// this interval
boolean captureTimeLapse = mCaptureRate > 0;
if (captureTimeLapse) {
double timeBetweenFrameCapture = 1 / mCaptureRate;
recorder.setInputValue("timelapseRecordingIntervalUs",
(long) (1000000 * timeBetweenFrameCapture));
} else {
recorder.setInputValue("timelapseRecordingIntervalUs", 0L);
}
if (mInfoListener != null) {
recorder.setInputValue("infoListener", mInfoListener);
}
if (mErrorListener != null) {
recorder.setInputValue("errorListener", mErrorListener);
}
recorder.setInputValue("maxFileSize", mMaxFileSize);
recorder.setInputValue("recording", true);
if (mRecordSound != null) mRecordSound.play();
mState = STATE_RECORD;
}
public synchronized void stopRecording() {
if (mLogVerbose) Log.v(TAG, "Stop recording (" + this + ")");
switch (mState) {
case STATE_CONFIGURE:
case STATE_PREVIEW:
Log.w(TAG, "StopRecording called when recording not active!");
return;
case STATE_RELEASED:
throw new RuntimeException("stopRecording called on released EffectsRecorder!");
default:
break;
}
Filter recorder = mRunner.getGraph().getFilter("recorder");
recorder.setInputValue("recording", false);
if (mRecordSound != null) mRecordSound.play();
mState = STATE_PREVIEW;
}
// Stop and release effect resources
public synchronized void stopPreview() {
if (mLogVerbose) Log.v(TAG, "Stopping preview (" + this + ")");
switch (mState) {
case STATE_CONFIGURE:
Log.w(TAG, "StopPreview called when preview not active!");
return;
case STATE_RELEASED:
throw new RuntimeException("stopPreview called on released EffectsRecorder!");
default:
break;
}
if (mState == STATE_RECORD) {
stopRecording();
}
mCurrentEffect = EFFECT_NONE;
mCameraDevice.stopPreview();
try {
mCameraDevice.setPreviewTexture(null);
} catch(IOException e) {
throw new RuntimeException("Unable to connect camera to effect input", e);
}
mState = STATE_CONFIGURE;
mOldRunner = mRunner;
mRunner.stop();
mRunner = null;
// Rest of stop and release handled in mRunnerDoneCallback
}
// Try to enable/disable video stabilization if supported; otherwise return false
boolean tryEnableVideoStabilization(boolean toggle) {
Camera.Parameters params = mCameraDevice.getParameters();
String vstabSupported = params.get("video-stabilization-supported");
if ("true".equals(vstabSupported)) {
if (mLogVerbose) Log.v(TAG, "Setting video stabilization to " + toggle);
params.set("video-stabilization", toggle ? "true" : "false");
mCameraDevice.setParameters(params);
return true;
}
if (mLogVerbose) Log.v(TAG, "Video stabilization not supported");
return false;
}
// Try to enable/disable 3A locks if supported; otherwise return false
boolean tryEnable3ALocks(boolean toggle) {
Camera.Parameters params = mCameraDevice.getParameters();
if (params.isAutoExposureLockSupported() &&
params.isAutoWhiteBalanceLockSupported() ) {
params.setAutoExposureLock(toggle);
params.setAutoWhiteBalanceLock(toggle);
mCameraDevice.setParameters(params);
return true;
}
return false;
}
// Try to enable/disable 3A locks if supported; otherwise, throw error
// Use this when locks are essential to success
void enable3ALocks(boolean toggle) {
Camera.Parameters params = mCameraDevice.getParameters();
if (!tryEnable3ALocks(toggle)) {
throw new RuntimeException("Attempt to lock 3A on camera with no locking support!");
}
}
private OnRunnerDoneListener mRunnerDoneCallback =
new OnRunnerDoneListener() {
public void onRunnerDone(int result) {
synchronized(EffectsRecorder.this) {
if (mLogVerbose) {
Log.v(TAG,
"Graph runner done (" + EffectsRecorder.this
+ ", mRunner " + mRunner
+ ", mOldRunner " + mOldRunner + ")");
}
if (result == GraphRunner.RESULT_ERROR) {
// Handle error case
Log.e(TAG, "Error running filter graph!");
raiseError(mRunner == null ? null : mRunner.getError());
}
if (mOldRunner != null) {
// Tear down old graph if available
if (mLogVerbose) Log.v(TAG, "Tearing down old graph.");
GLEnvironment glEnv = mGraphEnv.getContext().getGLEnvironment();
if (glEnv != null && !glEnv.isActive()) {
glEnv.activate();
}
mOldRunner.getGraph().tearDown(mGraphEnv.getContext());
if (glEnv != null && glEnv.isActive()) {
glEnv.deactivate();
}
mOldRunner = null;
}
if (mState == STATE_PREVIEW) {
// Switching effects, start up the new runner
if (mLogVerbose) Log.v(TAG, "Previous effect halted, starting new effect.");
tryEnable3ALocks(false);
mRunner.run();
} else if (mState != STATE_RELEASED) {
// Shutting down effects
if (mLogVerbose) Log.v(TAG, "Runner halted, restoring direct preview");
tryEnable3ALocks(false);
sendMessage(EFFECT_NONE, EFFECT_MSG_EFFECTS_STOPPED);
} else {
// STATE_RELEASED - camera will be/has been released as well, do nothing.
}
}
}
};
// Indicates that all camera/recording activity needs to halt
public synchronized void release() {
if (mLogVerbose) Log.v(TAG, "Releasing (" + this + ")");
switch (mState) {
case STATE_RECORD:
case STATE_PREVIEW:
stopPreview();
// Fall-through
default:
mRecordSound.release();
mState = STATE_RELEASED;
break;
}
}
private void sendMessage(final int effect, final int msg) {
if (mEffectsListener != null) {
mHandler.post(new Runnable() {
public void run() {
mEffectsListener.onEffectsUpdate(effect, msg);
}
});
}
}
private void raiseError(final Exception exception) {
if (mEffectsListener != null) {
mHandler.post(new Runnable() {
public void run() {
if (mFd != null) {
mEffectsListener.onEffectsError(exception, null);
} else {
mEffectsListener.onEffectsError(exception, mOutputFile);
}
}
});
}
}
}
|