From 8fec4e97f7d0d3346f9dc9c8f985eeda057009ff Mon Sep 17 00:00:00 2001 From: pedro Date: Thu, 12 Oct 2023 23:11:38 +0200 Subject: [PATCH] move retry to streamclient --- .../defaultexample/ExampleRtmpActivity.java | 2 +- .../defaultexample/ExampleRtspActivity.java | 2 +- .../defaultexample/ExampleSrtActivity.java | 2 +- .../SurfaceModeRtmpActivity.java | 2 +- .../SurfaceModeRtspActivity.java | 2 +- .../com/pedro/library/base/DisplayBase.java | 1 - .../com/pedro/library/base/FromFileBase.java | 1 - .../com/pedro/library/base/OnlyAudioBase.java | 1 - .../java/com/pedro/library/base/StreamBase.kt | 1 - .../com/pedro/library/rtmp/RtmpCamera1.java | 33 +++++++------------ .../com/pedro/library/rtmp/RtmpCamera2.java | 32 +++++++----------- .../com/pedro/library/rtmp/RtmpDisplay.java | 26 ++++----------- .../com/pedro/library/rtmp/RtmpFromFile.java | 30 ++++++----------- .../com/pedro/library/rtmp/RtmpOnlyAudio.java | 4 +-- .../java/com/pedro/library/rtmp/RtmpStream.kt | 28 +++++----------- .../com/pedro/library/rtsp/RtspCamera1.java | 33 +++++++------------ .../com/pedro/library/rtsp/RtspCamera2.java | 33 +++++++------------ .../com/pedro/library/rtsp/RtspDisplay.java | 24 ++++---------- .../com/pedro/library/rtsp/RtspFromFile.java | 29 ++++++---------- .../com/pedro/library/rtsp/RtspOnlyAudio.java | 6 ++-- .../java/com/pedro/library/rtsp/RtspStream.kt | 28 +++++----------- .../com/pedro/library/srt/SrtCamera1.java | 31 +++++++---------- .../com/pedro/library/srt/SrtCamera2.java | 31 +++++++---------- .../com/pedro/library/srt/SrtDisplay.java | 23 ++++--------- .../com/pedro/library/srt/SrtFromFile.java | 29 ++++++---------- .../com/pedro/library/srt/SrtOnlyAudio.java | 4 +-- .../java/com/pedro/library/srt/SrtStream.kt | 26 +++++---------- .../RtmpStreamClient.kt | 7 ++-- .../RtspStreamClient.kt | 7 ++-- .../SrtStreamClient.kt | 7 ++-- .../StreamBaseClient.kt | 21 ++++++++++-- .../util/streamclient/StreamClientListener.kt | 8 +++++ 32 files changed, 197 insertions(+), 317 deletions(-) rename library/src/main/java/com/pedro/library/util/{client => streamclient}/RtmpStreamClient.kt (92%) rename library/src/main/java/com/pedro/library/util/{client => streamclient}/RtspStreamClient.kt (89%) rename library/src/main/java/com/pedro/library/util/{client => streamclient}/SrtStreamClient.kt (88%) rename library/src/main/java/com/pedro/library/util/{client => streamclient}/StreamBaseClient.kt (59%) create mode 100644 library/src/main/java/com/pedro/library/util/streamclient/StreamClientListener.kt diff --git a/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtmpActivity.java b/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtmpActivity.java index c7f0a0053..019e33302 100644 --- a/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtmpActivity.java +++ b/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtmpActivity.java @@ -85,7 +85,7 @@ public void onConnectionSuccessRtmp() { @Override public void onConnectionFailedRtmp(final String reason) { - if (rtmpCamera1.reTry(5000, reason, null)) { + if (rtmpCamera1.getStreamClient().reTry(5000, reason, null)) { Toast.makeText(ExampleRtmpActivity.this, "Retry", Toast.LENGTH_SHORT) .show(); } else { diff --git a/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtspActivity.java b/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtspActivity.java index 45d11b75a..dfb2f4833 100644 --- a/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtspActivity.java +++ b/app/src/main/java/com/pedro/streamer/defaultexample/ExampleRtspActivity.java @@ -87,7 +87,7 @@ public void onConnectionSuccessRtsp() { @Override public void onConnectionFailedRtsp(final String reason) { - if (rtspCamera1.reTry(5000, reason, null)) { + if (rtspCamera1.getStreamClient().reTry(5000, reason, null)) { Toast.makeText(ExampleRtspActivity.this, "Retry", Toast.LENGTH_SHORT) .show(); } else { diff --git a/app/src/main/java/com/pedro/streamer/defaultexample/ExampleSrtActivity.java b/app/src/main/java/com/pedro/streamer/defaultexample/ExampleSrtActivity.java index d65bc5396..053f01e7b 100644 --- a/app/src/main/java/com/pedro/streamer/defaultexample/ExampleSrtActivity.java +++ b/app/src/main/java/com/pedro/streamer/defaultexample/ExampleSrtActivity.java @@ -87,7 +87,7 @@ public void onConnectionSuccessSrt() { @Override public void onConnectionFailedSrt(final String reason) { - if (srtCamera1.reTry(5000, reason, null)) { + if (srtCamera1.getStreamClient().reTry(5000, reason, null)) { Toast.makeText(ExampleSrtActivity.this, "Retry", Toast.LENGTH_SHORT) .show(); } else { diff --git a/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtmpActivity.java b/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtmpActivity.java index 09d6ac300..d2dc771a3 100644 --- a/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtmpActivity.java +++ b/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtmpActivity.java @@ -87,7 +87,7 @@ public void onConnectionSuccessRtmp() { @Override public void onConnectionFailedRtmp(final String reason) { - if (rtmpCamera2.reTry(5000, reason, null)) { + if (rtmpCamera2.getStreamClient().reTry(5000, reason, null)) { Toast.makeText(SurfaceModeRtmpActivity.this, "Retry", Toast.LENGTH_SHORT) .show(); } else { diff --git a/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtspActivity.java b/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtspActivity.java index 8b6b6894d..0d428632e 100644 --- a/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtspActivity.java +++ b/app/src/main/java/com/pedro/streamer/surfacemodeexample/SurfaceModeRtspActivity.java @@ -91,7 +91,7 @@ public void onConnectionSuccessRtsp() { @Override public void onConnectionFailedRtsp(final String reason) { //Wait 5s and retry connect stream - if (rtspCamera2.reTry(5000, reason, null)) { + if (rtspCamera2.getStreamClient().reTry(5000, reason, null)) { Toast.makeText(SurfaceModeRtspActivity.this, "Retry", Toast.LENGTH_SHORT) .show(); } else { 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 cf6138884..56fd1440b 100644 --- a/library/src/main/java/com/pedro/library/base/DisplayBase.java +++ b/library/src/main/java/com/pedro/library/base/DisplayBase.java @@ -37,7 +37,6 @@ import androidx.annotation.RequiresApi; import com.pedro.encoder.EncoderErrorCallback; -import com.pedro.encoder.Frame; import com.pedro.encoder.audio.AudioEncoder; import com.pedro.encoder.audio.GetAacData; import com.pedro.encoder.input.audio.CustomAudioEffect; 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 0e88535da..5e116289a 100644 --- a/library/src/main/java/com/pedro/library/base/FromFileBase.java +++ b/library/src/main/java/com/pedro/library/base/FromFileBase.java @@ -29,7 +29,6 @@ import androidx.annotation.RequiresApi; import com.pedro.encoder.EncoderErrorCallback; -import com.pedro.encoder.Frame; import com.pedro.encoder.audio.AudioEncoder; import com.pedro.encoder.audio.GetAacData; import com.pedro.encoder.input.audio.GetMicrophoneData; diff --git a/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java b/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java index 14f006ed4..884f69555 100644 --- a/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java +++ b/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java @@ -26,7 +26,6 @@ import androidx.annotation.RequiresApi; import com.pedro.encoder.EncoderErrorCallback; -import com.pedro.encoder.Frame; import com.pedro.encoder.audio.AudioEncoder; import com.pedro.encoder.audio.GetAacData; import com.pedro.encoder.input.audio.CustomAudioEffect; 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 180d0c884..6b82165d4 100644 --- a/library/src/main/java/com/pedro/library/base/StreamBase.kt +++ b/library/src/main/java/com/pedro/library/base/StreamBase.kt @@ -19,7 +19,6 @@ 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.input.video.GetCameraData import com.pedro.encoder.video.FormatVideoEncoder import com.pedro.encoder.video.GetVideoData import com.pedro.encoder.video.VideoEncoder 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 e5ee699ee..e2a989bbc 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera1.java @@ -22,16 +22,14 @@ import android.view.SurfaceView; import android.view.TextureView; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera1Base; -import com.pedro.library.util.client.RtmpStreamClient; -import com.pedro.library.util.client.SrtStreamClient; +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.flv.video.ProfileIop; import com.pedro.rtmp.rtmp.RtmpClient; import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; @@ -45,7 +43,7 @@ * Created by pedro on 25/01/17. */ -public class RtmpCamera1 extends Camera1Base { +public class RtmpCamera1 extends Camera1Base implements StreamClientListener { private final RtmpClient rtmpClient; private final RtmpStreamClient streamClient; @@ -53,34 +51,34 @@ public class RtmpCamera1 extends Camera1Base { public RtmpCamera1(SurfaceView surfaceView, ConnectCheckerRtmp connectChecker) { super(surfaceView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpCamera1(TextureView textureView, ConnectCheckerRtmp connectChecker) { super(textureView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public RtmpCamera1(OpenGlView openGlView, ConnectCheckerRtmp connectChecker) { super(openGlView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public RtmpCamera1(LightOpenGlView lightOpenGlView, ConnectCheckerRtmp connectChecker) { super(lightOpenGlView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public RtmpCamera1(Context context, ConnectCheckerRtmp connectChecker) { super(context); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpStreamClient getStreamClient() { @@ -131,17 +129,8 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) rtmpClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 cd535b40e..cc50ea590 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpCamera2.java @@ -22,15 +22,14 @@ import android.view.SurfaceView; import android.view.TextureView; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera2Base; -import com.pedro.library.util.client.RtmpStreamClient; +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.flv.video.ProfileIop; import com.pedro.rtmp.rtmp.RtmpClient; import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; @@ -44,7 +43,7 @@ * Created by pedro on 6/07/17. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public class RtmpCamera2 extends Camera2Base { +public class RtmpCamera2 extends Camera2Base implements StreamClientListener { private final RtmpClient rtmpClient; private final RtmpStreamClient streamClient; @@ -58,7 +57,7 @@ public class RtmpCamera2 extends Camera2Base { public RtmpCamera2(SurfaceView surfaceView, ConnectCheckerRtmp connectChecker) { super(surfaceView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } /** @@ -70,25 +69,25 @@ public RtmpCamera2(SurfaceView surfaceView, ConnectCheckerRtmp connectChecker) { public RtmpCamera2(TextureView textureView, ConnectCheckerRtmp connectChecker) { super(textureView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpCamera2(OpenGlView openGlView, ConnectCheckerRtmp connectChecker) { super(openGlView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpCamera2(LightOpenGlView lightOpenGlView, ConnectCheckerRtmp connectChecker) { super(lightOpenGlView); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpCamera2(Context context, boolean useOpengl, ConnectCheckerRtmp connectChecker) { super(context, useOpengl); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpStreamClient getStreamClient() { @@ -139,18 +138,9 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) rtmpClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 412767907..b10f3a9d3 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpDisplay.java @@ -20,14 +20,12 @@ import android.media.MediaCodec; import android.os.Build; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.DisplayBase; -import com.pedro.library.util.client.RtmpStreamClient; -import com.pedro.library.util.client.StreamBaseClient; -import com.pedro.rtmp.flv.video.ProfileIop; +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; @@ -41,7 +39,7 @@ * Created by pedro on 9/08/17. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public class RtmpDisplay extends DisplayBase { +public class RtmpDisplay extends DisplayBase implements StreamClientListener { private final RtmpClient rtmpClient; private final RtmpStreamClient streamClient; @@ -49,7 +47,7 @@ public class RtmpDisplay extends DisplayBase { public RtmpDisplay(Context context, boolean useOpengl, ConnectCheckerRtmp connectChecker) { super(context, useOpengl); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpStreamClient getStreamClient() { @@ -99,18 +97,8 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) rtmpClient.sendVideo(h264Buffer, info); } - - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 e72aa8cb8..17a428630 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpFromFile.java @@ -20,17 +20,16 @@ import android.media.MediaCodec; import android.os.Build; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; 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.client.RtmpStreamClient; +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.flv.video.ProfileIop; import com.pedro.rtmp.rtmp.RtmpClient; import com.pedro.rtmp.rtmp.VideoCodec; import com.pedro.rtmp.utils.ConnectCheckerRtmp; @@ -44,7 +43,7 @@ * Created by pedro on 26/06/17. */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) -public class RtmpFromFile extends FromFileBase { +public class RtmpFromFile extends FromFileBase implements StreamClientListener { private final RtmpClient rtmpClient; private final RtmpStreamClient streamClient; @@ -53,28 +52,28 @@ public RtmpFromFile(ConnectCheckerRtmp connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(videoDecoderInterface, audioDecoderInterface); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpFromFile(Context context, ConnectCheckerRtmp connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(context, videoDecoderInterface, audioDecoderInterface); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpFromFile(OpenGlView openGlView, ConnectCheckerRtmp connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(openGlView, videoDecoderInterface, audioDecoderInterface); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpFromFile(LightOpenGlView lightOpenGlView, ConnectCheckerRtmp connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(lightOpenGlView, videoDecoderInterface, audioDecoderInterface); rtmpClient = new RtmpClient(connectChecker); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, this); } public RtmpStreamClient getStreamClient() { @@ -124,17 +123,8 @@ protected void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { rtmpClient.sendAudio(aacBuffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } diff --git a/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java b/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java index 2cda2aea6..f7c626336 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpOnlyAudio.java @@ -21,7 +21,7 @@ import androidx.annotation.Nullable; import com.pedro.library.base.OnlyAudioBase; -import com.pedro.library.util.client.RtmpStreamClient; +import com.pedro.library.util.streamclient.RtmpStreamClient; import com.pedro.rtmp.rtmp.RtmpClient; import com.pedro.rtmp.utils.ConnectCheckerRtmp; @@ -42,7 +42,7 @@ public RtmpOnlyAudio(ConnectCheckerRtmp connectChecker) { super(); rtmpClient = new RtmpClient(connectChecker); rtmpClient.setOnlyAudio(true); - streamClient = new RtmpStreamClient(rtmpClient); + streamClient = new RtmpStreamClient(rtmpClient, null); } public RtmpStreamClient getStreamClient() { 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 6754cbfc1..51ada3ec2 100644 --- a/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt +++ b/library/src/main/java/com/pedro/library/rtmp/RtmpStream.kt @@ -6,11 +6,10 @@ 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.client.RtmpStreamClient -import com.pedro.library.util.client.RtspStreamClient import com.pedro.library.util.sources.AudioManager import com.pedro.library.util.sources.VideoManager -import com.pedro.rtmp.flv.video.ProfileIop +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 @@ -24,11 +23,13 @@ import java.nio.ByteBuffer */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) -class RtmpStream(context: Context, connectCheckerRtmp: ConnectCheckerRtmp, videoSource: VideoManager.Source, - audioSource: AudioManager.Source): StreamBase(context, videoSource, audioSource) { +class RtmpStream( + context: Context, connectCheckerRtmp: ConnectCheckerRtmp, videoSource: VideoManager.Source, + audioSource: AudioManager.Source +): StreamBase(context, videoSource, audioSource), StreamClientListener { private val rtmpClient = RtmpClient(connectCheckerRtmp) - val streamClient = RtmpStreamClient(rtmpClient) + val streamClient = RtmpStreamClient(rtmpClient, this) constructor(context: Context, connectCheckerRtmp: ConnectCheckerRtmp): this(context, connectCheckerRtmp, VideoManager.Source.CAMERA2, AudioManager.Source.MICROPHONE) @@ -66,18 +67,7 @@ class RtmpStream(context: Context, connectCheckerRtmp: ConnectCheckerRtmp, video rtmpClient.sendAudio(aacBuffer, info) } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - @JvmOverloads - fun reTry(delay: Long, reason: String, backupUrl: String? = null): Boolean { - val result = streamClient.shouldRetry(reason) - if (result) { - requestKeyframe() - streamClient.reConnect(delay, backupUrl) - } - return result + override fun onRequestKeyframe() { + requestKeyframe() } } \ No newline at end of file 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 ae6a3bcf3..3d83722bb 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspCamera1.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspCamera1.java @@ -22,16 +22,14 @@ import android.view.SurfaceView; import android.view.TextureView; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera1Base; -import com.pedro.library.util.client.RtmpStreamClient; -import com.pedro.library.util.client.RtspStreamClient; +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.Protocol; import com.pedro.rtsp.rtsp.RtspClient; import com.pedro.rtsp.rtsp.VideoCodec; import com.pedro.rtsp.utils.ConnectCheckerRtsp; @@ -45,7 +43,7 @@ * Created by pedro on 10/02/17. */ -public class RtspCamera1 extends Camera1Base { +public class RtspCamera1 extends Camera1Base implements StreamClientListener { private final RtspClient rtspClient; private final RtspStreamClient streamClient; @@ -53,34 +51,34 @@ public class RtspCamera1 extends Camera1Base { public RtspCamera1(SurfaceView surfaceView, ConnectCheckerRtsp connectCheckerRtsp) { super(surfaceView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspCamera1(TextureView textureView, ConnectCheckerRtsp connectCheckerRtsp) { super(textureView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public RtspCamera1(OpenGlView openGlView, ConnectCheckerRtsp connectCheckerRtsp) { super(openGlView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public RtspCamera1(LightOpenGlView lightOpenGlView, ConnectCheckerRtsp connectCheckerRtsp) { super(lightOpenGlView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public RtspCamera1(Context context, ConnectCheckerRtsp connectCheckerRtsp) { super(context); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspStreamClient getStreamClient() { @@ -125,17 +123,8 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) rtspClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 388666aa1..263aaef14 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspCamera2.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspCamera2.java @@ -22,15 +22,14 @@ import android.view.SurfaceView; import android.view.TextureView; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera2Base; -import com.pedro.library.util.client.RtspStreamClient; +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.Protocol; import com.pedro.rtsp.rtsp.RtspClient; import com.pedro.rtsp.rtsp.VideoCodec; import com.pedro.rtsp.utils.ConnectCheckerRtsp; @@ -44,7 +43,7 @@ * Created by pedro on 4/06/17. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public class RtspCamera2 extends Camera2Base { +public class RtspCamera2 extends Camera2Base implements StreamClientListener { private final RtspClient rtspClient; private final RtspStreamClient streamClient; @@ -58,7 +57,7 @@ public class RtspCamera2 extends Camera2Base { public RtspCamera2(SurfaceView surfaceView, ConnectCheckerRtsp connectCheckerRtsp) { super(surfaceView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } /** @@ -70,25 +69,25 @@ public RtspCamera2(SurfaceView surfaceView, ConnectCheckerRtsp connectCheckerRts public RtspCamera2(TextureView textureView, ConnectCheckerRtsp connectCheckerRtsp) { super(textureView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspCamera2(OpenGlView openGlView, ConnectCheckerRtsp connectCheckerRtsp) { super(openGlView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspCamera2(LightOpenGlView lightOpenGlView, ConnectCheckerRtsp connectCheckerRtsp) { super(lightOpenGlView); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspCamera2(Context context, boolean useOpengl, ConnectCheckerRtsp connectCheckerRtsp) { super(context, useOpengl); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspStreamClient getStreamClient() { @@ -132,19 +131,9 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) rtspClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } - } 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 81b5466d9..1ee60b131 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspDisplay.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspDisplay.java @@ -20,13 +20,12 @@ import android.media.MediaCodec; import android.os.Build; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.DisplayBase; -import com.pedro.library.util.client.RtspStreamClient; -import com.pedro.rtsp.rtsp.Protocol; +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; @@ -40,7 +39,7 @@ * Created by pedro on 9/08/17. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public class RtspDisplay extends DisplayBase { +public class RtspDisplay extends DisplayBase implements StreamClientListener { private final RtspClient rtspClient; private final RtspStreamClient streamClient; @@ -48,7 +47,7 @@ public class RtspDisplay extends DisplayBase { public RtspDisplay(Context context, boolean useOpengl, ConnectCheckerRtsp connectCheckerRtsp) { super(context, useOpengl); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspStreamClient getStreamClient() { @@ -91,17 +90,8 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) rtspClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 01b630998..ae55ce5b1 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspFromFile.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspFromFile.java @@ -20,14 +20,14 @@ import android.media.MediaCodec; import android.os.Build; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; 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.client.RtspStreamClient; +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; @@ -43,7 +43,7 @@ * Created by pedro on 4/06/17. */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) -public class RtspFromFile extends FromFileBase { +public class RtspFromFile extends FromFileBase implements StreamClientListener { private final RtspClient rtspClient; private final RtspStreamClient streamClient; @@ -52,28 +52,28 @@ public RtspFromFile(ConnectCheckerRtsp connectCheckerRtsp, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(videoDecoderInterface, audioDecoderInterface); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspFromFile(Context context, ConnectCheckerRtsp connectCheckerRtsp, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(context, videoDecoderInterface, audioDecoderInterface); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspFromFile(OpenGlView openGlView, ConnectCheckerRtsp connectCheckerRtsp, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(openGlView, videoDecoderInterface, audioDecoderInterface); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspFromFile(LightOpenGlView lightOpenGlView, ConnectCheckerRtsp connectCheckerRtsp, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(lightOpenGlView, videoDecoderInterface, audioDecoderInterface); rtspClient = new RtspClient(connectCheckerRtsp); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, this); } public RtspStreamClient getStreamClient() { @@ -117,18 +117,9 @@ protected void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { rtspClient.sendAudio(aacBuffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } diff --git a/library/src/main/java/com/pedro/library/rtsp/RtspOnlyAudio.java b/library/src/main/java/com/pedro/library/rtsp/RtspOnlyAudio.java index eb9e54cbf..1819a8895 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspOnlyAudio.java +++ b/library/src/main/java/com/pedro/library/rtsp/RtspOnlyAudio.java @@ -21,9 +21,7 @@ import androidx.annotation.Nullable; import com.pedro.library.base.OnlyAudioBase; -import com.pedro.library.util.client.RtmpStreamClient; -import com.pedro.library.util.client.RtspStreamClient; -import com.pedro.rtsp.rtsp.Protocol; +import com.pedro.library.util.streamclient.RtspStreamClient; import com.pedro.rtsp.rtsp.RtspClient; import com.pedro.rtsp.utils.ConnectCheckerRtsp; @@ -44,7 +42,7 @@ public RtspOnlyAudio(ConnectCheckerRtsp connectCheckerRtsp) { super(); rtspClient = new RtspClient(connectCheckerRtsp); rtspClient.setOnlyAudio(true); - streamClient = new RtspStreamClient(rtspClient); + streamClient = new RtspStreamClient(rtspClient, null); } public RtspStreamClient getStreamClient() { 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 c4ef296b1..28a4d5e62 100644 --- a/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt +++ b/library/src/main/java/com/pedro/library/rtsp/RtspStream.kt @@ -6,11 +6,10 @@ 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.client.RtspStreamClient -import com.pedro.library.util.client.SrtStreamClient import com.pedro.library.util.sources.AudioManager import com.pedro.library.util.sources.VideoManager -import com.pedro.rtsp.rtsp.Protocol +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 @@ -24,11 +23,13 @@ import java.nio.ByteBuffer */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) -class RtspStream(context: Context, connectCheckerRtsp: ConnectCheckerRtsp, videoSource: VideoManager.Source, - audioSource: AudioManager.Source): StreamBase(context, videoSource, audioSource) { +class RtspStream( + context: Context, connectCheckerRtsp: ConnectCheckerRtsp, videoSource: VideoManager.Source, + audioSource: AudioManager.Source +): StreamBase(context, videoSource, audioSource), StreamClientListener { private val rtspClient = RtspClient(connectCheckerRtsp) - val streamClient = RtspStreamClient(rtspClient) + val streamClient = RtspStreamClient(rtspClient, this) constructor(context: Context, connectCheckerRtsp: ConnectCheckerRtsp): this(context, connectCheckerRtsp, VideoManager.Source.CAMERA2, AudioManager.Source.MICROPHONE) @@ -62,18 +63,7 @@ class RtspStream(context: Context, connectCheckerRtsp: ConnectCheckerRtsp, video rtspClient.sendAudio(aacBuffer, info) } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - @JvmOverloads - fun reTry(delay: Long, reason: String, backupUrl: String? = null): Boolean { - val result = streamClient.shouldRetry(reason) - if (result) { - requestKeyframe() - streamClient.reConnect(delay, backupUrl) - } - return result + override fun onRequestKeyframe() { + requestKeyframe() } } \ No newline at end of file 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 16986a5c8..d8d554688 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtCamera1.java +++ b/library/src/main/java/com/pedro/library/srt/SrtCamera1.java @@ -22,12 +22,12 @@ import android.view.SurfaceView; import android.view.TextureView; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera1Base; -import com.pedro.library.util.client.SrtStreamClient; +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; @@ -43,7 +43,7 @@ * Created by pedro on 8/9/23. */ -public class SrtCamera1 extends Camera1Base { +public class SrtCamera1 extends Camera1Base implements StreamClientListener { private final SrtClient srtClient; private final SrtStreamClient streamClient; @@ -51,34 +51,34 @@ public class SrtCamera1 extends Camera1Base { public SrtCamera1(SurfaceView surfaceView, ConnectCheckerSrt connectChecker) { super(surfaceView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtCamera1(TextureView textureView, ConnectCheckerSrt connectChecker) { super(textureView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public SrtCamera1(OpenGlView openGlView, ConnectCheckerSrt connectChecker) { super(openGlView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public SrtCamera1(LightOpenGlView lightOpenGlView, ConnectCheckerSrt connectChecker) { super(lightOpenGlView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public SrtCamera1(Context context, ConnectCheckerSrt connectChecker) { super(context); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtStreamClient getStreamClient() { @@ -123,17 +123,8 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) srtClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 03ca51f0f..b53268d74 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtCamera2.java +++ b/library/src/main/java/com/pedro/library/srt/SrtCamera2.java @@ -22,12 +22,12 @@ import android.view.SurfaceView; import android.view.TextureView; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.Camera2Base; -import com.pedro.library.util.client.SrtStreamClient; +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; @@ -43,7 +43,7 @@ * Created by pedro on 8/9/23. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public class SrtCamera2 extends Camera2Base { +public class SrtCamera2 extends Camera2Base implements StreamClientListener { private final SrtClient srtClient; private final SrtStreamClient streamClient; @@ -57,7 +57,7 @@ public class SrtCamera2 extends Camera2Base { public SrtCamera2(SurfaceView surfaceView, ConnectCheckerSrt connectChecker) { super(surfaceView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } /** @@ -69,25 +69,25 @@ public SrtCamera2(SurfaceView surfaceView, ConnectCheckerSrt connectChecker) { public SrtCamera2(TextureView textureView, ConnectCheckerSrt connectChecker) { super(textureView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtCamera2(OpenGlView openGlView, ConnectCheckerSrt connectChecker) { super(openGlView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtCamera2(LightOpenGlView lightOpenGlView, ConnectCheckerSrt connectChecker) { super(lightOpenGlView); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtCamera2(Context context, boolean useOpengl, ConnectCheckerSrt connectChecker) { super(context, useOpengl); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtStreamClient getStreamClient() { @@ -132,18 +132,9 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) srtClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 8d2917fc9..ab6007ebe 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtDisplay.java +++ b/library/src/main/java/com/pedro/library/srt/SrtDisplay.java @@ -20,12 +20,12 @@ import android.media.MediaCodec; import android.os.Build; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.pedro.encoder.utils.CodecUtil; import com.pedro.library.base.DisplayBase; -import com.pedro.library.util.client.SrtStreamClient; +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; @@ -39,7 +39,7 @@ * Created by pedro on 8/9/23. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public class SrtDisplay extends DisplayBase { +public class SrtDisplay extends DisplayBase implements StreamClientListener { private final SrtClient srtClient; private final SrtStreamClient streamClient; @@ -47,7 +47,7 @@ public class SrtDisplay extends DisplayBase { public SrtDisplay(Context context, boolean useOpengl, ConnectCheckerSrt connectChecker) { super(context, useOpengl); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public void setVideoCodec(VideoCodec videoCodec) { @@ -91,17 +91,8 @@ protected void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) srtClient.sendVideo(h264Buffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } 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 c9a66cf74..55f1161fb 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtFromFile.java +++ b/library/src/main/java/com/pedro/library/srt/SrtFromFile.java @@ -20,14 +20,14 @@ import android.media.MediaCodec; import android.os.Build; -import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; 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.client.SrtStreamClient; +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; @@ -43,7 +43,7 @@ * Created by pedro on 8/9/23. */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) -public class SrtFromFile extends FromFileBase { +public class SrtFromFile extends FromFileBase implements StreamClientListener { private final SrtClient srtClient; private final SrtStreamClient streamClient; @@ -52,28 +52,28 @@ public SrtFromFile(ConnectCheckerSrt connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(videoDecoderInterface, audioDecoderInterface); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtFromFile(Context context, ConnectCheckerSrt connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(context, videoDecoderInterface, audioDecoderInterface); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtFromFile(OpenGlView openGlView, ConnectCheckerSrt connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(openGlView, videoDecoderInterface, audioDecoderInterface); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public SrtFromFile(LightOpenGlView lightOpenGlView, ConnectCheckerSrt connectChecker, VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { super(lightOpenGlView, videoDecoderInterface, audioDecoderInterface); srtClient = new SrtClient(connectChecker); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, this); } public void setVideoCodec(VideoCodec videoCodec) { @@ -117,17 +117,8 @@ protected void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { srtClient.sendAudio(aacBuffer, info); } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - public boolean reTry(long delay, String reason, @Nullable String backupUrl) { - boolean result = streamClient.shouldRetry(reason); - if (result) { - requestKeyFrame(); - streamClient.reConnect(delay, backupUrl); - } - return result; + @Override + public void onRequestKeyframe() { + requestKeyFrame(); } } diff --git a/library/src/main/java/com/pedro/library/srt/SrtOnlyAudio.java b/library/src/main/java/com/pedro/library/srt/SrtOnlyAudio.java index 28a9d8fe7..9c03831a6 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtOnlyAudio.java +++ b/library/src/main/java/com/pedro/library/srt/SrtOnlyAudio.java @@ -21,7 +21,7 @@ import androidx.annotation.Nullable; import com.pedro.library.base.OnlyAudioBase; -import com.pedro.library.util.client.SrtStreamClient; +import com.pedro.library.util.streamclient.SrtStreamClient; import com.pedro.srt.srt.SrtClient; import com.pedro.srt.utils.ConnectCheckerSrt; @@ -42,7 +42,7 @@ public SrtOnlyAudio(ConnectCheckerSrt connectChecker) { super(); srtClient = new SrtClient(connectChecker); srtClient.setOnlyAudio(true); - streamClient = new SrtStreamClient(srtClient); + streamClient = new SrtStreamClient(srtClient, null); } public SrtStreamClient getStreamClient() { 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 802255262..672800544 100644 --- a/library/src/main/java/com/pedro/library/srt/SrtStream.kt +++ b/library/src/main/java/com/pedro/library/srt/SrtStream.kt @@ -22,9 +22,10 @@ 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.client.SrtStreamClient 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 @@ -38,11 +39,13 @@ import java.nio.ByteBuffer */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) -class SrtStream(context: Context, connectCheckerRtmp: ConnectCheckerSrt, videoSource: VideoManager.Source, - audioSource: AudioManager.Source): StreamBase(context, videoSource, audioSource) { +class SrtStream( + context: Context, connectCheckerRtmp: ConnectCheckerSrt, videoSource: VideoManager.Source, + audioSource: AudioManager.Source +): StreamBase(context, videoSource, audioSource), StreamClientListener { private val srtClient = SrtClient(connectCheckerRtmp) - val streamClient = SrtStreamClient(srtClient) + val streamClient = SrtStreamClient(srtClient, this) constructor(context: Context, connectCheckerRtmp: ConnectCheckerSrt): this(context, connectCheckerRtmp, VideoManager.Source.CAMERA2, AudioManager.Source.MICROPHONE) @@ -77,18 +80,7 @@ class SrtStream(context: Context, connectCheckerRtmp: ConnectCheckerSrt, videoSo srtClient.sendAudio(aacBuffer, info) } - /** - * Retries to connect with the given delay. You can pass an optional backupUrl - * if you'd like to connect to your backup server instead of the original one. - * Given backupUrl replaces the original one. - */ - @JvmOverloads - fun reTry(delay: Long, reason: String, backupUrl: String? = null): Boolean { - val result = streamClient.shouldRetry(reason) - if (result) { - requestKeyframe() - streamClient.reConnect(delay, backupUrl) - } - return result + override fun onRequestKeyframe() { + requestKeyframe() } } \ No newline at end of file diff --git a/library/src/main/java/com/pedro/library/util/client/RtmpStreamClient.kt b/library/src/main/java/com/pedro/library/util/streamclient/RtmpStreamClient.kt similarity index 92% rename from library/src/main/java/com/pedro/library/util/client/RtmpStreamClient.kt rename to library/src/main/java/com/pedro/library/util/streamclient/RtmpStreamClient.kt index 092467dc4..ca44639bc 100644 --- a/library/src/main/java/com/pedro/library/util/client/RtmpStreamClient.kt +++ b/library/src/main/java/com/pedro/library/util/streamclient/RtmpStreamClient.kt @@ -1,4 +1,4 @@ -package com.pedro.library.util.client +package com.pedro.library.util.streamclient import com.pedro.rtmp.flv.video.ProfileIop import com.pedro.rtmp.rtmp.RtmpClient @@ -6,7 +6,10 @@ import com.pedro.rtmp.rtmp.RtmpClient /** * Created by pedro on 12/10/23. */ -class RtmpStreamClient(private val rtmpClient: RtmpClient): StreamBaseClient() { +class RtmpStreamClient( + private val rtmpClient: RtmpClient, + streamClientListener: StreamClientListener? +): StreamBaseClient(streamClientListener) { /** * H264 profile. diff --git a/library/src/main/java/com/pedro/library/util/client/RtspStreamClient.kt b/library/src/main/java/com/pedro/library/util/streamclient/RtspStreamClient.kt similarity index 89% rename from library/src/main/java/com/pedro/library/util/client/RtspStreamClient.kt rename to library/src/main/java/com/pedro/library/util/streamclient/RtspStreamClient.kt index c8d1fdbd8..d7ae77f75 100644 --- a/library/src/main/java/com/pedro/library/util/client/RtspStreamClient.kt +++ b/library/src/main/java/com/pedro/library/util/streamclient/RtspStreamClient.kt @@ -1,4 +1,4 @@ -package com.pedro.library.util.client +package com.pedro.library.util.streamclient import com.pedro.rtsp.rtsp.Protocol import com.pedro.rtsp.rtsp.RtspClient @@ -6,7 +6,10 @@ import com.pedro.rtsp.rtsp.RtspClient /** * Created by pedro on 12/10/23. */ -class RtspStreamClient(private val rtspClient: RtspClient): StreamBaseClient() { +class RtspStreamClient( + private val rtspClient: RtspClient, + streamClientListener: StreamClientListener? +): StreamBaseClient(streamClientListener) { /** diff --git a/library/src/main/java/com/pedro/library/util/client/SrtStreamClient.kt b/library/src/main/java/com/pedro/library/util/streamclient/SrtStreamClient.kt similarity index 88% rename from library/src/main/java/com/pedro/library/util/client/SrtStreamClient.kt rename to library/src/main/java/com/pedro/library/util/streamclient/SrtStreamClient.kt index 19c3b8c39..e3f983445 100644 --- a/library/src/main/java/com/pedro/library/util/client/SrtStreamClient.kt +++ b/library/src/main/java/com/pedro/library/util/streamclient/SrtStreamClient.kt @@ -1,11 +1,14 @@ -package com.pedro.library.util.client +package com.pedro.library.util.streamclient import com.pedro.srt.srt.SrtClient /** * Created by pedro on 12/10/23. */ -class SrtStreamClient(private val srtClient: SrtClient): StreamBaseClient() { +class SrtStreamClient( + private val srtClient: SrtClient, + streamClientListener: StreamClientListener? +): StreamBaseClient(streamClientListener) { override fun setAuthorization(user: String?, password: String?) { srtClient.setAuthorization(user, password) diff --git a/library/src/main/java/com/pedro/library/util/client/StreamBaseClient.kt b/library/src/main/java/com/pedro/library/util/streamclient/StreamBaseClient.kt similarity index 59% rename from library/src/main/java/com/pedro/library/util/client/StreamBaseClient.kt rename to library/src/main/java/com/pedro/library/util/streamclient/StreamBaseClient.kt index e44979977..165dc04e3 100644 --- a/library/src/main/java/com/pedro/library/util/client/StreamBaseClient.kt +++ b/library/src/main/java/com/pedro/library/util/streamclient/StreamBaseClient.kt @@ -1,9 +1,26 @@ -package com.pedro.library.util.client +package com.pedro.library.util.streamclient /** * Created by pedro on 12/10/23. */ -abstract class StreamBaseClient { +abstract class StreamBaseClient( + private val streamClientListener: StreamClientListener? +) { + + /** + * Retries to connect with the given delay. You can pass an optional backupUrl + * if you'd like to connect to your backup server instead of the original one. + * Given backupUrl replaces the original one. + */ + @JvmOverloads + fun reTry(delay: Long, reason: String, backupUrl: String? = null): Boolean { + val result = shouldRetry(reason) + if (result) { + streamClientListener?.onRequestKeyframe() + reConnect(delay, backupUrl) + } + return result + } /** * diff --git a/library/src/main/java/com/pedro/library/util/streamclient/StreamClientListener.kt b/library/src/main/java/com/pedro/library/util/streamclient/StreamClientListener.kt new file mode 100644 index 000000000..cb3a5800e --- /dev/null +++ b/library/src/main/java/com/pedro/library/util/streamclient/StreamClientListener.kt @@ -0,0 +1,8 @@ +package com.pedro.library.util.streamclient + +/** + * Created by pedro on 12/10/23. + */ +interface StreamClientListener { + fun onRequestKeyframe() +} \ No newline at end of file