LibVLC Visual C

From VideoLAN Wiki
Jump to: navigation, search

Historical.png

This page is obsolete and kept only for historical interest. It may document features that are obsolete, superseded, or irrelevant. Do not rely on the information here being up-to-date.

The following is the simplest way to use libvlc with visual C++. What you need is: libvlc.dll and the whole folder plugins; Copy the dll file and plugins folder into your Debug or Release directory.

***DEPRECATED API*** USE NEW libvlc_ prefixed functions

1. Create VC console project

2. Implement some functions to load libvlc.dll into your application:

Take a look at [1] to get more information about functions in libvlc.dll

3. Create a command that is used to pass to libvlc.dll This command is used to stream a webcam to another machine:

  char ** commands = new char*[6];
  commands[0] = "testvlcconsole.exe"; //your application name
  commands[1] = "dshow://";
  commands[2] = ":dshow-vdev=Logitech QuickCam Pro 3000 (08B0)";
  commands[3] = ":dshow-adev=";
  commands[4] = ":dshow-size=176x144";
  commands[5] = ":sout=#transcode{vcodec=h264,vb=1024,scale=1}:duplicate{dst=rtp{mux=ts,dst=192.168.0.155,port=1234}}";

4. Main program:

- Initialize Library : load libvlc.dll and its functions
- Create VLC instance: id  = VLC_Create()
- Initialize VLC : VLC_Init(id, int, commands)
- Start playing: VLC_Play()

(Thanks neztol for the code)

5. Source code:

vlc.h

#ifndef _INTERFAZ_LIB_VLC_21_NOV_2006
#define _INTERFAZ_LIB_VLC_21_NOV_2006
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
using namespace std;
// Definición de las constantes de VLC
#define VLC_SUCCESS         0         // Sin error
#define VLC_ENOMEM         -1         // Memoria insuficiente
#define VLC_ETHREAD         -2         // Error de Thread
#define VLC_ETIMEOUT      -3         // Fuera de tiempo
#define VLC_ENOMOD         -10         // Módulo no localizado
#define VLC_ENOOBJ         -20         // Objeto no encontrado
#define VLC_EBADOBJ         -21         // Tipo de objeto inadecuado
#define VLC_ENOVAR         -30         // Variable no encontrada
#define VLC_EBADVAR         -31         // Valor de variable erróneo
#define VLC_EEXIT         -255      // El programa finalizó
#define VLC_EEXITSUCCESS   -999      // Se salió del programa con éxito
#define VLC_EGENERIC      -666      // Error genérico
#define VLC_FALSE         0   
#define VLC_TRUE         1
#define PLAYLIST_INSERT      1
#define PLAYLIST_APPEND      2
#define PLAYLIST_GO         4
#define PLAYLIST_PREPARSE   8
#define PLAYLIST_END      -666
#define EXITO            0
#define NO_LIB            -1
#define NO_ENCONTRADO      -2
#define FUNCION_FALLA      -3
// Definición de tipos propios de VLC
typedef int vlc_bool_t;
typedef struct vlc_list_t vlc_list_t;
typedef struct vlc_object_t vlc_object_t;
typedef signed __int64 vlc_int64_t;
typedef union{
   int             i_int;
   vlc_bool_t      b_bool;
   float           f_float;
   char *          psz_string;
   void *          p_address;
   vlc_object_t *   p_object;
   vlc_list_t *    p_list;
   vlc_int64_t     i_time;
   
   struct {
      char *psz_name;
      int i_object_id;
   } var;
   /* Make sure the structure is at least 64bits */
   struct { char a, b, c, d, e, f, g, h; } padding;
} vlc_value_t;
typedef const char * (*_VLC_VERSION)(void);
typedef const char * (*_VLC_ERROR)(int);
typedef int (*_VLC_CREATE)(void);
typedef int (*_VLC_INIT)(int, int, char **);
typedef int (*_VLC_ADDINTF)(int, const char *, vlc_bool_t, vlc_bool_t);
typedef int (*_VLC_DIE)(int);
typedef int (*_VLC_CLEANUP)(int);
typedef int (*_VLC_DESTROY)(int);
typedef int (*_VLC_VARIABLESET)(int, char const *, vlc_value_t);
typedef int (*_VLC_VARIABLEGET)(int, char const *, vlc_value_t *);
typedef int (*_VLC_VARIABLETYPE)(int, char const *, int *);
typedef int (*_VLC_ADDTARGET)(int, char const *, char const **, int, int, int);
typedef int (*_VLC_PLAY)(int);
typedef int (*_VLC_PAUSE)(int);
typedef int (*_VLC_STOP)(int);
typedef vlc_bool_t (*_VLC_ISPLAYING)(int);
typedef float (*_VLC_POSITIONGET)(int);
typedef float (*_VLC_POSITIONSET)(int, float);
typedef int (*_VLC_TIMEGET)(int);
typedef int (*_VLC_TIMESET)(int, int, vlc_bool_t);
typedef int (*_VLC_LENGTHGET)(int);
typedef float (*_VLC_SPEEDFASTER)(int);
typedef float (*_VLC_SPEEDSLOWER)(int);
typedef int (*_VLC_PLAYLISTINDEX)(int);
typedef int (*_VLC_PLAYLISTNUMBEROFITEMS)(int);
typedef int (*_VLC_PLAYLISTNEXT)(int);
typedef int (*_VLC_PLAYLISTPREV)(int);
typedef int (*_VLC_PLAYLISTCLEAR)(int);
typedef int (*_VLC_VOLUMESET)(int, int);
typedef int (*_VLC_VOLUMEGET)(int);
typedef int (*_VLC_VOLUMEMUTE)(int);
typedef int (*_VLC_FULLSCREEN)(int);

