vlm.c 37 KB
Newer Older
1
/*****************************************************************************
2
 * vlm.c: VLM interface plugin
3
 *****************************************************************************
Antoine Cellerier's avatar
Antoine Cellerier committed
4
 * Copyright (C) 2000-2005 the VideoLAN team
5
 * $Id$
6 7 8
 *
 * Authors: Simon Latapie <garf@videolan.org>
 *          Laurent Aimar <fenrir@videolan.org>
Gildas Bazin's avatar
Gildas Bazin committed
9
 *          Gildas Bazin <gbazin@videolan.org>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34 35

#include <stdio.h>
36
#include <ctype.h>                                              /* tolower() */
37
#include <assert.h>
38

39 40
#include <vlc_vlm.h>

Rafaël Carré's avatar
Rafaël Carré committed
41 42 43 44
#ifndef WIN32
#   include <sys/time.h>                                   /* gettimeofday() */
#endif

45 46 47 48
#ifdef UNDER_CE
#include <sys/time.h>                                      /* gettimeofday() */
#endif

49 50
#ifdef HAVE_TIME_H
#   include <time.h>                                              /* ctime() */
51
#   include <sys/timeb.h>                                         /* ftime() */
52 53
#endif

Clément Stenac's avatar
Clément Stenac committed
54 55
#include <vlc_input.h>
#include <vlc_stream.h>
56
#include "vlm_internal.h"
Laurent Aimar's avatar
Laurent Aimar committed
57
#include "vlm_event.h"
58
#include <vlc_vod.h>
Clément Stenac's avatar
Clément Stenac committed
59
#include <vlc_charset.h>
60 61
#include <vlc_sout.h>
#include "../stream_output/stream_output.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
#include "../libvlc.h"
63

64 65 66
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
67

68
static void vlm_Destructor( vlm_t *p_vlm );
69
static void* Manage( void * );
70
static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
71

72 73 74 75
/*****************************************************************************
 * vlm_New:
 *****************************************************************************/
vlm_t *__vlm_New ( vlc_object_t *p_this )
76
{
77 78 79 80
    vlc_value_t lockval;
    vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
    char *psz_vlmconf;
    static const char vlm_object_name[] = "vlm daemon";
Gildas Bazin's avatar
Gildas Bazin committed
81

82 83 84 85
    /* Avoid multiple creation */
    if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
        var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
        return NULL;
86

87
    vlc_mutex_lock( lockval.p_address );
88

89 90 91
    p_vlm = *pp_vlm;
    if( p_vlm )
    {   /* VLM already exists */
92
        vlc_object_hold( p_vlm );
93 94
        vlc_mutex_unlock( lockval.p_address );
        return p_vlm;
95
    }
96

97
    msg_Dbg( p_this, "creating VLM" );
98

99 100 101 102 103 104 105
    p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
                               vlm_object_name );
    if( !p_vlm )
    {
        vlc_mutex_unlock( lockval.p_address );
        return NULL;
    }
106

107 108 109 110 111 112
    vlc_mutex_init( &p_vlm->lock );
    p_vlm->i_id = 1;
    TAB_INIT( p_vlm->i_media, p_vlm->media );
    TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
    p_vlm->i_vod = 0;
    p_vlm->p_vod = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
113
    var_Create( p_vlm, "intf-event", VLC_VAR_ADDRESS );
114
    vlc_object_attach( p_vlm, p_this->p_libvlc );
Gildas Bazin's avatar
Gildas Bazin committed
115

116
    if( vlc_clone( &p_vlm->thread, Manage, p_vlm, VLC_THREAD_PRIORITY_LOW ) )
117 118 119 120
    {
        vlc_mutex_destroy( &p_vlm->lock );
        vlc_object_release( p_vlm );
        return NULL;
121 122
    }

123 124 125
    /* Load our configuration file */
    psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
    if( psz_vlmconf && *psz_vlmconf )
Clément Stenac's avatar
Clément Stenac committed
126
    {
127 128
        vlm_message_t *p_message = NULL;
        char *psz_buffer = NULL;
129

130
        msg_Dbg( p_this, "loading VLM configuration" );
131
        if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) != -1 )
