FakeESOut.cpp 9.31 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 27 28
#include "FakeESOut.hpp"
#include "FakeESOutID.hpp"
#include <vlc_es_out.h>
#include <vlc_block.h>
29
#include <cassert>
30

31
using namespace adaptive;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

FakeESOut::FakeESOut( es_out_t *es, CommandsFactory *factory )
{
    real_es_out = es;
    fakeesout = new es_out_t;

    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;

    commandsFactory = factory;
    timestamps_offset = 0;
    extrainfo = NULL;
}

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

FakeESOut::~FakeESOut()
{
    delete commandsFactory;

    recycleAll();
    gc();

62
    delete fakeesout;
63 64 65 66 67 68 69 70 71 72 73 74
}

void FakeESOut::setTimestampOffset(mtime_t offset)
{
    timestamps_offset = offset;
}

void FakeESOut::setExtraInfoProvider( ExtraFMTInfoInterface *extra )
{
    extrainfo = extra;
}

75
FakeESOutID * FakeESOut::createNewID( const es_format_t *p_fmt )
76 77 78 79
{
    es_format_t fmtcopy;
    es_format_Init( &fmtcopy, 0, 0 );
    es_format_Copy( &fmtcopy, p_fmt );
80
    fmtcopy.i_group = 0; /* Always ignore group for adaptive */
81 82 83 84

    if( extrainfo )
        extrainfo->fillExtraFMTInfo( &fmtcopy );

85 86 87 88 89 90 91 92 93 94 95 96 97 98
    FakeESOutID *es_id = new (std::nothrow) FakeESOutID( this, &fmtcopy );
    if(likely(es_id))
        fakeesidlist.push_back( es_id );

    es_format_Clean( &fmtcopy );

    return es_id;
}

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

99
    bool b_select = false;
100 101
    for( it=recycle_candidates.begin(); it!=recycle_candidates.end(); ++it )
    {
102 103
        FakeESOutID *cand = *it;
        if ( cand->isCompatible( es_id ) )
104
        {
105 106 107
            realid = cand->realESID();
            cand->setRealESID( NULL );
            delete *it;
108 109 110
            recycle_candidates.erase( it );
            break;
        }
111 112 113 114 115 116 117
        else if( cand->getFmt()->i_cat == es_id->getFmt()->i_cat )
        {
            /* 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 );
        }
118 119
    }

120
    if( !realid )
121
    {
122
        realid = es_out_Add( real_es_out, es_id->getFmt() );
123 124 125
        if( b_select )
            es_out_Control( real_es_out, ES_OUT_SET_ES_STATE, realid, b_select );
    }
126

127
    es_id->setRealESID( realid );
128 129 130 131
}

mtime_t FakeESOut::getTimestampOffset() const
{
132
    return timestamps_offset;
133 134 135 136 137 138 139 140 141 142 143
}

size_t FakeESOut::esCount() const
{
    size_t i_count = 0;
    std::list<FakeESOutID *>::const_iterator it;
    for( it=fakeesidlist.begin(); it!=fakeesidlist.end(); ++it )
        if( (*it)->realESID() )
            i_count++;
    return i_count;
}
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

void FakeESOut::schedulePCRReset()
{
    AbstractCommand *command = commandsFactory->creatEsOutControlResetPCRCommand();
    if( likely(command) )
        commandsqueue.Schedule( command );
}

void FakeESOut::scheduleAllForDeletion()
{
    std::list<FakeESOutID *>::const_iterator it;
    for( it=fakeesidlist.begin(); it!=fakeesidlist.end(); ++it )
    {
        FakeESOutID *es_id = *it;
        if(!es_id->scheduledForDeletion())
        {
            AbstractCommand *command = commandsFactory->createEsOutDelCommand( es_id );
            if( likely(command) )
            {
                commandsqueue.Schedule( command );
                es_id->setScheduledForDeletion();
            }
        }
    }
}

170 171
void FakeESOut::recycleAll()
{
172 173 174
    /* Only used when demux is killed and commands queue is cancelled */
    commandsqueue.Abort( true );
    assert(commandsqueue.isEmpty());
175 176 177 178 179 180 181 182 183 184 185 186
    recycle_candidates.splice( recycle_candidates.end(), fakeesidlist );
}

void FakeESOut::gc()
{
    if( recycle_candidates.empty() )
        return;

    std::list<FakeESOutID *>::iterator it;
    for( it=recycle_candidates.begin(); it!=recycle_candidates.end(); ++it )
    {
        if( (*it)->realESID() )
187 188
        {
            es_out_Control( real_es_out, ES_OUT_SET_ES_STATE, (*it)->realESID(), false );
189
            es_out_Del( real_es_out, (*it)->realESID() );
190
        }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        delete *it;
    }
    recycle_candidates.clear();
}

bool FakeESOut::hasSelectedEs() const
{
    bool b_selected = false;
    std::list<FakeESOutID *>::const_iterator it;
    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 );
    }
    return b_selected;
}

bool FakeESOut::restarting() const
{
    return !recycle_candidates.empty();
}

void FakeESOut::recycle( FakeESOutID *id )
{
    fakeesidlist.remove( id );
    recycle_candidates.push_back( id );
}

/* 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;
    /* Feed the slave demux/stream_Demux with FakeESOutID struct,
     * we'll create real ES later on main demux on execution */
227
    FakeESOutID *es_id = me->createNewID( p_fmt );
228 229
    if( likely(es_id) )
    {
230 231
        assert(!es_id->scheduledForDeletion());
        AbstractCommand *command = me->commandsFactory->createEsOutAddCommand( es_id );
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        if( likely(command) )
        {
            me->commandsqueue.Schedule( command );
            return reinterpret_cast<es_out_id_t *>(es_id);
        }
        else
        {
            delete es_id;
        }
    }
    return NULL;
}

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 );
249
    assert(!es_id->scheduledForDeletion());
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    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;
    }
    AbstractCommand *command = me->commandsFactory->createEsOutSendCommand( es_id, p_block );
    if( likely(command) )
    {
        me->commandsqueue.Schedule( command );
        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 );
    AbstractCommand *command = me->commandsFactory->createEsOutDelCommand( es_id );
    if( likely(command) )
272
    {
273
        me->commandsqueue.Schedule( command );
274 275
        es_id->setScheduledForDeletion();
    }
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
}

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 )
                i_group = static_cast<int>(va_arg( args, int ));
            else
                i_group = 0;
            int64_t  pcr = static_cast<int64_t>(va_arg( args, int64_t ));
            pcr += me->getTimestampOffset();
            AbstractCommand *command = me->commandsFactory->createEsOutControlPCRCommand( i_group, pcr );
            if( likely(command) )
            {
                me->commandsqueue.Schedule( command );
                return VLC_SUCCESS;
            }
        }
        break;

        /* For others, we don't have the delorean, so always lie */
        case ES_OUT_GET_ES_STATE:
        {
            static_cast<void*>(va_arg( args, es_out_id_t * ));
            bool *pb = static_cast<bool *>(va_arg( args, bool * ));
            *pb = true;
            // ft
        }
        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;
    AbstractCommand *command = me->commandsFactory->createEsOutDestroyCommand();
    if( likely(command) )
        me->commandsqueue.Schedule( command );
}
/* !Static callbacks */