alexeyvasilyev / rtsp-client-android

Lightweight RTSP client library for Android
Apache License 2.0
210 stars 54 forks source link

NullPointerException when RtspSurfaceView.start(boolean, boolean) is called in RtspStatusListener.onRtspStatusDisconnected() #65

Open rloschmann opened 9 months ago

rloschmann commented 9 months ago

The problem is in this method:

private fun onRtspClientStopped() {
    if (DEBUG) Log.v(TAG, "onRtspClientStopped()")
    uiHandler.post { statusListener?.onRtspStatusDisconnected() }
    stopDecoders()
    rtspThread = null
}

The listener is notified before the rtspThread is set to null. So in the start method there can be a NullPointerException in one of the 2 last lines:

fun start(requestVideo: Boolean, requestAudio: Boolean) {
    if (DEBUG) Log.v(TAG, "start(requestVideo=$requestVideo, requestAudio=$requestAudio)")
    if (rtspThread != null) rtspThread?.stopAsync()
    this.requestVideo = requestVideo
    this.requestAudio = requestAudio
    rtspThread = RtspThread()
    rtspThread!!.name = "RTSP IO thread [${getUriName()}]" // NullPointerException here
    rtspThread!!.start() // Or NullPointerException here
}

I propose the following fix (post the listener after rtspThread is set to null):

private fun onRtspClientStopped() {
    if (DEBUG) Log.v(TAG, "onRtspClientStopped()")
    stopDecoders()
    rtspThread = null
    uiHandler.post { statusListener?.onRtspStatusDisconnected() } // Move this line after nullifying everything
}