-
Notifications
You must be signed in to change notification settings - Fork 529
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
hq286
wants to merge
12
commits into
isl-org:master
Choose a base branch
from
hq286:haoquan/rtp-video-stream-control
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1f281f9
added libstreaming library in gradle file; added resource xml files
bfe6488
added a mobile network connection for vehicle control message
9ff45e7
updated controller config, and phone controller, and camera activity
d260dc5
added a new RTP video server type
75627c3
added a RTP video stream displayer, and a python joystick controller
7498931
updated README and root preference file
4425b08
updated README
6810eaf
fixed code styles for python and java
hq286 99f6c5c
fixed free roam bug, and changed the default ip in settings
hq286 83195ee
fixed code style
hq286 43e8d30
fixed webrtc init
hq286 49c1205
OpenCV install script
thias15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ public class ControllerConfig { | |
|
||
enum VIDEO_SERVER_TYPE { | ||
WEBRTC, | ||
RTSP | ||
RTSP, | ||
RTP | ||
} | ||
|
||
private String currentServerType; | ||
|
@@ -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) { | ||
|
@@ -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", "172.217.22.14"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have some neutral IP as default or just something like N/A if no server is setup. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it to "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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
android/app/src/main/java/org/openbot/env/MobileNetworkConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?