resource.c 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*****************************************************************************
 * resource.c
 *****************************************************************************
 * Copyright (C) 2008 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31 32
#include <assert.h>

33 34
#include <vlc_common.h>
#include <vlc_vout.h>
35
#include <vlc_spu.h>
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <vlc_aout.h>
#include <vlc_sout.h>
#include "../libvlc.h"
#include "../stream_output/stream_output.h"
#include "../audio_output/aout_internal.h"
#include "../video_output/vout_control.h"
#include "input_interface.h"
#include "resource.h"

struct input_resource_t
{
    /* This lock is used to serialize request and protect
     * our variables */
    vlc_mutex_t    lock;

    /* */
    input_thread_t *p_input;

    sout_instance_t *p_sout;
    vout_thread_t   *p_vout_free;

    /* This lock is used to protect vout resources access (for hold)
     * It is a special case because of embed video (possible deadlock
59 60 61 62 63 64
     * between vout window request and vout holds in some(qt4) interface)
     */
    vlc_mutex_t    lock_hold;

    /* You need lock+lock_hold to write to the following variables and
     * only lock or lock_hold to read them */
65

66 67
    int             i_vout;
    vout_thread_t   **pp_vout;
68 69

    aout_instance_t *p_aout;
70 71 72 73 74 75 76 77 78 79 80
};

/* */
static void DestroySout( input_resource_t *p_resource )
{
#ifdef ENABLE_SOUT
    if( p_resource->p_sout )
        sout_DeleteInstance( p_resource->p_sout );
#endif
    p_resource->p_sout = NULL;
}
81 82 83 84 85 86 87 88 89

