FakeESOut.cpp 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * FakeESOut.cpp
 *****************************************************************************
 * Copyright © 2014-2015 VideoLAN and VLC Authors
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *****************************************************************************/
20 21 22 23 24

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

25 26
#include "FakeESOut.hpp"
#include "FakeESOutID.hpp"
27
#include "CommandsQueue.hpp"
28 29
#include <vlc_es_out.h>
#include <vlc_block.h>
30
#include <cassert>
31

32
using namespace adaptive;
33

34
FakeESOut::FakeESOut( es_out_t *es, CommandsQueue *queue )
35 36 37 38 39 40 41
    : real_es_out( es )
    , extrainfo( NULL )
    , commandsqueue( queue )
    , fakeesout( new es_out_t )
    , timestamps_offset( 0 )
    , timestamps_expected( 0 )
    , timestamps_check_done( false )
42 43 44 45 46 47 48 49
{
    fakeesout->pf_add = esOutAdd_Callback;
    fakeesout->pf_control = esOutControl_Callback;
    fakeesout->pf_del = esOutDel_Callback;
    fakeesout->pf_destroy = esOutDestroy_Callback;
    fakeesout->pf_send = esOutSend_Callback;
    fakeesout->p_sys = (es_out_sys_t*) this;

50
    vlc_mutex_init(&lock);
51 52 53 54 55 56 57 58 59 60 61 62
}

es_out_t * FakeESOut::getEsOut()
{
    return fakeesout;
}

FakeESOut::~FakeESOut()
{
    recycleAll();
    gc();

63
    delete fakeesout;
64
    vlc_mutex_destroy(&lock);
65 66
}

67 68 69 70 71 72 73 74 75
void FakeESOut::setExpectedTimestampOffset(mtime_t offset)
{
    vlc_mutex_lock(&lock);
    timestamps_offset = 0;
    timestamps_expected = offset;
    timestamps_check_done = false;
    vlc_mutex_unlock(&lock);
}

76 77
void FakeESOut::setTimestampOffset(mtime_t offset)
{
78
    vlc_mutex_lock(&lock);
79
    timestamps_offset = offset;
80
    timestamps_check_done = true;
81
    vlc_mutex_unlock(&lock);
82 83 84 85
}

void FakeESOut::setExtraInfoProvider( ExtraFMTInfoInterface *extra )
{
86
    vlc_mutex_lock(&lock);
87
    extrainfo = extra;
88
    vlc_mutex_unlock(&lock);
89 90
}

91
FakeESOutID * FakeESOut::createNewID( const es_format_t *p_fmt )
92 93 94
{
    es_format_t fmtcopy;
    es_format_Copy( &fmtcopy, p_fmt );
95
    fmtcopy.i_group = 0; /* Always ignore group for adaptive */
96
    fmtcopy.i_id = -1;
97

98 99
    vlc_mutex_lock(&lock);

100 101 102
    if( extrainfo )
        extrainfo->fillExtraFMTInfo( &fmtcopy );

103 104 105 106
    FakeESOutID *es_id = new (std::nothrow) FakeESOutID( this, &fmtcopy );
    if(likely(es_id))
        fakeesidlist.push_back( es_id );

107 108
    vlc_mutex_unlock(&lock);

109 110 111 112 113 114 115 116 117 118
    es_format_Clean( &fmtcopy );

    return es_id;
}

void FakeESOut::createOrRecycleRealEsID( FakeESOutID *es_id )
{
    std::list<FakeESOutID *>::iterator it;
    es_out_id_t *realid = NULL;

119 120
    vlc_mutex_lock(&lock);

121
    bool b_select = false;
122 123
    for( it=recycle_candidates.begin(); it!=recycle_candidates.end(); ++it )
    {
124 125
        FakeESOutID *cand = *it;
        if ( cand->isCompatible( es_id ) )
126
        {
127 128 129
            realid = cand->realESID();
            cand->setRealESID( NULL );
            delete *it;
130 131 132
            recycle_candidates.erase( it );
            break;
        }
133
        else if( cand->getFmt()->i_cat == es_id->getFmt()->i_cat && cand->realESID() )
134 135 136 137 138
        {
            /* We need to enforce same selection when not reused
               Otherwise the es will select any other compatible track
               and will end this in a activate/select loop when reactivating a track */
            es_out_Control( real_es_out, ES_OUT_GET_ES_STATE, cand->realESID(), &b_select );
139
            break;
140
        }
141 142
    }

143
    if( !realid )
144
    {
145
        realid = es_out_Add( real_es_out, es_id->getFmt() );
146 147 148
        if( b_select )
            es_out_Control( real_es_out, ES_OUT_SET_ES_STATE, realid, b_select );
    }
149

150
    es_id->setRealESID( realid );
151 152

    vlc_mutex_unlock(&lock);
153 154 155 156
}

mtime_t FakeESOut::getTimestampOffset() const
{
157 158 159 160
    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
    mtime_t time = timestamps_offset;
    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
    return time;
161 162 163 164 165 166
}

