Search in sources :

Example 1 with NoFreeStorageException

use of net.sourceforge.opencamera.Preview.ApplicationInterface.NoFreeStorageException in project OpenCamera by ageback.

the class Preview method startVideoRecording.

/**
 * Start video recording.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void startVideoRecording(final boolean max_filesize_restart) {
    // clear focus rectangle (don't do for taking photos yet)
    focus_success = FOCUS_DONE;
    // initialise just in case:
    boolean created_video_file = false;
    video_method = ApplicationInterface.VIDEOMETHOD_FILE;
    video_uri = null;
    video_filename = null;
    ParcelFileDescriptor pfd_saf = null;
    try {
        video_method = applicationInterface.createOutputVideoMethod();
        if (MyDebug.LOG)
            Log.e(TAG, "video_method? " + video_method);
        if (video_method == ApplicationInterface.VIDEOMETHOD_FILE) {
            File videoFile = applicationInterface.createOutputVideoFile();
            video_filename = videoFile.getAbsolutePath();
            created_video_file = true;
            if (MyDebug.LOG)
                Log.d(TAG, "save to: " + video_filename);
        } else {
            if (video_method == ApplicationInterface.VIDEOMETHOD_SAF) {
                video_uri = applicationInterface.createOutputVideoSAF();
            } else {
                video_uri = applicationInterface.createOutputVideoUri();
            }
            created_video_file = true;
            if (MyDebug.LOG)
                Log.d(TAG, "save to: " + video_uri);
            pfd_saf = getContext().getContentResolver().openFileDescriptor(video_uri, "rw");
        }
    } catch (IOException e) {
        if (MyDebug.LOG)
            Log.e(TAG, "Couldn't create media video file; check storage permissions?");
        e.printStackTrace();
        applicationInterface.onFailedCreateVideoFileError();
        applicationInterface.cameraInOperation(false, true);
    }
    if (created_video_file) {
        final VideoProfile profile = getVideoProfile();
        if (MyDebug.LOG) {
            Log.d(TAG, "current_video_quality: " + this.video_quality_handler.getCurrentVideoQualityIndex());
            if (this.video_quality_handler.getCurrentVideoQualityIndex() != -1)
                Log.d(TAG, "current_video_quality value: " + this.video_quality_handler.getCurrentVideoQuality());
            Log.d(TAG, "resolution " + profile.videoFrameWidth + " x " + profile.videoFrameHeight);
            Log.d(TAG, "bit rate " + profile.videoBitRate);
        }
        boolean enable_sound = applicationInterface.getShutterSoundPref();
        if (MyDebug.LOG)
            Log.d(TAG, "enable_sound? " + enable_sound);
        // Camera2 API can disable video sound too
        camera_controller.enableShutterSound(enable_sound);
        MediaRecorder local_video_recorder = new MediaRecorder();
        this.camera_controller.unlock();
        if (MyDebug.LOG)
            Log.d(TAG, "set video listeners");
        local_video_recorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {

            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (MyDebug.LOG)
                    Log.d(TAG, "MediaRecorder info: " + what + " extra: " + extra);
                final int final_what = what;
                final int final_extra = extra;
                Activity activity = (Activity) Preview.this.getContext();
                activity.runOnUiThread(new Runnable() {

                    public void run() {
                        // we run on main thread to avoid problem of camera closing at the same time
                        onVideoInfo(final_what, final_extra);
                    }
                });
            }
        });
        local_video_recorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {

            public void onError(MediaRecorder mr, int what, int extra) {
                final int final_what = what;
                final int final_extra = extra;
                Activity activity = (Activity) Preview.this.getContext();
                activity.runOnUiThread(new Runnable() {

                    public void run() {
                        // we run on main thread to avoid problem of camera closing at the same time
                        onVideoError(final_what, final_extra);
                    }
                });
            }
        });
        camera_controller.initVideoRecorderPrePrepare(local_video_recorder);
        if (profile.no_audio_permission) {
            showToast(null, R.string.permission_record_audio_not_available);
        }
        boolean store_location = applicationInterface.getGeotaggingPref();
        if (store_location && applicationInterface.getLocation() != null) {
            Location location = applicationInterface.getLocation();
            if (MyDebug.LOG) {
                Log.d(TAG, "set video location: lat " + location.getLatitude() + " long " + location.getLongitude() + " accuracy " + location.getAccuracy());
            }
            local_video_recorder.setLocation((float) location.getLatitude(), (float) location.getLongitude());
        }
        if (MyDebug.LOG)
            Log.d(TAG, "copy video profile to media recorder");
        profile.copyToMediaRecorder(local_video_recorder);
        // true if we called applicationInterface.startingVideo()
        boolean told_app_starting = false;
        try {
            ApplicationInterface.VideoMaxFileSize video_max_filesize = applicationInterface.getVideoMaxFileSizePref();
            long max_filesize = video_max_filesize.max_filesize;
            // max_filesize = 15*1024*1024; // test
            if (max_filesize > 0) {
                if (MyDebug.LOG)
                    Log.d(TAG, "set max file size of: " + max_filesize);
                try {
                    local_video_recorder.setMaxFileSize(max_filesize);
                } catch (RuntimeException e) {
                    // Google Camera warns this can happen - for example, if 64-bit filesizes not supported
                    if (MyDebug.LOG)
                        Log.e(TAG, "failed to set max filesize of: " + max_filesize);
                    e.printStackTrace();
                }
            }
            // note, we set this even if max_filesize==0, as it will still apply when hitting device max filesize limit
            video_restart_on_max_filesize = video_max_filesize.auto_restart;
            // handle restart timer
            long video_max_duration = applicationInterface.getVideoMaxDurationPref();
            if (MyDebug.LOG)
                Log.d(TAG, "user preference video_max_duration: " + video_max_duration);
            if (max_filesize_restart) {
                if (video_max_duration > 0) {
                    video_max_duration -= video_accumulated_time;
                    // this should be greater or equal to min_safe_restart_video_time, as too short remaining time should have been caught in restartVideo()
                    if (video_max_duration < min_safe_restart_video_time) {
                        if (MyDebug.LOG)
                            Log.e(TAG, "trying to restart video with too short a time: " + video_max_duration);
                        video_max_duration = min_safe_restart_video_time;
                    }
                }
            } else {
                video_accumulated_time = 0;
            }
            if (MyDebug.LOG)
                Log.d(TAG, "actual video_max_duration: " + video_max_duration);
            local_video_recorder.setMaxDuration((int) video_max_duration);
            if (video_method == ApplicationInterface.VIDEOMETHOD_FILE) {
                local_video_recorder.setOutputFile(video_filename);
            } else {
                local_video_recorder.setOutputFile(pfd_saf.getFileDescriptor());
            }
            applicationInterface.cameraInOperation(true, true);
            told_app_starting = true;
            applicationInterface.startingVideo();
            /*if( true ) // test
        			throw new IOException();*/
            cameraSurface.setVideoRecorder(local_video_recorder);
            local_video_recorder.setOrientationHint(getImageVideoRotation());
            if (MyDebug.LOG)
                Log.d(TAG, "about to prepare video recorder");
            local_video_recorder.prepare();
            camera_controller.initVideoRecorderPostPrepare(local_video_recorder);
            if (MyDebug.LOG)
                Log.d(TAG, "about to start video recorder");
            try {
                local_video_recorder.start();
                this.video_recorder = local_video_recorder;
                videoRecordingStarted(max_filesize_restart);
            } catch (RuntimeException e) {
                // needed for emulator at least - although MediaRecorder not meant to work with emulator, it's good to fail gracefully
                Log.e(TAG, "runtime exception starting video recorder");
                e.printStackTrace();
                // still assign, so failedToStartVideoRecorder() will release the video_recorder
                this.video_recorder = local_video_recorder;
                // told_app_starting must be true if we're here
                applicationInterface.stoppingVideo();
                failedToStartVideoRecorder(profile);
            }
        /*final MediaRecorder local_video_recorder_f = local_video_recorder;
				new AsyncTask<Void, Void, Boolean>() {
					private static final String TAG = "video_recorder.start";

					@Override
					protected Boolean doInBackground(Void... voids) {
						if( MyDebug.LOG )
							Log.d(TAG, "doInBackground, async task: " + this);
						try {
							local_video_recorder_f.start();
						}
						catch(RuntimeException e) {
							// needed for emulator at least - although MediaRecorder not meant to work with emulator, it's good to fail gracefully
							Log.e(TAG, "runtime exception starting video recorder");
							e.printStackTrace();
							return false;
						}
						return true;
					}

					@Override
					protected void onPostExecute(Boolean success) {
						if( MyDebug.LOG ) {
							Log.d(TAG, "onPostExecute, async task: " + this);
							Log.d(TAG, "success: " + success);
						}
						 // still assign even if success==false, so failedToStartVideoRecorder() will release the video_recorder
						Preview.this.video_recorder = local_video_recorder_f;
						if( success ) {
							videoRecordingStarted(max_filesize_restart);
						}
						else {
							// told_app_starting must be true if we're here
							applicationInterface.stoppingVideo();
							failedToStartVideoRecorder(profile);
						}
					}
				}.execute();*/
        } catch (IOException e) {
            if (MyDebug.LOG)
                Log.e(TAG, "failed to save video");
            e.printStackTrace();
            this.video_recorder = local_video_recorder;
            if (told_app_starting) {
                applicationInterface.stoppingVideo();
            }
            applicationInterface.onFailedCreateVideoFileError();
            video_recorder.reset();
            video_recorder.release();
            video_recorder = null;
            video_recorder_is_paused = false;
            applicationInterface.cameraInOperation(false, true);
            this.reconnectCamera(true);
        } catch (CameraControllerException e) {
            if (MyDebug.LOG)
                Log.e(TAG, "camera exception starting video recorder");
            e.printStackTrace();
            // still assign, so failedToStartVideoRecorder() will release the video_recorder
            this.video_recorder = local_video_recorder;
            if (told_app_starting) {
                applicationInterface.stoppingVideo();
            }
            failedToStartVideoRecorder(profile);
        } catch (NoFreeStorageException e) {
            if (MyDebug.LOG)
                Log.e(TAG, "nofreestorageexception starting video recorder");
            e.printStackTrace();
            this.video_recorder = local_video_recorder;
            if (told_app_starting) {
                applicationInterface.stoppingVideo();
            }
            video_recorder.reset();
            video_recorder.release();
            video_recorder = null;
            video_recorder_is_paused = false;
            applicationInterface.cameraInOperation(false, true);
            this.reconnectCamera(true);
            this.showToast(null, R.string.video_no_free_space);
        }
    }
}
Also used : Activity(android.app.Activity) IOException(java.io.IOException) NoFreeStorageException(net.sourceforge.opencamera.Preview.ApplicationInterface.NoFreeStorageException) CameraControllerException(net.sourceforge.opencamera.CameraController.CameraControllerException) Paint(android.graphics.Paint) Point(android.graphics.Point) ParcelFileDescriptor(android.os.ParcelFileDescriptor) MediaRecorder(android.media.MediaRecorder) File(java.io.File) Location(android.location.Location) TargetApi(android.annotation.TargetApi)

Aggregations

TargetApi (android.annotation.TargetApi)1 Activity (android.app.Activity)1 Paint (android.graphics.Paint)1 Point (android.graphics.Point)1 Location (android.location.Location)1 MediaRecorder (android.media.MediaRecorder)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 File (java.io.File)1 IOException (java.io.IOException)1 CameraControllerException (net.sourceforge.opencamera.CameraController.CameraControllerException)1 NoFreeStorageException (net.sourceforge.opencamera.Preview.ApplicationInterface.NoFreeStorageException)1