1, Foreword
The last work is made by the ffmpeg kernel. Because ffmpeg is too powerful, many beginners will see the state of confusion. There are also many users who only need a simple video stream to play, which does not need to involve the decoding and transcoding. So vlc came on the stage. It directly encapsulates ffmpeg in depth and provides a friendly interface There is also an mpv. Compared with vlc, mpv is even more powerful in library files. It seems that it is packaged into a static library, and does not want vlc to bring a bunch of dynamic library files and plug-in files. Of course, the simplicity of vlc is that it only needs a few lines of code to roll it up, so that beginners can see the effect is very important and excited. They can carry out the next step of coding faster and experience The fun of coding.
2, Code thinking
Step 1: introduce the header file of vlc
# ifdef __cplusplus extern "C" { # endif #ifdef vlc3 #include <libvlc.h> #include <libvlc_renderer_discoverer.h> #include <libvlc_media.h> #include <libvlc_media_player.h> #include <libvlc_media_list.h> #include <libvlc_media_list_player.h> #include <libvlc_media_library.h> #include <libvlc_media_discoverer.h> #include <libvlc_events.h> #include <libvlc_dialog.h> #include <libvlc_vlm.h> #include <deprecated.h> #else #include <libvlc.h> #include <libvlc_structures.h> #include <libvlc_media.h> #include <libvlc_media_player.h> #include <libvlc_media_list.h> #include <libvlc_media_list_player.h> #include <libvlc_media_library.h> #include <libvlc_media_discoverer.h> #include <libvlc_events.h> #include <libvlc_vlm.h> #include <deprecated.h> #endif # ifdef __cplusplus } # endif
Step 2: set the handle to open the video stream
bool VlcThread::init() { const char *tempArg = ""; const char *vlc_args[9] = {"-I", "dummy", "--no-osd", "--no-stats", "--ignore-config", "--no-video-on-top", "--no-video-title-show", "--no-snapshot-preview", tempArg}; vlcInst = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); if (vlcInst == NULL) { return false; } vlcMedia = libvlc_media_new_location(vlcInst, url.toUtf8().constData()); vlcPlayer = libvlc_media_player_new_from_media(vlcMedia); if (vlcPlayer == NULL) { return false; } //Set playback handle VlcWidget *w = (VlcWidget *)this->parent(); #if defined(Q_OS_WIN) libvlc_media_player_set_hwnd(vlcPlayer, (void *)w->winId()); #elif defined(Q_OS_LINUX) libvlc_media_player_set_xwindow(vlcPlayer, w->winId()); #elif defined(Q_OS_MAC) libvlc_media_player_set_nsobject(vlcPlayer, (void *)w->winId()); #endif //Set hardware acceleration none auto any d3d11va dxva2 setOption(QString(":avcodec-hw=%1").arg("none")); //Set the communication protocol tcp udp setOption(QString(":rtsp-%1").arg("tcp")); //Set cache time default 500ms setOption(QString(":network-caching=%1").arg(300)); libvlc_media_player_play(vlcPlayer); //qDebug() << TIMEMS << "init vlc finsh"; return true; }
Step 3: suspend and release resources
void VlcThread::pause() { if (vlcPlayer != NULL) { libvlc_media_player_pause(vlcPlayer); } } void VlcThread::next() { if (vlcPlayer != NULL) { libvlc_media_player_pause(vlcPlayer); } } void VlcThread::free() { if (vlcInst != NULL) { libvlc_release(vlcInst); vlcInst = NULL; } if (vlcMedia != NULL) { libvlc_media_release(vlcMedia); vlcMedia = NULL; } if (vlcPlayer != NULL) { libvlc_media_player_release(vlcPlayer); vlcPlayer = NULL; } //qDebug() << TIMEMS << "close vlc ok"; }
3, Renderings
4, Open source Homepage
All of the above works are downloaded from the open source homepage, and the quantity and quality of works will be continuously updated. You are welcome to pay attention.
- Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
- International site: https://github.com/feiyangqingyun/QWidgetDemo
- Personal homepage: https://blog.csdn.net/feiyangqingyun
- Zhihu homepage: https://www.zhihu.com/people/feiyangqingyun/