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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * core.c: Core functions : init, playlist, stream management
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 the VideoLAN team
  5.  * $Id: 240c93fb7dc9b1498cdfd78dac1557cb5d14a508 $
  6.  *
  7.  * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
  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. #ifdef HAVE_CONFIG_H
  24. # include "config.h"
  25. #endif
  26. #include "mediacontrol_internal.h"
  27. #include <vlc/mediacontrol.h>
  28. #include <vlc/libvlc.h>
  29. #include <vlc_common.h>
  30. #include <vlc_interface.h>
  31. #include <vlc_playlist.h>
  32. #include <vlc_vout.h>
  33. #include <vlc_aout.h>
  34. #include <vlc_input.h>
  35. #include <vlc_osd.h>
  36. #include <stdlib.h>                                      /* malloc(), free() */
  37. #include <string.h>
  38. #include <errno.h>                                                 /* ENOMEM */
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #ifdef HAVE_UNISTD_H
  42. #    include <unistd.h>
  43. #endif
  44. #ifdef HAVE_SYS_TIME_H
  45. #    include <sys/time.h>
  46. #endif
  47. #ifdef HAVE_SYS_TYPES_H
  48. #    include <sys/types.h>
  49. #endif
  50. mediacontrol_Instance* mediacontrol_new( int argc, char** argv, mediacontrol_Exception *exception )
  51. {
  52.     mediacontrol_Instance* retval;
  53.     libvlc_exception_t ex;
  54.     libvlc_exception_init( &ex );
  55.     mediacontrol_exception_init( exception );
  56.     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
  57.     if( !retval )
  58.         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
  59.     retval->p_instance = libvlc_new( argc, (const char**)argv, &ex );
  60.     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
  61.     retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
  62.     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
  63.     return retval;
  64. }
  65. void
  66. mediacontrol_exit( mediacontrol_Instance *self )
  67. {
  68.     libvlc_release( self->p_instance );
  69. }
  70. libvlc_instance_t*
  71. mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
  72. {
  73.     return self->p_instance;
  74. }
  75. libvlc_media_player_t*
  76. mediacontrol_get_media_player( mediacontrol_Instance *self )
  77. {
  78.     return self->p_media_player;
  79. }
  80. mediacontrol_Instance *
  81. mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
  82.                 mediacontrol_Exception *exception )
  83. {
  84.     mediacontrol_Instance* retval;
  85.     libvlc_exception_t ex;
  86.     libvlc_exception_init( &ex );
  87.     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
  88.     if( ! retval )
  89.     {
  90.         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
  91.     }
  92.     retval->p_instance = p_instance;
  93.     retval->p_media_player = libvlc_media_player_new( retval->p_instance, &ex );
  94.     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
  95.     return retval;
  96. }
  97. /**************************************************************************
  98.  * Playback management
  99.  **************************************************************************/
  100. mediacontrol_Position*
  101. mediacontrol_get_media_position( mediacontrol_Instance *self,
  102.                                  const mediacontrol_PositionOrigin an_origin,
  103.                                  const mediacontrol_PositionKey a_key,
  104.                                  mediacontrol_Exception *exception )
  105. {
  106.     mediacontrol_Position* retval = NULL;
  107.     libvlc_exception_t ex;
  108.     int64_t pos;
  109.     mediacontrol_exception_init( exception );
  110.     libvlc_exception_init( &ex );
  111.     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
  112.     retval->origin = an_origin;
  113.     retval->key = a_key;
  114.     if(  an_origin != mediacontrol_AbsolutePosition )
  115.     {
  116.         free( retval );
  117.         /* Relative or ModuloPosition make no sense */
  118.         RAISE_NULL( mediacontrol_PositionOriginNotSupported,
  119.                     "Only absolute position is valid." );
  120.     }
  121.     /* We are asked for an AbsolutePosition. */
  122.     pos = libvlc_media_player_get_time( self->p_media_player, &ex );
  123.     if( a_key == mediacontrol_MediaTime )
  124.     {
  125.         retval->value = pos;
  126.     }
  127.     else
  128.     {
  129.         retval->value = private_mediacontrol_unit_convert( self->p_media_player,
  130.                                                            mediacontrol_MediaTime,
  131.                                                            a_key,
  132.                                                            pos );
  133.     }
  134.     return retval;
  135. }
  136. /* Sets the media position */
  137. void
  138. mediacontrol_set_media_position( mediacontrol_Instance *self,
  139.                                  const mediacontrol_Position * a_position,
  140.                                  mediacontrol_Exception *exception )
  141. {
  142.     libvlc_exception_t ex;
  143.     int64_t i_pos;
  144.     libvlc_exception_init( &ex );
  145.     mediacontrol_exception_init( exception );
  146.     i_pos = private_mediacontrol_position2microsecond( self->p_media_player, a_position );
  147.     libvlc_media_player_set_time( self->p_media_player, i_pos / 1000, &ex );
  148.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  149. }
  150. /* Starts playing a stream */
  151. /*
  152.  * Known issues: since moving in the playlist using playlist_Next
  153.  * or playlist_Prev implies starting to play items, the a_position
  154.  * argument will be only honored for the 1st item in the list.
  155.  *
  156.  * XXX:FIXME split moving in the playlist and playing items two
  157.  * different actions or make playlist_<Next|Prev> accept a time
  158.  * value to start to play from.
  159.  */
  160. void
  161. mediacontrol_start( mediacontrol_Instance *self,
  162.                     const mediacontrol_Position * a_position,
  163.                     mediacontrol_Exception *exception )
  164. {
  165.     libvlc_media_t * p_media;
  166.     char * psz_name;
  167.     libvlc_exception_t ex;
  168.     mediacontrol_exception_init( exception );
  169.     libvlc_exception_init( &ex );
  170.     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
  171.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  172.     if ( ! p_media )
  173.     {
  174.         /* No media was defined. */
  175.         RAISE( mediacontrol_PlaylistException, "No defined media." );
  176.     }
  177.     else
  178.     {
  179.         /* A media was defined. Get its mrl to reuse it, but reset the options
  180.            (because start-time may have been set on the previous invocation */
  181.         psz_name = libvlc_media_get_mrl( p_media, &ex );
  182.         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  183.         /* Create a new media */
  184.         p_media = libvlc_media_new( self->p_instance, psz_name, &ex );
  185.         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  186.         if( a_position->value )
  187.         {
  188.             char * psz_from;
  189.             libvlc_time_t i_from;
  190.             /* A start position was specified. Add it to media options */
  191.             psz_from = ( char * )malloc( 20 * sizeof( char ) );
  192.             i_from = private_mediacontrol_position2microsecond( self->p_media_player, a_position ) / 1000000;
  193.             snprintf( psz_from, 20, "start-time=%"PRId64, i_from );
  194.             libvlc_media_add_option( p_media, psz_from, &ex );
  195.             HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  196.         }
  197.         libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
  198.         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  199.         libvlc_media_player_play( self->p_media_player, &ex );
  200.         HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  201.     }
  202. }
  203. void
  204. mediacontrol_pause( mediacontrol_Instance *self,
  205.                     mediacontrol_Exception *exception )
  206. {
  207.     libvlc_exception_t ex;
  208.     mediacontrol_exception_init( exception );
  209.     libvlc_exception_init( &ex );
  210.     libvlc_media_player_pause( self->p_media_player, &ex );
  211.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  212. }
  213. void
  214. mediacontrol_resume( mediacontrol_Instance *self,
  215.                      mediacontrol_Exception *exception )
  216. {
  217.     libvlc_exception_t ex;
  218.     mediacontrol_exception_init( exception );
  219.     libvlc_exception_init( &ex );
  220.     libvlc_media_player_pause( self->p_media_player, &ex );
  221.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  222. }
  223. void
  224. mediacontrol_stop( mediacontrol_Instance *self,
  225.                    mediacontrol_Exception *exception )
  226. {
  227.     libvlc_exception_t ex;
  228.     mediacontrol_exception_init( exception );
  229.     libvlc_exception_init( &ex );
  230.     libvlc_media_player_stop( self->p_media_player, &ex );
  231.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  232. }
  233. /**************************************************************************
  234.  * File management
  235.  **************************************************************************/
  236. void
  237. mediacontrol_set_mrl( mediacontrol_Instance *self,
  238.                       const char * psz_file,
  239.                       mediacontrol_Exception *exception )
  240. {
  241.     libvlc_media_t * p_media;
  242.     libvlc_exception_t ex;
  243.     mediacontrol_exception_init( exception );
  244.     libvlc_exception_init( &ex );
  245.     p_media = libvlc_media_new( self->p_instance, psz_file, &ex );
  246.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  247.     libvlc_media_player_set_media( self->p_media_player, p_media, &ex );
  248.     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
  249. }
  250. char *
  251. mediacontrol_get_mrl( mediacontrol_Instance *self,
  252.                       mediacontrol_Exception *exception )
  253. {
  254.     libvlc_media_t * p_media;
  255.     libvlc_exception_t ex;
  256.     mediacontrol_exception_init( exception );
  257.     libvlc_exception_init( &ex );
  258.     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
  259.     HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
  260.     if ( ! p_media )
  261.     {
  262.         return strdup( "" );
  263.     }
  264.     else
  265.     {
  266.         char * psz_mrl;
  267.         psz_mrl = libvlc_media_get_mrl( p_media, &ex );
  268.         HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
  269.         return psz_mrl;
  270.     }
  271. }
  272. /***************************************************************************
  273.  * Status feedback
  274.  ***************************************************************************/
  275. mediacontrol_StreamInformation *
  276. mediacontrol_get_stream_information( mediacontrol_Instance *self,
  277.                                      mediacontrol_PositionKey a_key,
  278.                                      mediacontrol_Exception *exception )
  279. {
  280.     (void)a_key;
  281.     mediacontrol_StreamInformation *retval = NULL;
  282.     libvlc_media_t * p_media;
  283.     libvlc_exception_t ex;
  284.     libvlc_exception_init( &ex );
  285.     retval = ( mediacontrol_StreamInformation* )
  286.                             malloc( sizeof( mediacontrol_StreamInformation ) );
  287.     if( ! retval )
  288.     {
  289.         RAISE( mediacontrol_InternalException, "Out of memory" );
  290.         return NULL;
  291.     }
  292.     p_media = libvlc_media_player_get_media( self->p_media_player, &ex );
  293.     if( libvlc_exception_raised( &ex ) )
  294.     {
  295.         free( retval );
  296.         RAISE( mediacontrol_InternalException,
  297.                libvlc_exception_get_message( &ex ) );
  298.         libvlc_exception_clear( &ex );
  299.         return NULL;
  300.     }
  301.     if( ! p_media )
  302.     {
  303.         /* No p_media defined */
  304.         retval->streamstatus = mediacontrol_UndefinedStatus;
  305.         retval->url          = strdup( "" );
  306.         retval->position     = 0;
  307.         retval->length       = 0;
  308.     }
  309.     else
  310.     {
  311.         libvlc_state_t state;
  312.         state = libvlc_media_player_get_state( self->p_media_player, &ex );
  313.         if( libvlc_exception_raised( &ex ) )
  314.         {
  315.             free( retval );
  316.             RAISE( mediacontrol_InternalException,
  317.                    libvlc_exception_get_message( &ex ) );
  318.             libvlc_exception_clear( &ex );
  319.             return NULL;
  320.         }
  321.         switch( state )
  322.         {
  323.         case libvlc_NothingSpecial:
  324.             retval->streamstatus = mediacontrol_UndefinedStatus;
  325.             break;
  326.         case libvlc_Opening :
  327.             retval->streamstatus = mediacontrol_InitStatus;
  328.             break;
  329.         case libvlc_Buffering:
  330.             retval->streamstatus = mediacontrol_BufferingStatus;
  331.             break;
  332.         case libvlc_Playing:
  333.             retval->streamstatus = mediacontrol_PlayingStatus;
  334.             break;
  335.         case libvlc_Paused:
  336.             retval->streamstatus = mediacontrol_PauseStatus;
  337.             break;
  338.         case libvlc_Stopped:
  339.             retval->streamstatus = mediacontrol_StopStatus;
  340.             break;
  341.         case libvlc_Ended:
  342.             retval->streamstatus = mediacontrol_EndStatus;
  343.             break;
  344.         case libvlc_Error:
  345.             retval->streamstatus = mediacontrol_ErrorStatus;
  346.             break;
  347.         default :
  348.             retval->streamstatus = mediacontrol_UndefinedStatus;
  349.             break;
  350.         }
  351.         retval->url = libvlc_media_get_mrl( p_media, &ex );
  352.         retval->position = libvlc_media_player_get_time( self->p_media_player, &ex );
  353.         if( libvlc_exception_raised( &ex ) )
  354.         {
  355.             libvlc_exception_clear( &ex );
  356.             retval->position = 0;
  357.         }
  358.         retval->length = libvlc_media_player_get_length( self->p_media_player, &ex );
  359.         if( libvlc_exception_raised( &ex ) )
  360.         {
  361.             libvlc_exception_clear( &ex );
  362.             retval->length = 0;
  363.         }
  364.     }
  365.     return retval;
  366. }