size_t FakeESOut::esCount() const
{
    size_t i_count = 0;
    std::list<FakeESOutID *>::const_iterator it;
167
    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
168 169 170
    for( it=fakeesidlist.begin(); it!=fakeesidlist.end(); ++it )
        if( (*it)->realESID() )
            i_count++;
171
    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
172 173
    return i_count;
}
174 175 176

void FakeESOut::schedulePCRReset()
{
177
    AbstractCommand *command = commandsqueue->factory()->creatEsOutControlResetPCRCommand();
178
    if( likely(command) )
179
        commandsqueue->Schedule( command );
180 181 182 183 184
}

void FakeESOut::scheduleAllForDeletion()
{
    std::list<FakeESOutID *>::const_iterator it;
185
    vlc_mutex_lock(&lock);
186 187 188 189 190
    for( it=fakeesidlist.begin(); it!=fakeesidlist.end(); ++it )
    {
        FakeESOutID *es_id = *it;
        if(!es_id->scheduledForDeletion())
        {
191
            AbstractCommand *command = commandsqueue->factory()->createEsOutDelCommand( es_id );
192 193
            if( likely(command) )
            {
194
                commandsqueue->Schedule( command );
195 196 197 198
                es_id->setScheduledForDeletion();
            }
        }
    }
199
    vlc_mutex_unlock(&lock);
200 201
}

202 203
void FakeESOut::recycleAll()
{
204
    /* Only used when demux is killed and commands queue is cancelled */
205 206
    commandsqueue->Abort( true );
    assert(commandsqueue->isEmpty());
207
    vlc_mutex_lock(&lock);
208
    recycle_candidates.splice( recycle_candidates.end(), fakeesidlist );
209
    vlc_mutex_unlock(&lock);
210 211 212 213
}

void FakeESOut::gc()
{
214
    vlc_mutex_lock(&lock);
215
    if( recycle_candidates.empty() )
216 217
    {
        vlc_mutex_unlock(&lock);
218
        return;
219
    }
220 221 222 223 224

    std::list<FakeESOutID *>::iterator it;
    for( it=recycle_candidates.begin(); it!=recycle_candidates.end(); ++it )
    {
        if( (*it)->realESID() )
225 226
        {
            es_out_Control( real_es_out, ES_OUT_SET_ES_STATE, (*it)->realESID(), false );
227
            es_out_Del( real_es_out, (*it)->realESID() );
228
        }
229 230 231
        delete *it;
    }
    recycle_candidates.clear();
232
    vlc_mutex_unlock(&lock);
233 234 235 236 237 238
}

bool FakeESOut::hasSelectedEs() const
{
    bool b_selected = false;
    std::list<FakeESOutID *>::const_iterator it;
239
    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
240 241 242 243 244 245
    for( it=fakeesidlist.begin(); it!=fakeesidlist.end() && !b_selected; ++it )
    {
        FakeESOutID *esID = *it;
        if( esID->realESID() )
            es_out_Control( real_es_out, ES_OUT_GET_ES_STATE, esID->realESID(), &b_selected );
    }
246
    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
247 248 249
    return b_selected;
}

250
bool FakeESOut::decodersDrained()
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
{
    bool b_drained = true;
    std::list<FakeESOutID *>::const_iterator it;
    vlc_mutex_lock(&lock);
    for( it=fakeesidlist.begin(); it!=fakeesidlist.end(); ++it )
    {
        FakeESOutID *esID = *it;
        if( esID->realESID() )
        {
            bool b_empty;
            es_out_Control( real_es_out, ES_OUT_GET_EMPTY, &b_empty );
            b_drained &= b_empty;
        }
    }
    vlc_mutex_unlock(&lock);
    return b_drained;
}

269 270
bool FakeESOut::restarting() const
{
271 272 273 274
    vlc_mutex_lock(const_cast<vlc_mutex_t *>(&lock));
    bool b = !recycle_candidates.empty();
    vlc_mutex_unlock(const_cast<vlc_mutex_t *>(&lock));
    return b;
275 276 277 278
}

void FakeESOut::recycle( FakeESOutID *id )
{
279
    vlc_mutex_lock(&lock);
280 281
    fakeesidlist.remove( id );
    recycle_candidates.push_back( id );
282
    vlc_mutex_unlock(&lock);
283 284 285 286 287 288 289
}

