fyhertz / libstreaming

A solution for streaming H.264, H.263, AMR, AAC using RTP on Android
Apache License 2.0
3.49k stars 1.08k forks source link

Connecting to RtspServer via port forwarding #270

Open topapplabs opened 6 years ago

topapplabs commented 6 years ago

I have implemented example1 and can stream to VLC just fine on my local network. However, if I try to stream over the internet using port forwarding on my router (again with VLC with a URL of "rtsp://XXX.XX.XX.189:1234/" forwarded to the local address 10.0.1.80:1234/) , the RtspServer goes through the OPTIONS, DESCRIBE, SETUP, PLAY sequence. Then after 10 seconds I see a TEARDOWN and apparently a re-connection followed by OPTIONS, DESCRIBE and ERROR_CAMERA_ALREADY_IN_USE error. If I note there are two "sessionStopped" callbacks occurring afterwards, I suspect that the reconnection is happening before the first session has "cleaned things up" (that is the camera has not yet been released).

Here is the log:

01-04 14:45:21.895 2001-2001/com.example.me.myapplication D/RTSP: onCreate
01-04 14:45:21.942 2001-2001/com.example.me.myapplication D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@99d5551 className=net.majorkernelpanic.streaming.rtsp.RtspServer packageName=com.example.me.myapplication intent=null}
01-04 14:45:21.943 2001-2001/com.example.me.myapplication D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@99d5551 className=net.majorkernelpanic.streaming.rtsp.RtspServer packageName=com.example.me.myapplication intent=null}
01-04 14:45:21.944 2001-2001/com.example.me.myapplication D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@99d5551 startId=1 args=Intent { cmp=com.example.me.myapplication/net.majorkernelpanic.streaming.rtsp.RtspServer }}
01-04 14:45:21.943 2001-2001/com.example.me.myapplication D/ActivityThread: SVC-Calling onStartCommand: net.majorkernelpanic.streaming.rtsp.RtspServer@9f8ceb7, flags=0, startId=1
01-04 14:45:21.946 2001-8598/com.example.me.myapplication I/RtspServer: RTSP server listening on port 1234
01-04 14:45:21.993 2001-2001/com.example.me.myapplication D/RTSP: surfaceCreated...
01-04 14:45:21.993 2001-2001/com.example.me.myapplication D/RTSP: surfaceChanged...
01-04 14:45:49.174 2001-8834/com.example.me.myapplication I/RtspServer: Connection from XXX.XX.XX.189
01-04 14:45:49.177 2001-8834/com.example.me.myapplication E/RtspServer: OPTIONS rtsp://XXX.XX.XX.189:1234/
01-04 14:45:49.178 2001-8834/com.example.me.myapplication D/RtspServer: RTSP/1.0 200 OK
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 2
                                                                          Content-Length: 0
                                                                          Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE
01-04 14:45:49.194 2001-8834/com.example.me.myapplication E/RtspServer: DESCRIBE rtsp://XXX.XX.XX.189:1234/
01-04 14:45:49.914 2001-2001/com.example.me.myapplication D/RTSP: Streaming session configured.
01-04 14:45:49.914 2001-8834/com.example.me.myapplication D/RtspServer: RTSP/1.0 200 OK
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 3
                                                                          Content-Length: 282
                                                                          Content-Base: 10.0.1.80:1234/
                                                                          Content-Type: application/sdp

                                                                          v=0
                                                                          o=- 0 0 IN IP4 10.0.1.80
                                                                          s=Unnamed
                                                                          i=N/A
                                                                          c=IN IP4 XXX.XX.XX.189
                                                                          t=0 0
                                                                          a=recvonly
                                                                          m=video 5006 RTP/AVP 96
                                                                          a=rtpmap:96 H264/90000
                                                                          a=fmtp:96 packetization-mode=1;profile-level-id=42c014;sprop-parameter-sets=Z0LAFLkQWJ0IAAADAAgAAAMBRHihVQ==,aM48gA==;
                                                                          a=control:trackID=1
01-04 14:45:50.928 2001-8834/com.example.me.myapplication E/RtspServer: SETUP 10.0.1.80:1234/trackID=1
01-04 14:45:51.902 2001-2001/com.example.me.myapplication D/RTSP: Streaming session started.
01-04 14:45:51.903 2001-8834/com.example.me.myapplication D/RtspServer: RTSP/1.0 200 OK
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 4
                                                                          Content-Length: 0
                                                                          Transport: RTP/AVP/UDP;unicast;destination=XXX.XX.XX.189;client_port=5006-5007;server_port=36265-34259;ssrc=70d09744;mode=play
                                                                          Session: 1185d20035702ca
                                                                          Cache-Control: no-cache
01-04 14:45:51.916 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:52.158 2001-8834/com.example.me.myapplication E/RtspServer: PLAY 10.0.1.80:1234/
01-04 14:45:52.158 2001-8834/com.example.me.myapplication D/RtspServer: RTSP/1.0 200 OK
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 5
                                                                          Content-Length: 0
                                                                          RTP-Info: url=rtsp://10.0.1.80:1234/trackID=1;seq=0
                                                                          Session: 1185d20035702ca