132
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
133
            msg_Dbg( p_this, "%s", psz_buffer );
134 135
            if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
                msg_Warn( p_this, "error while loading the configuration file" );
136

137 138
            vlm_MessageDelete( p_message );
            free( psz_buffer );
139
        }
Gildas Bazin's avatar
Gildas Bazin committed
140
    }
141
    free( psz_vlmconf );
142

143 144 145
    vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
    *pp_vlm = p_vlm; /* for future reference */
    vlc_mutex_unlock( lockval.p_address );
146

147
    return p_vlm;
148
}
Gildas Bazin's avatar
Gildas Bazin committed
149 150

/*****************************************************************************
151
 * vlm_Delete:
Gildas Bazin's avatar
Gildas Bazin committed
152
 *****************************************************************************/
153
void vlm_Delete( vlm_t *p_vlm )
Gildas Bazin's avatar
Gildas Bazin committed
154
{
155
    vlc_value_t lockval;
Gildas Bazin's avatar
Gildas Bazin committed
156

157 158 159 160 161 162 163
    /* vlm_Delete() is serialized against itself, and against vlm_New().
     * This way, vlm_Destructor () (called from vlc_objet_release() above)
     * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
    var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
    vlc_mutex_lock( lockval.p_address );
    vlc_object_release( p_vlm );
    vlc_mutex_unlock( lockval.p_address );
Gildas Bazin's avatar
Gildas Bazin committed
164 165
}

166 167 168 169
/*****************************************************************************
 * vlm_Destructor:
 *****************************************************************************/
static void vlm_Destructor( vlm_t *p_vlm )
Gildas Bazin's avatar
Gildas Bazin committed
170
{
171
    libvlc_priv (p_vlm->p_libvlc)->p_vlm = NULL;
172

173 174
    vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
    TAB_CLEAN( p_vlm->i_media, p_vlm->media );
Gildas Bazin's avatar
Gildas Bazin committed
175

176 177
    vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
    TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
178

179
    vlc_object_kill( p_vlm );
180 181
    /*vlc_cancel( p_vlm->thread ); */
    vlc_join( p_vlm->thread, NULL );
182 183
    vlc_mutex_destroy( &p_vlm->lock );
}
184

185 186 187 188 189 190 191
/*****************************************************************************
 * vlm_ExecuteCommand:
 *****************************************************************************/
int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
                        vlm_message_t **pp_message)
{
    int i_result;
192

193 194 195
    vlc_mutex_lock( &p_vlm->lock );
    i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
    vlc_mutex_unlock( &p_vlm->lock );
196

197 198
    return i_result;
}
199 200


201 202
int64_t vlm_Date(void)
{
Geoffroy Couprie's avatar
Geoffroy Couprie committed
203
#if defined (WIN32) && !defined (UNDER_CE)
204 205 206 207 208
    struct timeb tm;
    ftime( &tm );
    return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
#else
    struct timeval tv_date;
209

210 211 212 213
    /* gettimeofday() cannot fail given &tv_date is a valid address */
    (void)gettimeofday( &tv_date, NULL );
    return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
#endif
214 215
}

216

217 218 219 220 221 222 223 224 225 226 227 228 229
/*****************************************************************************
 *
 *****************************************************************************/
