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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * services_discovery.c : Manage playlist services_discovery modules
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 the VideoLAN team
  5.  * $Id: 03c9f0de91d07a7558ce652a49effbe8d57d8591 $
  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. #ifdef HAVE_CONFIG_H
  24. # include "config.h"
  25. #endif
  26. #include <assert.h>
  27. #include <vlc_common.h>
  28. #include "vlc_playlist.h"
  29. #include "vlc_events.h"
  30. #include <vlc_services_discovery.h>
  31. #include "playlist_internal.h"
  32. #include "../libvlc.h"
  33. /*
  34.  * Services discovery
  35.  * Basically you just listen to Service discovery event through the
  36.  * sd's event manager.
  37.  * That's how the playlist get's Service Discovery information
  38.  */
  39. /**
  40.  * Gets the list of available services discovery plugins.
  41.  */
  42. char **vlc_sd_GetNames( char ***pppsz_longnames )
  43. {
  44.     return module_GetModulesNamesForCapability( "services_discovery",
  45.                                                 pppsz_longnames );
  46. }
  47. /***********************************************************************
  48.  * Create
  49.  ***********************************************************************/
  50. services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
  51. {
  52.     services_discovery_t *p_sd;
  53.     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
  54.                               "services discovery" );
  55.     if( !p_sd )
  56.         return NULL;
  57.     vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
  58.     vlc_event_manager_register_event_type( &p_sd->event_manager,
  59.             vlc_ServicesDiscoveryItemAdded );
  60.     vlc_event_manager_register_event_type( &p_sd->event_manager,
  61.             vlc_ServicesDiscoveryItemRemoved );
  62.     vlc_event_manager_register_event_type( &p_sd->event_manager,
  63.             vlc_ServicesDiscoveryStarted );
  64.     vlc_event_manager_register_event_type( &p_sd->event_manager,
  65.             vlc_ServicesDiscoveryEnded );
  66.     vlc_object_attach( p_sd, p_super );
  67.     return p_sd;
  68. }
  69. /***********************************************************************
  70.  * Stop
  71.  ***********************************************************************/
  72. bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
  73. {
  74.     assert(!p_sd->p_module);
  75.     p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
  76.     if( p_sd->p_module == NULL )
  77.     {
  78.         msg_Err( p_sd, "no suitable services discovery module" );
  79.         return false;
  80.     }
  81.         
  82.     vlc_event_t event = {
  83.         .type = vlc_ServicesDiscoveryStarted
  84.     };
  85.     vlc_event_send( &p_sd->event_manager, &event );
  86.     return true;
  87. }
  88.                           
  89. /***********************************************************************
  90.  * Stop
  91.  ***********************************************************************/
  92. void vlc_sd_Stop ( services_discovery_t * p_sd )
  93. {
  94.     vlc_event_t event = {
  95.         .type = vlc_ServicesDiscoveryEnded
  96.     };
  97.     
  98.     vlc_event_send( &p_sd->event_manager, &event );
  99.     module_unneed( p_sd, p_sd->p_module );
  100.     p_sd->p_module = NULL;
  101. }
  102. /***********************************************************************
  103.  * GetLocalizedName
  104.  ***********************************************************************/
  105. char *
  106. services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
  107. {
  108.     return strdup( module_get_name( p_sd->p_module, true ) );
  109. }
  110. /***********************************************************************
  111.  * EventManager
  112.  ***********************************************************************/
  113. vlc_event_manager_t *
  114. services_discovery_EventManager ( services_discovery_t * p_sd )
  115. {
  116.     return &p_sd->event_manager;
  117. }
  118. /***********************************************************************
  119.  * AddItem
  120.  ***********************************************************************/
  121. void
  122. services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
  123.                              const char * psz_category )
  124. {
  125.     vlc_event_t event;
  126.     event.type = vlc_ServicesDiscoveryItemAdded;
  127.     event.u.services_discovery_item_added.p_new_item = p_item;
  128.     event.u.services_discovery_item_added.psz_category = psz_category;
  129.     vlc_event_send( &p_sd->event_manager, &event );
  130. }
  131. /***********************************************************************
  132.  * RemoveItem
  133.  ***********************************************************************/
  134. void
  135. services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
  136. {
  137.     vlc_event_t event;
  138.     event.type = vlc_ServicesDiscoveryItemRemoved;
  139.     event.u.services_discovery_item_removed.p_item = p_item;
  140.     vlc_event_send( &p_sd->event_manager, &event );
  141. }
  142. /*
  143.  * Playlist - Services discovery bridge
  144.  */
  145.  /* A new item has been added to a certain sd */
  146. static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
  147. {
  148.     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
  149.     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
  150.     playlist_item_t *p_new_item, * p_parent = user_data;
  151.     playlist_t * p_playlist = p_parent->p_playlist;
  152.     msg_Dbg( p_playlist, "Adding %s in %s",
  153.                 p_input->psz_name ? p_input->psz_name : "(null)",
  154.                 psz_cat ? psz_cat : "(null)" );
  155.     PL_LOCK;
  156.     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
  157.     if( !EMPTY_STR(psz_cat) &&
  158.         p_parent->p_parent == p_playlist->p_root_category )
  159.     {
  160.         /* */
  161.         playlist_item_t * p_cat;
  162.         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
  163.         if( !p_cat )
  164.         {
  165.             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
  166.                                          p_parent, 0, NULL );
  167.             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
  168.         }
  169.         p_parent = p_cat;
  170.     }
  171.     p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
  172.                                         PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
  173.     if( p_new_item )
  174.     {
  175.         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
  176.         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
  177.     }
  178.     PL_UNLOCK;
  179. }
  180.  /* A new item has been removed from a certain sd */
  181. static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
  182. {
  183.     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
  184.     playlist_item_t * p_parent = user_data;
  185.     playlist_item_t * p_pl_item;
  186.     /* First make sure that if item is a node it will be deleted.
  187.      * XXX: Why don't we have a function to ensure that in the playlist code ? */
  188.     playlist_Lock( p_parent->p_playlist );
  189.     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
  190.             p_input->i_id, p_parent, false );
  191.     if( p_pl_item && p_pl_item->i_children > -1 )
  192.         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
  193.     else
  194.         /* Delete the non-node item normally */
  195.         playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
  196.                                           p_parent, pl_Locked );
  197.     playlist_Unlock( p_parent->p_playlist );
  198. }
  199. int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
  200. {
  201.     /* Perform the addition */
  202.     msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
  203.     services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
  204.     if( !p_sd )
  205.         return VLC_ENOMEM;
  206.     module_t *m = module_find_by_shortcut( psz_module );
  207.     if( !m )
  208.     {
  209.         msg_Err( p_playlist, "No such module: %s", psz_module );
  210.         vlc_sd_Destroy( p_sd );
  211.         return VLC_EGENERIC;
  212.     }
  213.     /* Free in playlist_ServicesDiscoveryRemove */
  214.     struct playlist_services_discovery_support_t * p_sds;
  215.     p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
  216.     if( !p_sds )
  217.     {
  218.         vlc_sd_Destroy( p_sd );
  219.         module_release( m );
  220.         return VLC_ENOMEM;
  221.     }
  222.     playlist_item_t * p_cat;
  223.     playlist_item_t * p_one;
  224.     PL_LOCK;
  225.     playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
  226.                               &p_cat, &p_one, false );
  227.     PL_UNLOCK;
  228.     module_release( m );
  229.     vlc_event_attach( services_discovery_EventManager( p_sd ),
  230.                       vlc_ServicesDiscoveryItemAdded,
  231.                       playlist_sd_item_added, p_one );
  232.         
  233.     vlc_event_attach( services_discovery_EventManager( p_sd ),
  234.                       vlc_ServicesDiscoveryItemAdded,
  235.                       playlist_sd_item_added, p_cat );
  236.     vlc_event_attach( services_discovery_EventManager( p_sd ),
  237.                       vlc_ServicesDiscoveryItemRemoved,
  238.                       playlist_sd_item_removed, p_one );
  239.     vlc_event_attach( services_discovery_EventManager( p_sd ),
  240.                       vlc_ServicesDiscoveryItemRemoved,
  241.                       playlist_sd_item_removed, p_cat );
  242.     if( !vlc_sd_Start( p_sd, psz_module ) )
  243.     {
  244.         vlc_sd_Destroy( p_sd );
  245.         free( p_sds );
  246.         return VLC_EGENERIC;
  247.     }
  248.     /* We want tree-view for service directory */
  249.     p_one->p_input->b_prefers_tree = true;
  250.     p_sds->p_sd = p_sd;
  251.     p_sds->p_one = p_one;
  252.     p_sds->p_cat = p_cat;
  253.     p_sds->psz_name = strdup( psz_module );
  254.     PL_LOCK;
  255.     TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
  256.     PL_UNLOCK;
  257.     return VLC_SUCCESS;
  258. }
  259. int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
  260.                                       const char *psz_name )
  261. {
  262.     playlist_private_t *priv = pl_priv( p_playlist );
  263.     struct playlist_services_discovery_support_t * p_sds = NULL;
  264.     PL_LOCK;
  265.     for( int i = 0; i < priv->i_sds; i++ )
  266.     {
  267.         if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
  268.         {
  269.             p_sds = priv->pp_sds[i];
  270.             REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
  271.             break;
  272.         }
  273.     }
  274.     PL_UNLOCK;
  275.     if( !p_sds )
  276.     {
  277.         msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
  278.         return VLC_EGENERIC;
  279.     }
  280.     services_discovery_t *p_sd = p_sds->p_sd;
  281.     assert( p_sd );
  282.     vlc_sd_Stop( p_sd );
  283.     vlc_event_detach( services_discovery_EventManager( p_sd ),
  284.                         vlc_ServicesDiscoveryItemAdded,
  285.                         playlist_sd_item_added,
  286.                         p_sds->p_one );
  287.     vlc_event_detach( services_discovery_EventManager( p_sd ),
  288.                         vlc_ServicesDiscoveryItemAdded,
  289.                         playlist_sd_item_added,
  290.                         p_sds->p_cat );
  291.     vlc_event_detach( services_discovery_EventManager( p_sd ),
  292.                         vlc_ServicesDiscoveryItemRemoved,
  293.                         playlist_sd_item_removed,
  294.                         p_sds->p_one );
  295.     vlc_event_detach( services_discovery_EventManager( p_sd ),
  296.                         vlc_ServicesDiscoveryItemRemoved,
  297.                         playlist_sd_item_removed,
  298.                         p_sds->p_cat );
  299.     /* Remove the sd playlist node if it exists */
  300.     PL_LOCK;
  301.     if( p_sds->p_cat != p_playlist->p_root_category &&
  302.         p_sds->p_one != p_playlist->p_root_onelevel )
  303.     {
  304.         playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
  305.         playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
  306.     }
  307.     PL_UNLOCK;
  308.     vlc_sd_Destroy( p_sd );
  309.     free( p_sds->psz_name );
  310.     free( p_sds );
  311.     return VLC_SUCCESS;
  312. }
  313. bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
  314.                                          const char *psz_name )
  315. {
  316.     playlist_private_t *priv = pl_priv( p_playlist );
  317.     bool found = false;
  318.     PL_LOCK;
  319.     for( int i = 0; i < priv->i_sds; i++ )
  320.     {
  321.         vlc_sd_internal_t *sd = priv->pp_sds[i];
  322.         if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
  323.         {
  324.             found = true;
  325.             break;
  326.         }
  327.     }
  328.     PL_UNLOCK;
  329.     return found;
  330. }
  331. void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
  332. {
  333.     playlist_private_t *priv = pl_priv( p_playlist );
  334.     while( priv->i_sds > 0 )
  335.         playlist_ServicesDiscoveryRemove( p_playlist,
  336.                                           priv->pp_sds[0]->psz_name );
  337. }