Play a memory buffer with libvlc - c++

I want to play a buffer in libvlc which is filled by a signal/slot from another thread. I've used read_callbacks of libvlc but it can be compiled with mingw and compiling it for msvc is a hard process (see here).(I want to compile my program in msvc2012). so I want to use imem option and I wrote the following code but it crashed and do not show anything.
#include <QMainWindow>
#include <QFile>
#include <vlc/vlc.h>
#include <vlc/libvlc_media_player.h>
#include <vlc/libvlc_media.h>
using namespace std ;
const int len = (820 * 600 * 4) + 31;
int imemGetCallback (void *data, const char *cookie,int64_t *dts, int64_t *pts,unsigned *flags, size_t * bufferSize, void ** buffer);
int imemReleaseCallback (void *data, const char *cookie, size_t bufferSize, void * buffer);
int main(){
char smem_options1[2000];
char venc_options[1000];
sprintf(venc_options,"bframes=6,ref=6");
char str_imem_get[100], str_imem_release[100],str_imem_data[100];
sprintf(str_imem_get, "--imem-get=%ld", imemGetCallback);
sprintf(str_imem_release, "--imem-release=%ld", imemReleaseCallback);
const char * const vlc_args[] = {
"-I","dummy",
"--ignore-config",
"--demux","rawvideo",
"--rawvid-fps","30",
"--rawvid-width","820",
"--rawvid-height","600",
"--rawvid-chroma","RV24",
"--imem-channels=1",
"--imem-data=0",
"--imem-cat=4",
"--imem-fps=30",
"--imem-codec=none",
str_imem_get,
str_imem_release
};
libvlc_instance_t * inst_broadcast =libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);;// libvlc_new(vlc_argc, vlc_argv);
libvlc_media_t * m = libvlc_media_new_location(inst_broadcast, "imem://");
auto _vlcmp = libvlc_media_player_new_from_media(m);
libvlc_media_player_play(_vlcmp);
}
int imemGetCallback (void *data, const char *cookie,int64_t *dts, int64_t *pts,unsigned *flags, size_t * bufferSize, void ** buffer)
{
int64_t uS = 33333; // 30 fps
static int index=0;
*bufferSize = len;
*buffer = buf;
*dts = *pts = 33333;
return 1;
}
int imemReleaseCallback (void *data, const char *cookie, size_t bufferSize, void * buffer)
{
return 0;
}
how can I use imem option to do the job?

Related

ERROR : ‘fuse_operations_compat2’ has no non-static data member named ‘readdir’

