This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] Camera fix android audio #3124
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
be25349
Set encoding bitrate to recording bitrate
cfchris 5184312
Updated version and changlog
cfchris 0a5fce6
Merge branch 'master' of github.com:flutter/plugins into camera-fix-a…
cfchris c83f73a
Merge branch 'master' of github.com:flutter/plugins into camera-fix-a…
cfchris 2fe6cfb
Merge remote-tracking branch 'upstream/master' into camera-fix-androi…
cfchris c4e09bd
Updated version to update PR
cfchris 8c09aa0
Extracted logic to create MediaRecord and test it.
mvanbeusekom fdf2674
Fixed JAVA formatting
mvanbeusekom 76c59ac
Fixed feedback
mvanbeusekom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...es/camera/android/src/main/java/io/flutter/plugins/camera/media/MediaRecorderBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
package io.flutter.plugins.camera.media; | ||
|
||
import android.media.CamcorderProfile; | ||
import android.media.MediaRecorder; | ||
import androidx.annotation.NonNull; | ||
import java.io.IOException; | ||
|
||
public class MediaRecorderBuilder { | ||
static class MediaRecorderFactory { | ||
MediaRecorder makeMediaRecorder() { | ||
return new MediaRecorder(); | ||
} | ||
} | ||
|
||
private final String outputFilePath; | ||
private final CamcorderProfile recordingProfile; | ||
private final MediaRecorderFactory recorderFactory; | ||
|
||
private boolean enableAudio; | ||
private int mediaOrientation; | ||
|
||
public MediaRecorderBuilder( | ||
@NonNull CamcorderProfile recordingProfile, @NonNull String outputFilePath) { | ||
this(recordingProfile, outputFilePath, new MediaRecorderFactory()); | ||
} | ||
|
||
MediaRecorderBuilder( | ||
@NonNull CamcorderProfile recordingProfile, | ||
@NonNull String outputFilePath, | ||
MediaRecorderFactory helper) { | ||
this.outputFilePath = outputFilePath; | ||
this.recordingProfile = recordingProfile; | ||
this.recorderFactory = helper; | ||
} | ||
|
||
public MediaRecorderBuilder setEnableAudio(boolean enableAudio) { | ||
this.enableAudio = enableAudio; | ||
return this; | ||
} | ||
|
||
public MediaRecorderBuilder setMediaOrientation(int orientation) { | ||
this.mediaOrientation = orientation; | ||
return this; | ||
} | ||
|
||
public MediaRecorder build() throws IOException { | ||
MediaRecorder mediaRecorder = recorderFactory.makeMediaRecorder(); | ||
|
||
// There's a specific order that mediaRecorder expects. Do not change the order | ||
// of these function calls. | ||
if (enableAudio) { | ||
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); | ||
mediaRecorder.setAudioEncodingBitRate(recordingProfile.audioBitRate); | ||
} | ||
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); | ||
mediaRecorder.setOutputFormat(recordingProfile.fileFormat); | ||
if (enableAudio) mediaRecorder.setAudioEncoder(recordingProfile.audioCodec); | ||
mediaRecorder.setVideoEncoder(recordingProfile.videoCodec); | ||
mediaRecorder.setVideoEncodingBitRate(recordingProfile.videoBitRate); | ||
if (enableAudio) mediaRecorder.setAudioSamplingRate(recordingProfile.audioSampleRate); | ||
mediaRecorder.setVideoFrameRate(recordingProfile.videoFrameRate); | ||
mediaRecorder.setVideoSize(recordingProfile.videoFrameWidth, recordingProfile.videoFrameHeight); | ||
mediaRecorder.setOutputFile(outputFilePath); | ||
mediaRecorder.setOrientationHint(this.mediaOrientation); | ||
|
||
mediaRecorder.prepare(); | ||
|
||
return mediaRecorder; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...amera/android/src/test/java/io/flutter/plugins/camera/media/MediaRecorderBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package io.flutter.plugins.camera.media; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
import android.media.CamcorderProfile; | ||
import android.media.MediaRecorder; | ||
import java.io.IOException; | ||
import java.lang.reflect.Constructor; | ||
import org.junit.Test; | ||
import org.mockito.InOrder; | ||
|
||
public class MediaRecorderBuilderTest { | ||
@Test | ||
public void ctor_test() { | ||
MediaRecorderBuilder builder = | ||
new MediaRecorderBuilder(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P), ""); | ||
|
||
assertNotNull(builder); | ||
} | ||
|
||
@Test | ||
public void build_Should_set_values_in_correct_order_When_audio_is_disabled() throws IOException { | ||
CamcorderProfile recorderProfile = getEmptyCamcorderProfile(); | ||
MediaRecorderBuilder.MediaRecorderFactory mockFactory = | ||
mock(MediaRecorderBuilder.MediaRecorderFactory.class); | ||
MediaRecorder mockMediaRecorder = mock(MediaRecorder.class); | ||
String outputFilePath = "mock_video_file_path"; | ||
int mediaOrientation = 1; | ||
MediaRecorderBuilder builder = | ||
new MediaRecorderBuilder(recorderProfile, outputFilePath, mockFactory) | ||
.setEnableAudio(false) | ||
.setMediaOrientation(mediaOrientation); | ||
|
||
when(mockFactory.makeMediaRecorder()).thenReturn(mockMediaRecorder); | ||
|
||
MediaRecorder recorder = builder.build(); | ||
|
||
InOrder inOrder = inOrder(recorder); | ||
inOrder.verify(recorder).setVideoSource(MediaRecorder.VideoSource.SURFACE); | ||
inOrder.verify(recorder).setOutputFormat(recorderProfile.fileFormat); | ||
inOrder.verify(recorder).setVideoEncoder(recorderProfile.videoCodec); | ||
inOrder.verify(recorder).setVideoEncodingBitRate(recorderProfile.videoBitRate); | ||
inOrder.verify(recorder).setVideoFrameRate(recorderProfile.videoFrameRate); | ||
inOrder | ||
.verify(recorder) | ||
.setVideoSize(recorderProfile.videoFrameWidth, recorderProfile.videoFrameHeight); | ||
inOrder.verify(recorder).setOutputFile(outputFilePath); | ||
inOrder.verify(recorder).setOrientationHint(mediaOrientation); | ||
inOrder.verify(recorder).prepare(); | ||
} | ||
|
||
@Test | ||
public void build_Should_set_values_in_correct_order_When_audio_is_enabled() throws IOException { | ||
CamcorderProfile recorderProfile = getEmptyCamcorderProfile(); | ||
MediaRecorderBuilder.MediaRecorderFactory mockFactory = | ||
mock(MediaRecorderBuilder.MediaRecorderFactory.class); | ||
MediaRecorder mockMediaRecorder = mock(MediaRecorder.class); | ||
String outputFilePath = "mock_video_file_path"; | ||
int mediaOrientation = 1; | ||
MediaRecorderBuilder builder = | ||
new MediaRecorderBuilder(recorderProfile, outputFilePath, mockFactory) | ||
.setEnableAudio(true) | ||
.setMediaOrientation(mediaOrientation); | ||
|
||
when(mockFactory.makeMediaRecorder()).thenReturn(mockMediaRecorder); | ||
|
||
MediaRecorder recorder = builder.build(); | ||
|
||
InOrder inOrder = inOrder(recorder); | ||
inOrder.verify(recorder).setAudioSource(MediaRecorder.AudioSource.MIC); | ||
inOrder.verify(recorder).setAudioEncodingBitRate(recorderProfile.audioBitRate); | ||
inOrder.verify(recorder).setVideoSource(MediaRecorder.VideoSource.SURFACE); | ||
inOrder.verify(recorder).setOutputFormat(recorderProfile.fileFormat); | ||
inOrder.verify(recorder).setAudioEncoder(recorderProfile.audioCodec); | ||
inOrder.verify(recorder).setVideoEncoder(recorderProfile.videoCodec); | ||
inOrder.verify(recorder).setVideoEncodingBitRate(recorderProfile.videoBitRate); | ||
inOrder.verify(recorder).setAudioSamplingRate(recorderProfile.audioSampleRate); | ||
inOrder.verify(recorder).setVideoFrameRate(recorderProfile.videoFrameRate); | ||
inOrder | ||
.verify(recorder) | ||
.setVideoSize(recorderProfile.videoFrameWidth, recorderProfile.videoFrameHeight); | ||
inOrder.verify(recorder).setOutputFile(outputFilePath); | ||
inOrder.verify(recorder).setOrientationHint(mediaOrientation); | ||
inOrder.verify(recorder).prepare(); | ||
} | ||
|
||
private CamcorderProfile getEmptyCamcorderProfile() { | ||
try { | ||
Constructor<CamcorderProfile> constructor = | ||
CamcorderProfile.class.getDeclaredConstructor( | ||
int.class, int.class, int.class, int.class, int.class, int.class, int.class, | ||
int.class, int.class, int.class, int.class, int.class); | ||
|
||
constructor.setAccessible(true); | ||
return constructor.newInstance(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | ||
} catch (Exception ignored) { | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing license header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added missing license header