how to play the video data from memory buffer using libvlc?
Posted: 27 Jan 2018 06:15
I have a buffer which store the video data readed from other place, and I need use libvlc to read and play it from the memory buffer,
I try to imem, but it doesn't work, I have no idea how to program with imem, or imem isn't used by this way?
please help me,thank you!
int MyImemGetCallback (void *data,
const char *cookie,
int64_t *dts,
int64_t *pts,
unsigned *flags,
size_t * bufferSize,
void ** buffer)
{
static int64_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+=30; _pts+=30;
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+=30; _pts+=30;
if(count>0) {
printf("read %d bytes\n",count);
return 0;
}else{
return true; /*eof*/
}
}
int MyImemReleaseCallback (void *data,
const char *cookie,
size_t bufferSize,
void * buffer)
{
// Since I did not allocate any new memory, I don't need
// to delete it here. However, if you did in your get method, you
// should delete/free it here.
return 0;
}
int main(int argc, char* argv[])
{
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;
libvlc_time_t length;
int wait_time=5000;
std::vector<const char*> options;
std::vector<const char*>::iterator option;
options.push_back("--no-video-title-show");
char imemDataArg[256];
sprintf(imemDataArg, "--imem-data=%#p", in_buffer);
options.push_back(imemDataArg);
char imemGetArg[256];
sprintf(imemGetArg, "--imem-get=%#p", MyImemGetCallback);
options.push_back(imemGetArg);
char imemReleaseArg[256];
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 file
m = 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_player
libvlc_media_player_play (mp);
//wait until the tracks are 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/1000);
printf("Resolution: %d x %d\n",IMG_WIDTH,IMG_HEIGHT);
//Let it play
_sleep (length-wait_time);
// Stop playing
libvlc_media_player_stop (mp);
// Free the media_player
libvlc_media_player_release (mp);
libvlc_release (inst);
return 0;
}
I try to imem, but it doesn't work, I have no idea how to program with imem, or imem isn't used by this way?
please help me,thank you!
Code: Select all
const char *cookie,
int64_t *dts,
int64_t *pts,
unsigned *flags,
size_t * bufferSize,
void ** buffer)
{
static int64_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+=30; _pts+=30;
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+=30; _pts+=30;
if(count>0) {
printf("read %d bytes\n",count);
return 0;
}else{
return true; /*eof*/
}
}
int MyImemReleaseCallback (void *data,
const char *cookie,
size_t bufferSize,
void * buffer)
{
// Since I did not allocate any new memory, I don't need
// to delete it here. However, if you did in your get method, you
// should delete/free it here.
return 0;
}
int main(int argc, char* argv[])
{
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;
libvlc_time_t length;
int wait_time=5000;
std::vector<const char*> options;
std::vector<const char*>::iterator option;
options.push_back("--no-video-title-show");
char imemDataArg[256];
sprintf(imemDataArg, "--imem-data=%#p", in_buffer);
options.push_back(imemDataArg);
char imemGetArg[256];
sprintf(imemGetArg, "--imem-get=%#p", MyImemGetCallback);
options.push_back(imemGetArg);
char imemReleaseArg[256];
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 file
m = 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_player
libvlc_media_player_play (mp);
//wait until the tracks are 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/1000);
printf("Resolution: %d x %d\n",IMG_WIDTH,IMG_HEIGHT);
//Let it play
_sleep (length-wait_time);
// Stop playing
libvlc_media_player_stop (mp);
// Free the media_player
libvlc_media_player_release (mp);
libvlc_release (inst);
return 0;
}