upnp_cc.cpp 8.33 KB
Newer Older
1
/*****************************************************************************
2
 * upnp_cc.cpp :  UPnP discovery module
3 4
 *****************************************************************************
 * Copyright (C) 2004-2005 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Rémi Denis-Courmont <rem # videolan.org>
8
 *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
9
 * Based on original wxWindows patch for VLC, and dependent on CyberLink
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * UPnP library from :
 *          Satoshi Konno <skonno@cybergarage.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 27 28 29 30 31 32 33 34
 *****************************************************************************/

/*****************************************************************************
 * Includes
 *****************************************************************************/

#include <cybergarage/upnp/media/player/MediaPlayer.h>

#undef PACKAGE_NAME
35 36 37 38
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

39
#include <vlc_common.h>
40
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
41
#include <vlc_playlist.h>
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

/* FIXME: thread-safety ?? */
/* FIXME: playlist locking */

/************************************************************************
 * Macros and definitions
 ************************************************************************/
using namespace std;
using namespace CyberLink;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

/* Callbacks */
    static int  Open ( vlc_object_t * );
    static void Close( vlc_object_t * );

vlc_module_begin();
61
    set_shortname( "UPnP");
62
    set_description( N_("Universal Plug'n'Play discovery") );
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    set_category( CAT_PLAYLIST );
    set_subcategory( SUBCAT_PLAYLIST_SD );

    set_capability( "services_discovery", 0 );
    set_callbacks( Open, Close );

vlc_module_end();

/*****************************************************************************
 * Run: main UPnP thread
 *****************************************************************************
 * Processes UPnP events
 *****************************************************************************/
class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
                    /*public EventListener,*/ public SearchResponseListener
{
    private:
        services_discovery_t *p_sd;

        Device *GetDeviceFromUSN( const string& usn )
        {
            return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
        }

        playlist_item_t *FindDeviceNode( Device *dev )
        {
89
            return playlist_ChildSearchName( p_sd->p_cat, dev->getFriendlyName() );
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        }

        playlist_item_t *FindDeviceNode( const string &usn )
        {
            return FindDeviceNode( GetDeviceFromUSN( usn ) );
        }

        playlist_item_t *AddDevice( Device *dev );
        void AddDeviceContent( Device *dev );
        void AddContent( playlist_item_t *p_parent, ContentNode *node );
        void RemoveDevice( Device *dev );

        /* CyberLink callbacks */
        virtual void deviceAdded( Device *dev );
        virtual void deviceRemoved( Device *dev );

        virtual void deviceSearchResponseReceived( SSDPPacket *packet );
        /*virtual void eventNotifyReceived( const char *uuid, long seq,
                                          const char *name,
                                          const char *value );*/

    public:
        UPnPHandler( services_discovery_t *p_this )
113
            : p_sd( p_this )
114 115 116 117 118 119 120
        {
            addDeviceChangeListener( this );
            addSearchResponseListener( this );
            //addEventListener( this );
        }
};

121 122 123 124
/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
125
{
126
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
127

128
    services_discovery_SetLocalizedName( p_sd, _("Devices") );
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    UPnPHandler *u = new UPnPHandler( p_sd );
    u->start( );
    msg_Dbg( p_sd, "upnp discovery started" );
    p_sd->p_private = u;

    return VLC_SUCCESS;
}


/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    UPnPHandler *u = (UPnPHandler *)p_this->p_private;
    u->stop( );
146

147
    msg_Dbg( p_this, "upnp discovery started" );
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}


playlist_item_t *UPnPHandler::AddDevice( Device *dev )
{
    if( dev == NULL )
        return NULL;

    /* We are not interested in IGD devices or whatever (at the moment) */
    if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
        return NULL;

    playlist_item_t *p_item = FindDeviceNode( dev );
    if ( p_item != NULL )
        return p_item;

    /* FIXME:
     * Maybe one day, VLC API will make sensible use of the const keyword;
     * That day, you will no longer need this strdup().
     */
    char *str = strdup( dev->getFriendlyName( ) );

170
    p_item = playlist_NodeCreate( p_playlist, str, p_sd->p_cat, 0, NULL );
171
    p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    msg_Dbg( p_sd, "device %s added", str );
    free( str );

    return p_item;
}

void UPnPHandler::AddDeviceContent( Device *dev )
{
    playlist_item_t *p_devnode = AddDevice( dev );

    if( p_devnode == NULL )
        return;

    AddContent( p_devnode, getContentDirectory( dev ) );
}

void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
{
    if( node == NULL )
        return;

    const char *title = node->getTitle();
    if( title == NULL )
        return;

    msg_Dbg( p_sd, "title = %s", title );
198

199
    if ( node->isItemNode() )
200
    {
201
        ItemNode *iNode = (ItemNode *)node;
202
        input_item_t *p_input = input_item_New( p_sd, iNode->getResource(), title );
203
        /* FIXME: playlist_AddInput() can fail */
204
        playlist_BothAddInput( p_playlist, p_input, p_parent,
205
                               PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL,
206
                               false );
Rafaël Carré's avatar
Rafaël Carré committed
207
        vlc_gc_decref( p_input );
208
    } else if ( node->isContainerNode() )
209 210
    {
        ContainerNode *conNode = (ContainerNode *)node;
211

212
        char* p_name = strdup(title); /* See other comment on strdup */
213 214
        playlist_item_t* p_node = playlist_NodeCreate( p_playlist, p_name,
                                                       p_parent, 0, NULL );
215
        free(p_name);
216

217
        unsigned nContentNodes = conNode->getNContentNodes();
218

219 220
        for( unsigned n = 0; n < nContentNodes; n++ )
           AddContent( p_node, conNode->getContentNode( n ) );
221
    }
222 223 224 225 226 227 228 229
}


void UPnPHandler::RemoveDevice( Device *dev )
{
    playlist_item_t *p_item = FindDeviceNode( dev );

    if( p_item != NULL )
230
        playlist_NodeDelete( p_playlist, p_item, true, true );
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}


void UPnPHandler::deviceAdded( Device *dev )
{
    msg_Dbg( p_sd, "adding device" );
    AddDeviceContent( dev );
}


void UPnPHandler::deviceRemoved( Device *dev )
{
    msg_Dbg( p_sd, "removing device" );
    RemoveDevice( dev );
}


void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
{
    if( !packet->isRootDevice() )
        return;

    string usn, nts, nt, udn;

    packet->getUSN( usn );
    packet->getNT( nt );
    packet->getNTS( nts );
    udn = usn.substr( 0, usn.find( "::" ) );

260 261
    /* Remove existing root device before adding updated one */

262
    Device *dev = GetDeviceFromUSN( usn );
263 264 265
    RemoveDevice( dev );

    if( !packet->isByeBye() )
266 267 268 269 270 271 272 273 274
        AddDeviceContent( dev );
}

/*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
                                       const char *name, const char *value )
{
    msg_Dbg( p_sd, "event notify received" );
    msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );
}*/