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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * mtp.c :  MTP interface module
  3.  *****************************************************************************
  4.  * Copyright (C) 2009 the VideoLAN team
  5.  *
  6.  * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  21.  *****************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. # include "config.h"
  24. #endif
  25. #include <vlc_common.h>
  26. #include <vlc_playlist.h>
  27. #include <vlc_plugin.h>
  28. #include <errno.h>
  29. #include <vlc_charset.h>
  30. #include <vlc_interface.h>
  31. #include <vlc_services_discovery.h>
  32. #ifdef HAVE_SYS_STAT_H
  33. #include <sys/stat.h>
  34. #endif
  35. #include "libmtp.h"
  36. /*****************************************************************************
  37.  * Module descriptor
  38.  *****************************************************************************/
  39. static int Open( vlc_object_t * );
  40. static void Close( vlc_object_t * );
  41. vlc_module_begin()
  42.     set_shortname( "MTP" )
  43.     set_description( N_( "MTP devices" ) )
  44.     set_category( CAT_PLAYLIST )
  45.     set_subcategory( SUBCAT_PLAYLIST_SD )
  46.     set_capability( "services_discovery", 0 )
  47.     set_callbacks( Open, Close )
  48.     linked_with_a_crap_library_which_uses_atexit()
  49. vlc_module_end()
  50. /*****************************************************************************
  51.  * Local prototypes
  52.  *****************************************************************************/
  53. static void *Run( void * );
  54. static int AddDevice( services_discovery_t *, LIBMTP_raw_device_t * );
  55. static void AddTrack( services_discovery_t *, LIBMTP_track_t *);
  56. static void CloseDevice( services_discovery_t * );
  57. static int CountTracks( uint64_t const, uint64_t const, void const * const );
  58. /*****************************************************************************
  59.  * Local structures
  60.  *****************************************************************************/
  61. struct services_discovery_sys_t
  62. {
  63.     int i_tracks_num;
  64.     input_item_t **pp_items;
  65.     int i_count;
  66.     char *psz_name;
  67.     uint32_t i_bus;
  68.     uint8_t i_dev;
  69.     uint16_t i_product_id;
  70.     vlc_thread_t thread;
  71. };
  72. static vlc_mutex_t mtp_lock = VLC_STATIC_MUTEX;
  73. static bool b_mtp_initialized = false;
  74. /*****************************************************************************
  75.  * Open: initialize and create stuff
  76.  *****************************************************************************/
  77. static int Open( vlc_object_t *p_this )
  78. {
  79.     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
  80.     services_discovery_sys_t *p_sys;
  81.     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
  82.         return VLC_ENOMEM;
  83.     p_sd->p_sys = p_sys;
  84.     p_sys->psz_name = NULL;
  85.     vlc_mutex_lock( &mtp_lock );
  86.     if( !b_mtp_initialized )
  87.     {
  88.         LIBMTP_Init();
  89.         b_mtp_initialized = true;
  90.     }
  91.     vlc_mutex_unlock( &mtp_lock );
  92.     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
  93.     {
  94.         free (p_sys);
  95.         return VLC_EGENERIC;
  96.     }
  97.     return VLC_SUCCESS;
  98. }
  99. /*****************************************************************************
  100.  * Close: cleanup
  101.  *****************************************************************************/
  102. static void Close( vlc_object_t *p_this )
  103. {
  104.     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
  105.     free( p_sd->p_sys->psz_name );
  106.     vlc_cancel (p_sd->p_sys->thread);
  107.     vlc_join (p_sd->p_sys->thread, NULL);
  108.     free( p_sd->p_sys );
  109. }
  110. /*****************************************************************************
  111.  * Run: main thread
  112.  *****************************************************************************/
  113. static void *Run( void *data )
  114. {
  115.     LIBMTP_raw_device_t *p_rawdevices;
  116.     int i_numrawdevices;
  117.     int i_ret;
  118.     int i_status = 0;
  119.     services_discovery_t *p_sd = data;
  120.     for(;;)
  121.     {
  122.         int canc = vlc_savecancel();
  123.         i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
  124.         if ( i_ret == 0 && i_numrawdevices > 0 && p_rawdevices != NULL &&
  125.              i_status == 0 )
  126.         {
  127.             /* Found a new device, add it */
  128.             msg_Dbg( p_sd, "New device found" );
  129.             if( AddDevice( p_sd, &p_rawdevices[0] ) == VLC_SUCCESS )
  130.                 i_status = 1;
  131.         }
  132.         else
  133.         {
  134.             if ( ( i_ret != 0 || i_numrawdevices == 0 || p_rawdevices == NULL )
  135.                  && i_status == 1)
  136.             {
  137.                 /* The device is not connected anymore, delete it */
  138.                 msg_Info( p_sd, "Device disconnected" );
  139.                 CloseDevice( p_sd );
  140.                 i_status = 0;
  141.             }
  142.         }
  143.         free( p_rawdevices );
  144.         vlc_restorecancel(canc);
  145.         msleep( 500000 );
  146.     }
  147.     return NULL;
  148. }
  149. /*****************************************************************************
  150.  * Everything else
  151.  *****************************************************************************/
  152. static int AddDevice( services_discovery_t *p_sd,
  153.                       LIBMTP_raw_device_t *p_raw_device )
  154. {
  155.     char *psz_name = NULL;
  156.     LIBMTP_mtpdevice_t *p_device;
  157.     LIBMTP_track_t *p_track, *p_tmp;
  158.     if( ( p_device = LIBMTP_Open_Raw_Device( p_raw_device ) ) != NULL )
  159.     {
  160.         if( !( psz_name = LIBMTP_Get_Friendlyname( p_device ) ) )
  161.             if( !( psz_name = LIBMTP_Get_Modelname( p_device ) ) )
  162.                 if( !( psz_name = strdup( N_( "MTP Device" ) ) ) )
  163.                     return VLC_ENOMEM;
  164.         msg_Info( p_sd, "Found device: %s", psz_name );
  165.         p_sd->p_sys->i_bus = p_raw_device->bus_location;
  166.         p_sd->p_sys->i_dev = p_raw_device->devnum;
  167.         p_sd->p_sys->i_product_id = p_raw_device->device_entry.product_id;
  168.         if( ( p_track = LIBMTP_Get_Tracklisting_With_Callback( p_device,
  169.                             CountTracks, p_sd ) ) == NULL )
  170.         {
  171.             msg_Warn( p_sd, "No tracks on the device" );
  172.         }
  173.         else
  174.         {
  175.             if( !( p_sd->p_sys->pp_items = calloc( p_sd->p_sys->i_tracks_num,
  176.                                                    sizeof( input_item_t * ) ) ) )
  177.             {
  178.                 free( psz_name );
  179.                 return VLC_ENOMEM;
  180.             }
  181.             p_sd->p_sys->i_count = 0;
  182.             while( p_track != NULL )
  183.             {
  184.                 msg_Dbg( p_sd, "Track found: %s - %s", p_track->artist,
  185.                          p_track->title );
  186.                 AddTrack( p_sd, p_track );
  187.                 p_tmp = p_track;
  188.                 p_track = p_track->next;
  189.                 LIBMTP_destroy_track_t( p_tmp );
  190.             }
  191.         }
  192.         p_sd->p_sys->psz_name = psz_name;
  193.         LIBMTP_Release_Device( p_device );
  194.         return VLC_SUCCESS;
  195.     }
  196.     else
  197.     {
  198.         msg_Warn( p_sd, "No device found, after all" );
  199.         return VLC_EGENERIC;
  200.     }
  201. }
  202. static void AddTrack( services_discovery_t *p_sd, LIBMTP_track_t *p_track )
  203. {
  204.     input_item_t *p_input;
  205.     char *psz_string;
  206.     char *extension;
  207.     extension = rindex( p_track->filename, '.' );
  208.     if( asprintf( &psz_string, "mtp://%"PRIu32":%"PRIu8":%"PRIu16":%d%s",
  209.                   p_sd->p_sys->i_bus, p_sd->p_sys->i_dev,
  210.                   p_sd->p_sys->i_product_id, p_track->item_id,
  211.                   extension ) == -1 )
  212.     {
  213.         msg_Err( p_sd, "Error adding %s, skipping it", p_track->filename );
  214.         return;
  215.     }
  216.     if( ( p_input = input_item_New( p_sd, psz_string,
  217.                                     p_track->title ) ) == NULL )
  218.     {
  219.         msg_Err( p_sd, "Error adding %s, skipping it", p_track->filename );
  220.         free( psz_string );
  221.         return;
  222.     }
  223.     free( psz_string );
  224.     input_item_SetArtist( p_input, p_track->artist );
  225.     input_item_SetGenre( p_input, p_track->genre );
  226.     input_item_SetAlbum( p_input, p_track->album );
  227.     if( asprintf( &psz_string, "%d", p_track->tracknumber ) != -1 )
  228.     {
  229.         input_item_SetTrackNum( p_input, psz_string );
  230.         free( psz_string );
  231.     }
  232.     if( asprintf( &psz_string, "%d", p_track->rating ) != -1 )
  233.     {
  234.         input_item_SetRating( p_input, psz_string );
  235.         free( psz_string );
  236.     }
  237.     input_item_SetDate( p_input, p_track->date );
  238.     input_item_SetDuration( p_input, p_track->duration * 1000 );
  239.     services_discovery_AddItem( p_sd, p_input, NULL );
  240.     p_sd->p_sys->pp_items[p_sd->p_sys->i_count++] = p_input;
  241. }
  242. static void CloseDevice( services_discovery_t *p_sd )
  243. {
  244.     input_item_t **pp_items = p_sd->p_sys->pp_items;
  245.     int i_i;
  246.     if( pp_items != NULL )
  247.     {
  248.         for( i_i = 0; i_i < p_sd->p_sys->i_count; i_i++ )
  249.         {
  250.             if( pp_items[i_i] != NULL )
  251.             {
  252.                 services_discovery_RemoveItem( p_sd, pp_items[i_i] );
  253.                 vlc_gc_decref( pp_items[i_i] );
  254.             }
  255.         }
  256.         free( pp_items );
  257.     }
  258. }
  259. static int CountTracks( uint64_t const sent, uint64_t const total,
  260.                         void const * const data )
  261. {
  262.     VLC_UNUSED( sent );
  263.     services_discovery_t *p_sd = (services_discovery_t *)data;
  264.     p_sd->p_sys->i_tracks_num = total;
  265.     return 0;
  266. }