From 0d35e08fa854c4310a8c707c7d87d8c2a5e41b87 Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 1 Nov 2023 11:09:57 +0100 Subject: [PATCH] move setVideoCodec to base --- .../java/com/pedro/library/base/Camera1Base.java | 11 ++++++++++- .../java/com/pedro/library/base/Camera2Base.java | 10 ++++++++++ .../java/com/pedro/library/base/DisplayBase.java | 10 ++++++++++ .../java/com/pedro/library/base/FromFileBase.java | 10 ++++++++++ .../java/com/pedro/library/base/StreamBase.kt | 12 ++++++++++++ .../pedro/library/multiple/MultiRtpCamera1.java | 11 +++++++++++ .../pedro/library/multiple/MultiRtpCamera2.java | 11 +++++++++++ .../pedro/library/multiple/MultiRtpDisplay.java | 11 +++++++++++ .../java/com/pedro/library/rtmp/RtmpCamera1.java | 11 ++++------- .../java/com/pedro/library/rtmp/RtmpCamera2.java | 11 ++++------- .../java/com/pedro/library/rtmp/RtmpDisplay.java | 11 ++++------- .../java/com/pedro/library/rtmp/RtmpFromFile.java | 11 ++++------- .../java/com/pedro/library/rtmp/RtmpStream.kt | 9 +++------ .../java/com/pedro/library/rtsp/RtspCamera1.java | 11 ++++------- .../java/com/pedro/library/rtsp/RtspCamera2.java | 10 ++++------ .../java/com/pedro/library/rtsp/RtspDisplay.java | 10 ++++------ .../java/com/pedro/library/rtsp/RtspFromFile.java | 10 ++++------ .../java/com/pedro/library/rtsp/RtspStream.kt | 8 +++----- .../java/com/pedro/library/srt/SrtCamera1.java | 11 ++++------- .../java/com/pedro/library/srt/SrtCamera2.java | 11 ++++------- .../java/com/pedro/library/srt/SrtDisplay.java | 11 ++++------- .../java/com/pedro/library/srt/SrtFromFile.java | 11 ++++------- .../main/java/com/pedro/library/srt/SrtStream.kt | 9 +++------ .../java/com/pedro/library/util/VideoCodec.kt | 8 ++++++++ .../main/java/com/pedro/rtsp/rtsp/RtspClient.kt | 8 ++++++-- .../pedro/rtsp/rtsp/commands/CommandsManager.kt | 15 ++++++++++++--- 26 files changed, 168 insertions(+), 104 deletions(-) create mode 100644 library/src/main/java/com/pedro/library/util/VideoCodec.kt diff --git a/library/src/main/java/com/pedro/library/base/Camera1Base.java b/library/src/main/java/com/pedro/library/base/Camera1Base.java index d65b6f3cf..8311c51c0 100644 --- a/library/src/main/java/com/pedro/library/base/Camera1Base.java +++ b/library/src/main/java/com/pedro/library/base/Camera1Base.java @@ -55,7 +55,7 @@ import com.pedro.library.base.recording.RecordController; import com.pedro.library.util.AndroidMuxerRecordController; import com.pedro.library.util.FpsListener; -import com.pedro.library.util.streamclient.RtmpStreamClient; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.library.view.GlInterface; import com.pedro.library.view.LightOpenGlView; @@ -1009,4 +1009,13 @@ public void onVideoFormat(@NonNull MediaFormat mediaFormat) { }; public abstract StreamBaseClient getStreamClient(); + + public void setVideoCodec(VideoCodec codec) { + recordController.setVideoMime( + codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + videoEncoder.setType(codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + setVideoCodecImp(codec); + } + + protected abstract void setVideoCodecImp(VideoCodec codec); } \ No newline at end of file diff --git a/library/src/main/java/com/pedro/library/base/Camera2Base.java b/library/src/main/java/com/pedro/library/base/Camera2Base.java index 916918c96..cd9dbbbc1 100644 --- a/library/src/main/java/com/pedro/library/base/Camera2Base.java +++ b/library/src/main/java/com/pedro/library/base/Camera2Base.java @@ -56,6 +56,7 @@ import com.pedro.library.base.recording.RecordController; import com.pedro.library.util.AndroidMuxerRecordController; import com.pedro.library.util.FpsListener; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.library.view.GlInterface; import com.pedro.library.view.LightOpenGlView; @@ -1055,4 +1056,13 @@ public void onVideoFormat(@NonNull MediaFormat mediaFormat) { }; public abstract StreamBaseClient getStreamClient(); + + public void setVideoCodec(VideoCodec codec) { + recordController.setVideoMime( + codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + videoEncoder.setType(codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + setVideoCodecImp(codec); + } + + protected abstract void setVideoCodecImp(VideoCodec codec); } diff --git a/library/src/main/java/com/pedro/library/base/DisplayBase.java b/library/src/main/java/com/pedro/library/base/DisplayBase.java index a8d69c1c1..d201d4b42 100644 --- a/library/src/main/java/com/pedro/library/base/DisplayBase.java +++ b/library/src/main/java/com/pedro/library/base/DisplayBase.java @@ -52,6 +52,7 @@ import com.pedro.library.base.recording.RecordController; import com.pedro.library.util.AndroidMuxerRecordController; import com.pedro.library.util.FpsListener; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.library.view.GlInterface; import com.pedro.library.view.OffScreenGlThread; @@ -604,5 +605,14 @@ public void onVideoFormat(@NonNull MediaFormat mediaFormat) { }; public abstract StreamBaseClient getStreamClient(); + + public void setVideoCodec(VideoCodec codec) { + recordController.setVideoMime( + codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + videoEncoder.setType(codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + setVideoCodecImp(codec); + } + + protected abstract void setVideoCodecImp(VideoCodec codec); } diff --git a/library/src/main/java/com/pedro/library/base/FromFileBase.java b/library/src/main/java/com/pedro/library/base/FromFileBase.java index 207488e44..3265b0cc8 100644 --- a/library/src/main/java/com/pedro/library/base/FromFileBase.java +++ b/library/src/main/java/com/pedro/library/base/FromFileBase.java @@ -46,6 +46,7 @@ import com.pedro.library.base.recording.RecordController; import com.pedro.library.util.AndroidMuxerRecordController; import com.pedro.library.util.FpsListener; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.library.view.GlInterface; import com.pedro.library.view.LightOpenGlView; @@ -673,4 +674,13 @@ public void onLoop() { }; public abstract StreamBaseClient getStreamClient(); + + public void setVideoCodec(VideoCodec codec) { + recordController.setVideoMime( + codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + videoEncoder.setType(codec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + setVideoCodecImp(codec); + } + + protected abstract void setVideoCodecImp(VideoCodec codec); } 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 57be4dba2..3fc40a17b 100644 --- a/library/src/main/java/com/pedro/library/base/StreamBase.kt +++ b/library/src/main/java/com/pedro/library/base/StreamBase.kt @@ -19,12 +19,14 @@ import com.pedro.encoder.audio.AudioEncoder import com.pedro.encoder.audio.GetAacData import com.pedro.encoder.input.audio.GetMicrophoneData import com.pedro.encoder.input.video.CameraHelper +import com.pedro.encoder.utils.CodecUtil import com.pedro.encoder.video.FormatVideoEncoder import com.pedro.encoder.video.GetVideoData import com.pedro.encoder.video.VideoEncoder 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.VideoCodec import com.pedro.library.util.sources.AudioManager import com.pedro.library.util.sources.VideoManager import com.pedro.library.util.streamclient.StreamBaseClient @@ -543,4 +545,14 @@ abstract class StreamBase( protected abstract fun getAacDataRtp(aacBuffer: ByteBuffer, info: MediaCodec.BufferInfo) abstract fun getStreamClient(): StreamBaseClient + + fun setVideoCodec(codec: VideoCodec) { + recordController.setVideoMime( + if (codec === VideoCodec.H265) CodecUtil.H265_MIME else CodecUtil.H264_MIME + ) + videoEncoder.type = if (codec === VideoCodec.H265) CodecUtil.H265_MIME else CodecUtil.H264_MIME + setVideoCodecImp(codec) + } + + protected abstract fun setVideoCodecImp(codec: VideoCodec) } \ No newline at end of file diff --git a/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera1.java b/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera1.java index 31937ff2f..66a052e76 100644 --- a/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera1.java +++ b/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera1.java @@ -26,6 +26,7 @@ import androidx.annotation.RequiresApi; import com.pedro.library.base.Camera1Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; @@ -449,6 +450,16 @@ public StreamBaseClient getStreamClient() { return null; } + @Override + protected void setVideoCodecImp(VideoCodec codec) { + for (RtmpClient rtmpClient: rtmpClients) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); + } + for (RtspClient rtspClient: rtspClients) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); + } + } + public void setLogs(boolean enable) { for (RtmpClient rtmpClient: rtmpClients) { rtmpClient.setLogs(enable); diff --git a/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera2.java b/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera2.java index d97ba7f20..ce73dc39d 100644 --- a/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera2.java +++ b/library/src/main/java/com/pedro/library/multiple/MultiRtpCamera2.java @@ -26,6 +26,7 @@ import androidx.annotation.RequiresApi; import com.pedro.library.base.Camera2Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; @@ -449,6 +450,16 @@ public StreamBaseClient getStreamClient() { return null; } + @Override + protected void setVideoCodecImp(VideoCodec codec) { + for (RtmpClient rtmpClient: rtmpClients) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); + } + for (RtspClient rtspClient: rtspClients) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); + } + } + public void setLogs(boolean enable) { for (RtmpClient rtmpClient: rtmpClients) { rtmpClient.setLogs(enable); diff --git a/library/src/main/java/com/pedro/library/multiple/MultiRtpDisplay.java b/library/src/main/java/com/pedro/library/multiple/MultiRtpDisplay.java index c8ed9492b..02eed6a4c 100644 --- a/library/src/main/java/com/pedro/library/multiple/MultiRtpDisplay.java +++ b/library/src/main/java/com/pedro/library/multiple/MultiRtpDisplay.java @@ -24,6 +24,7 @@ import androidx.annotation.RequiresApi; import com.pedro.library.base.DisplayBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.StreamBaseClient; import com.pedro.rtmp.flv.video.ProfileIop; import com.pedro.rtmp.rtmp.RtmpClient; @@ -375,6 +376,16 @@ public StreamBaseClient getStreamClient() { return null; } + @Override + protected void setVideoCodecImp(VideoCodec codec) { + for (RtmpClient rtmpClient: rtmpClients) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); + } + for (RtspClient rtspClient: rtspClients) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); + } + } + public void setLogs(boolean enable) { for (RtmpClient rtmpClient : rtmpClients) { rtmpClient.setLogs(enable); diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java index f85eff3cc..29460e6cd 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java @@ -24,14 +24,13 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera1Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtmpStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.rtmp.rtmp.RtmpClient; -import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; import java.nio.ByteBuffer; @@ -87,11 +86,9 @@ public RtmpStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - rtmpClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java index 2edbac1df..743c7eb34 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java @@ -24,14 +24,13 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera2Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtmpStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.rtmp.rtmp.RtmpClient; -import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; import java.nio.ByteBuffer; @@ -96,11 +95,9 @@ public RtmpStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - rtmpClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java b/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java index eed6a6eee..0904e7a93 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java @@ -22,12 +22,11 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.DisplayBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtmpStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.rtmp.rtmp.RtmpClient; -import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; import java.nio.ByteBuffer; @@ -56,11 +55,9 @@ public RtmpStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - rtmpClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java b/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java index dee7dd74d..ec5aafae1 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java @@ -24,14 +24,13 @@ import com.pedro.encoder.input.decoder.AudioDecoderInterface; import com.pedro.encoder.input.decoder.VideoDecoderInterface; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.FromFileBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtmpStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.rtmp.rtmp.RtmpClient; -import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; import java.nio.ByteBuffer; @@ -82,11 +81,9 @@ public RtmpStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - rtmpClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtmpClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtmp.rtmp.VideoCodec.H264 : com.pedro.rtmp.rtmp.VideoCodec.H265); } @Override 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 5f1f795b7..53177599b 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt @@ -4,14 +4,13 @@ import android.content.Context import android.media.MediaCodec import android.os.Build import androidx.annotation.RequiresApi -import com.pedro.encoder.utils.CodecUtil import com.pedro.library.base.StreamBase +import com.pedro.library.util.VideoCodec import com.pedro.library.util.sources.AudioManager import com.pedro.library.util.sources.VideoManager import com.pedro.library.util.streamclient.RtmpStreamClient import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.rtmp.rtmp.RtmpClient -import com.pedro.rtmp.rtmp.VideoCodec import com.pedro.rtmp.utils.ConnectCheckerRtmp import java.nio.ByteBuffer @@ -39,10 +38,8 @@ class RtmpStream( constructor(context: Context, connectCheckerRtmp: ConnectCheckerRtmp): this(context, connectCheckerRtmp, VideoManager.Source.CAMERA2, AudioManager.Source.MICROPHONE) - fun setVideoCodec(videoCodec: VideoCodec) { - val mime = if (videoCodec === VideoCodec.H265) CodecUtil.H265_MIME else CodecUtil.H264_MIME - super.setVideoMime(mime) - rtmpClient.setVideoCodec(videoCodec) + override fun setVideoCodecImp(codec: VideoCodec) { + rtmpClient.setVideoCodec(if (codec === VideoCodec.H264) com.pedro.rtmp.rtmp.VideoCodec.H264 else com.pedro.rtmp.rtmp.VideoCodec.H265) } override fun audioInfo(sampleRate: Int, isStereo: Boolean) { diff --git a/library/src/main/java/com/pedro/library/rtsp/RtspCamera1.java b/library/src/main/java/com/pedro/library/rtsp/RtspCamera1.java index c508f7534..fd823cc87 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspCamera1.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspCamera1.java @@ -24,14 +24,13 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera1Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtspStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.rtsp.rtsp.RtspClient; -import com.pedro.rtsp.rtsp.VideoCodec; import com.pedro.rtsp.utils.ConnectCheckerRtsp; import java.nio.ByteBuffer; @@ -87,11 +86,9 @@ public RtspStreamClient getStreamClient() { return streamClient; } - - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/rtsp/RtspCamera2.java b/library/src/main/java/com/pedro/library/rtsp/RtspCamera2.java index 6d5de07e2..85869f2c7 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspCamera2.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspCamera2.java @@ -24,14 +24,13 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera2Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtspStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.rtsp.rtsp.RtspClient; -import com.pedro.rtsp.rtsp.VideoCodec; import com.pedro.rtsp.utils.ConnectCheckerRtsp; import java.nio.ByteBuffer; @@ -96,10 +95,9 @@ public RtspStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/rtsp/RtspDisplay.java b/library/src/main/java/com/pedro/library/rtsp/RtspDisplay.java index b0c1b94c2..96d5d0e83 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspDisplay.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspDisplay.java @@ -22,12 +22,11 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.DisplayBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtspStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.rtsp.rtsp.RtspClient; -import com.pedro.rtsp.rtsp.VideoCodec; import com.pedro.rtsp.utils.ConnectCheckerRtsp; import java.nio.ByteBuffer; @@ -56,10 +55,9 @@ public RtspStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/rtsp/RtspFromFile.java b/library/src/main/java/com/pedro/library/rtsp/RtspFromFile.java index 6452ffe65..727a61bc2 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspFromFile.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspFromFile.java @@ -24,14 +24,13 @@ import com.pedro.encoder.input.decoder.AudioDecoderInterface; import com.pedro.encoder.input.decoder.VideoDecoderInterface; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.FromFileBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.RtspStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.rtsp.rtsp.RtspClient; -import com.pedro.rtsp.rtsp.VideoCodec; import com.pedro.rtsp.utils.ConnectCheckerRtsp; import java.nio.ByteBuffer; @@ -82,10 +81,9 @@ public RtspStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + rtspClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.rtsp.rtsp.VideoCodec.H264 : com.pedro.rtsp.rtsp.VideoCodec.H265); } @Override 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 3922daceb..dfba31e97 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt +++ b/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt @@ -4,14 +4,13 @@ import android.content.Context import android.media.MediaCodec import android.os.Build import androidx.annotation.RequiresApi -import com.pedro.encoder.utils.CodecUtil import com.pedro.library.base.StreamBase +import com.pedro.library.util.VideoCodec import com.pedro.library.util.sources.AudioManager import com.pedro.library.util.sources.VideoManager import com.pedro.library.util.streamclient.RtspStreamClient import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.rtsp.rtsp.RtspClient -import com.pedro.rtsp.rtsp.VideoCodec import com.pedro.rtsp.utils.ConnectCheckerRtsp import java.nio.ByteBuffer @@ -39,9 +38,8 @@ class RtspStream( constructor(context: Context, connectCheckerRtsp: ConnectCheckerRtsp): this(context, connectCheckerRtsp, VideoManager.Source.CAMERA2, AudioManager.Source.MICROPHONE) - fun setVideoCodec(videoCodec: VideoCodec) { - val mime = if (videoCodec === VideoCodec.H265) CodecUtil.H265_MIME else CodecUtil.H264_MIME - super.setVideoMime(mime) + override fun setVideoCodecImp(codec: VideoCodec) { + rtspClient.setVideoCodec(if (codec === VideoCodec.H264) com.pedro.rtsp.rtsp.VideoCodec.H264 else com.pedro.rtsp.rtsp.VideoCodec.H265) } override fun audioInfo(sampleRate: Int, isStereo: Boolean) { diff --git a/library/src/main/java/com/pedro/library/srt/SrtCamera1.java b/library/src/main/java/com/pedro/library/srt/SrtCamera1.java index cc87624df..a047f992a 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtCamera1.java +++ b/library/src/main/java/com/pedro/library/srt/SrtCamera1.java @@ -24,14 +24,13 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera1Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.SrtStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.srt.srt.SrtClient; -import com.pedro.srt.srt.VideoCodec; import com.pedro.srt.utils.ConnectCheckerSrt; import java.nio.ByteBuffer; @@ -87,11 +86,9 @@ public SrtStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - srtClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + srtClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.srt.srt.VideoCodec.H264 : com.pedro.srt.srt.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/srt/SrtCamera2.java b/library/src/main/java/com/pedro/library/srt/SrtCamera2.java index aa43dd476..c43976a77 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtCamera2.java +++ b/library/src/main/java/com/pedro/library/srt/SrtCamera2.java @@ -24,14 +24,13 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera2Base; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.SrtStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.srt.srt.SrtClient; -import com.pedro.srt.srt.VideoCodec; import com.pedro.srt.utils.ConnectCheckerSrt; import java.nio.ByteBuffer; @@ -96,11 +95,9 @@ public SrtStreamClient getStreamClient() { return streamClient; } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - srtClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + srtClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.srt.srt.VideoCodec.H264 : com.pedro.srt.srt.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/srt/SrtDisplay.java b/library/src/main/java/com/pedro/library/srt/SrtDisplay.java index 0c98a5927..29d601004 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtDisplay.java +++ b/library/src/main/java/com/pedro/library/srt/SrtDisplay.java @@ -22,12 +22,11 @@ import androidx.annotation.RequiresApi; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.DisplayBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.SrtStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.srt.srt.SrtClient; -import com.pedro.srt.srt.VideoCodec; import com.pedro.srt.utils.ConnectCheckerSrt; import java.nio.ByteBuffer; @@ -51,11 +50,9 @@ public SrtDisplay(Context context, boolean useOpengl, ConnectCheckerSrt connectC streamClient = new SrtStreamClient(srtClient, streamClientListener); } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - srtClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + srtClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.srt.srt.VideoCodec.H264 : com.pedro.srt.srt.VideoCodec.H265); } @Override diff --git a/library/src/main/java/com/pedro/library/srt/SrtFromFile.java b/library/src/main/java/com/pedro/library/srt/SrtFromFile.java index 585f80cae..d87bb61c5 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtFromFile.java +++ b/library/src/main/java/com/pedro/library/srt/SrtFromFile.java @@ -24,14 +24,13 @@ import com.pedro.encoder.input.decoder.AudioDecoderInterface; import com.pedro.encoder.input.decoder.VideoDecoderInterface; -import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.FromFileBase; +import com.pedro.library.util.VideoCodec; import com.pedro.library.util.streamclient.SrtStreamClient; import com.pedro.library.util.streamclient.StreamClientListener; import com.pedro.library.view.LightOpenGlView; import com.pedro.library.view.OpenGlView; import com.pedro.srt.srt.SrtClient; -import com.pedro.srt.srt.VideoCodec; import com.pedro.srt.utils.ConnectCheckerSrt; import java.nio.ByteBuffer; @@ -77,11 +76,9 @@ public SrtFromFile(LightOpenGlView lightOpenGlView, ConnectCheckerSrt connectChe streamClient = new SrtStreamClient(srtClient, streamClientListener); } - public void setVideoCodec(VideoCodec videoCodec) { - recordController.setVideoMime( - videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - videoEncoder.setType(videoCodec == VideoCodec.H265 ? CodecUtil.H265_MIME : CodecUtil.H264_MIME); - srtClient.setVideoCodec(videoCodec); + @Override + protected void setVideoCodecImp(VideoCodec codec) { + srtClient.setVideoCodec(codec == VideoCodec.H264 ? com.pedro.srt.srt.VideoCodec.H264 : com.pedro.srt.srt.VideoCodec.H265); } @Override 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 c819e5a67..78bda98a3 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtStream.kt +++ b/library/src/main/java/com/pedro/library/srt/SrtStream.kt @@ -20,14 +20,13 @@ import android.content.Context import android.media.MediaCodec import android.os.Build import androidx.annotation.RequiresApi -import com.pedro.encoder.utils.CodecUtil import com.pedro.library.base.StreamBase +import com.pedro.library.util.VideoCodec import com.pedro.library.util.sources.AudioManager import com.pedro.library.util.sources.VideoManager import com.pedro.library.util.streamclient.SrtStreamClient import com.pedro.library.util.streamclient.StreamClientListener import com.pedro.srt.srt.SrtClient -import com.pedro.srt.srt.VideoCodec import com.pedro.srt.utils.ConnectCheckerSrt import java.nio.ByteBuffer @@ -55,10 +54,8 @@ class SrtStream( constructor(context: Context, connectCheckerRtmp: ConnectCheckerSrt): this(context, connectCheckerRtmp, VideoManager.Source.CAMERA2, AudioManager.Source.MICROPHONE) - fun setVideoCodec(videoCodec: VideoCodec) { - val mime = if (videoCodec === VideoCodec.H265) CodecUtil.H265_MIME else CodecUtil.H264_MIME - super.setVideoMime(mime) - srtClient.setVideoCodec(videoCodec) + override fun setVideoCodecImp(codec: VideoCodec) { + srtClient.setVideoCodec(if (codec === VideoCodec.H264) com.pedro.srt.srt.VideoCodec.H264 else com.pedro.srt.srt.VideoCodec.H265) } override fun audioInfo(sampleRate: Int, isStereo: Boolean) { diff --git a/library/src/main/java/com/pedro/library/util/VideoCodec.kt b/library/src/main/java/com/pedro/library/util/VideoCodec.kt new file mode 100644 index 000000000..1bf11d017 --- /dev/null +++ b/library/src/main/java/com/pedro/library/util/VideoCodec.kt @@ -0,0 +1,8 @@ +package com.pedro.library.util + +/** + * Created by pedro on 1/11/23. + */ +enum class VideoCodec { + H264, H265 +} \ No newline at end of file diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt index 1b8b67f9d..0a3f97693 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt @@ -146,6 +146,10 @@ class RtspClient(private val connectCheckerRtsp: ConnectCheckerRtsp) { commandsManager.setAudioInfo(sampleRate, isStereo) } + fun setVideoCodec(codec: VideoCodec) { + commandsManager.setCodec(codec) + } + @JvmOverloads fun connect(url: String?, isRetry: Boolean = false) { if (!isRetry) doingRetry = true @@ -188,12 +192,12 @@ class RtspClient(private val connectCheckerRtsp: ConnectCheckerRtsp) { rtspSender.setAudioInfo(commandsManager.sampleRate) } if (!commandsManager.videoDisabled) { - if (commandsManager.sps == null || commandsManager.pps == null) { + if (!commandsManager.videoInfoReady()) { Log.i(TAG, "waiting for sps and pps") withTimeoutOrNull(5000) { mutex.lock() } - if (commandsManager.sps == null || commandsManager.pps == null) { + if (!commandsManager.videoInfoReady()) { onMainThread { connectCheckerRtsp.onConnectionFailedRtsp("sps or pps is null") } diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt index eb9494ac5..994c8a4b3 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt @@ -20,6 +20,7 @@ import android.util.Base64 import android.util.Log import com.pedro.rtsp.BuildConfig import com.pedro.rtsp.rtsp.Protocol +import com.pedro.rtsp.rtsp.VideoCodec import com.pedro.rtsp.rtsp.commands.SdpBody.createAacBody import com.pedro.rtsp.rtsp.commands.SdpBody.createH264Body import com.pedro.rtsp.rtsp.commands.SdpBody.createH265Body @@ -59,7 +60,7 @@ open class CommandsManager { var videoDisabled = false var audioDisabled = false private val commandParser = CommandParser() - + private var codec: VideoCodec = VideoCodec.H264 //For udp val audioClientPorts = intArrayOf(5000, 5001) val videoClientPorts = intArrayOf(5002, 5003) @@ -87,10 +88,18 @@ open class CommandsManager { / 1000) // NTP timestamp } + fun setCodec(codec: VideoCodec) { + this.codec = codec + } + + fun videoInfoReady(): Boolean { + return sps != null && pps != null && if (codec == VideoCodec.H264) true else vps != null + } + fun setVideoInfo(sps: ByteBuffer, pps: ByteBuffer, vps: ByteBuffer?) { this.sps = sps.getData() this.pps = pps.getData() - this.vps = vps?.getData() //H264 has no vps so if not null assume H265 + this.vps = vps?.getData() } fun setAudioInfo(sampleRate: Int, isStereo: Boolean) { @@ -138,7 +147,7 @@ open class CommandsManager { private fun createBody(): String { var videoBody = "" if (!videoDisabled) { - videoBody = if (vps == null) { + videoBody = if (codec == VideoCodec.H264) { createH264Body(RtpConstants.trackVideo, spsString, ppsString) } else { createH265Body(RtpConstants.trackVideo, spsString, ppsString, vpsString)