Set id and password correctly to Android RTSP module
authorChanhee Lee <ch2102.lee@samsung.com>
Wed, 11 Jan 2023 04:19:13 +0000 (13:19 +0900)
committerChanhee Lee <ch2102.lee@samsung.com>
Thu, 9 Mar 2023 02:18:37 +0000 (11:18 +0900)
[Problem] Credential information is not set properly in an Android
          RTSP module.
[Solution] Set id and password obtained from MQTT discovery to
           the module.

android/aitt/src/main/java/com/samsung/android/aitt/stream/RTSPStream.java
android/modules/rtsp/src/main/java/com/samsung/android/modules/rtsp/RTSPClient.java

index f46ae82..bb19c0d 100644 (file)
@@ -56,7 +56,8 @@ public class RTSPStream implements AittStream {
 
     /**
      * 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) {
@@ -86,7 +87,8 @@ public class RTSPStream implements AittStream {
 
     /**
      * 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
      */
@@ -99,7 +101,8 @@ public class RTSPStream implements AittStream {
 
     /**
      * 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
      */
@@ -112,6 +115,7 @@ public class RTSPStream implements AittStream {
 
     /**
      * Method to set configuration
+     *
      * @param config AittStreamConfig object
      */
     @Override
@@ -158,9 +162,10 @@ public class RTSPStream implements AittStream {
 
     /**
      * 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
      */
@@ -185,7 +190,7 @@ public class RTSPStream implements AittStream {
      */
     @Override
     public void stop() {
-        if(streamRole == StreamRole.SUBSCRIBER) {
+        if (streamRole == StreamRole.SUBSCRIBER) {
             if (clientState == StreamState.PLAYING)
                 rtspClient.stop();
             updateState(streamRole, StreamState.INIT);
@@ -219,11 +224,12 @@ public class RTSPStream implements AittStream {
 
     /**
      * 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");
@@ -231,6 +237,7 @@ public class RTSPStream implements AittStream {
 
     /**
      * Method to receive stream height
+     *
      * @return returns height of the stream
      */
     @Override
@@ -240,6 +247,7 @@ public class RTSPStream implements AittStream {
 
     /**
      * Method to receive stream width
+     *
      * @return returns width of the stream
      */
     @Override
@@ -249,6 +257,7 @@ public class RTSPStream implements AittStream {
 
     /**
      * Method to set subscribe callback
+     *
      * @param jniInterface JniInterface object
      */
     public void setJNIInterface(JniInterface jniInterface) {
@@ -297,7 +306,7 @@ public class RTSPStream implements AittStream {
 
             if (serverState == StreamState.READY) {
                 if (clientState == StreamState.READY) {
-                    startRtspClient();
+                    startRtspClient(discoveryInfo.id, discoveryInfo.password);
                 }
             } else if (serverState == StreamState.INIT) {
                 if (clientState == StreamState.PLAYING) {
@@ -326,10 +335,14 @@ public class RTSPStream implements AittStream {
     }
 
     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");
index 7bf67a0..defe1cc 100644 (file)
@@ -35,7 +35,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
 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;
@@ -63,8 +64,9 @@ public class RTSPClient {
 
     /**
      * 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;
@@ -73,9 +75,10 @@ public class RTSPClient {
 
     /**
      * 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;
@@ -84,13 +87,13 @@ public class RTSPClient {
         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");
                 }
             });
 
@@ -104,7 +107,7 @@ public class RTSPClient {
     /**
      * 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
@@ -115,7 +118,7 @@ public class RTSPClient {
             @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;
                 }
@@ -123,7 +126,7 @@ public class RTSPClient {
 
             @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
@@ -156,12 +159,22 @@ public class RTSPClient {
         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();
+        }
     }
 
     /**
@@ -175,7 +188,7 @@ public class RTSPClient {
      * Method to stop RTSP streaming
      */
     public void stop() {
-        try{
+        try {
             NetUtils.closeSocket(clientSocket);
             decoder.stopDecoder();
         } catch (Exception E) {
@@ -185,6 +198,7 @@ public class RTSPClient {
 
     /**
      * Method to set RTSP URL
+     *
      * @param url String for RTSP URL
      */
     public void setRtspUrl(String url) {
@@ -193,8 +207,9 @@ public class RTSPClient {
 
     /**
      * 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;