Proper/best LibVLC lifecycle?
Posted: 06 Jul 2021 17:31
I am using LibVLC (pulled from VLC version 3.0.9) in a Qt/C++ app I am running under xUbuntu Linux on an old (circa 2009 I think) MacBook Pro. The workflow I have is that, upon being triggered by external events VLC will be initialized using the code below, play for several seconds, then stop. This process will then repeat periodically at irregular intervals (anywhere from a few seconds to hours/days) forever, as external events trigger it. My question is what is the best lifecycle for LibVLC for this sort of use? What "cleanup" should I be doing after each play cycle to prevent memory leaks while ensuring the best performance/reliability of the system?
My initialization code currently looks like this:
And after playback is complete, I run the following cleanup code:
Is this enough cleanup? Too much? Not enough? Thanks in advance!
My initialization code currently looks like this:
Code: Select all
vlcInstance=libvlc_new(0,NULL);
QString rtsp_url="rtsp://<my_rtsp_source>";
libvlc_media_t *vlcMedia=libvlc_media_new_location(vlcInstance,rtsp_url.toUtf8());
vlcPlayer=libvlc_media_player_new_from_media(vlcMedia);
libvlc_audio_set_volume(vlcPlayer,75);
vlcEventManager=libvlc_media_player_event_manager(vlcPlayer);
libvlc_event_attach(vlcEventManager,libvlc_MediaPlayerPlaying,
&play_event_handler,this);
libvlc_event_attach(vlcEventManager,libvlc_MediaPlayerEncounteredError,
&vlc_error_handler,this);
libvlc_media_player_set_xwindow(vlcPlayer, ui->videoframe->winId());
libvlc_media_player_play(vlcPlayer);
Code: Select all
if (vlcPlayer!=nullptr){
if(libvlc_media_player_is_playing(vlcPlayer)){
libvlc_media_player_stop(vlcPlayer);
}
libvlc_media_player_release(vlcPlayer);
vlcPlayer=nullptr;
}
if(vlcInstance!=nullptr){
libvlc_release(vlcInstance);
vlcInstance=nullptr;
}