// Definición de variables globales para acceder a las funciones
_VLC_VERSION VLC_Version;
_VLC_ERROR VLC_Error;
_VLC_CREATE VLC_Create;
_VLC_INIT VLC_Init;
_VLC_ADDINTF VLC_AddIntf;
_VLC_DIE VLC_Die;
_VLC_CLEANUP VLC_CleanUp;
_VLC_DESTROY VLC_Destroy;
_VLC_VARIABLESET VLC_VariableSet;
_VLC_VARIABLEGET VLC_VariableGet;
_VLC_VARIABLETYPE VLC_VariableType;
_VLC_ADDTARGET VLC_AddTarget;
_VLC_PLAY VLC_Play;
_VLC_PAUSE VLC_Pause;
_VLC_STOP VLC_Stop;
_VLC_ISPLAYING VLC_IsPlaying;
_VLC_POSITIONGET VLC_PositionGet;
_VLC_POSITIONSET VLC_PositionSet;
_VLC_TIMEGET VLC_TimeGet;
_VLC_TIMESET VLC_TimeSet;
_VLC_LENGTHGET VLC_LengthGet;
_VLC_SPEEDFASTER VLC_SpeedFaster;
_VLC_SPEEDSLOWER VLC_SpeedSlower;
_VLC_PLAYLISTINDEX VLC_PlaylistIndex;
_VLC_PLAYLISTNUMBEROFITEMS VLC_PlaylistNumberOfItems;
_VLC_PLAYLISTNEXT VLC_PlaylistNext;
_VLC_PLAYLISTPREV VLC_PlaylistPrev;
_VLC_PLAYLISTCLEAR VLC_PlaylistClear;
_VLC_VOLUMESET VLC_VolumeSet;
_VLC_VOLUMEGET VLC_VolumeGet;
_VLC_VOLUMEMUTE VLC_VolumeMute;
_VLC_FULLSCREEN VLC_FullScreen;

HINSTANCE libvlc;

#endif

testvlcconsole.cpp

#include "stdafx.h"
#include <string.h>
#include "dshow.h"
#include <stdio.h>
#include "malloc.h"
#include "vlc.h"

/**********************************************************************
 * loadLibrary: Carga la librería VLC
 **********************************************************************/
int loadLibrary(LPWSTR libreria) {   
   LPCWSTR lib = L"\\libvlc.dll";
   DWORD tamano = GetCurrentDirectoryW(0, NULL);
   int liblen = lstrlenW(lib);

   printf("Tamano: %d, %d\n", tamano, liblen);   
   LPWSTR buffer = new WCHAR[tamano + liblen];
   tamano = GetCurrentDirectoryW(tamano, buffer);
   //buffer[tamano] = 0;   
   lstrcatW(buffer, lib);
   printf("Buffer: %S\n", buffer);   
   
   if (libvlc == NULL) {
      libvlc = LoadLibraryW(libreria);   
      delete buffer;
   }
   if (libvlc != NULL) {
      return EXITO;
      delete buffer;
   } else {
      printf("Libreria libvlc.dll no encontrada, %d\n", GetLastError());
      return NO_LIB;
      delete buffer;
   }
}

/**********************************************************************
 * freeLibrary: Libera la librería VLC
 **********************************************************************/