static sout_instance_t *DetachSout( input_resource_t *p_resource )
{
    sout_instance_t *p_sout = p_resource->p_sout;
    p_resource->p_sout = NULL;

    return p_sout;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
static sout_instance_t *RequestSout( input_resource_t *p_resource,
                                     sout_instance_t *p_sout, const char *psz_sout )
{
#ifdef ENABLE_SOUT
    if( !p_sout && !psz_sout )
    {
        if( p_resource->p_sout )
            msg_Dbg( p_resource->p_sout, "destroying useless sout" );
        DestroySout( p_resource );
        return NULL;
    }

    assert( p_resource->p_input );
    assert( !p_sout || ( !p_resource->p_sout && !psz_sout ) );

    /* Check the validity of the sout */
    if( p_resource->p_sout &&
        strcmp( p_resource->p_sout->psz_sout, psz_sout ) )
    {
        msg_Dbg( p_resource->p_input, "destroying unusable sout" );
        DestroySout( p_resource );
    }

    if( psz_sout )
    {
        if( p_resource->p_sout )
        {
            /* Reuse it */
            msg_Dbg( p_resource->p_input, "reusing sout" );
            msg_Dbg( p_resource->p_input, "you probably want to use gather stream_out" );
            vlc_object_attach( p_resource->p_sout, p_resource->p_input );
        }
        else
        {
            /* Create a new one */
            p_resource->p_sout = sout_NewInstance( p_resource->p_input, psz_sout );
        }

        p_sout = p_resource->p_sout;
        p_resource->p_sout = NULL;

        return p_sout;
    }
    else
    {
        vlc_object_detach( p_sout );
        p_resource->p_sout = p_sout;

        return NULL;
    }
#else
    return NULL;
#endif
}

/* */
static void DestroyVout( input_resource_t *p_resource )
{
    assert( p_resource->i_vout == 0 );

    if( p_resource->p_vout_free )
        vout_CloseAndRelease( p_resource->p_vout_free );

    p_resource->p_vout_free = NULL;
}
155 156
static vout_thread_t *DetachVout( input_resource_t *p_resource )
{
157
    vlc_assert_locked( &p_resource->lock );
158 159 160 161 162 163 164
    assert( p_resource->i_vout == 0 );
    vout_thread_t *p_vout = p_resource->p_vout_free;
    p_resource->p_vout_free = NULL;

    return p_vout;
}

165 166 167 168 169 170 171 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 198 199 200 201 202 203 204 205 206 207
static void DisplayVoutTitle( input_resource_t *p_resource,
                              vout_thread_t *p_vout )
{
    assert( p_resource->p_input );

    /* TODO display the title only one time for the same input ? */

    input_item_t *p_item = input_GetItem( p_resource->p_input );

    char *psz_nowplaying = input_item_GetNowPlaying( p_item );
    if( psz_nowplaying && *psz_nowplaying )
    {
        vout_DisplayTitle( p_vout, psz_nowplaying );
    }
    else
    {
        char *psz_artist = input_item_GetArtist( p_item );
        char *psz_name = input_item_GetTitle( p_item );

        if( !psz_name || *psz_name == '\0' )
        {
            free( psz_name );
            psz_name = input_item_GetName( p_item );
        }
        if( psz_artist && *psz_artist )
        {
            char *psz_string;
            if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
            {
                vout_DisplayTitle( p_vout, psz_string );
                free( psz_string );
            }
        }
        else if( psz_name )
        {
            vout_DisplayTitle( p_vout, psz_name );
        }
        free( psz_name );
        free( psz_artist );
    }
    free( psz_nowplaying );
}
static vout_thread_t *RequestVout( input_resource_t *p_resource,
208 209
                                   vout_thread_t *p_vout,
                                   video_format_t *p_fmt, unsigned dpb_size,
210 211
                                   bool b_recycle )
{
212 213
    vlc_assert_locked( &p_resource->lock );

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    if( !p_vout && !p_fmt )
    {
        if( p_resource->p_vout_free )
        {
            msg_Dbg( p_resource->p_vout_free, "destroying useless vout" );
            vout_CloseAndRelease( p_resource->p_vout_free );
            p_resource->p_vout_free = NULL;
        }
        return NULL;
    }

    assert( p_resource->p_input );
    if( p_fmt )
    {
        /* */
        if( !p_vout && p_resource->p_vout_free )
        {
            msg_Dbg( p_resource->p_input, "trying to reuse free vout" );
            p_vout = p_resource->p_vout_free;

            p_resource->p_vout_free = NULL;
        }
        else if( p_vout )
        {
            assert( p_vout != p_resource->p_vout_free );

240
            vlc_mutex_lock( &p_resource->lock_hold );
241
            TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
242
            vlc_mutex_unlock( &p_resource->lock_hold );
243 244 245
        }

        /* */
246
        vout_configuration_t cfg = {
247 248 249 250
            .vout     = p_vout,
            .input    = VLC_OBJECT(p_resource->p_input),
            .fmt      = p_fmt,
            .dpb_size = dpb_size,
251 252
        };
        p_vout = vout_Request( p_resource->p_input, &cfg );
253 254 255 256 257
        if( !p_vout )
            return NULL;

        DisplayVoutTitle( p_resource, p_vout );

258
        vlc_mutex_lock( &p_resource->lock_hold );
259
        TAB_APPEND( p_resource->i_vout, p_resource->pp_vout, p_vout );
260
        vlc_mutex_unlock( &p_resource->lock_hold );
261 262 263 264 265 266 267

        return p_vout;
    }
    else
    {
        assert( p_vout );

268
        vlc_mutex_lock( &p_resource->lock_hold );
269 270
        TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
        const int i_vout_active = p_resource->i_vout;
271
        vlc_mutex_unlock( &p_resource->lock_hold );
272 273 274 275 276 277 278 279 280 281 282

        if( p_resource->p_vout_free || i_vout_active > 0 || !b_recycle )
        {
            if( b_recycle )
                msg_Dbg( p_resource->p_input, "detroying vout (already one saved or active)" );
            vout_CloseAndRelease( p_vout );
        }
        else
        {
            msg_Dbg( p_resource->p_input, "saving a free vout" );
            vout_Flush( p_vout, 1 );
283
            vout_FlushSubpictureChannel( p_vout, -1 );
284

285
            vout_configuration_t cfg = {
286 287 288 289
                .vout     = p_vout,
                .input    = NULL,
                .fmt      = p_fmt,
                .dpb_size = 0,
290 291
            };
            p_resource->p_vout_free = vout_Request( p_resource->p_input, &cfg );
292 293 294 295 296 297 298
        }
        return NULL;
    }
}
static vout_thread_t *HoldVout( input_resource_t *p_resource )
{
    /* TODO FIXME: p_resource->pp_vout order is NOT stable */
299
    vlc_mutex_lock( &p_resource->lock_hold );
300

301 302 303
    vout_thread_t *p_vout = p_resource->i_vout > 0 ? p_resource->pp_vout[0] : NULL;
    if( p_vout )
        vlc_object_hold( p_vout );
304

305
    vlc_mutex_unlock( &p_resource->lock_hold );
306 307 308

    return p_vout;
}
309 310 311

static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
                       size_t *pi_vout )
