Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ConnectCheckerEvent #1431

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 20 additions & 29 deletions app/src/main/java/com/pedro/streamer/file/FromFileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.pedro.common.ConnectChecker
import com.pedro.common.ConnectCheckerEvent
import com.pedro.common.StreamEvent
import com.pedro.encoder.input.decoder.AudioDecoderInterface
import com.pedro.encoder.input.decoder.VideoDecoderInterface
import com.pedro.library.base.recording.RecordController
Expand Down Expand Up @@ -68,7 +70,7 @@ import kotlin.math.max
* [com.pedro.library.srt.SrtFromFile]
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
class FromFileActivity : AppCompatActivity(), ConnectChecker,
class FromFileActivity : AppCompatActivity(), ConnectCheckerEvent,
VideoDecoderInterface, AudioDecoderInterface, OnSeekBarChangeListener {

private lateinit var genericFromFile: GenericFromFile
Expand Down Expand Up @@ -178,34 +180,23 @@ class FromFileActivity : AppCompatActivity(), ConnectChecker,
activityResult.unregister()
}

override fun onConnectionStarted(url: String) {}

override fun onConnectionSuccess() {
toast("Connected")
}

override fun onConnectionFailed(reason: String) {
toast("Failed: $reason")
genericFromFile.stopStream()
bStream.setImageResource(R.drawable.stream_icon)
if (!genericFromFile.isRecording) ScreenOrientation.unlockScreen(this)
}

override fun onNewBitrate(bitrate: Long) {}

override fun onDisconnect() {
toast("Disconnected")
}

override fun onAuthError() {
toast("Auth error")
genericFromFile.stopStream()
bStream.setImageResource(R.drawable.stream_icon)
if (!genericFromFile.isRecording) ScreenOrientation.unlockScreen(this)
}

