opentok / opentok-android-sdk-samples

Sample applications illustrating best practices using OpenTok Android SDK.
https://tokbox.com/developer/sdks/android/
MIT License
211 stars 169 forks source link

Crash on 2.24.2 and 2.25.0 when enabling / disabling publisher video #461

Closed janoonaj closed 1 year ago

janoonaj commented 1 year ago

Describe the bug If you publish video, unpublish video and publish again, the app crashes. It works fine on 2.24.1

To Reproduce

  1. Update examples to 2.25.0

  2. Open example Camera2 basic video capturer

  3. Add a button

  4. Add this code in MainActivity

    Button clickButton = (Button) findViewById(R.id.button);
        clickButton.setOnClickListener( new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                if(publisher.getPublishVideo()) {
                    publisher.setPublishVideo(false);
                } else
                    publisher.setPublishVideo(true);
    
            }
        });
  5. Press the button two times.

  6. Crash.

FATAL EXCEPTION: main Process: com.tokbox.sample.basicvideocapturercamera2, PID: 5975 com.tokbox.sample.basicvideocapturercamera2.MirrorVideoCapturer$Camera2Exception: Start Capture called before init successfully completed at com.tokbox.sample.basicvideocapturercamera2.MirrorVideoCapturer.startCapture(MirrorVideoCapturer.java:401)

Device (please compete the following information):

Additional context After a publishvideo = false the camera should be released but it is not. It is not completely closed so when the user wants to open it again, the camera is on "CLOSING" state and it crashes.

In previous SDK versions the camera was not released and reopened again, it was stable.

DanielNovak commented 1 year ago

Same problem here: https://github.com/opentok/opentok-android-sdk-samples/issues/460

DanielNovak commented 1 year ago

I opened a ticket internally with Vonage through my company login.

vona-ben commented 1 year ago

@janoonaj could you please try with: https://github.com/opentok/opentok-android-sdk-samples/pull/462

janoonaj commented 1 year ago

@vona-ben @v-kpheng Hello, it is more stable .

For us, in production, your PR is a good enough, but I would like to share with you the crash we are seeing.

There is a race condition. If you take your time it works perfectly. If you make some quick changes it crashes

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tokbox.sample.basicvideocapturercamera2, PID: 25044
    java.lang.IllegalStateException: Session has been closed; further changes are illegal.
        at android.hardware.camera2.impl.CameraCaptureSessionImpl.checkNotClosed(CameraCaptureSessionImpl.java:902)
        at android.hardware.camera2.impl.CameraCaptureSessionImpl.stopRepeating(CameraCaptureSessionImpl.java:418)
        at com.tokbox.sample.basicvideocapturercamera2.MirrorVideoCapturer.stopCapture(MirrorVideoCapturer.java:447)
        at com.opentok.android.PublisherKit.d(Unknown Source:4)
        at com.opentok.android.PublisherKit.$r8$lambda$6BMkMXi2EstSU_HgeV7ZQb4y6yA(Unknown Source:0)
        at com.opentok.android.PublisherKit$$ExternalSyntheticLambda0.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7884)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Disconnected from the target VM, address: 'localhost:54156', transport: 'socket'

Steps to reproduce

  1. Add a button anyware
  2. Add the click event
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(!OpenTokConfig.isValid()) {
            finishWithMessage("Invalid OpenTokConfig. " + OpenTokConfig.getDescription());
            return;
        }

        publisherViewContainer = findViewById(R.id.publisherview);
        subscriberViewContainer = findViewById(R.id.subscriberview);

        requestPermissions();

        Button clickButton = (Button) findViewById(R.id.button);
        clickButton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(publisher.getPublishVideo()) {
                    publisher.setPublishVideo(false);
                } else
                    publisher.setPublishVideo(true);

            }
        });
    }
  1. Launch the application and click the button every second. It works great.
  2. Press the button quickly 3-4 times . The app crashes.
vona-ben commented 1 year ago

Thank you for sharing @janoonaj. I will follow up internally regarding this issue.

v-kpheng commented 1 year ago

@vona-ben, any updates?

michal-luszczuk commented 1 year ago

Hello I'm not sure if this is similar case or not.

But using previous version of SDK, if client camera and microphone were turned off, we were unpublishing the Publisher from the session and later we were calling publisher.onStop() (base on this docs https://api.support.vonage.com/hc/en-us/articles/6646183450396-Camara-Still-in-Use-after-Session-Disconnect-on-Android) to release the camera (and to hide the Android indicator that camera/mic is in use)

But now in the newest version (2.24.2-2.25.2) after releasing the publisher and calling publisher.onStop() the app is crashing with the log:

 java.lang.IllegalStateException: Session has been closed; further changes are illegal.
 at  android.hardware.camera2.impl.CameraCaptureSessionImpl.checkNotClosed(CameraCaptureSessionImpl.java:902)
 at android.hardware.camera2.impl.CameraCaptureSessionImpl.stopRepeating(CameraCaptureSessionImpl.java:418)
 at com.opentok.android.Camera2VideoCapturer.stopCapture(Unknown Source:30)
 at com.opentok.android.PublisherKit.d(Unknown Source:4)
 at com.opentok.android.PublisherKit.$r8$lambda$6BMkMXi2EstSU_HgeV7ZQb4y6yA(Unknown Source:0)
 at com.opentok.android.PublisherKit$$ExternalSyntheticLambda20.run(Unknown Source:2)

If i comment out the line about publisher.onStop there is no crash, and indication disappears after a short while. Does it mean that onStop is called automatically in the newer SDK version and we should not call it on our own? What should we do?

janoonaj commented 1 year ago

Hello @michal-luszczuk In our case, the problem is that custom publisher "MirrorVideoCapturer" from their example was too old.

We copy-paste and then, adapt them to our requeriments. But it was not updated in this examples.

Can you try to apply the changes or can you try this example project?

More information here https://github.com/opentok/opentok-android-sdk-samples/pull/462

vona-ben commented 1 year ago

Hi @michal-luszczuk

Sorry for the delay, indeed onStop is called automatically in the newer SDK version and it's not needed anymore to call it.

vona-ben commented 1 year ago

Hi @janoonaj

Thank you for reporting that issue, I managed to reproduce the issue, and I have opened a ticket internally. in the meantime It would be better to implement a safeguard to avoid switching the video on/off quickly.

v-kpheng commented 1 year ago

FYI, this is being tracked internally via https://jira.vonage.com/browse/VIDCS-1208

goncalocostamendes commented 1 year ago

@janoonaj our solution for our internal ticket https://jira.vonage.com/browse/VIDCS-1208 was already validated and closed by our QA department.

In a soon to be released v2.26.1 version (between this and the following week), we correctly handle the camera states for all invoked actions.

I will close the ticket. If for some reason you find these changes do not solve your problem, please re-open it.