Skip to content

Commit f0d8707

Browse files
author
Poter
committed
Fixed bug of duplicated connection
1 parent 810907e commit f0d8707

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

app/src/main/java/com/potterhsu/rtsplibrary/demo/MainActivity.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.graphics.Bitmap;
55
import android.graphics.Color;
66
import android.os.Bundle;
7-
import android.os.PowerManager;
87
import android.view.View;
98
import android.widget.EditText;
109
import android.widget.ImageView;
@@ -74,7 +73,23 @@ public void onBtnPlayClick(View view) {
7473
new Thread(new Runnable() {
7574
@Override
7675
public void run() {
77-
rtspClient.play(endpoint);
76+
while (true) {
77+
if (rtspClient.play(endpoint) == 0)
78+
break;
79+
80+
runOnUiThread(new Runnable() {
81+
@Override
82+
public void run() {
83+
Toast.makeText(MainActivity.this, "Connection error, retry after 3 seconds", Toast.LENGTH_SHORT).show();
84+
}
85+
});
86+
87+
try {
88+
Thread.sleep(3000);
89+
} catch (InterruptedException e) {
90+
e.printStackTrace();
91+
}
92+
}
7893
}
7994
}).start();
8095
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
android:id="@+id/edtEndpoint"
2121
android:layout_width="match_parent"
2222
android:layout_height="wrap_content"
23+
android:text="rtsp://"
2324
android:hint="Endpoint" />
2425

2526
<Button

library/src/main/cpp/native-lib.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ Java_com_potterhsu_rtsplibrary_RtspClient_play(
140140
av_read_pause(context);
141141
avio_close(oc->pb);
142142
avformat_free_context(oc);
143+
avformat_close_input(&context);
143144

144-
return JNI_OK;
145+
return isStop ? JNI_OK : JNI_ERR;
145146
}
146147

147148
extern "C"

library/src/main/java/com/potterhsu/rtsplibrary/RtspClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public RtspClient(NativeCallback callback) {
1818
}
1919

2020
private native int initialize(NativeCallback callback);
21+
22+
/**
23+
* Play stream synchronously.
24+
* @param endpoint resource endpoint
25+
* @return 0 if exit normally or -1 otherwise
26+
*/
2127
public native int play(String endpoint);
2228
public native void stop();
2329
public native void dispose();

0 commit comments

Comments
 (0)