Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
738 stars 186 forks source link

fix: Add a patch to fix Android crash when stopping video track #1002

Closed karasusan closed 8 months ago

karasusan commented 8 months ago

This patch fixes the crash of NativeToJavaVideoFrame in video_frame.cc.

#00 pc 0000000000394d9c  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::jni::WrapI420Buffer(_JNIEnv*, rtc::scoped_refptr<webrtc::I420BufferInterface> const&)+60)
#01 pc 00000000003941ac  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::jni::NativeToJavaVideoFrame(_JNIEnv*, webrtc::VideoFrame const&)+108)
#02 pc 0000000000391240  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::jni::VideoEncoderWrapper::Encode(webrtc::VideoFrame const&, std::__ndk1::vector<webrtc::VideoFrameType, std::__ndk1::allocator<webrtc::VideoFrameType> > const*)+348)
#03 pc 000000000012b7d4  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (unity::webrtc::UnityVideoEncoder::Encode(webrtc::VideoFrame const&, std::__ndk1::vector<webrtc::VideoFrameType, std::__ndk1::allocator<webrtc::VideoFrameType> > const*)+84)
#04 pc 000000000054998c  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::VideoStreamEncoder::EncodeVideoFrame(webrtc::VideoFrame const&, long)+1676)
#05 pc 0000000000548e04  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::VideoStreamEncoder::MaybeEncodeVideoFrame(webrtc::VideoFrame const&, long)+1400)
#06 pc 00000000005483e0  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::VideoStreamEncoder::OnFrame(webrtc::Timestamp, int, webrtc::VideoFrame const&)+656)
#07 pc 000000000053fe04  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (void absl::internal_any_invocable::RemoteInvoker<false, void, webrtc::(anonymous namespace)::FrameCadenceAdapterImpl::OnFrame(webrtc::VideoFrame const&)::$_0&&>(absl::internal_any_invocable::TypeErasedState*)+220)
#08 pc 0000000000368dd4  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (webrtc::(anonymous namespace)::TaskQueueLibevent::OnWakeup(int, short, void*)+240)
#09 pc 000000000036ab40  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (event_base_loop+1176)
#10 pc 0000000000369644  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (std::__ndk1::__function::__func<webrtc::(anonymous namespace)::TaskQueueLibevent::TaskQueueLibevent(std::__ndk1::basic_string_view<char, std::__ndk1::char_traits<char> >, rtc::ThreadPriority)::$_0, std::__ndk1::allocator<webrtc::(anonymous namespace)::TaskQueueLibevent::TaskQueueLibevent(std::__ndk1::basic_string_view<char, std::__ndk1::char_traits<char> >, rtc::ThreadPriority)::$_0>, void ()>::operator()()+76)
#11 pc 000000000040f870  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (std::__ndk1::__function::__func<rtc::PlatformThread::SpawnThread(std::__ndk1::function<void ()>, std::__ndk1::basic_string_view<char, std::__ndk1::char_traits<char> >, rtc::ThreadAttributes, bool)::$_0, std::__ndk1::allocator<rtc::PlatformThread::SpawnThread(std::__ndk1::function<void ()>, std::__ndk1::basic_string_view<char, std::__ndk1::char_traits<char> >, rtc::ThreadAttributes, bool)::$_0>, void ()>::operator()()+220)
#12 pc 000000000040f678  /data/app/com.DefaultCompany.sample-bK3oa2WEYI7iWytdp1Ujlw==/lib/arm64/libwebrtc.so (rtc::(anonymous namespace)::RunPlatformThread(void*)+20)
#13 pc 0000000000069b00  /system/lib64/libc.so (__pthread_start(void*)+36) (BuildId: f01907f9292ce7f34b800afcd8753d5c)
#14 pc 000000000001f958  /system/lib64/libc.so (__start_thread+68) (BuildId: f01907f9292ce7f34b800afcd8753d5c)

The cause of this issue is VideoFrameBuffer::ToI420 method often returns nullptr. however, NativeToJavaVideoFrame doesn't check nullptr. Therefore it occrrs crashes by null pointer access.

AndreiNegrila-SimplyVideo commented 7 months ago

I have been using the latest version of the WebRTC Unity package on the Magic Leap 2 and still seem to be having this issue of the app crashing whenever I stop the video track. Do you have any ideas on what could still be causing this issue for me even after the fix?