bool freeLibrary() {
   BOOL libera = false;
   if (libvlc != NULL) {
      printf("Liberando libreria libvlc.dll\n");
      libera = FreeLibrary(libvlc);
      if (!libera) {
         printf("Error al liberar\n");
      }
   }
   return (libera == VLC_TRUE);
}

/**********************************************************************
 * funcionFalla: Muestra un mensaje si una función no puede ser
 * obtenida desde la DLL
 **********************************************************************/
inline int funcionFalla(const char * msg) {
   printf("La funcion %s no pudo inicializarse\n", msg);
   freeLibrary();
   return FUNCION_FALLA;
}

/**********************************************************************
 * initLib: Carga todas las funciones desde la librería
 **********************************************************************/
int initLib(LPWSTR libreria) {

   if (libvlc == NULL) {
      if (loadLibrary(libreria) != EXITO) {
         return NO_LIB;
      }         
   }

   if ((VLC_AddIntf = (_VLC_ADDINTF) GetProcAddress(libvlc, "VLC_AddIntf")) == NULL)
      return funcionFalla("VLC_AddIntf");
   if ((VLC_AddTarget = (_VLC_ADDTARGET) GetProcAddress(libvlc, "VLC_AddTarget")) == NULL)
      return funcionFalla("VLC_AddTarget");
   if ((VLC_CleanUp = (_VLC_CLEANUP) GetProcAddress(libvlc, "VLC_CleanUp")) == NULL)
      return funcionFalla("VLC_CleanUp");
   if ((VLC_Create = (_VLC_CREATE) GetProcAddress(libvlc, "VLC_Create")) == NULL)
      return funcionFalla("VLC_Create");
   if ((VLC_Destroy = (_VLC_DESTROY) GetProcAddress(libvlc, "VLC_Destroy")) == NULL)
      return funcionFalla("VLC_Destroy");
   if ((VLC_Die = (_VLC_DIE) GetProcAddress(libvlc, "VLC_Die")) == NULL)
      return funcionFalla("VLC_Die");
   if ((VLC_Error = (_VLC_ERROR) GetProcAddress(libvlc, "VLC_Error")) == NULL)
      return funcionFalla("VLC_Error");
   if ((VLC_FullScreen = (_VLC_FULLSCREEN) GetProcAddress(libvlc, "VLC_FullScreen")) == NULL)
      return funcionFalla("VLC_FullScreen");
   if ((VLC_Init = (_VLC_INIT) GetProcAddress(libvlc, "VLC_Init")) == NULL)
      return funcionFalla("VLC_Init");
   if ((VLC_IsPlaying = (_VLC_ISPLAYING) GetProcAddress(libvlc, "VLC_IsPlaying")) == NULL)
      return funcionFalla("VLC_IsPlaying");
   if ((VLC_LengthGet = (_VLC_LENGTHGET) GetProcAddress(libvlc, "VLC_LengthGet")) == NULL)
      return funcionFalla("VLC_LengthGet");
   if ((VLC_Pause = (_VLC_PAUSE) GetProcAddress(libvlc, "VLC_Pause")) == NULL)
      return funcionFalla("VLC_Pause");
   if ((VLC_Play = (_VLC_PLAY) GetProcAddress(libvlc, "VLC_Play")) == NULL)
      return funcionFalla("VLC_Play");
   if ((VLC_PlaylistClear = (_VLC_PLAYLISTCLEAR) GetProcAddress(libvlc, "VLC_PlaylistClear")) == NULL)
      return funcionFalla("VLC_PlaylistClear");
   if ((VLC_PlaylistIndex = (_VLC_PLAYLISTINDEX) GetProcAddress(libvlc, "VLC_PlaylistIndex")) == NULL)
      return funcionFalla("VLC_PlaylistIndex");
   if ((VLC_PlaylistNext = (_VLC_PLAYLISTNEXT) GetProcAddress(libvlc, "VLC_PlaylistNext")) == NULL)
      return funcionFalla("VLC_PlaylistNext");
   if ((VLC_PlaylistNumberOfItems = (_VLC_PLAYLISTNUMBEROFITEMS) GetProcAddress(libvlc, "VLC_PlaylistNumberOfItems")) == NULL)
      return funcionFalla("VLC_PlaylistNumberOfItems");
   if ((VLC_PlaylistPrev = (_VLC_PLAYLISTPREV) GetProcAddress(libvlc, "VLC_PlaylistPrev")) == NULL)
      return funcionFalla("VLC_PlaylistPrev");
   if ((VLC_PositionGet = (_VLC_POSITIONGET) GetProcAddress(libvlc, "VLC_PositionGet")) == NULL)
      return funcionFalla("VLC_PositionGet");
   if ((VLC_PositionSet = (_VLC_POSITIONSET) GetProcAddress(libvlc, "VLC_PositionSet")) == NULL)
      return funcionFalla("VLC_PositionSet");
   if ((VLC_SpeedFaster = (_VLC_SPEEDFASTER) GetProcAddress(libvlc, "VLC_SpeedFaster")) == NULL)
      return funcionFalla("VLC_SpeedFaster");
   if ((VLC_SpeedSlower = (_VLC_SPEEDSLOWER) GetProcAddress(libvlc, "VLC_SpeedSlower")) == NULL)
      return funcionFalla("VLC_SpeedSlower");
   if ((VLC_Stop = (_VLC_STOP) GetProcAddress(libvlc, "VLC_Stop")) == NULL)
      return funcionFalla("VLC_Stop");
   if ((VLC_TimeGet = (_VLC_TIMEGET) GetProcAddress(libvlc, "VLC_TimeGet")) == NULL)
      return funcionFalla("VLC_TimeGet");
   if ((VLC_TimeSet = (_VLC_TIMESET) GetProcAddress(libvlc, "VLC_TimeSet")) == NULL)
      return funcionFalla("VLC_TimeSet");
   if ((VLC_VariableGet = (_VLC_VARIABLEGET) GetProcAddress(libvlc, "VLC_VariableGet")) == NULL)
      return funcionFalla("VLC_VariableGet");
   if ((VLC_VariableSet = (_VLC_VARIABLESET) GetProcAddress(libvlc, "VLC_VariableSet")) == NULL)
      return funcionFalla("VLC_VariableSet");
   if ((VLC_VariableType = (_VLC_VARIABLETYPE) GetProcAddress(libvlc, "VLC_VariableType")) == NULL)
      return funcionFalla("VLC_VariableType");
   if ((VLC_Version = (_VLC_VERSION) GetProcAddress(libvlc, "VLC_Version")) == NULL)
      return funcionFalla("VLC_Version");
   if ((VLC_VolumeGet = (_VLC_VOLUMEGET) GetProcAddress(libvlc, "VLC_VolumeGet")) == NULL)
      return funcionFalla("VLC_VolumeGet");
   if ((VLC_VolumeMute = (_VLC_VOLUMEMUTE) GetProcAddress(libvlc, "VLC_VolumeMute")) == NULL)
      return funcionFalla("VLC_VolumeMute");
   if ((VLC_VolumeSet = (_VLC_VOLUMESET) GetProcAddress(libvlc, "VLC_VolumeSet")) == NULL)
      return funcionFalla("VLC_VolumeSet");
   return EXITO;
}