static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
                                const char *psz_id, int i_query, va_list args )
{
    vlm_t *vlm = (vlm_t *)p_private;
    int i, i_ret;
    const char *psz;
    int64_t id;

    if( !p_private || !p_vod_media )
        return VLC_EGENERIC;
230

231
    vlc_mutex_lock( &vlm->lock );
232

233 234 235 236
    /* Find media id */
    for( i = 0, id = -1; i < vlm->i_media; i++ )
    {
        if( p_vod_media == vlm->media[i]->vod.p_media )
237
        {
238 239
            id = vlm->media[i]->cfg.id;
            break;
240
        }
241 242 243 244 245
    }
    if( id == -1 )
    {
        vlc_mutex_unlock( &vlm->lock );
        return VLC_EGENERIC;
246
    }
Gildas Bazin's avatar
Gildas Bazin committed
247

248
    switch( i_query )
249
    {
250 251 252 253
    case VOD_MEDIA_PLAY:
        psz = (const char *)va_arg( args, const char * );
        i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
        break;
254

255 256 257
    case VOD_MEDIA_PAUSE:
        i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
        break;
258

259 260 261
    case VOD_MEDIA_STOP:
        i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
        break;
262

263 264 265 266 267 268
    case VOD_MEDIA_SEEK:
    {
        double d_position = (double)va_arg( args, double );
        i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
        break;
    }
269

270 271 272 273
    case VOD_MEDIA_REWIND:
    {
        double d_scale = (double)va_arg( args, double );
        double d_position;
274

275 276 277 278 279 280 281
        vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
        d_position -= (d_scale / 1000.0);
        if( d_position < 0.0 )
            d_position = 0.0;
        i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
        break;
    }
282

283 284 285 286
    case VOD_MEDIA_FORWARD:
    {
        double d_scale = (double)va_arg( args, double );
        double d_position;
287

288 289 290 291 292 293 294
        vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
        d_position += (d_scale / 1000.0);
        if( d_position > 1.0 )
            d_position = 1.0;
        i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
        break;
    }
Gildas Bazin's avatar
Gildas Bazin committed
295

296 297 298 299
    default:
        i_ret = VLC_EGENERIC;
        break;
    }
300

301
    vlc_mutex_unlock( &vlm->lock );
Gildas Bazin's avatar
Gildas Bazin committed
302

303 304
    return i_ret;
}
305

Gildas Bazin's avatar
Gildas Bazin committed
306

307 308 309
/*****************************************************************************
 * Manage:
 *****************************************************************************/
310
static void* Manage( void* p_object )
311 312 313 314 315 316
{
    vlm_t *vlm = (vlm_t*)p_object;
    int i, j;
    mtime_t i_lastcheck;
    mtime_t i_time;

317
    int canc = vlc_savecancel ();
318 319 320
    i_lastcheck = vlm_Date();

    while( !vlm->b_die )
321
    {
322 323
        char **ppsz_scheduled_commands = NULL;
        int    i_scheduled_commands = 0;
324

325
        vlc_mutex_lock( &vlm->lock );
326

327 328
        /* destroy the inputs that wants to die, and launch the next input */
        for( i = 0; i < vlm->i_media; i++ )
329
        {
330
            vlm_media_sys_t *p_media = vlm->media[i];
331

332 333 334
            for( j = 0; j < p_media->i_instance; )
            {
                vlm_media_instance_sys_t *p_instance = p_media->instance[j];
335

336 337 338
                if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
                {
                    int i_new_input_index;
339

340 341 342 343 344 345 346 347 348
                    /* */
                    i_new_input_index = p_instance->i_index + 1;
                    if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
                        i_new_input_index = 0;

                    /* FIXME implement multiple input with VOD */
                    if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
                        vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
                    else
349
                        vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
350 351 352 353

                    j = 0;
                }
                else
Gildas Bazin's avatar
Gildas Bazin committed
354 355
                {
                    j++;
356
                }
Gildas Bazin's avatar
Gildas Bazin committed
357
            }
358
        }
Gildas Bazin's avatar
Gildas Bazin committed
359

360 361 362 363 364 365 366
        /* scheduling */
        i_time = vlm_Date();

        for( i = 0; i < vlm->i_schedule; i++ )
        {
            mtime_t i_real_date = vlm->schedule[i]->i_date;

367
            if( vlm->schedule[i]->b_enabled == true )
Gildas Bazin's avatar
Gildas Bazin committed
368
            {
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
                if( vlm->schedule[i]->i_date == 0 ) // now !
                {
                    vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
                    i_real_date = i_time;
                }
                else if( vlm->schedule[i]->i_period != 0 )
                {
                    int j = 0;
                    while( vlm->schedule[i]->i_date + j *
                           vlm->schedule[i]->i_period <= i_lastcheck &&
                           ( vlm->schedule[i]->i_repeat > j ||
                             vlm->schedule[i]->i_repeat == -1 ) )
                    {
                        j++;
                    }
384

385 386 387
                    i_real_date = vlm->schedule[i]->i_date + j *
                        vlm->schedule[i]->i_period;
                }
388

389 390 391 392 393 394 395 396 397
                if( i_real_date <= i_time && i_real_date > i_lastcheck )
                {
                    for( j = 0; j < vlm->schedule[i]->i_command; j++ )
                    {
                        TAB_APPEND( i_scheduled_commands,
                                    ppsz_scheduled_commands,
                                    strdup(vlm->schedule[i]->command[j] ) );
                    }
                }
398 399
            }
        }
400 401 402 403 404 405 406 407 408 409 410 411 412
        while( i_scheduled_commands )
        {
            vlm_message_t *message = NULL;
            char *psz_command = ppsz_scheduled_commands[0];
            ExecuteCommand( vlm, psz_command,&message );

            /* for now, drop the message */
            vlm_MessageDelete( message );
            TAB_REMOVE( i_scheduled_commands,
                        ppsz_scheduled_commands,
                        psz_command );
            free( psz_command );
        }
413

414 415 416 417 418
        i_lastcheck = i_time;

        vlc_mutex_unlock( &vlm->lock );

        msleep( 100000 );
419
    }
Gildas Bazin's avatar
Gildas Bazin committed
420

421
    vlc_restorecancel (canc);
422
    return NULL;
423 424 425 426 427 428 429 430
}

