TakuSemba / RtmpPublisher

Rtmp client on Android. Live Video Streaming.
Apache License 2.0
628 stars 149 forks source link

Fail to connect to camera service #14

Open GitEliteNovice opened 6 years ago

GitEliteNovice commented 6 years ago

Process: request.elgroupinternational.com.randsoom, PID: 8025 java.lang.RuntimeException: Unable to resume activity {request.elgroupinternational.com.randsoom/request.elgroupinternational.com.randsoom.MainActivity}: java.lang.RuntimeException: Failed to call observer method at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3788) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3036) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.RuntimeException: Failed to call observer method at android.arch.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:225) at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:193) at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:184) at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36) at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354) at android.arch.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:292) at android.arch.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:332) at android.arch.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:137) at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:123) at android.arch.lifecycle.ReportFragment.dispatch(ReportFragment.java:121) at android.arch.lifecycle.ReportFragment.onResume(ReportFragment.java:88) at android.app.Fragment.performResume(Fragment.java:2657) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1319) at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1549) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1611) at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3045) at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3007) at android.app.FragmentController.dispatchResume(FragmentController.java:201) at android.app.Activity.performResume(Activity.java:7365) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3763) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3036)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6938)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)  Caused by: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:510) at android.hardware.Camera.open(Camera.java:359) at com.takusemba.rtmppublisher.CameraClient.initCamera(CameraClient.java:76) at com.takusemba.rtmppublisher.CameraClient.open(CameraClient.java:30) at com.takusemba.rtmppublisher.RtmpPublisher.onResume(RtmpPublisher.java:100) at java.lang.reflect.Method.invoke(Native Method) at android.arch.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:218) at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:193)  at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:184)  at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)  at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)  at android.arch.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:292)  at android.arch.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:332)  at android.arch.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:137)  at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:123)  at android.arch.lifecycle.ReportFragment.dispatch(ReportFragment.java:121)  at android.arch.lifecycle.ReportFragment.onResume(ReportFragment.java:88)  at android.app.Fragment.performResume(Fragment.java:2657)  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1319)  at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1549)  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1611)  at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3045)  at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3007)  at android.app.FragmentController.dispatchResume(FragmentController.java:201)  at android.app.Activity.performResume(Activity.java:7365)  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3763)  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3036)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6938)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

anuranBarman commented 6 years ago

App is not given the camera and microphone permission. Go to settings of your phone and from apps section select RTMPPublisher and click on permissions and allow two permissions

Nazacheres commented 5 years ago

Greetings, I am having the same issue, permission are checked. Tried on different devices, still same error. Seems like some issue with View Lifecycle. Here is my stack trace:

        at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:226)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:194)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:185)
        at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:188)
        at com.takusemba.rtmppublisher.RtmpPublisher.<init>(RtmpPublisher.java:39)
        at com.takusemba.rtmppublisher.Publisher$Builder.build(Publisher.java:158)
        at com.provoco.screen.broadcast.FragmentBroadcast$subscribeUi$1$$special$$inlined$let$lambda$1.onPermissionsChecked(FragmentBroadcast.kt:106)
        at com.karumi.dexter.DexterInstance$1.run(Unknown Source:43)
        at com.karumi.dexter.MainThread.execute(Unknown Source:6)
        at com.karumi.dexter.DexterInstance.checkMultiplePermissions(Unknown Source:56)
        at com.karumi.dexter.DexterInstance.checkPermissions(Unknown Source:0)
        at com.karumi.dexter.Dexter.check(Unknown Source:10)
        at com.provoco.screen.broadcast.FragmentBroadcast$subscribeUi$1.onChanged(FragmentBroadcast.kt:124)
        at com.provoco.screen.broadcast.FragmentBroadcast$subscribeUi$1.onChanged(FragmentBroadcast.kt:31)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at com.provoco.arch.StateHolder$internalUpdate$$inlined$synchronized$lambda$1.invoke(StateHolder.kt:47)
        at com.provoco.arch.StateHolder$internalUpdate$$inlined$synchronized$lambda$1.invoke(StateHolder.kt:14)
        at com.provoco.arch.RecursiveLock.run(StateHolder.kt:75)
        at com.provoco.arch.StateHolder.internalUpdate(StateHolder.kt:41)
        at com.provoco.arch.StateHolder.updateNow(StateHolder.kt:36)
        at com.provoco.arch.MutableStateHolder.updateNow(StateHolder.kt:62)
        at com.provoco.arch.BaseViewModel.calculateState(BaseViewModel.kt:53)
        at com.provoco.arch.BaseViewModel.access$calculateState(BaseViewModel.kt:7)
        at com.provoco.arch.BaseViewModel$observeNewVariables$$inlined$forEach$lambda$1.onChanged(BaseViewModel.kt:43)
        at androidx.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at com.provoco.screen.broadcast.BroadcastViewModel$createStreamNow$1.invokeSuspend(BroadcastViewModel.kt:40)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:251)
        at android.app.ActivityThread.main(ActivityThread.java:6589)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters com.takusemba.rtmppublisher.CameraClient.open()' on a null object reference
        at com.takusemba.rtmppublisher.RtmpPublisher.onResume(RtmpPublisher.java:100) 

As far as I understand initialisation of RTMPPublisher fails on: activity.getLifecycle().addObserver(this);, and then when onResume is called, camera is not inited. I do not know why, they only difference between example and my app, that I am using fragment, and starting streaming inside: viewModel.state.liveData.observe(viewLifecycleOwner, Observer { state -> ... }

Nazacheres commented 5 years ago

Ok, so the issue seems to be here: LifecycleRegistry when adds the observer, may call the last event. As I am initialising the publisher in fragment after i get the url from server, activity is already in resume. so in addObserver method statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState)); is called.

Correspondingly onResume of RtmpPublisher is called event before the camera is initialised.

The solution could be following: to add rtmppublisher as observer of lifecycle, only when it is actually ready to do it -> so after initialization of all the fields.

Aroniez commented 5 years ago

Hey @Nazacheres , could you share code snippet of the solution. I'm using fragment and I'm running on the same solution. My code roughly looks like this:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val cameraSurface = view.findViewById<GLSurfaceView>(R.id.surface_view)
        publisher = Publisher.Builder(context as MainActivity)
                .setGlView(cameraSurface)
                .setUrl(streamingUrl)
                .setSize(Publisher.Builder.DEFAULT_WIDTH, Publisher.Builder.DEFAULT_HEIGHT)
                .setAudioBitrate(Publisher.Builder.DEFAULT_AUDIO_BITRATE)
                .setVideoBitrate(Publisher.Builder.DEFAULT_VIDEO_BITRATE)
                .setCameraMode(Publisher.Builder.DEFAULT_MODE)
                .setListener(this)
                .build()

        toggle_camera.setOnClickListener { publisher.switchCamera() }
        toggle_publish.setOnClickListener { if (publisher.isPublishing) publisher.stopPublishing() else publisher.startPublishing() }

    }

Thanks