Skip to content

Commit

Permalink
disconnect WebRTC clients on pause()
Browse files Browse the repository at this point in the history
  • Loading branch information
angel-ayala committed Aug 29, 2024
1 parent f490767 commit cd7a3bc
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 17 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/sq/rogue/rosettadrone/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ protected void onResume() {
*/

resumeVideo();
pluginManager.resume();
}

void resumeVideo() {
Expand Down Expand Up @@ -429,6 +430,7 @@ void resumeVideo() {
protected void onPause() {
Log.e(TAG, "onPause()");
super.onPause();
pluginManager.pause();
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/sq/rogue/rosettadrone/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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.
*/
Expand All @@ -34,7 +37,7 @@ public String getPrefString(String pref, String defPref) {
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);
}

Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/sq/rogue/rosettadrone/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;

public class PluginManager {
private final String TAG = DroneModel.class.getSimpleName();
private final String TAG = PluginManager.class.getSimpleName();

public MainActivity mainActivity;

Expand Down Expand Up @@ -57,6 +57,10 @@ public void stop() {
}
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ private void setupSocketEvent(String stunServer){
}
});
}

public void closeVideoStream() {
Log.i(TAG, "closeVideoStream()");
ongoingConnections.keySet().forEach(socketID -> {
WebRTCClient client = getClient(socketID);
client.stopCapture();
client.close();
removeClient(socketID);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public void startCapture(int width, int height, int framerate) {

@Override
public void stopCapture() throws InterruptedException {
codecManager.enabledYuvData(false);
codecManager.setYuvDataCallback(null);
codecManager.destroyCodec();
codecManager = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void handleWebRTCMessage(JSONObject message){
}
}
catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
Log.e(TAG, "JSONException: " + e.getMessage());
}
}

Expand Down Expand Up @@ -148,6 +148,42 @@ private void startStreamingVideo() {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -60,24 +63,33 @@ public void initStreaming() {
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");
Expand All @@ -87,11 +99,10 @@ public void stop() {
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();
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit cd7a3bc

Please sign in to comment.