millicast / millicast-native-sdk

SDK to build native clients using the Millicast platform.
Other
17 stars 5 forks source link

Auto reconnect cannot be stopped on Android #21

Closed florentmaitre closed 8 months ago

florentmaitre commented 8 months ago

My team and I are using the Android SDK and we encounter an issue with the auto reconnect feature of a subscriber since version 1.7.3.

When this feature is enabled and the subscriber tries to reconnect to the Millicast platform, then the connection cannot be stopped and the subscriber tries to reconnect forever.

Here is a simple sample code which illustrates the issue:

class Player {

    private val subscriber by lazy { Core.createSubscriber() }

    private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
        Log.d("Millicast", "exception = $throwable")
    }

    private val coroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob() + coroutineExceptionHandler)

    init {
        Core.initialize()
        Logger.setLogLevels(LogLevel.MC_ERROR)
        Logger.setLoggerListener { p0, p1 -> Log.d("Millicast", p0) }

        coroutineScope.launch {
            subscriber.state
                .map { it.connectionState }
                .distinctUntilChanged()
                .collect { connectionState ->
                    if (connectionState == SubscriberConnectionState.Connected) {
                        subscriber.subscribe()
                    }
                }
        }
    }

    fun play() {
        Log.d("Millicast", "play")
        coroutineScope.launch {
            val credentials = Credential(
                "streamName",
                "accountId",
                "https://director.millicast.com/api/director/subscribe",
                null
            )
            subscriber.setCredentials(credentials)
            val connectionOptions = ConnectionOptions(true)
            subscriber.connect(connectionOptions)
        }
    }

    fun stop() {
        Log.d("Millicast", "stop")
        coroutineScope.launch {
            subscriber.disconnect()
        }
    }
}

And here are the associated logs:

1.7.2 (OK)

2024-03-20 16:42:11.066 30484-30484 Millicast               com.example.myapplication            D  play
2024-03-20 16:42:11.074 30484-30531 Millicast               com.example.myapplication            D  Using non secure viewer
2024-03-20 16:42:12.091 30484-30531 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:42:12.092 30484-30531 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:42:12.092 30484-30531 Millicast               com.example.myapplication            D  Could not connect via HTTPS. Code: -1, Error: Could not make http request. It is possible that the serveryou want to reach does not exist or you do not have a valid root CA
2024-03-20 16:42:12.093 30484-30531 Millicast               com.example.myapplication            D  [Sub][Fail] Connection failed, trying to reconnect if allowed in config...
2024-03-20 16:42:12.814 30484-30531 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:42:12.814 30484-30531 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:42:12.815 30484-30531 Millicast               com.example.myapplication            D  Could not connect via HTTPS. Code: -1, Error: Could not make http request. It is possible that the serveryou want to reach does not exist or you do not have a valid root CA
2024-03-20 16:42:12.816 30484-30531 Millicast               com.example.myapplication            D  [Sub][Fail] Connection failed, trying to reconnect if allowed in config...
2024-03-20 16:42:13.356 30484-30484 Millicast               com.example.myapplication            D  stop
2024-03-20 16:42:13.364 30484-30529 Millicast               com.example.myapplication            D  exception = com.millicast.utils.MillicastException: Operation cancelled: operation aborted

1.7.3 (KO)

2024-03-20 16:41:29.176 30302-30302 Millicast               com.example.myapplication            D  play
2024-03-20 16:41:29.202 30302-30344 Millicast               com.example.myapplication            D  Using non secure viewer
2024-03-20 16:41:30.102 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:30.102 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:30.102 30302-30344 Millicast               com.example.myapplication            D  Could not connect via HTTPS. Code: -1, Error: Could not make http request. It is possible that the serveryou want to reach does not exist or you do not have a valid root CA
2024-03-20 16:41:30.102 30302-30344 Millicast               com.example.myapplication            D  [Sub][Fail] Connection failed, trying to reconnect if allowed in config...
2024-03-20 16:41:30.809 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:30.809 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:30.810 30302-30344 Millicast               com.example.myapplication            D  Could not connect via HTTPS. Code: -1, Error: Could not make http request. It is possible that the serveryou want to reach does not exist or you do not have a valid root CA
2024-03-20 16:41:30.810 30302-30344 Millicast               com.example.myapplication            D  [Sub][Fail] Connection failed, trying to reconnect if allowed in config...
2024-03-20 16:41:31.410 30302-30302 Millicast               com.example.myapplication            D  stop
2024-03-20 16:41:32.015 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:32.015 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:32.015 30302-30344 Millicast               com.example.myapplication            D  Could not connect via HTTPS. Code: -1, Error: Could not make http request. It is possible that the serveryou want to reach does not exist or you do not have a valid root CA
2024-03-20 16:41:32.015 30302-30344 Millicast               com.example.myapplication            D  [Sub][Fail] Connection failed, trying to reconnect if allowed in config...
2024-03-20 16:41:34.300 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:34.301 30302-30344 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:41:34.302 30302-30344 Millicast               com.example.myapplication            D  Could not connect via HTTPS. Code: -1, Error: Could not make http request. It is possible that the serveryou want to reach does not exist or you do not have a valid root CA
2024-03-20 16:41:34.302 30302-30344 Millicast               com.example.myapplication            D  [Sub][Fail] Connection failed, trying to reconnect if allowed in config...

