Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haoquan/rtp video stream control #308

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ dependencies {
implementation 'com.github.pedroSG94:RTSP-Server:1.0.8'
implementation 'com.github.pedroSG94.rtmp-rtsp-stream-client-java:rtplibrary:2.0.2'

//RTP Client
implementation 'com.github.ar-android:libstreaming:1.0.0'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the original library.
https://github.com/fyhertz/libstreaming

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the original library in the beginning. However, the official libstreaming published on jitpack is the very old version, and they didn't publish their latest version. I'm not sure if there is another way around. Do you have any idea?


// RxJava
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
Expand Down
14 changes: 14 additions & 0 deletions android/app/src/main/java/org/openbot/env/ConnectionSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class ConnectionSelector {
private static final String TAG = "ConnectionManager";
Expand All @@ -11,6 +12,7 @@ public class ConnectionSelector {
private ILocalConnection connection;

private final ILocalConnection networkConnection = new NetworkServiceConnection();
private final ILocalConnection mobileConnection = new MobileNetworkConnection();
private final ILocalConnection nearbyConnection = new NearbyConnection();

private ConnectionSelector() {
Expand Down Expand Up @@ -38,8 +40,13 @@ ILocalConnection getConnection() {
}

if (isConnectedViaWifi()) {
Log.i(TAG, "Connected via Wifi");
connection = networkConnection;
} else if (isConnectedViaMobile()) {
Log.i(TAG, "Connected via mobile network");
connection = mobileConnection;
} else {
Log.i(TAG, "Connected via Peer-to-Peer");
connection = nearbyConnection;
}

Expand All @@ -52,4 +59,11 @@ private boolean isConnectedViaWifi() {
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}

private boolean isConnectedViaMobile() {
ConnectivityManager connectivityManager =
(ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return mMobile.isConnected();
}
}
14 changes: 11 additions & 3 deletions android/app/src/main/java/org/openbot/env/ControllerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class ControllerConfig {

enum VIDEO_SERVER_TYPE {
WEBRTC,
RTSP
RTSP,
RTP
}

private String currentServerType;
Expand All @@ -27,7 +28,7 @@ public static ControllerConfig getInstance() {

void init(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
currentServerType = get("video_server", "WEBRTC");
currentServerType = get("video_server", "RTP");
}

private void set(String name, String value) {
Expand Down Expand Up @@ -59,7 +60,14 @@ private void setBoolean(String name, boolean value) {
}

public String getVideoServerType() {
return get("video_server", "WEBRTC");
return get("video_server", "RTP");
}

public String[] getVideoServerAddress() {
String ip = get("ip", "127.0.0.1");
String port_stream = get("port_stream", "8046");
String port_control = get("port_control", "8040");
return new String[] {ip, port_stream, port_control};
}

public void setVideoServerType(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public interface ILocalConnection {

void start();

void setServerAddress(String ip, String port);

boolean isVideoCapable();
}
2 changes: 2 additions & 0 deletions android/app/src/main/java/org/openbot/env/IVideoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public interface IVideoServer {
void setView(com.pedro.rtplibrary.view.OpenGlView view);

void setCanStart(boolean canStart);

void setServerAddress(String ip, String port);
}
236 changes: 236 additions & 0 deletions android/app/src/main/java/org/openbot/env/MobileNetworkConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package org.openbot.env;

import static timber.log.Timber.e;
import static timber.log.Timber.i;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.openbot.utils.ConnectionUtils;
import timber.log.Timber;

public class MobileNetworkConnection implements ILocalConnection {

private static final String TAG = "MobileNetworkConn";
private Context context;

private String HOST;
private int PORT;
private IDataReceived dataReceivedCallback;
private SocketHandler socketHandler;
private BlockingQueue<String> messageQueue = new ArrayBlockingQueue<>(25);
private boolean stopped = true;

@Override
public void init(Context context) {
socketHandler = new SocketHandler(messageQueue);
}

@Override
public void setDataCallback(IDataReceived dataCallback) {
this.dataReceivedCallback = dataCallback;
}

@Override
public void connect(Context context) {
this.context = context;
start();
runConnection();
}

@Override
public void disconnect(Context context) {
stop();

if (socketHandler == null) {
return;
}
socketHandler.close();
}

@Override
public void stop() {
stopped = true;
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("CONNECTION_ACTIVE", false));
}

@Override
public void start() {

stopped = false;
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("CONNECTION_ACTIVE", true));
}

@Override
public void setServerAddress(String ip, String port) {
this.HOST = ip;
this.PORT = Integer.parseInt(port);
}

@Override
public boolean isVideoCapable() {
return true;
}

@Override
public boolean isConnected() {
return socketHandler != null && socketHandler.isConnected();
}

@Override
public void sendMessage(String message) {
if (socketHandler != null) {
socketHandler.put(message);
}
}
// end of interface

private void runConnection() {
Timber.d("PORT: " + PORT + ", address: " + HOST);

((Activity) context)
.runOnUiThread(
() -> {
ControllerToBotEventBus.emitEvent("{command: \"CONNECTED\"}");
});

new Thread("Receiver Thread") {
public void run() {
SocketHandler.ClientInfo clientInfo = socketHandler.connect(HOST, PORT);
if (clientInfo == null) {
Timber.d("Could not get a connection");
return;
}
startReceiver(socketHandler, clientInfo.reader);
startSender(socketHandler, clientInfo.writer);
}
}.start();
}

private void startReceiver(SocketHandler socketHandler, Scanner reader) {
new Thread("startReceiver Thread") {
public void run() {
socketHandler.runReceiver(reader);
}
}.start();
}

private void startSender(SocketHandler socketHandler, OutputStream writer) {
new Thread("startSender Thread") {
public void run() {
try {
socketHandler.runSender(writer);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}

class SocketHandler {
private BlockingQueue<String> messageQueue;
private Socket client;

boolean isConnected() {
return client != null && !client.isClosed();
}

SocketHandler(BlockingQueue<String> messageQueue) {
this.messageQueue = messageQueue;
}

class ClientInfo {
Scanner reader;
OutputStream writer;

ClientInfo(Scanner reader, OutputStream writer) {
this.reader = reader;
this.writer = writer;
}
}

ClientInfo connect(String host, int port) {
ClientInfo clientInfo;

try {
client = new Socket(host, port);
clientInfo =
new ClientInfo(
new Scanner(new DataInputStream(new BufferedInputStream(client.getInputStream()))),
client.getOutputStream());
} catch (Exception e) {
return null;
}

return clientInfo;
}

void runReceiver(Scanner reader) {
try {
while (true) {
String msg = reader.nextLine().trim();

if (!stopped) {
((Activity) context).runOnUiThread(() -> dataReceivedCallback.dataReceived(msg));
}
}
} catch (Exception e) {
close();
}
}

void put(String message) {
try {
this.messageQueue.put(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@SuppressLint("TimberArgCount")
void runSender(OutputStream writer) {
while (true) {
try {
String message = messageQueue.take();
i(TAG, "queue capacity: " + messageQueue.remainingCapacity());
writer.write((message + "\n").getBytes(StandardCharsets.UTF_8));
} catch (InterruptedException | IOException e) {
i(TAG, "runSender got exception: " + e);
close();

// reconnect again
if (isConnected()) {
runConnection();
}
break;
}
}
}

void close() {
try {
if (client == null || client.isClosed()) {
return;
}
client.close();

((Activity) context)
.runOnUiThread(
() -> {
ControllerToBotEventBus.emitEvent("{command: \"DISCONNECTED\"}");
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ public void start() {
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("CONNECTION_ACTIVE", true));
}

@Override
public void setServerAddress(String ip, String port) {}

@Override
public boolean isVideoCapable() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public void start() {
BotToControllerEventBus.emitEvent(ConnectionUtils.createStatus("CONNECTION_ACTIVE", true));
}

@Override
public void setServerAddress(String ip, String port) {}

@Override
public boolean isVideoCapable() {
return true;
Expand Down
Loading