I am trying to build a simple filesystem using fuse 2.9.7 to store an avifile in it. But i am having trouble in finding the solution for these error.
‘fuse_operations_compat2’ has no non-static data member named ‘readdir’
I have this main.cpp
#include<iostream>
#include<fuse.h>
#include "include/AVIContainer.h"
#include "include/Fuse.h"
using namespace std;
int main(int argc, char* argv[])
{
AVIContainer *avi = new AVIContainer(320, 240, 30, 90);
avi->WriteToFile("test.avi");
struct fuse_operations oper = {
.getattr = getattr_callback,
.readdir = readdir_callback,
.open = open_callback,
.read = read_callback,
};
return fuse_main(argc, argv , &oper);
}
and these are the header file(.cpp)
#define FUSE_USE_VERSION 30
#include<fuse.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include "Fuse.h"
char filename[30] = "/avifile";
char filename2[30] = "avifile";
int getattr_callback(const char *path, struct stat *st)
{
st->st_uid = getuid();
st->st_gid = getgid();
st->st_atime = time(NULL);
st->st_mtime = time(NULL);
if(strcmp(path, "/") == 0)
{
st->st_mode = S_IFDIR | 0755;
st->st_nlink = 2;
}
if(strcmp(path,filename) == 0)
{
st->st_mode = S_IFREG | 0644;
st->st_nlink = 1;
st->st_size = datasize;
}
return 0;
}
int readdir_callback(const char *path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
filler(buffer, ".", NULL, 0);
filler(buffer, "..", NULL, 0);
if(strcmp(path, "/") == 0)
{
filler(buffer, filename2, NULL, 0);
}
return 0;
}
int read_callback(const char *path, char *buffer, size_t size, off_t offset, struct fuse_file_info *fi)
{
unsigned int SizetoRead = size;
if( (offset + size) > datasize)
{
SizetoRead = datasize - offset;
}
memcpy(buffer, databuffer + offset, SizetoRead);
return SizetoRead;
}
int open_callback(const char *path, fuse_file_info *fi)
{
return 0;
}
and this is .h file
#ifndef FUSE
#define FUSE
#include <fuse.h>
uint8_t* get_data();
unsigned int get_size();
int getattr_callback(const char *path, struct stat *st);
int read_callback(const char *path, char *buffer, size_t size, off_t offset, struct fuse_file_info *fi);
int readdir_callback(const char *path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi);
int open_callback(const char *path, fuse_file_info *fi);
#endif
I think the problem is with version of fuse , though not sure.
please help, thanks in advance
Edit:
You are using the high level fuse ops (vs the low level ones).
here is what you can do with it (fuse_compat.h)
struct fuse_operations_compat2 {
int (*getattr) (const char *, struct stat *);
int (*readlink) (const char *, char *, size_t);
int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t_compat);
int (*mknod) (const char *, mode_t, dev_t);
int (*mkdir) (const char *, mode_t);
int (*unlink) (const char *);
int (*rmdir) (const char *);
int (*symlink) (const char *, const char *);
int (*rename) (const char *, const char *);
int (*link) (const char *, const char *);
int (*chmod) (const char *, mode_t);
int (*chown) (const char *, uid_t, gid_t);
int (*truncate) (const char *, off_t);
int (*utime) (const char *, struct utimbuf *);
int (*open) (const char *, int);
int (*read) (const char *, char *, size_t, off_t);
int (*write) (const char *, const char *, size_t, off_t);
int (*statfs) (const char *, struct statfs *);
int (*flush) (const char *);
int (*release) (const char *, int);
int (*fsync) (const char *, int);
int (*setxattr) (const char *, const char *, const char *, size_t, int);
int (*getxattr) (const char *, const char *, char *, size_t);
int (*listxattr) (const char *, char *, size_t);
int (*removexattr) (const char *, const char *);
};
try having the version as 32
In your main.cpp, the fuse.h Header is included before the version is set, so when your main file is compiled (where the readdir is supposed to be set), FUSE is loaded in compatibility mode. Removing the #include<fuse.h> from your main file should solve the problem as FUSE is then included via your header file, which sets the correct version.

LibVLC empty image