/* Static callbacks */
/* Always pass Fake ES ID to slave demuxes, it is just an opaque struct to them */
es_out_id_t * FakeESOut::esOutAdd_Callback(es_out_t *fakees, const es_format_t *p_fmt)
{
    FakeESOut *me = (FakeESOut *) fakees->p_sys;
290 291 292 293

    if( p_fmt->i_cat != VIDEO_ES && p_fmt->i_cat != AUDIO_ES && p_fmt->i_cat != SPU_ES )
        return NULL;

294 295
    /* Feed the slave demux/stream_Demux with FakeESOutID struct,
     * we'll create real ES later on main demux on execution */
296
    FakeESOutID *es_id = me->createNewID( p_fmt );
297 298
    if( likely(es_id) )
    {
299
        assert(!es_id->scheduledForDeletion());
300
        AbstractCommand *command = me->commandsqueue->factory()->createEsOutAddCommand( es_id );
301 302
        if( likely(command) )
        {
303
            me->commandsqueue->Schedule( command );
304 305 306 307 308 309 310 311 312 313
            return reinterpret_cast<es_out_id_t *>(es_id);
        }
        else
        {
            delete es_id;
        }
    }
    return NULL;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
void FakeESOut::checkTimestampsStart(mtime_t i_start)
{
    if( i_start == VLC_TS_INVALID )
        return;

    vlc_mutex_lock(&lock);
    if( !timestamps_check_done )
    {
        if( i_start < CLOCK_FREQ ) /* Starts 0 */
            timestamps_offset = timestamps_expected;
        timestamps_check_done = true;
    }
    vlc_mutex_unlock(&lock);
}

329 330 331 332
int FakeESOut::esOutSend_Callback(es_out_t *fakees, es_out_id_t *p_es, block_t *p_block)
{
    FakeESOut *me = (FakeESOut *) fakees->p_sys;
    FakeESOutID *es_id = reinterpret_cast<FakeESOutID *>( p_es );
333
    assert(!es_id->scheduledForDeletion());
334 335 336

    me->checkTimestampsStart( p_block->i_dts );

337 338 339 340 341 342 343
    mtime_t offset = me->getTimestampOffset();
    if( p_block->i_dts > VLC_TS_INVALID )
    {
        p_block->i_dts += offset;
        if( p_block->i_pts > VLC_TS_INVALID )
                p_block->i_pts += offset;
    }
344
    AbstractCommand *command = me->commandsqueue->factory()->createEsOutSendCommand( es_id, p_block );
345 346
    if( likely(command) )
    {
347
        me->commandsqueue->Schedule( command );
348 349 350 351 352 353 354 355 356
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}

void FakeESOut::esOutDel_Callback(es_out_t *fakees, es_out_id_t *p_es)
{
    FakeESOut *me = (FakeESOut *) fakees->p_sys;
    FakeESOutID *es_id = reinterpret_cast<FakeESOutID *>( p_es );
357
    AbstractCommand *command = me->commandsqueue->factory()->createEsOutDelCommand( es_id );
358
    if( likely(command) )
359 360
    {
        es_id->setScheduledForDeletion();
361
        me->commandsqueue->Schedule( command );
362
    }
363 364 365 366 367 368 369 370 371 372 373 374 375
}

int FakeESOut::esOutControl_Callback(es_out_t *fakees, int i_query, va_list args)
{
    FakeESOut *me = (FakeESOut *) fakees->p_sys;

    switch( i_query )
    {
        case ES_OUT_SET_PCR:
        case ES_OUT_SET_GROUP_PCR:
        {
            int i_group;
            if( i_query == ES_OUT_SET_GROUP_PCR )
376
                i_group = va_arg( args, int );
377 378
            else
                i_group = 0;
379
            int64_t  pcr = va_arg( args, int64_t );
Zhao Zhili's avatar
Zhao Zhili committed
380
            me->checkTimestampsStart( pcr );
381
            pcr += me->getTimestampOffset();
382
            AbstractCommand *command = me->commandsqueue->factory()->createEsOutControlPCRCommand( i_group, pcr );
383 384
            if( likely(command) )
            {
385
                me->commandsqueue->Schedule( command );
386 387 388 389 390
                return VLC_SUCCESS;
            }
        }
        break;

391 392 393
        case ES_OUT_SET_GROUP_META:
        {
            static_cast<void>(va_arg( args, int )); /* ignore group */
394
            const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
395 396 397 398 399 400 401 402 403
            AbstractCommand *command = me->commandsqueue->factory()->createEsOutMetaCommand( -1, p_meta );
            if( likely(command) )
            {
                me->commandsqueue->Schedule( command );
                return VLC_SUCCESS;
            }
        }
        break;

404 405 406
        /* For others, we don't have the delorean, so always lie */
        case ES_OUT_GET_ES_STATE:
        {
407
            static_cast<void>(va_arg( args, es_out_id_t * ));
408
            bool *pb = va_arg( args, bool * );
409
            *pb = true;
410
            return VLC_SUCCESS;
411
        }
412

413 414 415 416 417 418 419 420 421 422 423 424
        case ES_OUT_SET_ES:
        case ES_OUT_SET_ES_DEFAULT:
        case ES_OUT_SET_ES_STATE:
            return VLC_SUCCESS;
    }

    return VLC_EGENERIC;
}

void FakeESOut::esOutDestroy_Callback(es_out_t *fakees)
{
    FakeESOut *me = (FakeESOut *) fakees->p_sys;
425
    AbstractCommand *command = me->commandsqueue->factory()->createEsOutDestroyCommand();
426
    if( likely(command) )
427
        me->commandsqueue->Schedule( command );
428 429
}
/* !Static callbacks */