/* New API
 */
/*
typedef struct
{
    struct
431
    {
432 433 434 435 436 437
        int i_connection_count;
        int i_connection_active;
    } vod;
    struct
    {
        int        i_count;
438
        bool b_playing;
439 440
        int        i_playing_index;
    } broadcast;
441

442 443
} vlm_media_status_t;
*/
444

445 446 447 448
/* */
static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
{
    int i;
449

450 451 452 453
    for( i = 0; i < p_vlm->i_media; i++ )
    {
        if( p_vlm->media[i]->cfg.id == id )
            return p_vlm->media[i];
454
    }
455 456 457 458 459
    return NULL;
}
static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
{
    int i;
Gildas Bazin's avatar
Gildas Bazin committed
460

461
    for( i = 0; i < p_vlm->i_media; i++ )
462
    {
463 464
        if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
            return p_vlm->media[i];
465
    }
466
    return NULL;
467
}
468
static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
469
{
470
    int i;
471

472 473 474
    if( !p_cfg || !p_cfg->psz_name ||
        !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
        return VLC_EGENERIC;
475

476
    for( i = 0; i < p_vlm->i_media; i++ )
477
    {
478 479 480 481 482 483 484
        if( p_vlm->media[i]->cfg.id == p_cfg->id )
            continue;
        if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
485 486


487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
/* Called after a media description is changed/added */
static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
{
    vlm_media_t *p_cfg = &p_media->cfg;
    /* Check if we need to create/delete a vod media */
    if( p_cfg->b_vod )
    {
        if( !p_cfg->b_enabled && p_media->vod.p_media )
        {
            p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
            p_media->vod.p_media = NULL;
        }
        else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
        {
            /* Pre-parse the input */
            input_thread_t *p_input;
            char *psz_output;
            char *psz_header;
            char *psz_dup;
            int i;
507

508
            vlc_gc_decref( p_media->vod.p_item );
509
            p_media->vod.p_item = input_item_New( p_vlm, p_cfg->ppsz_input[0],
510
                p_cfg->psz_name );
511 512

            if( p_cfg->psz_output )
513 514 515 516
            {
                if( asprintf( &psz_output, "%s:description", p_cfg->psz_output )  == -1 )
                    psz_output = NULL;
            }
517
            else
518
                psz_output = strdup( "#description" );
519

520
            if( psz_output && asprintf( &psz_dup, "sout=%s", psz_output ) != -1 )
521
            {
522
                input_item_AddOption( p_media->vod.p_item, psz_dup, VLC_INPUT_OPTION_TRUSTED );
523 524
                free( psz_dup );
            }
525 526
            free( psz_output );

527
            for( i = 0; i < p_cfg->i_option; i++ )
528
                input_item_AddOption( p_media->vod.p_item,
529
                                      p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
530

531 532
            if( asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name ) == -1 )
                psz_header = NULL;
533

534
            if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
535 536 537
            {
                while( !p_input->b_eof && !p_input->b_error )
                    msleep( 100000 );
538

539
                input_StopThread( p_input, false );
540
                vlc_thread_join( p_input );
541
                vlc_object_release( p_input );
542 543
            }
            free( psz_header );
544

545 546 547 548 549
            if( p_cfg->vod.psz_mux )
            {
                input_item_t item;
                es_format_t es, *p_es = &es;
                char fourcc[5];
550

551 552 553 554
                sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
                fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
                fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);

555 556
                /* XXX: Don't do it that way, but properly use a new input item ref. */
                item = *p_media->vod.p_item;
557 558 559 560 561 562 563 564
                item.i_es = 1;
                item.es = &p_es;
                es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );

                p_media->vod.p_media =
                    p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
            }
            else
565
            {
566
                p_media->vod.p_media =
567
                    p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
568
            }
569
        }
570 571 572 573
    }
    else
    {
        /* TODO start media if needed */
574
    }
Gildas Bazin's avatar
Gildas Bazin committed
575

576
    /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
577

578
    vlm_SendEventMediaChanged( p_vlm, p_cfg->id, p_cfg->psz_name );
579 580 581
    return VLC_SUCCESS;
}
static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
582
{
583
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
584

585 586 587 588 589 590 591
    /* */
    if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
        return VLC_EGENERIC;
    if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
        return VLC_EGENERIC;

    if( 0 )
592
    {
593 594
        /* TODO check what are the changes being done (stop instance if needed) */
    }
595

596 597
    vlm_media_Clean( &p_media->cfg );
    vlm_media_Copy( &p_media->cfg, p_cfg );
598

599 600
    return vlm_OnMediaUpdate( p_vlm, p_media );
}
601

602 603 604
static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
{
    vlm_media_sys_t *p_media;
605

606 607 608 609 610 611 612 613
    if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
    {
        msg_Err( p_vlm, "invalid media description" );
        return VLC_EGENERIC;
    }
    /* Check if we need to load the VOD server */
    if( p_cfg->b_vod && !p_vlm->i_vod )
    {
614
        p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
615
                                          VLC_OBJECT_GENERIC, "vod server" );
616
        vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
617
        p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", NULL, false );
618
        if( !p_vlm->p_vod->p_module )
619
        {
620 621
            msg_Err( p_vlm, "cannot find vod server" );
            vlc_object_detach( p_vlm->p_vod );
622
            vlc_object_release( p_vlm->p_vod );
Sébastien Escudier's avatar
Sébastien Escudier committed
623
            p_vlm->p_vod = NULL;
624
            return VLC_EGENERIC;
625 626
        }

627 628 629
        p_vlm->p_vod->p_data = p_vlm;
        p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
    }
630

631
    p_media = calloc( 1, sizeof( vlm_media_sys_t ) );
632 633
    if( !p_media )
        return VLC_ENOMEM;
634

635 636 637
    if( p_cfg->b_vod )
        p_vlm->i_vod++;

638 639 640
    vlm_media_Copy( &p_media->cfg, p_cfg );
    p_media->cfg.id = p_vlm->i_id++;
    /* FIXME do we do something here if enabled is true ? */
641

642
    p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
643

644 645
    p_media->vod.p_media = NULL;
    TAB_INIT( p_media->i_instance, p_media->instance );
646

647 648
    /* */
    TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
649

650 651
    if( p_id )
        *p_id = p_media->cfg.id;
652

Laurent Aimar's avatar
Laurent Aimar committed
653
    /* */
654
    vlm_SendEventMediaAdded( p_vlm, p_media->cfg.id, p_media->cfg.psz_name );
655 656
    return vlm_OnMediaUpdate( p_vlm, p_media );
}
657

