Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max video file size #104

Merged
merged 5 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ Other APIs not mentioned above are provided, and are well documented and comment
|`getPreviewSize()`|Returns the size of the preview surface. If CameraView was not constrained in its layout phase (e.g. it was `wrap_content`), this will return the same aspect ratio of CameraView.|
|`getSnapshotSize()`|Returns `getPreviewSize()`, since a snapshot is a preview frame.|
|`getPictureSize()`|Returns the size of the output picture. The aspect ratio is consistent with `getPreviewSize()`.|
|`setVideoMaxSize(long)`|Set a max file size (in bytes) for a video recording. There is no file size limit by default unless set by the user.|

Take also a look at public methods in `CameraUtils`, `CameraOptions`, `ExtraProperties`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@

import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyFloat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.media.MediaRecorder;
import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
Expand All @@ -20,8 +19,13 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@ public void onSurfaceAvailable() {
@Override
public void onBufferAvailable(byte[] buffer) {
}

@Override
void setVideoMaxSize(long videoMaxSizeInBytes) {

}
}
28 changes: 28 additions & 0 deletions cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,25 @@ private void initMediaRecorder() {

mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
mMediaRecorder.setOrientationHint(computeSensorToOutputOffset());

//If the user sets a max file size, set it to the max file size
if(mVideoMaxSizeInBytes > 0) {
mMediaRecorder.setMaxFileSize(mVideoMaxSizeInBytes);

//Attach a listener to the media recorder to listen for file size notifications
mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mediaRecorder, int i, int i1) {
switch (i){
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:{
endVideoImmediately();
break;
}
}

}
});
}
// Not needed. mMediaRecorder.setPreviewDisplay(mPreview.getSurface());
}

Expand Down Expand Up @@ -833,4 +852,13 @@ private List<Size> sizesFromList(List<Camera.Size> sizes) {
LOG.i("size:", "sizesFromList:", result);
return result;
}

@Override
void setVideoMaxSize(long videoMaxSizeInBytes) {
mVideoMaxSizeInBytes = videoMaxSizeInBytes;
}

// -----------------
// Additional helper info
}

16 changes: 5 additions & 11 deletions cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package com.otaliastudios.cameraview;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.PointF;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@TargetApi(21)
class Camera2 extends CameraController {
Expand Down Expand Up @@ -125,4 +114,9 @@ void startAutoFocus(@Nullable Gesture gesture, PointF point) {
public void onBufferAvailable(byte[] buffer) {

}

@Override
void setVideoMaxSize(long videoMaxSizeInBytes) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ abstract class CameraController implements
protected boolean mIsCapturingVideo = false;

protected int mState = STATE_STOPPED;
protected long mVideoMaxSizeInBytes = 0;

// Used for testing.
Task<Void> mZoomTask = new Task<>();
Expand Down Expand Up @@ -317,6 +318,8 @@ final void setPictureSizeSelector(SizeSelector selector) {

abstract void startAutoFocus(@Nullable Gesture gesture, PointF point);

abstract void setVideoMaxSize(long videoMaxSizeInBytes);

//endregion

//region final getters
Expand Down Expand Up @@ -383,6 +386,10 @@ final Size getPreviewSize() {
return mPreviewSize;
}

final boolean isCapturingVideo() {
return mIsCapturingVideo;
}

//endregion

//region Orientation utils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value()));
GestureAction scrollHorizontalGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureScrollHorizontal, GestureAction.DEFAULT_SCROLL_HORIZONTAL.value()));
GestureAction scrollVerticalGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureScrollVertical, GestureAction.DEFAULT_SCROLL_VERTICAL.value()));

//Get max size
float cameraVideoMaxSize = a.getFloat(R.styleable.CameraView_cameraVideoMaxSize, -1);

a.recycle();

// Components
Expand Down Expand Up @@ -186,6 +190,11 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
mapGesture(Gesture.SCROLL_HORIZONTAL, scrollHorizontalGesture);
mapGesture(Gesture.SCROLL_VERTICAL, scrollVerticalGesture);

//Set camera video maxSize
if(cameraVideoMaxSize > 0) {
setVideoMaxSize((long)cameraVideoMaxSize);
}

if (!isInEditMode()) {
mOrientationHelper = new OrientationHelper(context, mCameraCallbacks);
}
Expand Down Expand Up @@ -1364,6 +1373,24 @@ public boolean getPlaySounds() {
return mPlaySounds;
}

/**
* Set a max file size (in bytes) for a video recording. There is no file size limit by default
* unless set by the user.
*
* @param videoMaxSizeInBytes The maximum size of videos in bytes
*/
public void setVideoMaxSize(long videoMaxSizeInBytes){
mCameraController.setVideoMaxSize(videoMaxSizeInBytes);
}

/**
* Returns true if the camera is currently recording a video
* @return boolean indicating if the camera is recording a video
*/
public boolean isCapturingVideo(){
return mCameraController.isCapturingVideo();
}

//endregion

//region Callbacks and dispatching
Expand Down
2 changes: 2 additions & 0 deletions cameraview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@

<attr name="cameraPlaySounds" format="boolean" />

<attr name="cameraVideoMaxSize" format="float" />

<!-- deprecated attr name="cameraZoomMode" format="enum">
<enum name="off" value="0" />
<enum name="pinch" value="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Toast;

import com.otaliastudios.cameraview.Audio;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.Flash;
import com.otaliastudios.cameraview.Grid;
import com.otaliastudios.cameraview.SessionType;
import com.otaliastudios.cameraview.Size;
import com.otaliastudios.cameraview.VideoQuality;
import com.otaliastudios.cameraview.WhiteBalance;

import java.io.File;

Expand Down Expand Up @@ -53,7 +47,12 @@ protected void onCreate(Bundle savedInstanceState) {
camera.addCameraListener(new CameraListener() {
public void onCameraOpened(CameraOptions options) { onOpened(); }
public void onPictureTaken(byte[] jpeg) { onPicture(jpeg); }
public void onVideoTaken(File video) { onVideo(video); }

@Override
public void onVideoTaken(File video) {
super.onVideoTaken(video);
onVideo(video);
}
});

findViewById(R.id.edit).setOnClickListener(this);
Expand Down