Skip to content

Commit 9c4ff3f

Browse files
RTMPとSRTにリトライ処理を追加
1 parent dee0db8 commit 9c4ff3f

9 files changed

Lines changed: 359 additions & 14 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/recorder/AbstractRTMPBroadcaster.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public void start(OnStartCallback callback) {
6868
setAudioQuality(audioEncoder.getAudioQuality());
6969
}
7070

71+
HostMediaRecorder.Settings settings = getRecorder().getSettings();
72+
7173
mRtmpClient = new RtmpClient(getBroadcastURI());
74+
mRtmpClient.setMaxRetryCount(settings.getRetryCount());
75+
mRtmpClient.setRetryInterval(settings.getRetryInterval());
7276
mRtmpClient.setVideoEncoder(videoEncoder);
7377
mRtmpClient.setAudioEncoder(audioEncoder);
7478
mRtmpClient.setOnEventListener(new RtmpClient.OnEventListener() {

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/recorder/AbstractSRTBroadcaster.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public void start(OnStartCallback callback) {
6868
setAudioQuality(audioEncoder.getAudioQuality());
6969
}
7070

71+
HostMediaRecorder.Settings settings = getRecorder().getSettings();
72+
7173
mSrtClient = new SRTClient(getBroadcastURI());
74+
mSrtClient.setMaxRetryCount(settings.getRetryCount());
75+
mSrtClient.setRetryInterval(settings.getRetryInterval());
7276
mSrtClient.setVideoEncoder(videoEncoder);
7377
mSrtClient.setAudioEncoder(audioEncoder);
7478
mSrtClient.setOnEventListener(new SRTClient.OnEventListener() {

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/recorder/HostMediaRecorder.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,50 @@ public void setBroadcastURI(String broadcastURI) {
16851685
mPref.put("broadcast_uri", broadcastURI);
16861686
}
16871687

1688+
/**
1689+
* リトライ回数を取得します.
1690+
*
1691+
* @return リトライ回数
1692+
*/
1693+
public int getRetryCount() {
1694+
return mPref.getInteger("broadcast_retry_count", 0);
1695+
}
1696+
1697+
/**
1698+
* リトライ回数を設定します.
1699+
*
1700+
* @param count リトライ回数
1701+
*/
1702+
public void setRetryCount(int count) {
1703+
if (count < 0) {
1704+
mPref.remove("broadcast_retry_count");
1705+
} else {
1706+
mPref.put("broadcast_retry_count", count);
1707+
}
1708+
}
1709+
1710+
/**
1711+
* リトライのインターバルを取得します.
1712+
*
1713+
* @return リトライのインターバル
1714+
*/
1715+
public int getRetryInterval() {
1716+
return mPref.getInteger("broadcast_retry_interval", 3000);
1717+
}
1718+
1719+
/**
1720+
* リトライのインターバルを設定します.
1721+
*
1722+
* @param interval リトライのインターバル
1723+
*/
1724+
public void setRetryInterval(int interval) {
1725+
if (interval < 0) {
1726+
mPref.remove("broadcast_retry_interval");
1727+
} else {
1728+
mPref.put("broadcast_retry_interval", interval);
1729+
}
1730+
}
1731+
16881732
// ポート番号
16891733

16901734
/**

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/res/values-ja/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@
132132

133133
<!-- Broadcast -->
134134
<string name="host_recorder_settings_broadcast_uri">URL</string>
135+
<string name="host_recorder_settings_broadcast_retry_count">リトライ回数</string>
136+
<string name="host_recorder_settings_broadcast_retry_interval">リトライ間隔(ミリ秒)</string>
135137

136138
<!-- ポート設定 -->
137139
<string name="host_recorder_settings_port_motion_jpeg">MotionJPEG 設定</string>

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
<string name="host_recorder_settings_audio_source_app">App Audio</string>
128128

129129
<string name="host_recorder_settings_broadcast_uri">URL</string>
130+
<string name="host_recorder_settings_broadcast_retry_count">Retry Count</string>
131+
<string name="host_recorder_settings_broadcast_retry_interval">Retry Interval (msec)</string>
130132

131133
<string name="host_recorder_settings_port_motion_jpeg">MotionJPEG Settings</string>
132134
<string name="host_recorder_settings_port_rtsp">RTSP Settings</string>

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/res/xml/settings_host_recorder_broadcast.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
app:key="broadcast_uri"
1515
app:useSimpleSummaryProvider="true" />
1616

17+
<EditTextPreference
18+
android:defaultValue="0"
19+
android:singleLine="true"
20+
android:title="@string/host_recorder_settings_broadcast_retry_count"
21+
app:iconSpaceReserved="false"
22+
app:key="broadcast_retry_count"
23+
app:useSimpleSummaryProvider="true" />
24+
25+
<EditTextPreference
26+
android:defaultValue="3000"
27+
android:singleLine="true"
28+
android:title="@string/host_recorder_settings_broadcast_retry_interval"
29+
app:iconSpaceReserved="false"
30+
app:key="broadcast_retry_interval"
31+
app:useSimpleSummaryProvider="true" />
1732
</PreferenceCategory>
1833

1934
</PreferenceScreen>

dConnectSDK/dConnectLibStreaming/libmedia/src/main/java/org/deviceconnect/android/libmedia/streaming/rtmp/RtmpClient.java

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.deviceconnect.android.libmedia.streaming.rtmp;
22

3+
import android.os.Handler;
4+
import android.os.Looper;
5+
6+
import org.deviceconnect.android.libmedia.streaming.MediaEncoderException;
37
import org.deviceconnect.android.libmedia.streaming.MediaStreamer;
48
import org.deviceconnect.android.libmedia.streaming.audio.AudioEncoder;
59
import org.deviceconnect.android.libmedia.streaming.muxer.RtmpMuxer;
@@ -16,6 +20,39 @@ public class RtmpClient {
1620
*/
1721
private final MediaStreamer mMediaStreamer;
1822

23+
/**
24+
* ハンドラ.
25+
*/
26+
private final Handler mHandler = new Handler(Looper.getMainLooper());
27+
28+
/**
29+
* リトライ数.
30+
*/
31+
private int mRetryCount;
32+
33+
/**
34+
* リトライ回数.
35+
*/
36+
private int mMaxRetryCount = 3;
37+
38+
/**
39+
* リトライを行うインターバル.
40+
*/
41+
private int mRetryInterval = 2000;
42+
43+
/**
44+
* リトライフラグ.
45+
* <p>
46+
* このフラグがtrueの場合はリトライ処理中になります。
47+
* </p>
48+
*/
49+
private boolean mRetryFlag;
50+
51+
/**
52+
* 起動フラグ.
53+
*/
54+
private boolean mRunnableFlag;
55+
1956
/**
2057
* コンストラクタ.
2158
*/
@@ -24,14 +61,105 @@ public RtmpClient(String broadcastURI) {
2461
mMediaStreamer = new MediaStreamer(mRtmpMuxer);
2562
}
2663

64+
/**
65+
* リトライ回数を設定します.
66+
*
67+
* @param maxRetryCount リトライ回数
68+
*/
69+
public void setMaxRetryCount(int maxRetryCount) {
70+
mMaxRetryCount = maxRetryCount;
71+
}
72+
73+
/**
74+
* リトライ回数を取得します.
75+
*
76+
* @return リトライ回数
77+
*/
78+
public int getMaxRetryCount() {
79+
return mMaxRetryCount;
80+
}
81+
82+
/**
83+
* リトライのインターバルを設定します.
84+
*
85+
* @param interval リトライを行うインターバル
86+
*/
87+
public void setRetryInterval(int interval) {
88+
mRetryInterval = interval;
89+
}
90+
91+
/**
92+
* リトライのインターバルを取得します.
93+
*
94+
* @return リトライのインターバル
95+
*/
96+
public int getRetryInterval() {
97+
return mRetryInterval;
98+
}
99+
27100
/**
28101
* イベントを通知するためのリスナーを設定します.
29102
*
30103
* @param listener リスナー
31104
*/
32105
public void setOnEventListener(OnEventListener listener) {
33-
mRtmpMuxer.setOnEventListener(listener);
34106
mMediaStreamer.setOnEventListener(listener);
107+
mRtmpMuxer.setOnEventListener(new RtmpMuxer.OnEventListener() {
108+
@Override
109+
public void onConnected() {
110+
// 接続できたのでリトライ数を初期化
111+
mRetryCount = 0;
112+
if (listener != null) {
113+
listener.onConnected();
114+
}
115+
}
116+
117+
@Override
118+
public void onDisconnected() {
119+
if (mRetryFlag) {
120+
// エラーが発生して切断された時も、このイベントが呼び出されるので
121+
// リトライの処理を行っている間はリスナーに通知しないようにブロックする。
122+
return;
123+
}
124+
125+
if (listener != null) {
126+
listener.onDisconnected();
127+
}
128+
}
129+
130+
@Override
131+
public void onNewBitrate(long bitrate) {
132+
if (listener != null) {
133+
listener.onNewBitrate(bitrate);
134+
}
135+
}
136+
137+
@Override
138+
public void onError(MediaEncoderException e) {
139+
if (mRetryFlag) {
140+
return;
141+
}
142+
143+
mMediaStreamer.stop();
144+
145+
if (mRetryCount < mMaxRetryCount) {
146+
mRetryCount++;
147+
mRetryFlag = true;
148+
149+
mHandler.postDelayed(() -> {
150+
mRetryFlag = false;
151+
if (mRunnableFlag) {
152+
mMediaStreamer.start();
153+
}
154+
}, mRetryInterval);
155+
} else {
156+
mRunnableFlag = false;
157+
if (listener != null) {
158+
listener.onError(e);
159+
}
160+
}
161+
}
162+
});
35163
}
36164

37165
/**
@@ -46,14 +174,17 @@ public boolean isRunning() {
46174
/**
47175
* RTMP のセッションを開始します.
48176
*/
49-
public void start() {
177+
public synchronized void start() {
178+
mRetryCount = mMaxRetryCount;
179+
mRunnableFlag = true;
50180
mMediaStreamer.start();
51181
}
52182

53183
/**
54184
* RTMP のセッションを停止します.
55185
*/
56-
public void stop() {
186+
public synchronized void stop() {
187+
mRunnableFlag = false;
57188
mMediaStreamer.stop();
58189
}
59190

0 commit comments

Comments
 (0)