Android VLC: Crash while reproducing videos in hidden SurfaceView
Hi.
I'm embedding VLC into an android video streaming app and I get crashes while trying to reproduce a video in a surfaceView. My goal is to allow users to leave the player activity (where the surfaceView needed to reproduce a video is holded), so I can implement an autostop feature or a clock alarm (which can start a video even with a turned off screen) among others.
Please don't discuss the useless of these features cause it has sense, please beleive me.
To do this I implemented a service and a notificator with buttons to stop, or mute it; very similar to what VLC for android has.
My problem is that android will remove its surfaceView resources while going to background and it regenerates it if it goes to foreground. Having this in mind LibVLC doesn't detect when the surface is erased by android or not and it still tries to dump video frames into the surface.
In conclusion, SurfaceView can't be hidden in any place nor using a static variable in a non activity class (like a service) or it will crash.
I tested this in VLC for android project and it also crashes if you try to reproduce a video with its SurfaceView being hidden (the one in res/layout/player.xml with id player_surface)
On the other hand If you try to use a visible surfaceView in an activity, then you go to home and pause or stop video with the notificator buttons, libVLC.stop() command will start but won't finish. This happens as VLC can't join and close all its remaining threads, so it waits until the ones that uses the surface do it. But they won't until you resume the activity that holds it.
I debugged LibVLC to find where the crash happens and I found that sys->window at line 390 in vlc\modules\video_output\android\surface.c (which currently is 'int32_t err = sys->native_window.winLock(sys->window, &buf, NULL);') is null or empty so when native_window tries to call perform inside this line VLC crashes.
You can find native_window.cpp here: https://gitorious.org/rowboat/frameworks-base/source/319256206c89b1b19d128eee6947aa75792e7bc0:native/android/native_window.cpp#L34-79
Tested on next devices: Model Number: GT-S7580 and GT-I9506 (Galaxy S4) Android Version: 4.2.2, 4.3 VLC for Android 0.9.9 Last version of LibVLC in https://github.com/videolan/vlc at day of 24/09/2014