312 313 314 315 316 317
{
    vout_thread_t **pp_vout;

    *pi_vout = 0;
    *ppp_vout = NULL;

318
    vlc_mutex_lock( &p_resource->lock_hold );
319 320 321 322

    if( p_resource->i_vout <= 0 )
        goto exit;

323
    pp_vout = malloc( p_resource->i_vout * sizeof(*pp_vout) );
324 325 326 327 328 329 330 331 332 333 334 335 336
    if( !pp_vout )
        goto exit;

    *ppp_vout = pp_vout;
    *pi_vout = p_resource->i_vout;

    for( int i = 0; i < p_resource->i_vout; i++ )
    {
        pp_vout[i] = p_resource->pp_vout[i];
        vlc_object_hold( pp_vout[i] );
    }

exit:
337
    vlc_mutex_unlock( &p_resource->lock_hold );
338 339 340 341 342 343 344 345 346
}

/* */
static void DestroyAout( input_resource_t *p_resource )
{
    if( p_resource->p_aout )
        vlc_object_release( p_resource->p_aout );
    p_resource->p_aout = NULL;
}
347 348
static aout_instance_t *DetachAout( input_resource_t *p_resource )
{
349
    vlc_assert_locked( &p_resource->lock );
350 351
    vlc_mutex_lock( &p_resource->lock_hold );

352 353 354
    aout_instance_t *p_aout = p_resource->p_aout;
    p_resource->p_aout = NULL;

355 356
    vlc_mutex_unlock( &p_resource->lock_hold );

357 358 359
    return p_aout;
}

360 361
static aout_instance_t *RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
{
362
    vlc_assert_locked( &p_resource->lock );
363 364 365 366 367 368 369 370 371 372
    assert( p_resource->p_input );

    if( p_aout )
    {
        msg_Dbg( p_resource->p_input, "releasing aout" );
        vlc_object_release( p_aout );
        return NULL;
    }
    else
    {
373 374
        p_aout = p_resource->p_aout;
        if( !p_aout )
375 376
        {
            msg_Dbg( p_resource->p_input, "creating aout" );
377 378 379 380 381
            p_aout = aout_New( p_resource->p_input );

            vlc_mutex_lock( &p_resource->lock_hold );
            p_resource->p_aout = p_aout;
            vlc_mutex_unlock( &p_resource->lock_hold );
382 383 384 385 386 387
        }
        else
        {
            msg_Dbg( p_resource->p_input, "reusing aout" );
        }

388
        if( !p_aout )
389 390
            return NULL;

391 392 393 394
        vlc_object_detach( p_aout );
        vlc_object_attach( p_aout, p_resource->p_input );
        vlc_object_hold( p_aout );
        return p_aout;
395 396 397 398
    }
}
static aout_instance_t *HoldAout( input_resource_t *p_resource )
{
399
    vlc_mutex_lock( &p_resource->lock_hold );
400 401

    aout_instance_t *p_aout = p_resource->p_aout;
402 403
    if( p_aout )
        vlc_object_hold( p_aout );
404

405
    vlc_mutex_unlock( &p_resource->lock_hold );
406 407 408 409 410 411 412 413 414 415 416 417

    return p_aout;
}

