vlm.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:18k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * vlm.c: libvlc new API VLM handling functions
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 the VideoLAN team
  5.  * $Id: 6393cfbc26c7083fb5c1fd5f7b47dad9b4106b27 $
  6.  *
  7.  * Authors: Clément Stenac <zorglub@videolan.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. #include <vlc/libvlc.h>
  24. #include <vlc/libvlc_vlm.h>
  25. #include <vlc_es.h>
  26. #include <vlc_input.h>
  27. #include <vlc_vlm.h>
  28. #include "libvlc_internal.h"
  29. #if 0
  30. /* local function to be used in libvlc_vlm_show_media only */
  31. static char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
  32.     char* psz_childprefix;
  33.     char* psz_response="";
  34.     char* response_tmp;
  35.     int i;
  36.     vlm_message_t *aw_child, **paw_child;
  37.     asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
  38.     if ( p_answer->i_child )
  39.     {
  40.         paw_child = p_answer->child;
  41.         aw_child = *( paw_child );
  42.         for( i = 0; i < p_answer->i_child; i++ )
  43.         {
  44.             asprintf( &response_tmp, "%s%s%s:%sn",
  45.                       psz_response, psz_prefix, aw_child->psz_name,
  46.                       aw_child->psz_value );
  47.             free( psz_response );
  48.             psz_response = response_tmp;
  49.             if ( aw_child->i_child )
  50.             {
  51.                 asprintf(&response_tmp, "%s%s", psz_response,
  52.                          recurse_answer(psz_childprefix, aw_child));
  53.                 free( psz_response );
  54.                 psz_response = response_tmp;
  55.             }
  56.             paw_child++;
  57.             aw_child = *( paw_child );
  58.         }
  59.     }
  60.     free( psz_childprefix );
  61.     return psz_response;
  62. }
  63. char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
  64.                              libvlc_exception_t *p_exception )
  65. {
  66.     char *psz_message;
  67.     vlm_message_t *answer;
  68.     char *psz_response;
  69.     CHECK_VLM;
  70.     asprintf( &psz_message, "show %s", psz_name );
  71.     asprintf( &psz_response, "", psz_name );
  72.     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
  73.     if( answer->psz_value )
  74.     {
  75.         libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
  76.                                 psz_name, answer->psz_value );
  77.     }
  78.     else
  79.     {
  80.         if ( answer->child )
  81.         {
  82.             psz_response = recurse_answer( "", answer );
  83.         }
  84.     }
  85.     free( psz_message );
  86.     return(psz_response );
  87. }
  88. #else
  89. char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
  90.                              const char *psz_name,
  91.                              libvlc_exception_t *p_exception )
  92. {
  93.     (void)p_instance;
  94.     /* FIXME is it needed ? */
  95.     libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );
  96.     return NULL;
  97. }
  98. #endif /* 0 */
  99. static int libvlc_vlm_init( libvlc_instance_t *p_instance,
  100.                             libvlc_exception_t *p_exception )
  101. {
  102.     if( !p_instance->p_vlm )
  103.         p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
  104.     if( !p_instance->p_vlm )
  105.     {
  106.         libvlc_exception_raise( p_exception,
  107.                                 "Unable to create VLM." );
  108.         return VLC_EGENERIC;
  109.     }
  110.     return VLC_SUCCESS;
  111. }
  112. #define VLM_RET(p,ret) do {                                     
  113.     if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;
  114.     (p) = p_instance->p_vlm;                                    
  115.   } while(0)
  116. #define VLM(p) VLM_RET(p,)
  117. static vlm_media_instance_t *
  118. libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
  119.                                const char *psz_name, int i_minstance_idx,
  120.                                libvlc_exception_t *p_exception )
  121. {
  122.     vlm_t *p_vlm;
  123.     vlm_media_instance_t **pp_minstance;
  124.     vlm_media_instance_t *p_minstance;
  125.     int i_minstance;
  126.     int64_t id;
  127.     VLM_RET(p_vlm, NULL);
  128.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
  129.         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
  130.                      &i_minstance ) )
  131.     {
  132.         libvlc_exception_raise( p_exception, "Unable to get %s instances",
  133.                                 psz_name );
  134.         return NULL;
  135.     }
  136.     p_minstance = NULL;
  137.     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
  138.     {
  139.         p_minstance = pp_minstance[i_minstance_idx];
  140.         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
  141.     }
  142.     while( i_minstance > 0 )
  143.         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
  144.     TAB_CLEAN( i_minstance, pp_minstance );
  145.     return p_minstance;
  146. }
  147. void libvlc_vlm_release( libvlc_instance_t *p_instance,
  148.                          libvlc_exception_t *p_exception)
  149. {
  150.     vlm_t *p_vlm;
  151.     VLM(p_vlm);
  152.     vlm_Delete( p_vlm );
  153.     p_instance->p_vlm = NULL;
  154. }
  155. void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
  156.                                const char *psz_name,
  157.                                const char *psz_input,
  158.                                const char *psz_output, int i_options,
  159.                                const char * const *ppsz_options,
  160.                                int b_enabled, int b_loop,
  161.                                libvlc_exception_t *p_exception )
  162. {
  163.     vlm_t *p_vlm;
  164.     vlm_media_t m;
  165.     int n;
  166.     VLM(p_vlm);
  167.     vlm_media_Init( &m );
  168.     m.psz_name = strdup( psz_name );
  169.     m.b_enabled = b_enabled;
  170.     m.b_vod = false;
  171.     m.broadcast.b_loop = b_loop;
  172.     if( psz_input )
  173.         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
  174.     if( psz_output )
  175.         m.psz_output = strdup( psz_output );
  176.     for( n = 0; n < i_options; n++ )
  177.         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
  178.     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
  179.     vlm_media_Clean( &m );
  180.     if( n )
  181.         libvlc_exception_raise( p_exception, "Media %s creation failed",
  182.                                 psz_name );
  183. }
  184. void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
  185.                          const char *psz_input, int i_options,
  186.                          const char * const *ppsz_options, int b_enabled,
  187.                          const char *psz_mux, libvlc_exception_t *p_exception )
  188. {
  189.     vlm_t *p_vlm;
  190.     vlm_media_t m;
  191.     int n;
  192.     VLM(p_vlm);
  193.     vlm_media_Init( &m );
  194.     m.psz_name = strdup( psz_name );
  195.     m.b_enabled = b_enabled;
  196.     m.b_vod = true;
  197.     m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
  198.     if( psz_input )
  199.         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
  200.     for( n = 0; n < i_options; n++ )
  201.         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
  202.     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
  203.     vlm_media_Clean( &m );
  204.     if( n )
  205.         libvlc_exception_raise( p_exception, "Media %s creation failed",
  206.                                 psz_name );
  207. }
  208. void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
  209.                            libvlc_exception_t *p_exception )
  210. {
  211.     vlm_t *p_vlm;
  212.     int64_t id;
  213.     VLM(p_vlm);
  214.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
  215.         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
  216.     {
  217.         libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
  218.     }
  219. }
  220. #define VLM_CHANGE(psz_error, code ) do {   
  221.     vlm_media_t *p_media;   
  222.     vlm_t *p_vlm;           
  223.     int64_t id;             
  224.     VLM(p_vlm);             
  225.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||    
  226.         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) {       
  227.         libvlc_exception_raise( p_exception, psz_error, psz_name ); 
  228.         return;             
  229.     }                       
  230.     if( !p_media ) goto error;                                      
  231.                             
  232.     code;                   
  233.                             
  234.     if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) {         
  235.         vlm_media_Delete( p_media );                                
  236.         goto error;         
  237.     }                       
  238.     vlm_media_Delete( p_media );                                    
  239.     return;                 
  240.   error:                    
  241.     libvlc_exception_raise( p_exception, psz_error, psz_name );
  242.   } while(0)
  243. void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
  244.                              const char *psz_name, int b_enabled,
  245.                              libvlc_exception_t *p_exception )
  246. {
  247. #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
  248.     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
  249. #undef VLM_CHANGE_CODE
  250. }
  251. void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
  252.                           int b_loop, libvlc_exception_t *p_exception )
  253. {
  254. #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
  255.     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
  256. #undef VLM_CHANGE_CODE
  257. }
  258. void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
  259.                          const char *psz_mux, libvlc_exception_t *p_exception )
  260. {
  261. #define VLM_CHANGE_CODE { if( p_media->b_vod ) { 
  262.                             free( p_media->vod.psz_mux ); 
  263.                             p_media->vod.psz_mux = psz_mux 
  264.                                  ? strdup( psz_mux ) : NULL; 
  265.                           } }
  266.     VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
  267. #undef VLM_CHANGE_CODE
  268. }
  269. void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
  270.                             const char *psz_name, const char *psz_output,
  271.                             libvlc_exception_t *p_exception )
  272. {
  273. #define VLM_CHANGE_CODE { free( p_media->psz_output ); 
  274.                           p_media->psz_output = strdup( psz_output ); }
  275.     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
  276. #undef VLM_CHANGE_CODE
  277. }
  278. void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
  279.                            const char *psz_name, const char *psz_input,
  280.                            libvlc_exception_t *p_exception )
  281. {
  282. #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) 
  283.                             free( p_media->ppsz_input[--p_media->i_input] );
  284.                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, 
  285.                                       strdup(psz_input) ); }
  286.     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
  287. #undef VLM_CHANGE_CODE
  288. }
  289. void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
  290.                            const char *psz_name, const char *psz_input,
  291.                            libvlc_exception_t *p_exception )
  292. {
  293. #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, 
  294.                           strdup(psz_input) ); }
  295.     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
  296. #undef VLM_CHANGE_CODE
  297. }
  298. void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
  299.                               const char *psz_name, const char *psz_input,
  300.                               const char *psz_output, int i_options,
  301.                               const char * const *ppsz_options, int b_enabled,
  302.                               int b_loop, libvlc_exception_t *p_exception )
  303. {
  304. #define VLM_CHANGE_CODE { int n;        
  305.     p_media->b_enabled = b_enabled;     
  306.     p_media->broadcast.b_loop = b_loop; 
  307.     while( p_media->i_input > 0 )       
  308.         free( p_media->ppsz_input[--p_media->i_input] );    
  309.     if( psz_input )                     
  310.         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); 
  311.     free( p_media->psz_output );        
  312.     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; 
  313.     while( p_media->i_option > 0 )     
  314.         free( p_media->ppsz_option[--p_media->i_option] );        
  315.     for( n = 0; n < i_options; n++ )    
  316.         TAB_APPEND( p_media->i_option, p_media->ppsz_option, 
  317.                     strdup(ppsz_options[n]) );   
  318.   }
  319.     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
  320. #undef VLM_CHANGE_CODE
  321. }
  322. void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
  323.                             const char *psz_name,
  324.                             libvlc_exception_t *p_exception )
  325. {
  326.     vlm_t *p_vlm;
  327.     int64_t id;
  328.     VLM(p_vlm);
  329.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
  330.         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
  331.     {
  332.         libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
  333.     }
  334. }
  335. void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
  336.                             const char *psz_name,
  337.                             libvlc_exception_t *p_exception )
  338. {
  339.     vlm_t *p_vlm;
  340.     int64_t id;
  341.     VLM(p_vlm);
  342.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
  343.         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
  344.     {
  345.         libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
  346.     }
  347. }
  348. void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
  349.                              const char *psz_name,
  350.                              libvlc_exception_t *p_exception )
  351. {
  352.     vlm_t *p_vlm;
  353.     int64_t id;
  354.     VLM(p_vlm);
  355.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
  356.         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
  357.     {
  358.         libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
  359.     }
  360. }
  361. void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
  362.                             const char *psz_name, float f_percentage,
  363.                             libvlc_exception_t *p_exception )
  364. {
  365.     vlm_t *p_vlm;
  366.     int64_t id;
  367.     VLM(p_vlm);
  368.     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
  369.         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
  370.                      f_percentage ) )
  371.         libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
  372.                                 psz_name, f_percentage );
  373. }
  374. float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
  375.                                               const char *psz_name,
  376.                                               int i_instance,
  377.                                               libvlc_exception_t *p_exception )
  378. {
  379.     vlm_media_instance_t *p_mi;
  380.     float result = -1.;
  381.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  382.                                           i_instance, p_exception );
  383.     if( p_mi )
  384.     {
  385.         result = p_mi->d_position;
  386.         vlm_media_instance_Delete( p_mi );
  387.     }
  388.     return result;
  389. }
  390. int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
  391.                                         const char *psz_name, int i_instance,
  392.                                         libvlc_exception_t *p_exception )
  393. {
  394.     vlm_media_instance_t *p_mi;
  395.     int result = -1;
  396.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  397.                                         i_instance, p_exception );
  398.     if( p_mi )
  399.     {
  400.         result = p_mi->i_time;
  401.         vlm_media_instance_Delete( p_mi );
  402.     }
  403.     return result;
  404. }
  405. int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
  406.                                           const char *psz_name,
  407.                                           int i_instance,
  408.                                           libvlc_exception_t *p_exception )
  409. {
  410.     vlm_media_instance_t *p_mi;
  411.     int result = -1;
  412.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  413.                                           i_instance, p_exception );
  414.     if( p_mi )
  415.     {
  416.         result = p_mi->i_length;
  417.         vlm_media_instance_Delete( p_mi );
  418.     }
  419.     return result;
  420. }
  421. int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
  422.                                         const char *psz_name, int i_instance,
  423.                                         libvlc_exception_t *p_exception )
  424. {
  425.     vlm_media_instance_t *p_mi;
  426.     int result = -1;
  427.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  428.                                           i_instance, p_exception );
  429.     if( p_mi )
  430.     {
  431.         result = p_mi->i_rate;
  432.         vlm_media_instance_Delete( p_mi );
  433.     }
  434.     return result;
  435. }
  436. int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
  437.                                          const char *psz_name, int i_instance,
  438.                                          libvlc_exception_t *p_exception )
  439. {
  440.     vlm_media_instance_t *p_mi;
  441.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  442.                                           i_instance, p_exception );
  443.     if( p_mi )
  444.         vlm_media_instance_Delete( p_mi );
  445.     return p_mi ? 0 : -1;
  446. }
  447. int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
  448.                                            const char *psz_name,
  449.                                            int i_instance,
  450.                                            libvlc_exception_t *p_exception )
  451. {
  452.     vlm_media_instance_t *p_mi;
  453.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  454.                                           i_instance, p_exception );
  455.     if( p_mi )
  456.         vlm_media_instance_Delete( p_mi );
  457.     return p_mi ? 0 : -1;
  458. }
  459. int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
  460.                                             const char *psz_name,
  461.                                             int i_instance,
  462.                                             libvlc_exception_t *p_exception )
  463. {
  464.     vlm_media_instance_t *p_mi;
  465.     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
  466.                                           i_instance, p_exception );
  467.     if( p_mi )
  468.         vlm_media_instance_Delete( p_mi );
  469.     return p_mi ? 0 : -1;
  470. }