override fun onAuthSuccess() {
toast("Auth success")
override fun onStreamEvent(event: StreamEvent, message: String) {
when (event) {
StreamEvent.STARTED, StreamEvent.NEW_BITRATE -> return
StreamEvent.FAILED -> {
genericFromFile.stopStream()
bStream.setImageResource(R.drawable.stream_icon)
if (!genericFromFile.isRecording) ScreenOrientation.unlockScreen(this)
toast("${event.name}: $message")
}
StreamEvent.AUTH_ERROR -> {
genericFromFile.stopStream()
bStream.setImageResource(R.drawable.stream_icon)
if (!genericFromFile.isRecording) ScreenOrientation.unlockScreen(this)
toast(event.name)
}
else -> toast(event.name)
}
}

@Throws(IOException::class)
Expand Down
49 changes: 20 additions & 29 deletions app/src/main/java/com/pedro/streamer/oldapi/OldApiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.pedro.common.ConnectChecker
import com.pedro.common.ConnectCheckerEvent
import com.pedro.common.StreamEvent
import com.pedro.encoder.input.video.CameraHelper
import com.pedro.encoder.input.video.CameraOpenException
import com.pedro.library.base.recording.RecordController
Expand Down Expand Up @@ -53,7 +55,7 @@ import java.util.Locale
* Support SRT with all SRT features
* [com.pedro.library.srt.SrtCamera1]
*/
class OldApiActivity : AppCompatActivity(), ConnectChecker, TextureView.SurfaceTextureListener {
class OldApiActivity : AppCompatActivity(), ConnectCheckerEvent, TextureView.SurfaceTextureListener {

private lateinit var genericCamera1: GenericCamera1
private lateinit var bStream: ImageView
Expand Down Expand Up @@ -134,34 +136,23 @@ class OldApiActivity : AppCompatActivity(), ConnectChecker, TextureView.SurfaceT
autoFitTextureView.setAspectRatio(w, h)
}

override fun onConnectionStarted(url: String) {}

override fun onConnectionSuccess() {
toast("Connected")
}

override fun onConnectionFailed(reason: String) {
toast("Failed: $reason")
genericCamera1.stopStream()
if (!genericCamera1.isRecording) ScreenOrientation.unlockScreen(this)
bStream.setImageResource(R.drawable.stream_icon)
}

override fun onNewBitrate(bitrate: Long) {}

override fun onDisconnect() {
toast("Disconnected")
}

override fun onAuthError() {
toast("Auth error")
genericCamera1.stopStream()
bStream.setImageResource(R.drawable.stream_icon)
if (!genericCamera1.isRecording) ScreenOrientation.unlockScreen(this)
}

override fun onAuthSuccess() {
toast("Auth success")
override fun onStreamEvent(event: StreamEvent, message: String) {
when (event) {
StreamEvent.STARTED, StreamEvent.NEW_BITRATE -> return
StreamEvent.FAILED -> {
genericCamera1.stopStream()
if (!genericCamera1.isRecording) ScreenOrientation.unlockScreen(this)
bStream.setImageResource(R.drawable.stream_icon)
toast("${event.name}: $message")
}
StreamEvent.AUTH_ERROR -> {
genericCamera1.stopStream()
bStream.setImageResource(R.drawable.stream_icon)
if (!genericCamera1.isRecording) ScreenOrientation.unlockScreen(this)
toast(event.name)
}
else -> toast(event.name)
}
}

override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
Expand Down
54 changes: 21 additions & 33 deletions app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import android.widget.EditText
import android.widget.ImageView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.pedro.common.ConnectChecker
import com.pedro.common.ConnectCheckerEvent
import com.pedro.common.StreamEvent
import com.pedro.library.base.recording.RecordController
import com.pedro.library.generic.GenericStream
import com.pedro.library.util.sources.video.Camera1Source
Expand Down Expand Up @@ -65,7 +66,7 @@ import java.util.Locale
* [com.pedro.library.srt.SrtStream]
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class CameraFragment: Fragment(), ConnectChecker {
class CameraFragment: Fragment(), ConnectCheckerEvent {

companion object {
fun getInstance(): CameraFragment = CameraFragment()
Expand Down Expand Up @@ -185,37 +186,24 @@ class CameraFragment: Fragment(), ConnectChecker {
genericStream.release()
}

override fun onConnectionStarted(url: String) {
}

override fun onConnectionSuccess() {
toast("Connected")
}

override fun onConnectionFailed(reason: String) {
if (genericStream.getStreamClient().reTry(5000, reason, null)) {
toast("Retry")
} else {
genericStream.stopStream()
bStartStop.setImageResource(R.drawable.stream_icon)
toast("Failed: $reason")
override fun onStreamEvent(event: StreamEvent, message: String) {
when (event) {
StreamEvent.STARTED, StreamEvent.NEW_BITRATE -> return
StreamEvent.FAILED -> {
if (genericStream.getStreamClient().reTry(5000, message, null)) {
toast("Retry")
} else {
genericStream.stopStream()
bStartStop.setImageResource(R.drawable.stream_icon)
toast("${event.name}: $message")
}
}
StreamEvent.AUTH_ERROR -> {
genericStream.stopStream()
bStartStop.setImageResource(R.drawable.stream_icon)
toast(event.name)
}
else -> toast(event.name)
}
}

override fun onNewBitrate(bitrate: Long) {
}

override fun onDisconnect() {
toast("Disconnected")
}

override fun onAuthError() {
genericStream.stopStream()
bStartStop.setImageResource(R.drawable.stream_icon)
toast("Auth error")
}

override fun onAuthSuccess() {
toast("Auth success")
}
}
40 changes: 16 additions & 24 deletions app/src/main/java/com/pedro/streamer/screen/ScreenActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.pedro.common.ConnectChecker
import com.pedro.common.ConnectCheckerEvent
import com.pedro.common.StreamEvent
import com.pedro.streamer.R
import com.pedro.streamer.utils.toast

Expand All @@ -44,7 +46,7 @@ import com.pedro.streamer.utils.toast
* [com.pedro.library.srt.SrtDisplay]
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class ScreenActivity : AppCompatActivity(), ConnectChecker {
class ScreenActivity : AppCompatActivity(), ConnectCheckerEvent {

private lateinit var button: ImageView
private lateinit var etUrl: EditText
Expand Down Expand Up @@ -114,28 +116,18 @@ class ScreenActivity : AppCompatActivity(), ConnectChecker {
button.setImageResource(R.drawable.stream_icon)
}

override fun onConnectionStarted(url: String) {}

override fun onConnectionSuccess() {
toast("Connected")
}

override fun onConnectionFailed(reason: String) {
stopStream()
toast("Failed: $reason")
}

override fun onNewBitrate(bitrate: Long) {}
override fun onDisconnect() {
toast("Disconnected")
}

override fun onAuthError() {
stopStream()
toast("Auth error")
}

override fun onAuthSuccess() {
toast("Auth success")
override fun onStreamEvent(event: StreamEvent, message: String) {
when (event) {
StreamEvent.STARTED, StreamEvent.NEW_BITRATE -> return
StreamEvent.FAILED -> {
stopStream()
toast("${event.name}: $message")
}
StreamEvent.AUTH_ERROR -> {
stopStream()
toast(event.name)
}
else -> toast(event.name)
}
}
}
62 changes: 62 additions & 0 deletions common/src/main/java/com/pedro/common/ConnectCheckerEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2023 pedroSG94.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pedro.common;

import org.jetbrains.annotations.NotNull;

/**
* Created by pedro on 8/3/24.
*/
public interface ConnectCheckerEvent extends ConnectChecker {

void onStreamEvent(StreamEvent event, String message);

@Override
default void onConnectionStarted(@NotNull String url) {
onStreamEvent(StreamEvent.STARTED, url);
}

@Override
default void onConnectionSuccess() {
onStreamEvent(StreamEvent.CONNECTED, "");
}

@Override
default void onConnectionFailed(@NotNull String reason) {
onStreamEvent(StreamEvent.FAILED, reason);
}

@Override
default void onNewBitrate(long bitrate) {
onStreamEvent(StreamEvent.NEW_BITRATE, Long.toString(bitrate));
}

@Override
default void onDisconnect() {
onStreamEvent(StreamEvent.DISCONNECTED, "");
}

@Override
default void onAuthError() {
onStreamEvent(StreamEvent.AUTH_ERROR, "");
}

@Override
default void onAuthSuccess() {
onStreamEvent(StreamEvent.AUTH_SUCCESS, "");
}
}
25 changes: 25 additions & 0 deletions common/src/main/java/com/pedro/common/StreamEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 pedroSG94.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pedro.common

/**
* Created by pedro on 8/3/24.
*/
enum class StreamEvent {

STARTED, CONNECTED, DISCONNECTED, FAILED, NEW_BITRATE, AUTH_ERROR, AUTH_SUCCESS
}
Loading