658 659 660
static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
{
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
661

662 663
    if( !p_media )
        return VLC_EGENERIC;
664

665 666
    while( p_media->i_instance > 0 )
        vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
667

668 669
    if( p_media->cfg.b_vod )
    {
670
        p_media->cfg.b_enabled = false;
671 672
        vlm_OnMediaUpdate( p_vlm, p_media );
        p_vlm->i_vod--;
673 674
    }

675 676 677
    /* */
    vlm_SendEventMediaRemoved( p_vlm, id, p_media->cfg.psz_name );

678
    vlm_media_Clean( &p_media->cfg );
679

680
    vlc_gc_decref( p_media->vod.p_item );
681

682
    TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
683

684 685 686 687
    free( p_media );

    /* Check if we need to unload the VOD server */
    if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
688
    {
689
        module_unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
690
        vlc_object_detach( p_vlm->p_vod );
691
        vlc_object_release( p_vlm->p_vod );
692 693
        p_vlm->p_vod = NULL;
    }
Laurent Aimar's avatar
Laurent Aimar committed
694

695 696
    return VLC_SUCCESS;
}
697

698 699 700 701 702
static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
{
    vlm_media_t **pp_dsc;
    int                     i_dsc;
    int i;
703

704 705 706 707 708 709
    TAB_INIT( i_dsc, pp_dsc );
    for( i = 0; i < p_vlm->i_media; i++ )
    {
        vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
        TAB_APPEND( i_dsc, pp_dsc, p_dsc );
    }
710

711 712
    *ppp_dsc = pp_dsc;
    *pi_dsc = i_dsc;
713

714 715 716 717 718 719
    return VLC_SUCCESS;
}
static int vlm_ControlMediaClear( vlm_t *p_vlm )
{
    while( p_vlm->i_media > 0 )
        vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
720

721 722 723 724 725 726 727
    return VLC_SUCCESS;
}
static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
{
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
    if( !p_media )
        return VLC_EGENERIC;
728

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
    return VLC_SUCCESS;
}
static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
{
    vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
    if( !p_media )
        return VLC_EGENERIC;

    *p_id = p_media->cfg.id;
    return VLC_SUCCESS;
}