I'm trying to use LibVLC to decode a video file and render it to a texture.
The code for opening and start playing works, the audio plays perfectly, but the pixel buffer is always filled with 0xCD. The videos I try to render work on VLC, and even in a C# implementation I did they work, but with this new code in C I can't manage to get it working...
I'm using the x64 version of the vlc libs and the program is compiled for x64 if this makes any difference.
Here is my code:
#include "stdafx.h"
#include "video.h"
#include <string.h>
#include <vlc\libvlc.h>
#include <vlc\libvlc_media.h>
#include <vlc\libvlc_media_player.h>
libvlc_instance_t* instance;
libvlc_media_t* media;
libvlc_media_player_t* player;
struct videoContext
{
unsigned char *pixeldata;
unsigned char currentFrame;
int width;
int height;
};
struct videoContext mainContext;
bool gotData = false;
int width;
int height;
static void *lock(void *data, void **p_pixels)
{
videoContext* context = (videoContext*)data;
*p_pixels = context->pixeldata;
return NULL;
}
static void unlock(void *data, void *id, void *const *p_pixels)
{
mainContext.currentFrame++;
//If I check here mainContext.pixeldata is filled with 0xCD
}
static void display(void *data, void *id)
{
}
static unsigned int formatSetup(void **opaque, char *chroma, unsigned *w, unsigned *h, unsigned *pitches, unsigned *lines)
{
chroma = "RV24";
width = *w;
height = *h;
mainContext.pixeldata = (unsigned char*)malloc(width * height * 3);
mainContext.width = width;
mainContext.height = height;
return 1;
}
void getVideoSize(int* w, int* h, int* bpp)
{
*w = width;
*h = height;
*bpp = 3 * 8;
}
videoContext* initVideo(const char* fileName)
{
mainContext.pixeldata = 0;
instance = libvlc_new(0, NULL);
media = libvlc_media_new_location(instance, fileName);
player = libvlc_media_player_new_from_media(media);
libvlc_video_set_callbacks(player, lock, unlock, display, &mainContext);
libvlc_video_set_format_callbacks(player, formatSetup, NULL);
libvlc_media_player_play(player);
return &mainContext;
}
UPDATE:
It seems that nothing is being written to the buffer as anything I set is left there.
UPDATE2:
If I remove the format setup callback and I hardcode libvlc_video_set_format with the "RV24" chroma mode and the resolution it works, so, did I understood wrongly what libvlc_video_set_format_callbacks does?
According to the documentation on the format setup callback the params can be changed to enable transcoding but even if I just leave the format as is whenever I set the format setup callback it doesn't works...
Ok, so, the problem was extrmely stupid. First, the pitches and lines must be set so libvlc to know the allocated sizes. Second, can't just assign chroma with a const char, it seems as the const char has an extra "\0x00" at the end liblvc counts it and then doesn't finds the decoder.
The final callback is this:
static unsigned int formatSetup(void **opaque, char *chroma, unsigned *w, unsigned *h, unsigned *pitches, unsigned *lines)
{
memcpy(chroma, "RV24", sizeof("RV24") - 1);
width = *w;
height = *h;
*pitches = width * 3;
*lines = height;
mainContext.pixeldata = (unsigned char*)malloc(width * height * 4);
mainContext.width = width;
mainContext.height = height;
return 1;
}

multiple LD_REPLOAD sharing variables

