From b6f09d1a2fb2b7dcf97a3f00412bc9b34c2e48ff Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Tue, 10 Sep 2024 11:54:01 +0200 Subject: [PATCH 1/5] add camerauvcsource to rotation example --- app/build.gradle.kts | 3 +- .../streamer/rotation/CameraUvcSource.kt | 88 +++++++++++++++++++ .../pedro/streamer/rotation/CameraXSource.kt | 2 +- .../streamer/rotation/RotationActivity.kt | 27 ++++++ app/src/main/res/menu/rotation_menu.xml | 4 + app/src/main/res/values/strings.xml | 1 + gradle/libs.versions.toml | 2 + 7 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 258f78ca7..e36c43f4b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -9,7 +9,7 @@ android { defaultConfig { applicationId = "com.pedro.streamer" - minSdk = 16 + minSdk = 19 targetSdk = 34 versionCode = libs.versions.versionCode.get().toInt() versionName = libs.versions.versionName.get() @@ -34,6 +34,7 @@ android { } dependencies { + implementation(libs.uvcandroid) implementation(project(":library")) implementation(libs.androidx.constraintlayout) implementation(libs.androidx.appcompat) diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt new file mode 100644 index 000000000..771d2c20d --- /dev/null +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt @@ -0,0 +1,88 @@ +/* + * + * * Copyright (C) 2024 pedroSG94. + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package com.pedro.streamer.rotation + +import android.graphics.SurfaceTexture +import android.hardware.usb.UsbDevice +import android.view.Surface +import com.herohan.uvcapp.CameraHelper +import com.herohan.uvcapp.ICameraHelper +import com.pedro.library.util.sources.video.VideoSource + + +/** + * Created by pedro on 10/9/24. + */ +class CameraUvcSource: VideoSource() { + + private var cameraHelper: ICameraHelper? = null + private var running = false + private var surface: Surface? = null + + override fun create(width: Int, height: Int, fps: Int, rotation: Int): Boolean { + return true + } + + override fun start(surfaceTexture: SurfaceTexture) { + this.surfaceTexture = surfaceTexture + surface = Surface(surfaceTexture) + cameraHelper = CameraHelper() + cameraHelper?.setStateCallback(stateCallback) + running = true + } + + override fun stop() { + surface?.let { cameraHelper?.removeSurface(it) } + surface?.release() + surface = null + cameraHelper?.release() + cameraHelper = null + running = false + } + + override fun release() { + } + + override fun isRunning(): Boolean = running + + private val stateCallback: ICameraHelper.StateCallback = object : ICameraHelper.StateCallback { + override fun onAttach(device: UsbDevice) { + cameraHelper?.selectDevice(device) + } + + override fun onDeviceOpen(device: UsbDevice, isFirstOpen: Boolean) { + cameraHelper?.openCamera() + } + + override fun onCameraOpen(device: UsbDevice) { + cameraHelper?.startPreview() + surface?.let { cameraHelper?.addSurface(it, false) } + } + + override fun onCameraClose(device: UsbDevice) { + + } + + override fun onDeviceClose(device: UsbDevice) {} + + override fun onDetach(device: UsbDevice) {} + + override fun onCancel(device: UsbDevice) {} + } +} \ No newline at end of file diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt index fbecadd1c..0fa0d8223 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt @@ -118,5 +118,5 @@ class CameraXSource( } } - override fun getLifecycle(): Lifecycle = lifecycleRegistry + override val lifecycle: Lifecycle = lifecycleRegistry } \ No newline at end of file diff --git a/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt b/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt index 630a426e3..fdc59554c 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt @@ -24,11 +24,14 @@ import android.view.MenuItem import android.view.MotionEvent import android.view.View import android.view.View.OnTouchListener +import android.view.WindowManager import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity +import com.pedro.encoder.input.video.CameraHelper import com.pedro.library.util.sources.audio.MicrophoneSource import com.pedro.library.util.sources.video.Camera1Source import com.pedro.library.util.sources.video.Camera2Source +import com.pedro.library.view.OrientationForced import com.pedro.streamer.R import com.pedro.streamer.utils.FilterMenu import com.pedro.streamer.utils.setColor @@ -73,19 +76,28 @@ class RotationActivity : AppCompatActivity(), OnTouchListener { R.id.video_source_camera1 -> { currentVideoSource = updateMenuColor(currentVideoSource, item) cameraFragment.genericStream.changeVideoSource(Camera1Source(applicationContext)) + updateOrientation(false) } R.id.video_source_camera2 -> { currentVideoSource = updateMenuColor(currentVideoSource, item) cameraFragment.genericStream.changeVideoSource(Camera2Source(applicationContext)) + updateOrientation(false) } R.id.video_source_camerax -> { currentVideoSource = updateMenuColor(currentVideoSource, item) cameraFragment.genericStream.changeVideoSource(CameraXSource(applicationContext)) + updateOrientation(false) + } + R.id.video_source_camera_uvc -> { + currentVideoSource = updateMenuColor(currentVideoSource, item) + cameraFragment.genericStream.changeVideoSource(CameraUvcSource()) + updateOrientation(true) } R.id.video_source_bitmap -> { currentVideoSource = updateMenuColor(currentVideoSource, item) val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher) cameraFragment.genericStream.changeVideoSource(BitmapSource(bitmap)) + updateOrientation(false) } R.id.audio_source_microphone -> { currentAudioSource = updateMenuColor(currentAudioSource, item) @@ -125,4 +137,19 @@ class RotationActivity : AppCompatActivity(), OnTouchListener { item.setColor(this, R.color.appColor) return item } + + private fun updateOrientation(isUvc: Boolean) { + //UVC cameras can't adapt orientation depend of the device orientation so we need force use always landscape orientations + if (isUvc) { + cameraFragment.genericStream.getGlInterface().forceOrientation(OrientationForced.LANDSCAPE) + cameraFragment.genericStream.getGlInterface().autoHandleOrientation = false + cameraFragment.genericStream.getGlInterface().setCameraOrientation(0) + } else { //Reset orientation to the correct orientation depend of device orientation + cameraFragment.genericStream.getGlInterface().forceOrientation(OrientationForced.NONE) + cameraFragment.genericStream.getGlInterface().autoHandleOrientation = true + val orientation = CameraHelper.getCameraOrientation(this) + cameraFragment.genericStream.getGlInterface().setCameraOrientation(if (orientation == 0) 270 else orientation - 90) + cameraFragment.genericStream.getGlInterface().setIsPortrait(CameraHelper.isPortrait(this)) + } + } } \ No newline at end of file diff --git a/app/src/main/res/menu/rotation_menu.xml b/app/src/main/res/menu/rotation_menu.xml index d4d84c9c3..793cc2a06 100644 --- a/app/src/main/res/menu/rotation_menu.xml +++ b/app/src/main/res/menu/rotation_menu.xml @@ -14,6 +14,10 @@ android:title="@string/camerax" android:id="@+id/video_source_camerax" /> + Camera1 Camera2 CameraX + CameraUVC Bitmap diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1d164d1c7..bc913b202 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,6 +18,7 @@ annotation = "1.8.2" coroutines = "1.8.1" junit = "4.13.2" mockito = "5.4.0" +uvcandroid = "1.0.7" [libraries] androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "annotation" } @@ -31,6 +32,7 @@ junit = { module = "junit:junit", version.ref = "junit" } kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" } kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockito" } +uvcandroid = { module = "com.herohan:UVCAndroid", version.ref = "uvcandroid" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } From 94fa53a1e557a0cc56ebe3116aea0e22d953d084 Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Fri, 13 Sep 2024 01:37:20 +0200 Subject: [PATCH 2/5] set optimal resolution to camerauvcsource --- .../pedro/streamer/rotation/CameraUvcSource.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt index 771d2c20d..c19e3c7ff 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraUvcSource.kt @@ -20,15 +20,21 @@ package com.pedro.streamer.rotation import android.graphics.SurfaceTexture import android.hardware.usb.UsbDevice +import android.os.Build import android.view.Surface +import androidx.annotation.RequiresApi import com.herohan.uvcapp.CameraHelper import com.herohan.uvcapp.ICameraHelper +import com.pedro.encoder.input.video.Camera2ResolutionCalculator import com.pedro.library.util.sources.video.VideoSource +import com.serenegiant.usb.Size +import kotlin.math.abs /** * Created by pedro on 10/9/24. */ +@RequiresApi(Build.VERSION_CODES.LOLLIPOP) class CameraUvcSource: VideoSource() { private var cameraHelper: ICameraHelper? = null @@ -72,6 +78,8 @@ class CameraUvcSource: VideoSource() { override fun onCameraOpen(device: UsbDevice) { cameraHelper?.startPreview() + val resolution = getOptimalResolution() + if (resolution != null) cameraHelper?.previewSize = resolution surface?.let { cameraHelper?.addSurface(it, false) } } @@ -85,4 +93,12 @@ class CameraUvcSource: VideoSource() { override fun onCancel(device: UsbDevice) {} } + + private fun getOptimalResolution(): Size? { + val supportedSizes = cameraHelper?.supportedSizeList ?: return null + val supportedResolutions = supportedSizes.map { android.util.Size(it.width, it.height) }.toTypedArray() + val resolution = Camera2ResolutionCalculator.getOptimalResolution(android.util.Size(width, height), supportedResolutions) + val validSizes = supportedSizes.filter { it.width == resolution.width && it.height == resolution.height } + return validSizes.minByOrNull { abs(fps - it.fps) } + } } \ No newline at end of file From 7a70efa3f2e5907c0a1f166a6e7a0d524d5bf6bf Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Sat, 14 Sep 2024 19:20:42 +0200 Subject: [PATCH 3/5] move sources to encoder module --- .../com/pedro/streamer/rotation/BitmapSource.kt | 2 +- .../pedro/streamer/rotation/CameraFragment.kt | 4 ++-- .../com/pedro/streamer/rotation/CameraXSource.kt | 2 +- .../pedro/streamer/rotation/RotationActivity.kt | 6 +++--- .../com/pedro/streamer/screen/ScreenActivity.kt | 6 +++--- .../com/pedro/streamer/screen/ScreenService.kt | 12 ++++++------ .../-gl-stream-interface/force-orientation.html | 2 +- .../input}/sources/MediaProjectionHandler.kt | 2 +- .../encoder/input/sources}/OrientationForced.kt | 2 +- .../input}/sources/audio/AudioFileSource.kt | 2 +- .../encoder/input}/sources/audio/AudioSource.kt | 2 +- .../input}/sources/audio/InternalAudioSource.kt | 4 ++-- .../input}/sources/audio/MicrophoneSource.kt | 2 +- .../input}/sources/audio/MixAudioSource.kt | 2 +- .../input}/sources/audio/NoAudioSource.kt | 2 +- .../input}/sources/video/Camera1Source.kt | 2 +- .../input}/sources/video/Camera2Source.kt | 2 +- .../input}/sources/video/NoVideoSource.kt | 2 +- .../encoder/input}/sources/video/ScreenSource.kt | 4 ++-- .../input}/sources/video/VideoFileSource.kt | 5 ++++- .../encoder/input}/sources/video/VideoSource.kt | 4 +++- .../java/com/pedro/library/base/StreamBase.kt | 14 ++++++++------ .../com/pedro/library/generic/GenericStream.kt | 16 ++++++++-------- .../com/pedro/library/multiple/MultiStream.kt | 8 ++++---- .../java/com/pedro/library/rtmp/RtmpStream.kt | 12 ++++++------ .../java/com/pedro/library/rtsp/RtspStream.kt | 12 ++++++------ .../main/java/com/pedro/library/srt/SrtStream.kt | 12 ++++++------ .../main/java/com/pedro/library/udp/UdpStream.kt | 12 ++++++------ .../com/pedro/library/view/GlStreamInterface.kt | 2 +- 29 files changed, 83 insertions(+), 76 deletions(-) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/MediaProjectionHandler.kt (76%) rename {library/src/main/java/com/pedro/library/view => encoder/src/main/java/com/pedro/encoder/input/sources}/OrientationForced.kt (94%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/audio/AudioFileSource.kt (99%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/audio/AudioSource.kt (97%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/audio/InternalAudioSource.kt (97%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/audio/MicrophoneSource.kt (98%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/audio/MixAudioSource.kt (99%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/audio/NoAudioSource.kt (96%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/video/Camera1Source.kt (99%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/video/Camera2Source.kt (99%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/video/NoVideoSource.kt (96%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/video/ScreenSource.kt (97%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/video/VideoFileSource.kt (93%) rename {library/src/main/java/com/pedro/library/util => encoder/src/main/java/com/pedro/encoder/input}/sources/video/VideoSource.kt (89%) diff --git a/app/src/main/java/com/pedro/streamer/rotation/BitmapSource.kt b/app/src/main/java/com/pedro/streamer/rotation/BitmapSource.kt index 2fa414e85..85553d3f7 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/BitmapSource.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/BitmapSource.kt @@ -21,7 +21,7 @@ import android.graphics.Paint import android.graphics.SurfaceTexture import android.view.Surface import androidx.core.graphics.scale -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.video.VideoSource import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt index c18875d57..fd7578e5b 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt @@ -32,8 +32,8 @@ import androidx.fragment.app.Fragment import com.pedro.common.ConnectChecker import com.pedro.library.base.recording.RecordController import com.pedro.library.generic.GenericStream -import com.pedro.library.util.sources.video.Camera1Source -import com.pedro.library.util.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.Camera1Source +import com.pedro.encoder.input.sources.video.Camera2Source import com.pedro.streamer.R import com.pedro.streamer.utils.PathUtils import com.pedro.streamer.utils.toast diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt index fbecadd1c..deaf39844 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraXSource.kt @@ -33,7 +33,7 @@ import androidx.core.content.ContextCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.video.VideoSource import java.util.concurrent.ExecutionException import java.util.concurrent.Executors diff --git a/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt b/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt index 390472ee6..063058f8c 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/RotationActivity.kt @@ -27,9 +27,9 @@ import android.view.View import android.view.View.OnTouchListener import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera1Source -import com.pedro.library.util.sources.video.Camera2Source +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera1Source +import com.pedro.encoder.input.sources.video.Camera2Source import com.pedro.streamer.R import com.pedro.streamer.utils.FilterMenu import com.pedro.streamer.utils.toast diff --git a/app/src/main/java/com/pedro/streamer/screen/ScreenActivity.kt b/app/src/main/java/com/pedro/streamer/screen/ScreenActivity.kt index 3f0b28bb3..f32797620 100644 --- a/app/src/main/java/com/pedro/streamer/screen/ScreenActivity.kt +++ b/app/src/main/java/com/pedro/streamer/screen/ScreenActivity.kt @@ -28,9 +28,9 @@ import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import com.pedro.common.ConnectChecker import com.pedro.library.base.recording.RecordController -import com.pedro.library.util.sources.audio.MixAudioSource -import com.pedro.library.util.sources.audio.InternalAudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.audio.MixAudioSource +import com.pedro.encoder.input.sources.audio.InternalAudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource import com.pedro.streamer.R import com.pedro.streamer.utils.toast import com.pedro.streamer.utils.updateMenuColor diff --git a/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt b/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt index ade995e98..017ffbf94 100644 --- a/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt +++ b/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt @@ -31,12 +31,12 @@ import androidx.core.app.NotificationCompat import com.pedro.common.ConnectChecker import com.pedro.library.base.recording.RecordController import com.pedro.library.generic.GenericStream -import com.pedro.library.util.sources.audio.MixAudioSource -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.InternalAudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.NoVideoSource -import com.pedro.library.util.sources.video.ScreenSource +import com.pedro.encoder.input.sources.audio.MixAudioSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.InternalAudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.NoVideoSource +import com.pedro.encoder.input.sources.video.ScreenSource import com.pedro.streamer.R import com.pedro.streamer.utils.PathUtils import com.pedro.streamer.utils.toast diff --git a/docs/library/com.pedro.library.view/-gl-stream-interface/force-orientation.html b/docs/library/com.pedro.library.view/-gl-stream-interface/force-orientation.html index 5ab1022ed..97663cc7a 100644 --- a/docs/library/com.pedro.library.view/-gl-stream-interface/force-orientation.html +++ b/docs/library/com.pedro.library.view/-gl-stream-interface/force-orientation.html @@ -58,7 +58,7 @@
-
+

forceOrientation

diff --git a/library/src/main/java/com/pedro/library/util/sources/MediaProjectionHandler.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/MediaProjectionHandler.kt similarity index 76% rename from library/src/main/java/com/pedro/library/util/sources/MediaProjectionHandler.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/MediaProjectionHandler.kt index 1b87e72e3..b15a359c3 100644 --- a/library/src/main/java/com/pedro/library/util/sources/MediaProjectionHandler.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/MediaProjectionHandler.kt @@ -1,4 +1,4 @@ -package com.pedro.library.util.sources +package com.pedro.encoder.input.sources import android.media.projection.MediaProjection diff --git a/library/src/main/java/com/pedro/library/view/OrientationForced.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/OrientationForced.kt similarity index 94% rename from library/src/main/java/com/pedro/library/view/OrientationForced.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/OrientationForced.kt index 31b6ee900..a4c4bc85e 100644 --- a/library/src/main/java/com/pedro/library/view/OrientationForced.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/OrientationForced.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.view +package com.pedro.encoder.input.sources /** * Created by pedro on 16/2/24. diff --git a/library/src/main/java/com/pedro/library/util/sources/audio/AudioFileSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/AudioFileSource.kt similarity index 99% rename from library/src/main/java/com/pedro/library/util/sources/audio/AudioFileSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/audio/AudioFileSource.kt index dc20ddcf4..a2677c9c8 100644 --- a/library/src/main/java/com/pedro/library/util/sources/audio/AudioFileSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/AudioFileSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.audio +package com.pedro.encoder.input.sources.audio import android.content.Context import android.media.AudioFormat diff --git a/library/src/main/java/com/pedro/library/util/sources/audio/AudioSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/AudioSource.kt similarity index 97% rename from library/src/main/java/com/pedro/library/util/sources/audio/AudioSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/audio/AudioSource.kt index 49ee4ed92..acc00292f 100644 --- a/library/src/main/java/com/pedro/library/util/sources/audio/AudioSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/AudioSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.audio +package com.pedro.encoder.input.sources.audio import com.pedro.encoder.input.audio.GetMicrophoneData diff --git a/library/src/main/java/com/pedro/library/util/sources/audio/InternalAudioSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/InternalAudioSource.kt similarity index 97% rename from library/src/main/java/com/pedro/library/util/sources/audio/InternalAudioSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/audio/InternalAudioSource.kt index db5fa85a8..13a2bb256 100644 --- a/library/src/main/java/com/pedro/library/util/sources/audio/InternalAudioSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/InternalAudioSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.audio +package com.pedro.encoder.input.sources.audio import android.media.AudioAttributes import android.media.AudioPlaybackCaptureConfiguration @@ -28,7 +28,7 @@ import com.pedro.encoder.input.audio.CustomAudioEffect import com.pedro.encoder.input.audio.GetMicrophoneData import com.pedro.encoder.input.audio.MicrophoneManager import com.pedro.encoder.input.audio.VolumeEffect -import com.pedro.library.util.sources.MediaProjectionHandler +import com.pedro.encoder.input.sources.MediaProjectionHandler /** * Created by pedro on 12/1/24. diff --git a/library/src/main/java/com/pedro/library/util/sources/audio/MicrophoneSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MicrophoneSource.kt similarity index 98% rename from library/src/main/java/com/pedro/library/util/sources/audio/MicrophoneSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/audio/MicrophoneSource.kt index 73e2eb76b..224df0ac0 100644 --- a/library/src/main/java/com/pedro/library/util/sources/audio/MicrophoneSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MicrophoneSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.audio +package com.pedro.encoder.input.sources.audio import android.media.AudioDeviceInfo import android.media.MediaRecorder diff --git a/library/src/main/java/com/pedro/library/util/sources/audio/MixAudioSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt similarity index 99% rename from library/src/main/java/com/pedro/library/util/sources/audio/MixAudioSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt index 998d96b8c..0ce046a6f 100644 --- a/library/src/main/java/com/pedro/library/util/sources/audio/MixAudioSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt @@ -16,7 +16,7 @@ * */ -package com.pedro.library.util.sources.audio +package com.pedro.encoder.input.sources.audio import android.media.projection.MediaProjection import android.os.Build diff --git a/library/src/main/java/com/pedro/library/util/sources/audio/NoAudioSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/NoAudioSource.kt similarity index 96% rename from library/src/main/java/com/pedro/library/util/sources/audio/NoAudioSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/audio/NoAudioSource.kt index b522d6d95..256164294 100644 --- a/library/src/main/java/com/pedro/library/util/sources/audio/NoAudioSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/NoAudioSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.audio +package com.pedro.encoder.input.sources.audio import android.os.Build import androidx.annotation.RequiresApi diff --git a/library/src/main/java/com/pedro/library/util/sources/video/Camera1Source.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera1Source.kt similarity index 99% rename from library/src/main/java/com/pedro/library/util/sources/video/Camera1Source.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera1Source.kt index 02b879d62..bba5a960f 100644 --- a/library/src/main/java/com/pedro/library/util/sources/video/Camera1Source.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera1Source.kt @@ -16,7 +16,7 @@ @file:Suppress("DEPRECATION") -package com.pedro.library.util.sources.video +package com.pedro.encoder.input.sources.video import android.content.Context import android.graphics.SurfaceTexture diff --git a/library/src/main/java/com/pedro/library/util/sources/video/Camera2Source.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt similarity index 99% rename from library/src/main/java/com/pedro/library/util/sources/video/Camera2Source.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt index 5d9bcfd0c..f1c195096 100644 --- a/library/src/main/java/com/pedro/library/util/sources/video/Camera2Source.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.video +package com.pedro.encoder.input.sources.video import android.content.Context import android.graphics.SurfaceTexture diff --git a/library/src/main/java/com/pedro/library/util/sources/video/NoVideoSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/NoVideoSource.kt similarity index 96% rename from library/src/main/java/com/pedro/library/util/sources/video/NoVideoSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/video/NoVideoSource.kt index 33938e6e8..d2058bb5b 100644 --- a/library/src/main/java/com/pedro/library/util/sources/video/NoVideoSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/NoVideoSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.video +package com.pedro.encoder.input.sources.video import android.graphics.SurfaceTexture import android.os.Build diff --git a/library/src/main/java/com/pedro/library/util/sources/video/ScreenSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/ScreenSource.kt similarity index 97% rename from library/src/main/java/com/pedro/library/util/sources/video/ScreenSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/video/ScreenSource.kt index aa911d065..150b99da8 100644 --- a/library/src/main/java/com/pedro/library/util/sources/video/ScreenSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/ScreenSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.video +package com.pedro.encoder.input.sources.video import android.content.Context import android.graphics.SurfaceTexture @@ -26,7 +26,7 @@ import android.os.Handler import android.os.HandlerThread import android.view.Surface import androidx.annotation.RequiresApi -import com.pedro.library.util.sources.MediaProjectionHandler +import com.pedro.encoder.input.sources.MediaProjectionHandler /** * Created by pedro on 11/1/24. diff --git a/library/src/main/java/com/pedro/library/util/sources/video/VideoFileSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/VideoFileSource.kt similarity index 93% rename from library/src/main/java/com/pedro/library/util/sources/video/VideoFileSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/video/VideoFileSource.kt index 106e9f3f0..5f0657aab 100644 --- a/library/src/main/java/com/pedro/library/util/sources/video/VideoFileSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/VideoFileSource.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.library.util.sources.video +package com.pedro.encoder.input.sources.video import android.content.Context import android.graphics.SurfaceTexture @@ -22,6 +22,7 @@ import android.net.Uri import android.view.Surface import com.pedro.encoder.input.decoder.DecoderInterface import com.pedro.encoder.input.decoder.VideoDecoder +import com.pedro.encoder.input.sources.OrientationForced import java.io.IOException /** @@ -75,6 +76,8 @@ class VideoFileSource( override fun isRunning(): Boolean = running + override fun getOrientationConfig(): OrientationForced = OrientationForced.LANDSCAPE + fun moveTo(time: Double) { videoDecoder.moveTo(time) } diff --git a/library/src/main/java/com/pedro/library/util/sources/video/VideoSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/VideoSource.kt similarity index 89% rename from library/src/main/java/com/pedro/library/util/sources/video/VideoSource.kt rename to encoder/src/main/java/com/pedro/encoder/input/sources/video/VideoSource.kt index a76212c8c..42a70cf01 100644 --- a/library/src/main/java/com/pedro/library/util/sources/video/VideoSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/VideoSource.kt @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.pedro.library.util.sources.video +package com.pedro.encoder.input.sources.video import android.graphics.SurfaceTexture +import com.pedro.encoder.input.sources.OrientationForced /** * Created by pedro on 11/1/24. @@ -44,4 +45,5 @@ abstract class VideoSource { abstract fun stop() abstract fun release() abstract fun isRunning(): Boolean + open fun getOrientationConfig() = OrientationForced.NONE } \ No newline at end of file diff --git a/library/src/main/java/com/pedro/library/base/StreamBase.kt b/library/src/main/java/com/pedro/library/base/StreamBase.kt index 1ed1a601a..57d7303b1 100644 --- a/library/src/main/java/com/pedro/library/base/StreamBase.kt +++ b/library/src/main/java/com/pedro/library/base/StreamBase.kt @@ -41,9 +41,9 @@ import com.pedro.library.base.recording.BaseRecordController import com.pedro.library.base.recording.RecordController import com.pedro.library.util.AndroidMuxerRecordController import com.pedro.library.util.FpsListener -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.video.NoVideoSource -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.video.NoVideoSource +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.StreamBaseClient import com.pedro.library.view.GlStreamInterface import java.nio.ByteBuffer @@ -59,9 +59,9 @@ import java.nio.ByteBuffer */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) abstract class StreamBase( - context: Context, - vSource: VideoSource, - aSource: AudioSource + context: Context, + vSource: VideoSource, + aSource: AudioSource ) { private val getMicrophoneData = object: GetMicrophoneData { @@ -112,6 +112,7 @@ abstract class StreamBase( val isPortrait = rotation == 90 || rotation == 270 glInterface.setIsPortrait(isPortrait) glInterface.setCameraOrientation(if (rotation == 0) 270 else rotation - 90) + glInterface.forceOrientation(videoSource.getOrientationConfig()) return videoEncoder.prepareVideoEncoder(width, height, fps, bitrate, rotation, iFrameInterval, FormatVideoEncoder.SURFACE, profile, level) } @@ -313,6 +314,7 @@ abstract class StreamBase( videoSource.stop() videoSource.release() if (wasRunning) source.start(glInterface.surfaceTexture) + glInterface.forceOrientation(source.getOrientationConfig()) videoSource = source } diff --git a/library/src/main/java/com/pedro/library/generic/GenericStream.kt b/library/src/main/java/com/pedro/library/generic/GenericStream.kt index b84ecd0e3..4b70e2e84 100644 --- a/library/src/main/java/com/pedro/library/generic/GenericStream.kt +++ b/library/src/main/java/com/pedro/library/generic/GenericStream.kt @@ -25,10 +25,10 @@ import com.pedro.common.ConnectChecker import com.pedro.common.VideoCodec import com.pedro.common.onMainThreadHandler import com.pedro.library.base.StreamBase -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera2Source -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.GenericStreamClient import com.pedro.library.util.streamclient.RtmpStreamClient import com.pedro.library.util.streamclient.RtspStreamClient @@ -49,10 +49,10 @@ import java.nio.ByteBuffer */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class GenericStream( - context: Context, - private val connectChecker: ConnectChecker, - videoSource: VideoSource, - audioSource: AudioSource + context: Context, + private val connectChecker: ConnectChecker, + videoSource: VideoSource, + audioSource: AudioSource ): StreamBase(context, videoSource, audioSource) { private val streamClientListener = object: StreamClientListener { diff --git a/library/src/main/java/com/pedro/library/multiple/MultiStream.kt b/library/src/main/java/com/pedro/library/multiple/MultiStream.kt index 38b7a477e..bba34da75 100644 --- a/library/src/main/java/com/pedro/library/multiple/MultiStream.kt +++ b/library/src/main/java/com/pedro/library/multiple/MultiStream.kt @@ -23,10 +23,10 @@ import com.pedro.common.AudioCodec import com.pedro.common.ConnectChecker import com.pedro.common.VideoCodec import com.pedro.library.base.StreamBase -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera2Source -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.RtmpStreamClient import com.pedro.library.util.streamclient.RtspStreamClient import com.pedro.library.util.streamclient.SrtStreamClient diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt b/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt index c74e5eef6..027091bb5 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt @@ -24,10 +24,10 @@ import com.pedro.common.AudioCodec import com.pedro.common.ConnectChecker import com.pedro.common.VideoCodec import com.pedro.library.base.StreamBase -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera2Source -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.RtmpStreamClient import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.rtmp.rtmp.RtmpClient @@ -42,8 +42,8 @@ import java.nio.ByteBuffer @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class RtmpStream( - context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, - audioSource: AudioSource + context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, + audioSource: AudioSource ): StreamBase(context, videoSource, audioSource) { private val rtmpClient = RtmpClient(connectChecker) diff --git a/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt b/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt index 76bcc2014..a5852da41 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt +++ b/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt @@ -24,10 +24,10 @@ import com.pedro.common.AudioCodec import com.pedro.common.ConnectChecker import com.pedro.common.VideoCodec import com.pedro.library.base.StreamBase -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera2Source -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.RtspStreamClient import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.rtsp.rtsp.RtspClient @@ -42,8 +42,8 @@ import java.nio.ByteBuffer @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class RtspStream( - context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, - audioSource: AudioSource + context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, + audioSource: AudioSource ): StreamBase(context, videoSource, audioSource) { private val rtspClient = RtspClient(connectChecker) diff --git a/library/src/main/java/com/pedro/library/srt/SrtStream.kt b/library/src/main/java/com/pedro/library/srt/SrtStream.kt index fbc0fa6a1..bdad6447a 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtStream.kt +++ b/library/src/main/java/com/pedro/library/srt/SrtStream.kt @@ -24,10 +24,10 @@ import com.pedro.common.AudioCodec import com.pedro.common.ConnectChecker import com.pedro.common.VideoCodec import com.pedro.library.base.StreamBase -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera2Source -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.SrtStreamClient import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.srt.srt.SrtClient @@ -42,8 +42,8 @@ import java.nio.ByteBuffer @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class SrtStream( - context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, - audioSource: AudioSource + context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, + audioSource: AudioSource ): StreamBase(context, videoSource, audioSource) { private val srtClient = SrtClient(connectChecker) diff --git a/library/src/main/java/com/pedro/library/udp/UdpStream.kt b/library/src/main/java/com/pedro/library/udp/UdpStream.kt index 91573b68c..a97ea6c50 100644 --- a/library/src/main/java/com/pedro/library/udp/UdpStream.kt +++ b/library/src/main/java/com/pedro/library/udp/UdpStream.kt @@ -24,10 +24,10 @@ import com.pedro.common.AudioCodec import com.pedro.common.ConnectChecker import com.pedro.common.VideoCodec import com.pedro.library.base.StreamBase -import com.pedro.library.util.sources.audio.AudioSource -import com.pedro.library.util.sources.audio.MicrophoneSource -import com.pedro.library.util.sources.video.Camera2Source -import com.pedro.library.util.sources.video.VideoSource +import com.pedro.encoder.input.sources.audio.AudioSource +import com.pedro.encoder.input.sources.audio.MicrophoneSource +import com.pedro.encoder.input.sources.video.Camera2Source +import com.pedro.encoder.input.sources.video.VideoSource import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.library.util.streamclient.UdpStreamClient import com.pedro.udp.UdpClient @@ -42,8 +42,8 @@ import java.nio.ByteBuffer @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class UdpStream( - context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, - audioSource: AudioSource + context: Context, connectChecker: ConnectChecker, videoSource: VideoSource, + audioSource: AudioSource ): StreamBase(context, videoSource, audioSource) { private val streamClientListener = object: StreamClientListener { diff --git a/library/src/main/java/com/pedro/library/view/GlStreamInterface.kt b/library/src/main/java/com/pedro/library/view/GlStreamInterface.kt index bf6ee8497..e7d8e9d72 100644 --- a/library/src/main/java/com/pedro/library/view/GlStreamInterface.kt +++ b/library/src/main/java/com/pedro/library/view/GlStreamInterface.kt @@ -30,6 +30,7 @@ import com.pedro.encoder.input.gl.SurfaceManager import com.pedro.encoder.input.gl.render.MainRender import com.pedro.encoder.input.gl.render.filters.BaseFilterRender import com.pedro.encoder.input.gl.render.filters.NoFilterRender +import com.pedro.encoder.input.sources.OrientationForced import com.pedro.encoder.input.video.FpsLimiter import com.pedro.encoder.utils.gl.AspectRatioMode import com.pedro.encoder.utils.gl.GlUtil @@ -37,7 +38,6 @@ import com.pedro.library.util.Filter import com.pedro.library.util.SensorRotationManager import java.util.concurrent.BlockingQueue import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.atomic.AtomicBoolean From 35d6e8902f6a9852836b81dd1be15806a832dc21 Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Sat, 14 Sep 2024 19:50:38 +0200 Subject: [PATCH 4/5] change include settings --- settings.gradle.kts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/settings.gradle.kts b/settings.gradle.kts index 94c584dde..dba9a2a53 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,5 +16,4 @@ dependencyResolutionManagement { } rootProject.name = "RootEncoder" -include(":app", ":rtmp", ":encoder", ":rtsp", ":library", ":srt", ":udp", ":common") -include(":extra-sources") +include(":app", ":rtmp", ":encoder", ":rtsp", ":library", ":srt", ":udp", ":common", ":extra-sources") From 9cc8a6128b8569dd445a2c6fd32dfd140a8da4ac Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Mon, 16 Sep 2024 21:46:59 +0200 Subject: [PATCH 5/5] update mix audio info and add more config --- .../input/sources/audio/MixAudioSource.kt | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt index 0ce046a6f..36acb5f36 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/audio/MixAudioSource.kt @@ -18,6 +18,8 @@ package com.pedro.encoder.input.sources.audio +import android.media.AudioDeviceInfo +import android.media.MediaRecorder import android.media.projection.MediaProjection import android.os.Build import androidx.annotation.RequiresApi @@ -45,21 +47,31 @@ import java.util.concurrent.TimeUnit * This is to avoid echo in microphone track. * * Recommended increase microphone volume to 2f, - * because the internal audio normally is higher and you can't hear audio track properly + * because the internal audio normally is higher and you can't hear audio track properly. + * + * Tested in 2 devices (Android 12 and Android 14). This could change depend of the model or not, I'm not sure: + * MediaRecorder.AudioSource.DEFAULT, MediaRecorder.AudioSource.MIC -> If other app open the microphone you receive buffers with silence from the microphone until the other app release the microphone (maybe you need close the app). + * MediaRecorder.AudioSource.CAMCORDER -> Block the access to microphone to others apps. Others apps can't instantiate the microphone. + * MediaRecorder.AudioSource.VOICE_COMMUNICATION -> Block the access to microphone to others apps. Others apps can instantiate the microphone but receive buffers with silence from the microphone. */ @RequiresApi(Build.VERSION_CODES.Q) class MixAudioSource( - mediaProjection: MediaProjection + mediaProjection: MediaProjection, + microphoneAudioSource: Int = MediaRecorder.AudioSource.DEFAULT ): AudioSource() { private val microphoneVolumeEffect = VolumeEffect() private val internalVolumeEffect = VolumeEffect() - private val microphone = MicrophoneSource().apply { setAudioEffect(microphoneVolumeEffect) } - private val internal = InternalAudioSource(mediaProjection).apply { setAudioEffect(internalVolumeEffect) } + private val microphone = MicrophoneSource(audioSource = microphoneAudioSource).apply { + setAudioEffect(microphoneVolumeEffect) + } + private val internal = InternalAudioSource(mediaProjection).apply { + setAudioEffect(internalVolumeEffect) + } private val scope = CoroutineScope(Dispatchers.IO) - private val microphoneQueue: BlockingQueue = LinkedBlockingQueue(500) - private val internalQueue: BlockingQueue = LinkedBlockingQueue(500) + private val microphoneQueue: BlockingQueue = LinkedBlockingQueue(2) + private val internalQueue: BlockingQueue = LinkedBlockingQueue(2) private var running = false //We need read with a higher buffer to get enough time to mix it private val inputSize = AudioEncoder.inputSize @@ -73,8 +85,8 @@ class MixAudioSource( if (!isRunning()) { microphoneQueue.clear() internalQueue.clear() - microphone.start(callback1) - internal.start(callback2) + microphone.start(microphoneCallback) + internal.start(internalCallback) running = true scope.launch { val min = Byte.MIN_VALUE.toInt() @@ -82,17 +94,17 @@ class MixAudioSource( while (running) { runCatching { - val frame1 = async { runInterruptible { microphoneQueue.poll(1, TimeUnit.SECONDS) } } - val frame2 = async { runInterruptible { internalQueue.poll(1, TimeUnit.SECONDS) } } - val r = awaitAll(frame1, frame2) + val microphoneFrame = async { runInterruptible { microphoneQueue.poll(1, TimeUnit.SECONDS) } } + val internalFrame = async { runInterruptible { internalQueue.poll(1, TimeUnit.SECONDS) } } + val result = awaitAll(microphoneFrame, internalFrame) async { - val microphoneBuffer = r[0]?.buffer ?: return@async - val internalBuffer = r[1]?.buffer ?: return@async + val microphoneBuffer = result[0]?.buffer ?: return@async + val internalBuffer = result[1]?.buffer ?: return@async val mixBuffer = ByteArray(inputSize) for (i in mixBuffer.indices) { //mix buffers with same config mixBuffer[i] = (microphoneBuffer[i] + internalBuffer[i]).coerceIn(min, max).toByte() } - getMicrophoneData.inputPCMData(Frame(mixBuffer, 0, mixBuffer.size, r[0].timeStamp)) + getMicrophoneData.inputPCMData(Frame(mixBuffer, 0, mixBuffer.size, result[0].timeStamp)) } }.exceptionOrNull() } @@ -116,13 +128,13 @@ class MixAudioSource( override fun isRunning(): Boolean = running - private val callback1 = object: GetMicrophoneData { + private val microphoneCallback = object: GetMicrophoneData { override fun inputPCMData(frame: Frame) { microphoneQueue.trySend(frame) } } - private val callback2 = object: GetMicrophoneData { + private val internalCallback = object: GetMicrophoneData { override fun inputPCMData(frame: Frame) { internalQueue.trySend(frame) } @@ -155,4 +167,8 @@ class MixAudioSource( } fun isInternalAudioMuted(): Boolean = internal.isMuted() + + fun setMicrophonePreferredDevice(deviceInfo: AudioDeviceInfo?) { + microphone.setPreferredDevice(deviceInfo) + } } \ No newline at end of file