Play the video stream in the specified buffer with LIBVLC

Source: Internet
Author: User

Sometimes, we need to play other modules transmitted over the video stream, VLC provides such a mechanism, but generally rarely used, the following example realizes such a function.

One of the key module Imem is used. VLC provides a variety of ways to create media, such as data from a specified cache, which can be specified in the following ways

// Create A media item from file    " imem:// ");   /* # #use Memory as input */

The following is a complete example

//vlcTest.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<Windows.h>#include"vlc/vlc.h"#include<vector>#include<qmutex>#include<sstream>#include<qimage>Qmutex G_mutex;BOOLG_isinit =false;intImg_width =640; intImg_height =480; Charin_buffer[640*480*4];Charout_buffer[640*480*4]; FILE*Local;intFramenum =0;Const Char* Testfile ="B040_20170106.dat";///////////////////////////////////////////////////////////////////////////*  * \brief Callback method triggered by VLC to get the image data from a custom memory source.    This was used to tell VLC where the data was and to allocate buffers as needed. To set this callback, use the "--imem-get=<memory_address>" option, with memory_address the address of this Func    tion in memory. When using Imem, being sure to indicate the format for your data using "--imem-cat=2" where 2 is video.  Other options for categories is 0 = Unknown, 1 = Audio, 2 = Video, 3 = Subtitle, 4 = Data when creating    Your media instance, use Libvlc_media_new_location and set the location to "imem:/" and then play. \param[in] Data Pointer to user-defined data, which is your data and set by passing the "--imem-data=<memory_add    ress> "option when initializing VLC instance. \param[in] Cookie A User defined string. This works the same-as data, but for string. You set it by adding the '--imem-cookie=<your_string> ' option when YOU initialize VLC.    Use the When multiple VLC instances is running. \param[out] DTS The decode timestamp, value is in microseconds. This value is the time when the frame was decoded/generated.    For example, the FPS video would is every MS, so values would be 0, 33333, 66666, 99999, etc. \param[out] pts The presentation timestamp, value is in microseconds. This value tells the receiver when the present the frame.    For example, the FPS video would is every MS, so values would be 0, 33333, 66666, 99999, etc.    \param[out] Flags Unused,ignore.    \param[out] BufferSize Use this to set the size of the buffer in bytes.         \param[out] Buffer change to point to your encoded frame/audio/video data. The codec format of the frame is user defined and set using the '--imem-codec=<four_letter>, ' where 4 letter is the code for your codec of your source data.*/intMyimemgetcallback (void*data,Const Char*cookies, int64_t*DTS, int64_t*pts, unsigned*flags, size_t.*buffersize,void**buffer) {    Staticint64_t _dts =0, _pts =0; if(!g_isinit) {        /*Load Local file*/Local= fopen (Testfile,"RB"); if(!local) {            return true; } size_t Count= Fread (In_buffer,1, img_width*img_height*4, local); *buffersize =count; *buffer =In_buffer; G_isinit=true; *dts = _dts; *pts =_pts; _dts+= -; _pts+= -; return 0 ; } size_t Count= Fread (In_buffer,1, img_width*img_height*4, local); *buffersize =count; *buffer =In_buffer; *dts = _dts; *pts =_pts; _dts+= -; _pts+= -; if(count>0) {printf ("Read%d bytes\n", Count); return 0; }Else{        return true;/*EOF*/    }}/** \brief Callback method triggered by VLC to release memory allocated during the GET Callback. To set this callback, use the "--imem-release=<memory_address>" option, with memory_address the address of this    function in memory. \param[in] Data Pointer to user-defined data, which is your data and set by passing the "--imem-data=<memory_add    ress> "option when initializing VLC instance. \param[in] Cookie A User defined string. This works the same-as data, but for string. You set it by adding the "--imem-cookie=<your_string>" option when you initialize VLC.    Use the When multiple VLC instances is running.    \param[int] buffersize the size of the buffer in bytes. \param[out] Buffer Pointer to data, allocated or set during the GET callback to handle or delete as needed.*/intMyimemreleasecallback (void*data,Const Char*cookies, size_t buffersize,void*buffer) {    //Since I did not allocate any new memory, I don ' t need//To delete it here. However, if you do in your get method, you//should delete/free it here.    return 0;}//////////////////////////////////////////////////////////////////////////intMainintargcChar*argv[]) {libvlc_instance_t*Inst; libvlc_media_player_t*MP; libvlc_media_t*m;       libvlc_time_t length; intWait_time= the; Std::vector<Const Char*>options; Std::vector<Const Char*>:: Iterator option; Options.push_back ("--no-video-title-show"); Charimemdataarg[ the]; sprintf (Imemdataarg,"--imem-data=% #p", In_buffer);    Options.push_back (IMEMDATAARG); Charimemgetarg[ the]; sprintf (Imemgetarg,"--imem-get=% #p", Myimemgetcallback);    Options.push_back (IMEMGETARG); Charimemreleasearg[ the]; sprintf (Imemreleasearg,"--imem-release=% #p", Myimemreleasecallback);    Options.push_back (IMEMRELEASEARG); Options.push_back ("--imem-cookie=\ "Imem\""); Options.push_back ("--imem-codec=h264"); //Video data.Options.push_back ("--imem-cat=2"); /*Load the VLC engine*/Inst= Libvlc_new (int(Options.size ()), Options.data ()); //Configure any transcoding or streaming//options for the media source.options.clear (); //Create A media item from filem = libvlc_media_new_location (Inst,"imem://");/*# #use Memory as input*/    //Set Media Options     for(option = Options.begin (); Option! = Options.end (); option++) {libvlc_media_add_option (M,*option); }    /*Create A Media player playing environment*/MP=Libvlc_media_player_new_from_media (m); /*No need to keep the media now*/libvlc_media_release (m); //Play the Media_playerLibvlc_media_player_play (MP); //wait until the tracks is created_sleep (wait_time); Length=Libvlc_media_player_get_length (MP); Img_width=Libvlc_video_get_width (MP); Img_height=Libvlc_video_get_height (MP); printf ("Stream Duration:%ds\n", length/ +); printf ("Resolution:%d x%d\n", Img_width,img_height); //Let it play_sleep (length-wait_time); //Stop playingLibvlc_media_player_stop (MP); //Free the media_playerLibvlc_media_player_release (MP);      Libvlc_release (inst); return 0; } 

The above code should pay attention to: with the options to tell VLC, to use imem as input, but also to tell VLC which callback functions, this and directly set the callback method is not the same, it is in the options of the string representation.

Play the video stream in the specified buffer with LIBVLC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.