My purpose is quite simple: when I start the mongoose server, the server will initialize a variable which is defined by me. To do this, I hooked __libc_start_main. Then when the server receives a request, it will print out that initialized variable. To do this, I hooked recv. Below is my code.
#include <string>
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include "common-structure.h"
# define dprintf(fmt...)
data datainfo; //defined in common-structure.h
typedef int (*main_type)(int, char**, char**);
struct arg_type
{
char **argv;
int (*main_func) (int, char **, char **);
};
main_type saved_init_func = NULL;
void tern_init_func(int argc, char **argv, char **env){
dprintf("%04d: __tern_init_func() called.\n", (int) pthread_self());
if(saved_init_func)
saved_init_func(argc, argv, env);
datainfo.age = 10;
}
typedef void (*fini_type)(void*);
fini_type saved_fini_func = NULL;
extern "C" int my_main(int argc, char **pt, char **aa)
{
int ret;
arg_type *args = (arg_type*)pt;
dprintf("%04d: __libc_start_main() called.\n", (int) pthread_self());
ret = args->main_func(argc, args->argv, aa);
return ret;
}
extern "C" int __libc_start_main(
void *func_ptr,
int argc,
char* argv[],
void (*init_func)(void),
void (*fini_func)(void),
void (*rtld_fini_func)(void),
void *stack_end)
{
typedef void (*fnptr_type)(void);
typedef int (*orig_func_type)(void *, int, char *[], fnptr_type,
fnptr_type, fnptr_type, void*);
orig_func_type orig_func;
arg_type args;
void * handle;
int ret;
// Get lib path.
Dl_info dli;
dladdr((void *)dlsym, &dli);
std::string libPath = dli.dli_fname;
libPath = dli.dli_fname;
size_t lastSlash = libPath.find_last_of("/");
libPath = libPath.substr(0, lastSlash);
libPath += "/libc.so.6";
libPath = "/lib/x86_64-linux-gnu/libc.so.6";
if(!(handle=dlopen(libPath.c_str(), RTLD_LAZY))) {
puts("dlopen error");
abort();
}
orig_func = (orig_func_type) dlsym(handle, "__libc_start_main");
if(dlerror()) {
puts("dlerror");
abort();
}
dlclose(handle);
dprintf("%04d: __libc_start_main is hooked.\n", (int) pthread_self());
args.argv = argv;
args.main_func = (main_type)func_ptr;
saved_init_func = (main_type)init_func;
saved_fini_func = (fini_type)rtld_fini_func;
ret = orig_func((void*)my_main, argc, (char**)(&args),
(fnptr_type)tern_init_func, (fnptr_type)fini_func,
rtld_fini_func, stack_end);
return ret;
}
//hook recv
extern "C" ssize_t recv(int sockfd, void *buf, size_t len, int flags)
{
ssize_t (*orig_recv)(int sockfd, void *buf, size_t len, int flags);
orig_recv = dlsym(RTLD_NEXT, "recv");
orig_recv(sockfd, buf, len, flags);
printf("age is %d\n", datainfo.age);
}
However, when I makefile, I get the error: invalid conversion from ‘void*’ to ‘ssize_t (*)(int, void*, size_t, int) coming from dlsym(RTLD_NEXT, "recv");. My another question is can I achieve my goal in this way? If not, what is the correct way?
C++ is much more strongly typed than C, you need to explicitly cast void * to the correct type.
For example:
extern "C" ssize_t recv(int sockfd, void *buf, size_t len, int flags)
{
using orig_recv_t = ssize_t (*)(int, void *, size_t, int);
// Or for pre C++11 compilers: typedef ssize_t (*orig_recv_t)(int, void *, size_t, int);
orig_recv_t orig_recv;
orig_recv = reinterpret_cast<orig_recv_t>(dlsym(RTLD_NEXT, "recv"));
orig_recv(sockfd, buf, len, flags);
printf("age is %d\n", datainfo.age);
}

Boost::mpi sending array

