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

Conversation

chaitanyaraghav
Copy link
Contributor

Hello,

I had a need to set a max file size limit for my implementation of the camera and added the API to set a limit. I added a new API method setMaxFileSize(long maxFileSizeInBytes) to set a custom maximum file size for a video recording. The camera ignores the max file size if its not set or is invalid.

I added another boolean parameter to the video callback method indicating if the video ended due to a file size limit. I also included a helper getter method to get the status of the current camera video recording. It felt easier as I could rely on cameraView.isRecordingVideo() instead of having my own boolean tracking in my activity.

Thanks!

@natario1
Copy link
Owner

Nice job, thanks!

I'll take a look now. But we can't change the signature of the listener. That would force everyone to rewrite their code. So I think you should revert that, for now the user can manually check the video size. The rest seems like cool additions!

@codecov
Copy link

codecov bot commented Nov 15, 2017

Codecov Report

Merging #104 into master will decrease coverage by 0.3%.
The diff coverage is 11.7%.

Impacted Files Coverage Δ Complexity Δ
...ain/java/com/otaliastudios/cameraview/Camera2.java 0% <0%> (ø) 0 <0> (ø) ⬇️
...ain/java/com/otaliastudios/cameraview/Camera1.java 0% <0%> (ø) 0 <0> (ø) ⬇️
.../java/com/otaliastudios/cameraview/CameraView.java 67.2% <16.6%> (-0.6%) 104 <0> (ø)
...com/otaliastudios/cameraview/CameraController.java 25.9% <50%> (+0.2%) 23 <0> (ø) ⬇️
...otaliastudios/cameraview/TextureCameraPreview.java 65.2% <0%> (-4.4%) 4% <0%> (-1%)

switch (i){
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:{
mHasReachedMaxSize = true;
endVideo();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think endVideoImmediately would work better.

// Additional helper info

@Override
boolean isRecordingVideo() {
Copy link
Owner

@natario1 natario1 Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this to CameraController and make it final? So Camera1 just inherits.

*/
@UiThread
public void onVideoTaken(File video) {
public void onVideoTaken(File video, boolean hasReachedMaxFileSize) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said, I think this is unacceptable at the moment. We can't force everyone to rewrite their code. But in the future I will change the signature with a VideoResult so we can add everything.

@@ -1346,7 +1364,7 @@ public boolean getPlaySounds() {
void onShutter(boolean shouldPlaySound);
void processImage(byte[] jpeg, boolean consistentWithView, boolean flipHorizontally);
void processSnapshot(YuvImage image, boolean consistentWithView, boolean flipHorizontally);
void dispatchOnVideoTaken(File file);
void dispatchOnVideoTaken(File file, boolean hasReachedMaxFileSize);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we can't do this right now.

*
* @param maxFileSizeInBytes The maximum size of videos in bytes
*/
public void setMaxFileSize(long maxFileSizeInBytes){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename to setVideoMaxSize?

@chaitanyaraghav
Copy link
Contributor Author

@natario1 Ya I agree. I was on the fence about that and did not quite like changing the signature. How about adding a new callback method to notify file size limits? That way only people requiring a notification can override it.

@natario1
Copy link
Owner

That would be better but I prefer not doing it. You know, there is a lot of video metadata which would be cool to pass around, but we can't have a callback for everything only to deprecate them later.

The good solution is really to change the signature as you did with a VideoResult object but I can only do that in a major release like v2. So, please be patient. You can easily call file.length() for now.

@chaitanyaraghav
Copy link
Contributor Author

@natario1 Ya makes sense to me. I reverted the changes to the method signature. Please take a look!
I'll stick with file.length() for now :)

@natario1
Copy link
Owner

Looks good to me. I will add tests for this later. Two things,

  • can you add a cameraVideoMaxSize XML attribute? It's just a few lines in the view constructor
  • can you add some info in README.md? You can use the Camera Controls table where all other attrs are.

If you don't want to, no problem.

@natario1
Copy link
Owner

And, since all video API use capturing, can you rename isRecordingVideo to isCapturingVideo? Thanks!

@chaitanyaraghav
Copy link
Contributor Author

@natario1 Sure, I'll make the changes

@Johnson145
Copy link
Contributor

I'm afraid this feature is a bit problematic, isn't it? I was thinking about implementing the same for the setMaxDuration part to get a constant video length as requested in #65. However, according to the docs both events will result in: Stopping happens asynchronously, there is no guarantee that the recorder will have stopped by the time the listener is notified. So we may inform the user about a captured video which isn't available yet, right? Furthermore, your current implementation stops the video a second time, although the media recorder itself stopped it already. This could cause troubles, too, couldn't it?

@Johnson145
Copy link
Contributor

A bit hacky, but maybe you could listen to the MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING event instead? Then we were able to keep control about stopping the video. You may internally scale the max file size that is given by the user such that the MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING event is triggered after 90% of the scaled size has reached, but 100% of the unscaled one.

@natario1
Copy link
Owner

natario1 commented Nov 16, 2017

@Johnson145 I have not tried of course but I think the two issues solve each other.

When we get the callback the recorder is about to auto-stop. We anticipate and stop it, then notify the user. If it stops itself before we do, there should be no problem because our stopping is in a try-catch block.

Does this make sense to you?

The best thing would be to try but I'm not able right now. Maybe after it's merged. If it proves to be flaky, we can do the trick with 90%, seems good! Or @chaitanyaraghav can implement it now if he wants. Just be sure to add some code comments about it.

These APIs do really suck.

@Johnson145
Copy link
Contributor

May work, I don't know. Depends on the question whether the mMediaRecorder.stop() call is thread safe. If one of the two calls is guaranteed to finish its job, you're right. I'm afraid it could be possible that our second call is failing, because the first one is still running (but not yet finished). In that case we would just continue after the try-catch and inform the user about the video to early. In each case, I guess problems are very unlikely, but who knows whether they are impossible..

Sure, we could try it first!

@Johnson145
Copy link
Contributor

Johnson145 commented Nov 16, 2017

Maybe it's even a good idea to give it a try first. If it proves to work, we could implement the MEDIA_RECORDER_INFO_MAX_DURATION_REACHED part, too. Unfortunately, the latter does not have a MEDIA_RECORDER_INFO_MAX_DURATION_APPROACHING version.

@natario1
Copy link
Owner

Yes, given how unfriendly MediaRecorder is, issues might be likely. The max duration feature would be cool as well if you want to work on it! We could then deprecate startCapturingVideo(File, long).

@natario1
Copy link
Owner

@chaitanyaraghav are you planning to work on this soon? There's no hurry of course, I want to know if I should release 1.4.1 today or wait for you. This Sunday I'll leave for a while so it will be hard for me to add tests etc. Thanks so much!

@chaitanyaraghav
Copy link
Contributor Author

@natario1 It may be better to push this to the next release. I'm currently out of town and I won't be able to make the changes until I get back this Sunday.

@natario1
Copy link
Owner

That's fine man 👍 I'll release today then!

@chaitanyaraghav
Copy link
Contributor Author

@natario1 Changed the method name, added xml attribute for video size and updated the documentation.

The 90% trick seems good but I have not made that change yet. The current implementation appears to work fine in my testing. If it turns out to be flaky, I'll make the change to scale the max size.

Thanks for the patience!

@natario1 natario1 merged commit 1bd1816 into natario1:master Dec 3, 2017
@natario1
Copy link
Owner

natario1 commented Dec 3, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants