Skip to content

Commit 52300bc

Browse files
RtspClient にタイムアウト設定を追加
サンプルのActivityで終了後に UI 操作が行われないように修正
1 parent e086741 commit 52300bc

5 files changed

Lines changed: 115 additions & 40 deletions

File tree

dConnectSDK/dConnectLibStreaming/libmedia/src/main/java/org/deviceconnect/android/libmedia/streaming/rtsp/RtspClient.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.IOException;
1616
import java.io.InputStreamReader;
1717
import java.io.OutputStream;
18+
import java.net.InetSocketAddress;
1819
import java.net.Socket;
1920
import java.util.ArrayList;
2021
import java.util.List;
@@ -50,6 +51,11 @@ public class RtspClient {
5051
*/
5152
private OnEventListener mOnEventListener;
5253

54+
/**
55+
* RTSP サーバとの接続タイムアウト.
56+
*/
57+
private int mConnectionTimeout = 10 * 1000;
58+
5359
/**
5460
* コンストラクタ.
5561
* @param url RTSP サーバの URL
@@ -70,6 +76,19 @@ public void setOnEventListener(OnEventListener listener) {
7076
mOnEventListener = listener;
7177
}
7278

79+
/**
80+
* RTSP サーバとの接続タイムアウト時間を設定します.
81+
*
82+
* 単位: ミリ秒
83+
*
84+
* デフォルトでは、10000 (10秒) が設定されています。
85+
*
86+
* @param connectionTimeout 接続タイムアウト時間
87+
*/
88+
public void setConnectionTimeout(int connectionTimeout) {
89+
mConnectionTimeout = connectionTimeout;
90+
}
91+
7392
/**
7493
* ユーザエージェントを設定します.
7594
*
@@ -243,7 +262,11 @@ public void run() {
243262
Log.d(TAG, " PORT: " + uri.getPort());
244263
}
245264

246-
mSocket = new Socket(uri.getHost(), uri.getPort());
265+
InetSocketAddress endpoint= new InetSocketAddress(uri.getHost(), uri.getPort());
266+
267+
mSocket = new Socket();
268+
mSocket.setKeepAlive(true);
269+
mSocket.connect(endpoint, mConnectionTimeout);
247270
mReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
248271
mWriter = new BufferedOutputStream(mSocket.getOutputStream());
249272

@@ -272,9 +295,13 @@ public void run() {
272295

273296
processTearDown();
274297
} catch (RtspClientException e) {
275-
postOnError(e);
298+
if (!mStopFlag) {
299+
postOnError(e);
300+
}
276301
} catch (Exception e) {
277-
postOnError(new RtspClientException(e, RtspResponse.Status.STATUS_UNKNOWN));
302+
if (!mStopFlag) {
303+
postOnError(new RtspClientException(e, RtspResponse.Status.STATUS_UNKNOWN));
304+
}
278305
} finally {
279306
synchronized (mRtpReceivers) {
280307
for (RtpReceiver receiver : mRtpReceivers) {

dConnectSDK/dConnectLibStreaming/libmedia/src/main/java/org/deviceconnect/android/libmedia/streaming/rtsp/player/RtspPlayer.java

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,12 @@ public synchronized void start() {
167167
mRtspClient.setOnEventListener(new RtspClient.OnEventListener() {
168168
@Override
169169
public void onConnected() {
170-
if (mOnEventListener != null) {
171-
mOnEventListener.onConnected();
172-
}
170+
postOnConnected();
173171
}
174172

175173
@Override
176174
public void onDisconnected() {
177-
if (mOnEventListener != null) {
178-
mOnEventListener.onDisconnected();
179-
}
175+
postOnDisconnected();
180176
}
181177

182178
@Override
@@ -199,14 +195,10 @@ public void onError(RtspClientException e) {
199195
start();
200196
}).start();
201197
} else {
202-
if (mOnEventListener != null) {
203-
mOnEventListener.onError(e);
204-
}
198+
postOnError(e);
205199
}
206200
} else {
207-
if (mOnEventListener != null) {
208-
mOnEventListener.onError(e);
209-
}
201+
postOnError(e);
210202
}
211203
}
212204

@@ -223,9 +215,7 @@ public void onSdpReceived(SessionDescription sdp) {
223215
}
224216
}
225217

226-
if (mOnEventListener != null) {
227-
mOnEventListener.onReady();
228-
}
218+
postOnReady();
229219
}
230220

231221
@Override
@@ -273,16 +263,8 @@ private Decoder createDecoder(MediaDescription md) {
273263
VideoDecoder decoder = createVideoDecoder(md);
274264
if (decoder != null) {
275265
decoder.setSurface(mSurface);
276-
decoder.setErrorCallback((e) -> {
277-
if (mOnEventListener != null) {
278-
mOnEventListener.onError(e);
279-
}
280-
});
281-
decoder.setEventCallback(((width, height) -> {
282-
if (mOnEventListener != null) {
283-
mOnEventListener.onSizeChanged(width, height);
284-
}
285-
}));
266+
decoder.setErrorCallback(this::postOnError);
267+
decoder.setEventCallback(this::postOnSizeChanged);
286268
decoder.onInit(md);
287269
return decoder;
288270
} else {
@@ -293,11 +275,7 @@ private Decoder createDecoder(MediaDescription md) {
293275
} else if ("audio".equalsIgnoreCase(md.getMedia())) {
294276
AudioDecoder decoder = createAudioDecoder(md);
295277
if (decoder != null) {
296-
decoder.setErrorCallback((e) -> {
297-
if (mOnEventListener != null) {
298-
mOnEventListener.onError(e);
299-
}
300-
});
278+
decoder.setErrorCallback(this::postOnError);
301279
decoder.setMute(mMute);
302280
decoder.onInit(md);
303281
return decoder;
@@ -372,6 +350,36 @@ private String getEncodingName(MediaDescription md) {
372350
return null;
373351
}
374352

353+
private void postOnConnected() {
354+
if (mOnEventListener != null) {
355+
mOnEventListener.onConnected();
356+
}
357+
}
358+
359+
private void postOnDisconnected() {
360+
if (mOnEventListener != null) {
361+
mOnEventListener.onDisconnected();
362+
}
363+
}
364+
365+
private void postOnReady() {
366+
if (mOnEventListener != null) {
367+
mOnEventListener.onReady();
368+
}
369+
}
370+
371+
private void postOnSizeChanged(int width, int height) {
372+
if (mOnEventListener != null) {
373+
mOnEventListener.onSizeChanged(width, height);
374+
}
375+
}
376+
377+
private void postOnError(Exception e) {
378+
if (mOnEventListener != null) {
379+
mOnEventListener.onError(e);
380+
}
381+
}
382+
375383
public interface OnEventListener {
376384
/**
377385
* RTSP サーバの接続したことを通知します.

dConnectSDK/dConnectLibStreaming/libsrt/src/main/java/org/deviceconnect/android/libsrt/client/SRTClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ public void run() {
305305
}
306306
}
307307
} catch (Exception e) {
308-
// ignore.
308+
if (!mStopFlag) {
309+
postOnError(e);
310+
}
309311
} finally {
310312
stopStatsTimer();
311313

dConnectSDK/dConnectLibStreaming/rtsp-player-app/src/main/java/org/deviceconnect/android/rtspplayer/RTSPPlayerActivity.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class RTSPPlayerActivity extends AppCompatActivity {
2424

2525
private RtspPlayer mRtspPlayer;
2626
private RTSPSetting mSetting;
27+
private boolean mRunningFlag;
2728

2829
@Override
2930
protected void onCreate(Bundle savedInstanceState) {
@@ -38,13 +39,15 @@ protected void onCreate(Bundle savedInstanceState) {
3839

3940
@Override
4041
protected void onPause() {
42+
mRunningFlag = false;
4143
stopPlayer();
4244
super.onPause();
4345
}
4446

4547
@Override
4648
protected void onResume() {
4749
super.onResume();
50+
mRunningFlag = true;
4851
startPlayer();
4952
}
5053

@@ -107,22 +110,30 @@ public void onError(Exception e) {
107110
private void stopPlayer() {
108111
if (mRtspPlayer != null) {
109112
mRtspPlayer.stop();
113+
mRtspPlayer.setOnEventListener(null);
110114
mRtspPlayer = null;
111115
}
112116
}
113117

114118
private final List<String> mDebugMessage = new ArrayList<>();
115119

116120
private void addDebugLog(String message) {
121+
if (!mRunningFlag) {
122+
return;
123+
}
124+
117125
synchronized (mDebugMessage) {
118126
mDebugMessage.add(message);
119127

120128
if (mDebugMessage.size() > 5) {
121129
mDebugMessage.remove(0);
122130
}
123131
}
132+
124133
runOnUiThread(() -> {
125-
TextView textView = findViewById(R.id.text_view_debug);
134+
if (!mRunningFlag) {
135+
return;
136+
}
126137

127138
StringBuilder sb = new StringBuilder();
128139
synchronized (mDebugMessage) {
@@ -134,19 +145,26 @@ private void addDebugLog(String message) {
134145
}
135146
}
136147

148+
TextView textView = findViewById(R.id.text_view_debug);
137149
textView.setText(sb.toString());
138150
});
139151
}
140152

141153
private void showErrorDialog(String title, String message) {
142-
runOnUiThread(() ->
154+
if (!mRunningFlag) {
155+
return;
156+
}
157+
runOnUiThread(() -> {
158+
if (mRunningFlag) {
143159
new AlertDialog.Builder(this)
144160
.setTitle(title)
145161
.setMessage(message)
146162
.setPositiveButton("はい", null)
147163
.setCancelable(false)
148164
.setOnDismissListener((dialogInterface) -> finish())
149-
.show());
165+
.show();
166+
}
167+
});
150168
}
151169

152170
private void changePlayerSize(int pictureWidth, int pictureHeight) {

dConnectSDK/dConnectLibStreaming/srt-player-app/src/main/java/org/deviceconnect/android/srt_player_app/SRTPlayerActivity.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class SRTPlayerActivity extends AppCompatActivity {
3030

3131
private SRTPlayer mSRTPlayer;
3232
private SRTSetting mSetting;
33+
private boolean mRunningFlag;
3334

3435
@Override
3536
protected void onCreate(Bundle savedInstanceState) {
@@ -44,13 +45,15 @@ protected void onCreate(Bundle savedInstanceState) {
4445

4546
@Override
4647
protected void onPause() {
48+
mRunningFlag = false;
4749
stopSRTPlayer();
4850
super.onPause();
4951
}
5052

5153
@Override
5254
protected void onResume() {
5355
super.onResume();
56+
mRunningFlag = true;
5457
startSRTPlayer();
5558
}
5659

@@ -128,13 +131,18 @@ public void onStats(SRTStats stats) {
128131
private void stopSRTPlayer() {
129132
if (mSRTPlayer != null) {
130133
mSRTPlayer.stop();
134+
mSRTPlayer.setOnEventListener(null);
131135
mSRTPlayer = null;
132136
}
133137
}
134138

135139
private final List<String> mDebugMessage = new ArrayList<>();
136140

137141
private void addDebugLog(String message) {
142+
if (!mRunningFlag) {
143+
return;
144+
}
145+
138146
synchronized (mDebugMessage) {
139147
mDebugMessage.add(message);
140148

@@ -143,7 +151,9 @@ private void addDebugLog(String message) {
143151
}
144152
}
145153
runOnUiThread(() -> {
146-
TextView textView = findViewById(R.id.text_view_debug);
154+
if (!mRunningFlag) {
155+
return;
156+
}
147157

148158
StringBuilder sb = new StringBuilder();
149159
synchronized (mDebugMessage) {
@@ -155,19 +165,29 @@ private void addDebugLog(String message) {
155165
}
156166
}
157167

168+
TextView textView = findViewById(R.id.text_view_debug);
158169
textView.setText(sb.toString());
159170
});
160171
}
161172

162173
private void showErrorDialog(String title, String message) {
163-
runOnUiThread(() ->
174+
if (!mRunningFlag) {
175+
return;
176+
}
177+
178+
runOnUiThread(() -> {
179+
if (!mRunningFlag) {
180+
return;
181+
}
182+
164183
new AlertDialog.Builder(this)
165184
.setTitle(title)
166185
.setMessage(message)
167186
.setPositiveButton("はい", null)
168187
.setCancelable(false)
169188
.setOnDismissListener((dialogInterface) -> finish())
170-
.show());
189+
.show();
190+
});
171191
}
172192

173193
private void changePlayerSize(int pictureWidth, int pictureHeight) {

0 commit comments

Comments
 (0)