/**
* RTSPStream constructor
- * @param topic Topic to which streaming is invoked
+ *
+ * @param topic Topic to which streaming is invoked
* @param streamRole Role of the RTSPStream object
*/
private RTSPStream(String topic, StreamRole streamRole) {
/**
* Create and return RTSPStream object for subscriber role
- * @param topic Topic to which Subscribe role is set
+ *
+ * @param topic Topic to which Subscribe role is set
* @param streamRole Role of the RTSPStream object
* @return RTSPStream object
*/
/**
* Create and return RTSPStream object for publisher role
- * @param topic Topic to which Publisher role is set
+ *
+ * @param topic Topic to which Publisher role is set
* @param streamRole Role of the RTSPStream object
* @return RTSPStream object
*/
/**
* Method to set configuration
+ *
* @param config AittStreamConfig object
*/
@Override
/**
* Method to publish to a topic
- * @param topic String topic to which data is published
- * @param ip Ip of the receiver
- * @param port Port of the receiver
+ *
+ * @param topic String topic to which data is published
+ * @param ip Ip of the receiver
+ * @param port Port of the receiver
* @param message Data to be published
* @return returns status
*/
*/
@Override
public void stop() {
- if(streamRole == StreamRole.SUBSCRIBER) {
+ if (streamRole == StreamRole.SUBSCRIBER) {
if (clientState == StreamState.PLAYING)
rtspClient.stop();
updateState(streamRole, StreamState.INIT);
/**
* Method to set subscribe callback
+ *
* @param streamDataCallback subscribe callback object
*/
@Override
public void setReceiveCallback(StreamDataCallback streamDataCallback) {
- if(streamRole == StreamRole.SUBSCRIBER)
+ if (streamRole == StreamRole.SUBSCRIBER)
streamCallback = streamDataCallback;
else
throw new IllegalArgumentException("The role of this stream is not subscriber");
/**
* Method to receive stream height
+ *
* @return returns height of the stream
*/
@Override
/**
* Method to receive stream width
+ *
* @return returns width of the stream
*/
@Override
/**
* Method to set subscribe callback
+ *
* @param jniInterface JniInterface object
*/
public void setJNIInterface(JniInterface jniInterface) {
if (serverState == StreamState.READY) {
if (clientState == StreamState.READY) {
- startRtspClient();
+ startRtspClient(discoveryInfo.id, discoveryInfo.password);
}
} else if (serverState == StreamState.INIT) {
if (clientState == StreamState.PLAYING) {
}
private void startRtspClient() {
+ startRtspClient("", "");
+ }
+
+ private void startRtspClient(String id, String password) {
RTSPClient.SocketConnectCallback cb = socketSuccess -> {
if (socketSuccess) {
updateState(streamRole, StreamState.PLAYING);
- rtspClient.initRtspClient();
+ rtspClient.initRtspClient(id, password);
rtspClient.start();
} else {
Log.e(TAG, "Error creating socket");
public class RTSPClient {
private static final String TAG = "RTSPClient";
private static volatile Socket clientSocket;
- private static final int sdpInfoSize = 30;
+ // TODO: Set sdpInfoSize properly without a hard-coded value.
+ private static final int sdpInfoSize = 35;
private static final int socketTimeout = 10000;
private String rtspUrl = null;
private int height;
/**
* RTSPClient class constructor
+ *
* @param exitFlag AtomicBoolean flag to exit execution
- * @param cb callback object to send data to upper layer
+ * @param cb callback object to send data to upper layer
*/
public RTSPClient(AtomicBoolean exitFlag, ReceiveDataCallback cb) {
this.exitFlag = exitFlag;
/**
* Method to create a client socket for RTSP connection with RTSP server
+ *
* @param socketCB socket connection callback to notify success/failure of socket creation
*/
- public void createClientSocket(SocketConnectCallback socketCB){
+ public void createClientSocket(SocketConnectCallback socketCB) {
if (rtspUrl == null || rtspUrl.isEmpty()) {
Log.e(TAG, "Failed create client socket: Invalid RTSP URL");
return;
Uri uri = Uri.parse(rtspUrl);
try {
Thread thread = new Thread(() -> {
- try {
+ try {
clientSocket = NetUtils.createSocketAndConnect(uri.getHost(), uri.getPort(), socketTimeout);
- if(clientSocket != null)
+ if (clientSocket != null)
socketCB.socketConnect(true);
} catch (Exception e) {
socketCB.socketConnect(false);
- Log.d(TAG, "Exception in RTSP client socket creation");
+ Log.e(TAG, "Exception in RTSP client socket creation");
}
});
/**
* Method to create RtspClient object to access RTSP lib from dependency
*/
- public void initRtspClient() {
+ public void initRtspClient(String id, String password) {
RtspClient.RtspClientListener clientListener = new RtspClient.RtspClientListener() {
@Override
@Override
public void onRtspConnected(@NonNull @NotNull RtspClient.SdpInfo sdpInfo) {
Log.d(TAG, "Connected to RTSP server");
- if(sdpInfo.videoTrack != null) {
+ if (sdpInfo.videoTrack != null) {
sps = sdpInfo.videoTrack.sps;
pps = sdpInfo.videoTrack.pps;
}
@Override
public void onRtspVideoNalUnitReceived(@NonNull @NotNull byte[] bytes, int i, int i1, long l) {
- Log.d(TAG, "RTSP video stream callback -- video NAL units received");
+ Log.i(TAG, "RTSP video stream callback -- video NAL units received. bytes.length = " + bytes.length + ", sdpInfoSize = " + sdpInfoSize);
if (bytes.length < sdpInfoSize)
decoder.initH264Decoder(sps, pps);
else
Uri uri = Uri.parse(rtspUrl);
decoder = new H264Decoder(streamCb, height, width);
- mRtspClient = new RtspClient.Builder(clientSocket, uri.toString(), exitFlag, clientListener)
- .requestAudio(false)
- .requestVideo(true)
- .withDebug(true)
- .withUserAgent("RTSP sample Client")
- .build();
+ if ("".equalsIgnoreCase(id)) {
+ mRtspClient = new RtspClient.Builder(clientSocket, uri.toString(), exitFlag, clientListener)
+ .requestAudio(false)
+ .requestVideo(true)
+ .withDebug(true)
+ .withUserAgent("RTSP sample Client")
+ .build();
+ } else {
+ mRtspClient = new RtspClient.Builder(clientSocket, uri.toString(), exitFlag, clientListener)
+ .requestAudio(false)
+ .requestVideo(true)
+ .withCredentials(id, password)
+ .withDebug(true)
+ .withUserAgent("RTSP sample Client")
+ .build();
+ }
}
/**
* Method to stop RTSP streaming
*/
public void stop() {
- try{
+ try {
NetUtils.closeSocket(clientSocket);
decoder.stopDecoder();
} catch (Exception E) {
/**
* Method to set RTSP URL
+ *
* @param url String for RTSP URL
*/
public void setRtspUrl(String url) {
/**
* Method to set RTSP frame resolution
+ *
* @param height Height of the RTSP stream
- * @param width Width of the RTSP stream
+ * @param width Width of the RTSP stream
*/
public void setResolution(int height, int width) {
this.height = height;