/* */
input_resource_t *input_resource_New( void )
{
    input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
    if( !p_resource )
        return NULL;

    vlc_mutex_init( &p_resource->lock );
418
    vlc_mutex_init( &p_resource->lock_hold );
419 420 421 422 423 424 425 426 427
    return p_resource;
}

void input_resource_Delete( input_resource_t *p_resource )
{
    DestroySout( p_resource );
    DestroyVout( p_resource );
    DestroyAout( p_resource );

428
    vlc_mutex_destroy( &p_resource->lock_hold );
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    vlc_mutex_destroy( &p_resource->lock );
    free( p_resource );
}

void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
{
    vlc_mutex_lock( &p_resource->lock );

    if( p_resource->p_input && !p_input )
    {
        if( p_resource->p_aout )
            vlc_object_detach( p_resource->p_aout );

        assert( p_resource->i_vout == 0 );
        if( p_resource->p_vout_free )
            vlc_object_detach( p_resource->p_vout_free );

        if( p_resource->p_sout )
            vlc_object_detach( p_resource->p_sout );
    }

    /* */
    p_resource->p_input = p_input;

    vlc_mutex_unlock( &p_resource->lock );
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
input_resource_t *input_resource_Detach( input_resource_t *p_resource )
{
    input_resource_t *p_ret = input_resource_New();
    if( !p_ret )
        return NULL;

    vlc_mutex_lock( &p_resource->lock );
    assert( !p_resource->p_input );
    p_ret->p_sout = DetachSout( p_resource );
    p_ret->p_vout_free = DetachVout( p_resource );
    p_ret->p_aout = DetachAout( p_resource );
    vlc_mutex_unlock( &p_resource->lock );

    return p_ret;
}

472
vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
473 474 475
                                            vout_thread_t *p_vout,
                                            video_format_t *p_fmt, unsigned dpb_size,
                                            bool b_recycle )
476 477
{
    vlc_mutex_lock( &p_resource->lock );
478
    vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, dpb_size, b_recycle );
479 480 481 482 483 484 485 486
    vlc_mutex_unlock( &p_resource->lock );

    return p_ret;
}
vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
{
    return HoldVout( p_resource );
}
487 488 489

void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
                               size_t *pi_vout )
490 491 492
{
    HoldVouts( p_resource, ppp_vout, pi_vout );
}
493

494 495
void input_resource_TerminateVout( input_resource_t *p_resource )
{
496
    input_resource_RequestVout( p_resource, NULL, NULL, 0, false );
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
}
bool input_resource_HasVout( input_resource_t *p_resource )
{
    vlc_mutex_lock( &p_resource->lock );
    assert( !p_resource->p_input );
    const bool b_vout = p_resource->p_vout_free != NULL;
    vlc_mutex_unlock( &p_resource->lock );

    return b_vout;
}

/* */
aout_instance_t *input_resource_RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
{
    vlc_mutex_lock( &p_resource->lock );
    aout_instance_t *p_ret = RequestAout( p_resource, p_aout );
    vlc_mutex_unlock( &p_resource->lock );

    return p_ret;
}
aout_instance_t *input_resource_HoldAout( input_resource_t *p_resource )
{
519
    return HoldAout( p_resource );
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
}
/* */
sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
{
    vlc_mutex_lock( &p_resource->lock );
    sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
    vlc_mutex_unlock( &p_resource->lock );

    return p_ret;
}
void input_resource_TerminateSout( input_resource_t *p_resource )
{
    input_resource_RequestSout( p_resource, NULL, NULL );
}