static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
{
    int i;

    for( i = 0; i < p_media->i_instance; i++ )
    {
        const char *psz = p_media->instance[i]->psz_name;
        if( ( psz == NULL && psz_id == NULL ) ||
            ( psz && psz_id && !strcmp( psz, psz_id ) ) )
            return p_media->instance[i];
752
    }
753 754 755 756
    return NULL;
}
static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
{
757
    vlm_media_instance_sys_t *p_instance = calloc( 1, sizeof(vlm_media_instance_sys_t) );
758 759
    if( !p_instance )
        return NULL;
760

761 762 763 764
    p_instance->psz_name = NULL;
    if( psz_name )
        p_instance->psz_name = strdup( psz_name );

765
    p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
766 767

    p_instance->i_index = 0;
768
    p_instance->b_sout_keep = false;
769
    p_instance->p_input = NULL;
770
    p_instance->p_input_resource = NULL;
771 772 773

    return p_instance;
}
774
static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instance_sys_t *p_instance, const char *psz_name )
775
{
776 777
    input_thread_t *p_input = p_instance->p_input;
    if( p_input )
778
    {
779
        input_resource_t *p_resource;
780

781
        input_StopThread( p_input, true );
782
        vlc_thread_join( p_input );
783

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
784
        p_resource = input_DetachResource( p_input );
785
        input_resource_Delete( p_resource );
786

787
        vlc_object_release( p_input );
Laurent Aimar's avatar
Laurent Aimar committed
788

789
        vlm_SendEventMediaInstanceStopped( p_vlm, id, psz_name );
790
    }
791 792
    if( p_instance->p_input_resource )
        input_resource_Delete( p_instance->p_input_resource );
793

794
    vlc_gc_decref( p_instance->p_item );
795
    free( p_instance->psz_name );
796 797 798 799 800 801 802 803 804
    free( p_instance );
}


