From 4364ec7c3ad22d6356091f4a75d106bf2e48777f Mon Sep 17 00:00:00 2001 From: Ryan McClarnon Date: Sat, 23 May 2020 23:44:42 +0100 Subject: [PATCH] Add capability to set audio codec to encode video files with. (#861) * Add capability to set audio codec to encode video files with. * Cleanup * Handle audio codec in SnapshotVideoRecorder.java * Remove two AMR codecs and VORBIS, replace if with switch * All chosen codecs in SnapshotVideoRecorder.java should use "audio/mp4a-latm". Setting DEVICE_DEFAULT to use it's actual value would change the behaviour of the class. * Match style of video codec switch block above. --- .../cameraview/CameraViewTest.java | 15 +++++ .../cameraview/VideoResultTest.java | 19 ++++-- .../engine/options/Camera1OptionsTest.java | 3 + .../cameraview/CameraOptions.java | 3 + .../otaliastudios/cameraview/CameraView.java | 31 +++++++++ .../otaliastudios/cameraview/VideoResult.java | 14 +++++ .../cameraview/controls/AudioCodec.java | 63 +++++++++++++++++++ .../cameraview/controls/ControlParser.java | 8 +++ .../cameraview/engine/CameraBaseEngine.java | 15 +++++ .../cameraview/engine/CameraEngine.java | 4 ++ .../cameraview/video/FullVideoRecorder.java | 13 ++++ .../video/SnapshotVideoRecorder.java | 12 +++- cameraview/src/main/res/values/attrs.xml | 7 +++ .../cameraview/demo/CameraActivity.java | 4 +- .../otaliastudios/cameraview/demo/Option.java | 6 ++ .../cameraview/demo/VideoPreviewActivity.java | 2 + .../res/layout/activity_video_preview.xml | 5 ++ docs/_docs/controls.md | 18 ++++++ 18 files changed, 233 insertions(+), 9 deletions(-) create mode 100644 cameraview/src/main/java/com/otaliastudios/cameraview/controls/AudioCodec.java diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index 538703ac..4ee81244 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -19,6 +19,7 @@ import android.view.View; import android.view.ViewGroup; import com.otaliastudios.cameraview.controls.Audio; +import com.otaliastudios.cameraview.controls.AudioCodec; import com.otaliastudios.cameraview.controls.ControlParser; import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.controls.Facing; @@ -165,6 +166,7 @@ public class CameraViewTest extends BaseTest { assertEquals(cameraView.getHdr(), controls.getHdr()); assertEquals(cameraView.getAudio(), controls.getAudio()); assertEquals(cameraView.getVideoCodec(), controls.getVideoCodec()); + assertEquals(cameraView.getAudioCodec(), controls.getAudioCodec()); assertEquals(cameraView.getPictureFormat(), controls.getPictureFormat()); //noinspection SimplifiableJUnitAssertion assertEquals(cameraView.getLocation(), null); @@ -773,6 +775,19 @@ public class CameraViewTest extends BaseTest { assertEquals(cameraView.get(Audio.class), Audio.STEREO); } + @Test + public void testAudioCodec() { + cameraView.set(AudioCodec.DEVICE_DEFAULT); + assertEquals(cameraView.get(AudioCodec.class), AudioCodec.DEVICE_DEFAULT); + cameraView.set(AudioCodec.AAC); + assertEquals(cameraView.get(AudioCodec.class), AudioCodec.AAC); + cameraView.set(AudioCodec.HE_AAC); + assertEquals(cameraView.get(AudioCodec.class), AudioCodec.HE_AAC); + cameraView.set(AudioCodec.AAC_ELD); + assertEquals(cameraView.get(AudioCodec.class), AudioCodec.AAC_ELD); + } + + @Test public void testVideoCodec() { cameraView.set(VideoCodec.H_263); diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java index b4d9b6fd..e4ed22a4 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java @@ -4,6 +4,7 @@ package com.otaliastudios.cameraview; import android.location.Location; import com.otaliastudios.cameraview.controls.Audio; +import com.otaliastudios.cameraview.controls.AudioCodec; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.VideoCodec; import com.otaliastudios.cameraview.size.Size; @@ -32,7 +33,8 @@ public class VideoResultTest extends BaseTest { File file = Mockito.mock(File.class); int rotation = 90; Size size = new Size(20, 120); - VideoCodec codec = VideoCodec.H_263; + VideoCodec videoCodec = VideoCodec.H_263; + AudioCodec audioCodec = AudioCodec.DEVICE_DEFAULT; Location location = Mockito.mock(Location.class); boolean isSnapshot = true; int maxDuration = 1234; @@ -47,7 +49,8 @@ public class VideoResultTest extends BaseTest { stub.file = file; stub.rotation = rotation; stub.size = size; - stub.videoCodec = codec; + stub.videoCodec = videoCodec; + stub.audioCodec = audioCodec; stub.location = location; stub.isSnapshot = isSnapshot; stub.maxDuration = maxDuration; @@ -63,7 +66,8 @@ public class VideoResultTest extends BaseTest { assertEquals(result.getFile(), file); assertEquals(result.getRotation(), rotation); assertEquals(result.getSize(), size); - assertEquals(result.getVideoCodec(), codec); + assertEquals(result.getVideoCodec(), videoCodec); + assertEquals(result.getAudioCodec(), audioCodec); assertEquals(result.getLocation(), location); assertEquals(result.isSnapshot(), isSnapshot); assertEquals(result.getMaxSize(), maxFileSize); @@ -81,7 +85,8 @@ public class VideoResultTest extends BaseTest { FileDescriptor fileDescriptor = FileDescriptor.in; int rotation = 90; Size size = new Size(20, 120); - VideoCodec codec = VideoCodec.H_263; + VideoCodec videoCodec = VideoCodec.H_263; + AudioCodec audioCodec = AudioCodec.DEVICE_DEFAULT; Location location = Mockito.mock(Location.class); boolean isSnapshot = true; int maxDuration = 1234; @@ -96,7 +101,8 @@ public class VideoResultTest extends BaseTest { stub.fileDescriptor = fileDescriptor; stub.rotation = rotation; stub.size = size; - stub.videoCodec = codec; + stub.videoCodec = videoCodec; + stub.audioCodec = audioCodec; stub.location = location; stub.isSnapshot = isSnapshot; stub.maxDuration = maxDuration; @@ -112,7 +118,8 @@ public class VideoResultTest extends BaseTest { assertEquals(result.getFileDescriptor(), fileDescriptor); assertEquals(result.getRotation(), rotation); assertEquals(result.getSize(), size); - assertEquals(result.getVideoCodec(), codec); + assertEquals(result.getVideoCodec(), videoCodec); + assertEquals(result.getAudioCodec(), audioCodec); assertEquals(result.getLocation(), location); assertEquals(result.isSnapshot(), isSnapshot); assertEquals(result.getMaxSize(), maxFileSize); diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/options/Camera1OptionsTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/options/Camera1OptionsTest.java index 4dbbe0b4..8574c9ef 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/options/Camera1OptionsTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/options/Camera1OptionsTest.java @@ -7,6 +7,7 @@ import android.hardware.Camera; import com.otaliastudios.cameraview.BaseTest; import com.otaliastudios.cameraview.CameraOptions; import com.otaliastudios.cameraview.controls.Audio; +import com.otaliastudios.cameraview.controls.AudioCodec; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.Flash; import com.otaliastudios.cameraview.controls.PictureFormat; @@ -244,10 +245,12 @@ public class Camera1OptionsTest extends BaseTest { Collection grids = o.getSupportedControls(Grid.class); Collection video = o.getSupportedControls(VideoCodec.class); + Collection audioCodecs = o.getSupportedControls(AudioCodec.class); Collection sessions = o.getSupportedControls(Mode.class); Collection