01-04 14:45:52.403 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:52.903 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:53.404 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:53.905 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:54.405 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:54.906 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:55.407 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:55.908 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:56.408 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:56.909 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:57.410 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:57.910 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:58.411 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:58.912 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:59.412 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:45:59.913 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:00.414 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:00.914 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:01.415 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:01.915 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:02.415 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:02.688 2001-8834/com.example.me.myapplication E/RtspServer: TEARDOWN 10.0.1.80:1234/
01-04 14:46:02.688 2001-8834/com.example.me.myapplication D/RtspServer: RTSP/1.0 200 OK
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 6
                                                                          Content-Length: 0
01-04 14:46:02.693 2001-8988/com.example.me.myapplication I/RtspServer: Connection from XXX.XX.XX.189
01-04 14:46:02.740 2001-8988/com.example.me.myapplication E/RtspServer: OPTIONS rtsp://XXX.XX.XX.189:1234/
01-04 14:46:02.740 2001-8988/com.example.me.myapplication D/RtspServer: RTSP/1.0 200 OK
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 2
                                                                          Content-Length: 0
                                                                          Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE
01-04 14:46:02.743 2001-8988/com.example.me.myapplication E/RtspServer: DESCRIBE rtsp://XXX.XX.XX.189:1234/
01-04 14:46:02.751 2001-8988/com.example.me.myapplication E/RtspServer: Fail to connect to camera service
01-04 14:46:02.751 2001-2001/com.example.me.myapplication D/RTSP: Streaming session ERROR_CAMERA_ALREADY_IN_USE.
01-04 14:46:02.752 2001-8988/com.example.me.myapplication D/RtspServer: RTSP/1.0 500 Internal Server Error
                                                                          Server: MajorKernelPanic RTSP Server
                                                                          Cseq: 3
                                                                          Content-Length: 0
01-04 14:46:02.756 2001-2001/com.example.me.myapplication D/RTSP: Streaming session stopped.
01-04 14:46:02.756 2001-8988/com.example.me.myapplication I/RtspServer: Client disconnected
01-04 14:46:02.916 2001-2001/com.example.me.myapplication D/RTSP: Streaming session Bitrate updated.
01-04 14:46:03.206 2001-2001/com.example.me.myapplication D/RTSP: Streaming session stopped.
01-04 14:46:03.303 2001-8834/com.example.me.myapplication I/RtspServer: Client disconnected

Here is the code:

public class MainActivity extends AppCompatActivity implements Session.Callback, SurfaceHolder.Callback

{
    private final static String TAG = "RTSP";

    private SurfaceView mSurfaceView;
    private View mBottomView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");

        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mSurfaceView = (SurfaceView) findViewById(R.id.surface1);
        mBottomView = (View) findViewById(R.id.bottomhalf);

        // Sets the port of the RTSP server to 1234
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        editor.putString(RtspServer.KEY_PORT, String.valueOf(1234));
        editor.commit();

        mSurfaceView.getHolder().addCallback(this);

        // Configures the SessionBuilder
        SessionBuilder.getInstance()
                .setSurfaceView(mSurfaceView)
                .setPreviewOrientation(90)
                .setContext(getApplicationContext())
                .setAudioEncoder(SessionBuilder.AUDIO_NONE)
                .setVideoEncoder(SessionBuilder.VIDEO_H264)
                .setCallback(this)
                .build();

        // Starts the RTSP server
        this.startService(new Intent(this, RtspServer.class));
    }

    @Override
    protected void onDestroy()
    {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        this.stopService(new Intent(this, RtspServer.class));
    }

    // Session Callbacks
    @Override
    public void onBitrateUpdate(long bitrate) {
        Log.d(TAG,"Streaming session Bitrate updated.");
    }

    @Override
    public void onPreviewStarted() {
        Log.d(TAG,"Streaming session preview started");
    }

    @Override
    public void onSessionConfigured() {
        Log.d(TAG,"Streaming session configured.");
    }

    @Override
    public void onSessionStarted() {
        mBottomView.setBackgroundColor(Color.RED);
        Log.d(TAG,"Streaming session started.");
    }

    @Override
    public void onSessionStopped() {
        mBottomView.setBackgroundColor(Color.GRAY);
        Log.d(TAG,"Streaming session stopped."
        );
    }

    @Override
    public void onSessionError(int reason, int streamType, Exception e) {
        switch (reason) {
            case Session.ERROR_CAMERA_ALREADY_IN_USE:
                Log.d(TAG,"Streaming session ERROR_CAMERA_ALREADY_IN_USE.");
                break;
            case Session.ERROR_CAMERA_HAS_NO_FLASH:
                Log.d(TAG,"Streaming session ERROR_CAMERA_HAS_NO_FLASH.");
                break;
            case Session.ERROR_INVALID_SURFACE:
                Log.d(TAG,"Streaming session ERROR_INVALID_SURFACE.");
                break;
            case Session.ERROR_STORAGE_NOT_READY:
                Log.d(TAG,"Streaming session ERROR_STORAGE_NOT_READY.");
                break;
            case Session.ERROR_CONFIGURATION_NOT_SUPPORTED:
                Log.d(TAG,"Streaming session ERROR_CONFIGURATION_NOT_SUPPORTED.");
                e.printStackTrace();
                return;
            case Session.ERROR_OTHER:
                Log.d(TAG,"Streaming session ERROR_OTHER.");
                break;
        }
        if (e != null) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG,"surfaceChanged...");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG,"surfaceCreated...");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG,"surfaceDestroyed...");
    }

}

Here's the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.me.myapplication">

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="net.majorkernelpanic.streaming.rtsp.RtspServer" />
    </application>

</manifest>

Any guidance would be appreciated.