alexeyvasilyev / rtsp-client-android

Lightweight low-latency RTSP client library for Android
Apache License 2.0
245 stars 67 forks source link

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

Closed rloschmann closed 2 months ago

rloschmann commented 1 year 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
}
alexeyvasilyev commented 2 months ago

Fixed in https://github.com/alexeyvasilyev/rtsp-client-android/releases/tag/4.0.2