1.8.0 (KO)

2024-03-20 16:45:39.034 31235-31235 Millicast               com.example.myapplication            D  play
2024-03-20 16:45:39.972 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:39.972 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:39.975 31235-31287 Millicast               com.example.myapplication            D  [millicast_client.cpp:473]  Could not make HTTPS request. It is possible that there is no network connectivity, or that the server you want to reach does not exist, or that you do not have a valid root CA.: Http Error: Request aborted
2024-03-20 16:45:40.803 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:40.804 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:40.805 31235-31287 Millicast               com.example.myapplication            D  [millicast_client.cpp:473]  Could not make HTTPS request. It is possible that there is no network connectivity, or that the server you want to reach does not exist, or that you do not have a valid root CA.: Http Error: Request aborted
2024-03-20 16:45:41.412 31235-31235 Millicast               com.example.myapplication            D  stop
2024-03-20 16:45:42.007 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:42.007 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:42.007 31235-31287 Millicast               com.example.myapplication            D  [millicast_client.cpp:473]  Could not make HTTPS request. It is possible that there is no network connectivity, or that the server you want to reach does not exist, or that you do not have a valid root CA.: Http Error: Request aborted
2024-03-20 16:45:44.229 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:44.229 31235-31287 Millicast               com.example.myapplication            D  Http connection failed: 400 Error 400 : stream not being published
2024-03-20 16:45:44.230 31235-31287 Millicast               com.example.myapplication            D  [millicast_client.cpp:473]  Could not make HTTPS request. It is possible that there is no network connectivity, or that the server you want to reach does not exist, or that you do not have a valid root CA.: Http Error: Request aborted

Are we doing something wrong or is this a regression? Thanks for you help

djova-dolby commented 8 months ago

Hi sorry for the late reply, for some reason I don't get automatic emails when issues are posted here.

@codlab can you take a look at these logs? There was state change management change introduce for Android between 1.7.2 -> 1.7.3. I think those may have inadvertently caused this. I will send you the commits in messages.

codlab commented 8 months ago

From the logs, there are 2 possibilities. One that I'd like to be sure of, @florentmaitre can you check that for any reason, the sdk or the app was not forcing the coroutineScope calling the launch to be canceled which means that the stop being logged was not followed by an actual attempt to disconnect()

A breaking point on the subscriber.disconnect() should suffice or changing the order with

    fun stop() {
        coroutineScope.launch {
            Log.d("Millicast", "stop")
            subscriber.disconnect()
            Log.d("Millicast", "stopped")
        }
    }

I'll test rn for the possibility that it's internal

codlab commented 8 months ago

We indeed have an issue that is related to the changes in the state but this also hides something which was used as a feature in this snippet. This will likely change in future version of the sdk but for now a 1.8.1 is scheduled to be released without any modifications in behaviour - so normally there will be no changes required in the above code

djova-dolby commented 8 months ago

Issue has been fixed with 1.8.1 release: https://github.com/millicast/millicast-native-sdk/releases/tag/v1.8.1. There is some issue with Sonatype so the publishing of Android artifacts there is encountering some issues. We will keep this ticket open until we can publish to Maven repo. @codlab FYI

codlab commented 8 months ago

The 1.8.1 containing the fix for android is live and can be used right away (edit: no changes in the API are expected so it should be working by just updating the referenced version)

florentmaitre commented 8 months ago

Issue is fixed on our side with 1.8.1. Thank you 👍