using libvlc to stream with rtp protocol from memory.
Posted: 06 Mar 2019 17:38
Hello,
I've been trying to use libvlc to create an rtp stream and send simple images loaded into memory. For this I'm creating simple OpenCV Matrices and passing them through the callback api.
Here is what my code looks like :
Simple Mem class that contains the buffer to stream
Callback open, read, seek and close implementation:
And this is how I instantiate the whole thing on my main.cpp
The problem I've encountered :
While trying to read the stream with the vlc application I get nothing, small blue load bar and both timers to 00:00
I checked my localhost ports with netstat and I see my port as ESTABLISHED but while checking with Wireshark I see nothing : No data is being sent through the stream.
The libvlc program log :
I had to stick with verbose to level 1
Other things I've tried :
Using the --imem-data directly : this gave me a deadlock error and seemed to produced a similar result.
Using the vlm API : I surprisingly managed to stream from a file with it, but not from memory, and I wouldn't know how to link it with the media callback API.
Different ports and launching it as root (I was desperate) .
Any help would be greatly appreciated !
I've been trying to use libvlc to create an rtp stream and send simple images loaded into memory. For this I'm creating simple OpenCV Matrices and passing them through the callback api.
Here is what my code looks like :
Simple Mem class that contains the buffer to stream
Code: Select all
class MemVideoData
{
public:
MemVideoData(unsigned char *data, int data_bytes) : video(data), bytes(data_bytes)
{
} //init
~MemVideoData() {}
unsigned char* video; // pointer to video in memory
int bytes;
};
Code: Select all
ssize_t media_read_cb(void *opaque, unsigned char* buf, size_t len)
{
MemVideoData *mVid = (MemVideoData*) opaque; //cast and give context
unsigned char *start = mVid->video;
std::memcpy(buf,start, mVid->bytes); //copy bytes requested to buffer.
return mVid->bytes;
}
int media_open_cb(void* opaque, void** datap, uint64_t* sizep)
{
//cast opaque to our video state struct
MemVideoData *mVid = static_cast<MemVideoData*> (opaque);
//TODO: get mutex on MemVideoData object pointed to by opaque
*sizep = (uint64_t) mVid->bytes; //set stream length
*datap = mVid; /*point to entire object. Think this was intended to
point to the raw video data but we use the MemVideoData object in read()
and seek()*/
return 0;
}
int media_seek_cb(void *opaque, uint64_t offset)
{
return 0;
}
void media_close_cb(void *opaque)
{
}
Code: Select all
// create a simple red image
::cv::Mat image(720,1080,CV_8UC3, ::cv::Scalar(255,0,0) );
int imgSize = image.rows * image.cols * image.channels();
MemVideoData* mem = new MemVideoData(image.data, imgSize);
libvlc_instance_t *vlc;
// add Verbose option to instance
std::vector<const char*> options;
options.push_back("--verbose=1 ");
vlc = libvlc_new (int(options.size()), options.data());
libvlc_media_t* media = libvlc_media_new_callbacks(vlc,media_open_cb, media_read_cb, media_seek_cb, media_close_cb, mem);
// Add the streaming string given by the vlc app
libvlc_media_add_option(media, ":sout=#transcode{vcodec=h264,venc=x264,vb=0,vbv-bufsize=1200,bframes=0,scale=0,acodec=none}:rtp{dst=localhost, port=9555, name=test, mux=ts,sap}");
// Create a media player playing environment
libvlc_media_player_t *mediaPlayer = libvlc_media_player_new_from_media (media);
// play the media_player
libvlc_media_player_play (mediaPlayer);
// Arbitrary value, should be replace with user interaction
boost::this_thread::sleep(boost::posix_time::milliseconds(60000000));
// Stop playing
libvlc_media_player_stop (mediaPlayer);
// Free the media_player
libvlc_media_player_release (mediaPlayer);
// Free vlc
libvlc_release (vlc);
While trying to read the stream with the vlc application I get nothing, small blue load bar and both timers to 00:00
I checked my localhost ports with netstat and I see my port as ESTABLISHED but while checking with Wireshark I see nothing : No data is being sent through the stream.
The libvlc program log :
I had to stick with verbose to level 1
Code: Select all
[00007ff470001760] stream_out_rtp stream out warning: unknown protocol for SDP ((null))
[00007ff470007ed0] main stream out warning: option vbv-bufsize is unknown
[00007ff470007ed0] main stream out warning: option bframes is unknown
[00007ff47000a230] imem demux error: Invalid get/release function pointers
[00007ff47000a230] imem demux error: Invalid get/release function pointers
[00007ff47000a230] ps demux warning: this does not look like an MPEG PS stream, continuing anyway
[00007ff47000a230] ps demux warning: garbage at input from 509, trying to resync...
Using the --imem-data directly : this gave me a deadlock error and seemed to produced a similar result.
Using the vlm API : I surprisingly managed to stream from a file with it, but not from memory, and I wouldn't know how to link it with the media callback API.
Different ports and launching it as root (I was desperate) .
Any help would be greatly appreciated !