disconnect WebRTC clients on pause()

Esse commit está contido em:
Angel Ayala
2024-08-29 12:20:32 -03:00
commit cd7a3bc679
9 arquivos alterados com 87 adições e 17 exclusões
@@ -376,6 +376,7 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
*/
resumeVideo();
pluginManager.resume();
}
void resumeVideo() {
@@ -429,6 +430,7 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
protected void onPause() {
Log.e(TAG, "onPause()");
super.onPause();
pluginManager.pause();
}
@Override
@@ -3,13 +3,16 @@ package sq.rogue.rosettadrone;
public class Plugin {
protected PluginManager pluginManager;
protected void init(PluginManager pluginManager) {
public void init(PluginManager pluginManager) {
this.pluginManager = pluginManager;
}
protected void start() {
}
protected void pause() {}
protected void resume() {}
/**
* Video mode, resolution or codec changed.
*/
@@ -34,7 +37,7 @@ public class Plugin {
return pluginManager.mainActivity.sharedPreferences.getString(pref, defPref);
}
public Boolean getPrefBoolean(String pref, Boolean defPref) {
public boolean getPrefBoolean(String pref, boolean defPref) {
return pluginManager.mainActivity.sharedPreferences.getBoolean(pref, defPref);
}
@@ -11,7 +11,7 @@ import java.util.Arrays;
import java.util.List;
public class PluginManager {
private final String TAG = DroneModel.class.getSimpleName();
private final String TAG = PluginManager.class.getSimpleName();
public MainActivity mainActivity;
@@ -57,6 +57,10 @@ public class PluginManager {
}
}
public void pause() { for (Plugin plugin : plugins) { plugin.pause(); } }
public void resume() { for (Plugin plugin : plugins) { plugin.resume(); } }
public void onVideoChange() {
for (Plugin plugin : plugins) {
plugin.onVideoChange();
@@ -69,4 +69,14 @@ public class DJIStreamer {
}
});
}
public void closeVideoStream() {
Log.i(TAG, "closeVideoStream()");
ongoingConnections.keySet().forEach(socketID -> {
WebRTCClient client = getClient(socketID);
client.stopCapture();
client.close();
removeClient(socketID);
});
}
}
@@ -148,6 +148,10 @@ public class DJIVideoCapturer implements VideoCapturer {
@Override
public void stopCapture() throws InterruptedException {
codecManager.enabledYuvData(false);
codecManager.setYuvDataCallback(null);
codecManager.destroyCodec();
codecManager = null;
}
@Override
@@ -93,7 +93,7 @@ public class WebRTCClient {
}
}
catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
Log.e(TAG, "JSONException: " + e.getMessage());
}
}
@@ -148,6 +148,42 @@ public class WebRTCClient {
peerConnection.addStream(mediaStream);
}
public void stopCapture() {
// Stop video capture if it is active
try {
if (videoCapturer != null) {
videoCapturer.stopCapture();
}
} catch (InterruptedException e) {
Log.e(TAG, "Error stopping video capture: " + e.getMessage());
} finally {
// Dispose of video capturer to free its resources
if (videoCapturer != null) {
videoCapturer.dispose();
Log.d(TAG, "Video capturer disposed.");
}
Log.d(TAG, "WebRTC client resources clean complete.");
}
}
public void close() {
// Close the peer connection if it exists
if (peerConnection != null) {
peerConnection.close();
peerConnection.dispose();
Log.d(TAG, "PeerConnection disposed.");
}
// Optionally dispose of the PeerConnectionFactory if this is the last WebRTCClient instance
if (factory != null) {
factory.dispose();
factory = null;
Log.d(TAG, "PeerConnectionFactory disposed.");
}
Log.d(TAG, "WebRTC client close complete.");
}
private PeerConnection createPeerConnection(String stunServer) {
ArrayList<PeerConnection.IceServer> iceServers = new ArrayList<>();
PeerConnection.IceServer stun = PeerConnection.IceServer.builder(stunServer).createIceServer();
@@ -1,7 +1,7 @@
package sq.rogue.rosettadrone.plugins.WebRTC;
public class WebRTCMediaOptions {
public static String MEDIA_STREAM_ID = "Phantom4Pro";
public String MEDIA_STREAM_ID = "Phantom4Pro";
String VIDEO_SOURCE_ID = "Phantom4Prov0";
int VIDEO_RESOLUTION_WIDTH = 320;
int VIDEO_RESOLUTION_HEIGHT = 240;
@@ -16,8 +16,11 @@ public class WebRTCStreaming extends Plugin {
private DJIStreamer djiStreamer;
private Socket mSocket;
private static final boolean TEST = false; // Send a testing stream
private String stunServer;
public void initWebSocket() {
Log.d(TAG, "initWebSocket");
stunServer = getPrefString("pref_webrtc_stun_server", "stun:stun.l.google.com:19302");
// init websocket
mSocket = SocketBuilder
.with(getPrefString("pref_webrtc_signaling_server", "ws://192.168.1.220:8090"))
@@ -60,24 +63,33 @@ public class WebRTCStreaming extends Plugin {
pluginManager.mainActivity.logMessageDJI(msg);
pluginManager.mainActivity.finish();
}
else {
else if (djiStreamer == null) {
djiStreamer = new DJIStreamer(pluginManager.mainActivity,
pluginManager.mainActivity.mModel.m_model,
getPrefString("pref_webrtc_stun_server", "stun:stun.l.google.com:19302"));
pluginManager.mainActivity.mModel.m_model, stunServer);
}
}
}
public void start() {
pluginManager.mainActivity.useCustomDecoder = false; // Messes up the buffer received by onYuvDataReceived()
pluginManager.mainActivity.useOutputSurface = false; // Avoid crash when clicking on minimap
if (getPrefBoolean("pref_enable_webrtc", false)) {
pluginManager.mainActivity.useCustomDecoder = false; // Messes up the buffer received by onYuvDataReceived()
pluginManager.mainActivity.useOutputSurface = false; // Avoid crash when clicking on minimap
initWebSocket();
}
}
public void resume() {
if (getPrefBoolean("pref_enable_webrtc", false)) {
initStreaming();
}
}
public void pause() {
if (getPrefBoolean("pref_enable_webrtc", false) && djiStreamer != null) {
djiStreamer.closeVideoStream();
}
}
public void onVideoChange() {
// TODO: stop/start connections?
Log.d(TAG, "onVideoChange");
@@ -87,11 +99,10 @@ public class WebRTCStreaming extends Plugin {
if(TEST || RDApplication.isTestMode) {
// TODO: stop fake video stream;
} else {
// TODO: stop DJIStreamer clients?;
pause();
}
// TODO: properly close client connections.
if (pluginManager.mainActivity.sharedPreferences.getBoolean("pref_enable_webrtc", false))
if (getPrefBoolean("pref_enable_webrtc", false) && mSocket != null)
mSocket.terminate();
}
+3 -3
Ver Arquivo
@@ -144,9 +144,9 @@
<!-- WebRTC -->
<string name="webrtc_settings">WebRTC Video</string>
<string name="pref_enable_webrtc">Enable WebRTC vide streaming</string>
<string name="pref_webrtc_signaling_address">WebRTC Signaling server address</string>
<string name="pref_webrtc_stun_address">WebRTC STUN server address</string>
<string name="pref_enable_webrtc">Enable WebRTC video streaming</string>
<string name="pref_webrtc_signaling_address">Signaling server address</string>
<string name="pref_webrtc_stun_address">STUN server address</string>
<!-- Communication Channels -->
<string name="dji">DJI</string>