Good Morning
I'm implementing a distributed image normalization algorithm an I'm using Boost::mpi with a class Pixel that contain the serialization code,
#ifndef PIXEL_H
#define PIXEL_H
#include <boost/mpi.hpp>
#include <boost/serialization/access.hpp>
class Pixel
{
private:
unsigned char m_red;
unsigned char m_green;
unsigned char m_blue;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int version) {
ar & m_red;
ar & m_green;
ar & m_blue;
}
public:
Pixel();
Pixel(unsigned char red,unsigned char green,unsigned char blue) : m_red(red), m_green(green), m_blue(blue) {};
virtual ~Pixel();
unsigned char getRed();
void setRed(unsigned char val);
unsigned char getGreen();
void setGreen(unsigned char val);
unsigned char getBlue();
void setBlue(unsigned char val);
void setColor (unsigned char red,unsigned char green,unsigned char blue);
};
The main.cpp is
#include <iostream>
#include <boost/mpi.hpp>
#include <vector>
#include "include/Pixel.h"
#include <cstdlib>
#include <ctime>
#define ALTEZZA 2
#define LARGHEZZA 2
namespace mpi=boost::mpi;
int main(int argc, char * argv[]) {
std::cout<<"Inizializzazione dell'ambiente MPI"<<std::endl;
mpi::environment env;
mpi::communicator world;
Pixel **vettore;
int i,j;
//Inizializzazione della matrice di test
if(world.rank() == 0){
std::cout<<"Inizializzazione matrice di test..."<<std::endl;
std::srand(std::time(0));
vettore = new Pixel *[ALTEZZA];
for (i = 0; i < ALTEZZA; i++) {
vettore[i] = new Pixel[LARGHEZZA];
}
for (i = 0; i < ALTEZZA; i++) {
for (j = 0; j < LARGHEZZA; j++) {
vettore[i][j].setColor(std::rand() % 256, std::rand() % 256, std::rand() % 256);
std::cout<<"Vettore["<<i<<"]["<<j<<"] = ("<<int(vettore[i][j].getRed())<<","<<int(vettore[i][j].getGreen())<<","<<int(vettore[i][j].getBlue())<<");"<<std::endl;
}
}
}
if (world.rank() == 0) {
std::cout<<"Invio matrice.."<<std::endl;
world.send(1, 0, vettore[0]);
}else {
Pixel *px;
world.recv(0, 0, px);
for (j = 0; j < LARGHEZZA; j++) {
std::cout<<int(px[j].getRed())<<" "<<int(px[j].getGreen())<<" "<<int(px[j].getBlue())<<std::endl;
}
}
return 0;
}
but when i run the program the cout on the receiving process print wrong value like this
Inizializzazione dell'ambiente MPI
Inizializzazione dell'ambiente MPI
Inizializzazione matrice di test...
Vettore[0][0] = (170,103,165);
Vettore[0][1] = (84,0,186);
Vettore[1][0] = (93,228,162);
Vettore[1][1] = (31,100,204);
Invio matrice..
170 103 165
217 1 0
I think that the problem is the 2d array because if I use std::vector i haven't this problem but I don't understand why.
I would imagine you have several problems (I can't test as I don't have a capable MPI installation..)
Firstly, your send() is wrong, currently you are triggering the overload:
template<typename T> void send(int, int, const T &) const;
But you are trying to send a raw array, I imagine the fix here has to be to pass the count, for example:
world.send(1, 0, vettore[0], 2); // 2 Pixels
Secondly, on the receiver side (this I'm not sure about), but I imagine you need to have a suitable array to read the data into.., for example:
Pixel px[LARGHEZZA];
world.recv(0, 0, px, 2);
I think this should fix your problems...

mpg123_open() and Const

Im a newbie on c++ programing and im basicly trying to make a simple mp3 player using mpg123, the code works great when in the main() but when i try to call it in a sub and submitting the path+file to play i just cant get it to work.
gcc complains about const char... and char conversion..
"1.c:71:18: error: invalid conversion from 'char' to 'const char*' [-fpermissive]"
#include <ao/ao.h>
#include <mpg123.h>
#define BITS 8
#include <iostream>
using namespace std;
void playaudio(const char * trakl)
{
//works when called here including path
//const char *traklink="/home/pi/downloads/00000050.mp3";
const char * traklink=trakl;
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
size_t done;
int err;
int driver;
ao_device *dev;
ao_sample_format format;
int channels, encoding;
long rate;
/* if(argc < 2)
exit(0);
*/
/* initializations */
ao_initialize();
driver = ao_default_driver_id();
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
/* open the file and get the decoding format */
//mpg123_open(mh,traklink);
mpg123_open(mh,traklink);
mpg123_getformat(mh, &rate, &channels, &encoding);
/* set the output format and open the output device */
format.bits = mpg123_encsize(encoding) * BITS;
format.rate = rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
dev = ao_open_live(driver, &format, NULL);
/* decode and play */
while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
//ao_play(dev, buffer, done);
ao_play(dev, (char*)buffer, done);
/* clean up */
free(buffer);
ao_close(dev);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
// return 0;
}
int main()
{
const char * trakl="/home/pi/downloads/00000050.mp3";
playaudio(* trakl);
return 0;
}
You're calling playaudio like this:
playaudio(* trakl);
This is invalid. trak1 is of type const char *, this implies that *trak1 is of type const char. playaudio expects a const char *, you should call it like this instead:
playaudio(trakl);