static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
{
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
    vlm_media_instance_sys_t *p_instance;
    char *psz_log;
805

806 807
    if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
        return VLC_EGENERIC;
808

809
    /* TODO support multiple input for VOD with sout-keep ? */
810

811 812
    if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
        return VLC_EGENERIC;
813

814 815
    if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
        return VLC_EGENERIC;
816

817 818 819 820 821
    p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
    if( !p_instance )
    {
        vlm_media_t *p_cfg = &p_media->cfg;
        int i;
822

823 824 825
        p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
        if( !p_instance )
            return VLC_ENOMEM;
826

827 828 829
        if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
        {
            char *psz_buffer;
830
            if( asprintf( &psz_buffer, "sout=%s%s%s",
831 832
                      p_cfg->psz_output ? p_cfg->psz_output : "",
                      (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
833 834
                      psz_vod_output ? psz_vod_output : "" ) != -1 )
            {
835
                input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
836 837
                free( psz_buffer );
            }
838 839
        }

840
        for( i = 0; i < p_cfg->i_option; i++ )
841
        {
842
            if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
843
                p_instance->b_sout_keep = true;
844
            else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
845
                p_instance->b_sout_keep = false;
846
            else
847
                input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
848
        }
849 850 851 852
        TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
    }

    /* Stop old instance */
853 854
    input_thread_t *p_input = p_instance->p_input;
    if( p_input )
855 856
    {
        if( p_instance->i_index == i_input_index &&
857
            !p_input->b_eof && !p_input->b_error )
858
        {
859 860
            if( var_GetInteger( p_input, "state" ) == PAUSE_S )
                var_SetInteger( p_input, "state",  PLAYING_S );
861
            return VLC_SUCCESS;
862 863
        }

864
        input_StopThread( p_input, !p_input->b_eof && !p_input->b_error );
865
        vlc_thread_join( p_input );
866

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
867
        p_instance->p_input_resource = input_DetachResource( p_input );
868

869
        vlc_object_release( p_input );
870

871
        if( !p_instance->b_sout_keep )
872 873
            input_resource_TerminateSout( p_instance->p_input_resource );
        input_resource_TerminateVout( p_instance->p_input_resource );
Laurent Aimar's avatar
Laurent Aimar committed
874

875
        vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
876 877 878 879
    }

    /* Start new one */
    p_instance->i_index = i_input_index;
880
    input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
881

882
    if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
883
    {
884
        p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item,
885 886
                                                          psz_log, p_instance->p_input_resource );
        p_instance->p_input_resource = NULL;
887

888 889 890
        if( !p_instance->p_input )
        {
            TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
891
            vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media->cfg.psz_name );
Laurent Aimar's avatar
Laurent Aimar committed
892 893 894
        }
        else
        {
895
            vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
896 897
        }
        free( psz_log );
898 899
    }

900
    return VLC_SUCCESS;
901 902
}

903
static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
904
{
905 906
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
    vlm_media_instance_sys_t *p_instance;
907

908 909
    if( !p_media )
        return VLC_EGENERIC;
910

911 912
    p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
    if( !p_instance )
913 914
        return VLC_EGENERIC;

915
    TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
916

917
    vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media->cfg.psz_name );
918

919 920 921 922 923 924 925
    return VLC_SUCCESS;
}
static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
{
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
    vlm_media_instance_sys_t *p_instance;
    int i_state;
926

927 928
    if( !p_media )
        return VLC_EGENERIC;
929

930 931 932
    p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
    if( !p_instance || !p_instance->p_input )
        return VLC_EGENERIC;
933

934 935 936 937 938 939 940 941 942 943 944 945
    /* Toggle pause state */
    i_state = var_GetInteger( p_instance->p_input, "state" );
    if( i_state == PAUSE_S )
        var_SetInteger( p_instance->p_input, "state", PLAYING_S );
    else if( i_state == PLAYING_S )
        var_SetInteger( p_instance->p_input, "state", PAUSE_S );
    return VLC_SUCCESS;
}
static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
{
    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
    vlm_media_instance_sys_t *p_instance;
946

947 948
    if( !p_media )
        return VLC_EGENERIC;
949

950 951 952
    p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
    if( !p_instance || !p_instance->p_input )
        return VLC_EGENERIC;
953

954 955 956 957 958
    if( pi_time )
        *pi_time = var_GetTime( p_instance->p_input, "time" );
    if( pd_position )
        *pd_position = var_GetFloat( p_instance->p_input, "position" );
    return VLC_SUCCESS;