/**********************************************************************
 * verError: Muestra un mensaje de error
 **********************************************************************/
void getError(int error) {
   fprintf(stderr, "Error: %s\n", (VLC_Error)(error));
} 


// Main
int main(int argc, char * argv[]) {
   if (initLib(L"libvlc.dll") != EXITO) {
      printf("No se pudo cargar la libreria\n");
      return -1;
   }
   
   int id = -1;

   // It creates the VLC instance
    int i_ret = (VLC_Create)();
    if( i_ret < 0 ) {
      getError(i_ret);
        return i_ret;
    }   

   id = i_ret;
   
   // Inits the instance
   printf("%d\n", id);

   char ** commands = new char*[6];
   commands[0] = "testvlcconsole.exe";
   commands[1] = "dshow://";
   commands[2] = ":dshow-vdev=Logitech QuickCam Pro 3000 (08B0)";
   commands[3] = ":dshow-adev=";
   commands[4] = ":dshow-size=176x144";
   commands[5] = ":sout=#transcode{vcodec=h264,vb=1024,scale=1}:duplicate{dst=rtp{mux=ts,dst=192.168.0.155,port=1234}}";


    i_ret = (VLC_Init)(id, 6, commands );
   printf("%d\n", i_ret);
    if( i_ret < 0 ) {
      getError(i_ret);
        (VLC_Destroy)( id );
        return -1;
    }   

    // Start playing
    i_ret = (VLC_Play)(id);
   if( i_ret < 0 ) {
      getError(i_ret);
      return false;
   }
   
   getchar();

   while((VLC_IsPlaying)(id));
   (VLC_Die)(id);
   (VLC_CleanUp)(id);
   (VLC_Destroy)(id);
   freeLibrary();
   return 0;   
}
Personal tools
Namespaces

Variants
Actions
Help / Documentation
Development
VideoLAN wiki
Toolbox