chromecast_demux.cpp 10.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * chromecast_demux.cpp: Chromecast demux filter module for vlc
 *****************************************************************************
4
 * Copyright © 2015-2016 VideoLAN
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 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * Authors: Steve Lhomme <robux4@videolabs.io>
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

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

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_demux.h>

#include "chromecast_common.h"

#include <new>

struct demux_sys_t
{
    demux_sys_t(demux_t * const demux, chromecast_common * const renderer)
        :p_demux(demux)
        ,p_renderer(renderer)
44
        ,i_length(-1)
45
        ,m_enabled( true )
46
        ,m_startTime( VLC_TS_INVALID )
47
    {
48 49
        vlc_meta_t *p_meta = vlc_meta_New();
        if( likely(p_meta != NULL) )
50
        {
51 52 53
            input_item_t *p_item = demux->p_next->p_input ?
                                   input_GetItem( demux->p_next->p_input ) : NULL;
            if( p_item )
54
            {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
                /* Favor Meta from the input item of the input_thread since
                 * it's pre-processed by the meta fetcher */
                for( int i = 0; i < VLC_META_TYPE_COUNT; ++i )
                {
                    char *psz_meta = input_item_GetMeta( p_item, (vlc_meta_type_t)i );
                    if( psz_meta )
                    {
                        vlc_meta_Set( p_meta, (vlc_meta_type_t)i, psz_meta );
                        free( psz_meta );
                    }
                }
                if( vlc_meta_Get( p_meta, vlc_meta_Title ) == NULL )
                {
                    char *psz_meta = input_item_GetName( p_item );
                    if( psz_meta )
                    {
                        vlc_meta_Set( p_meta, vlc_meta_Title, psz_meta );
                        free( psz_meta );
                    }
                }
                p_renderer->pf_set_meta( p_renderer->p_opaque, p_meta );
76
            }
77 78 79 80
            else if (demux_Control( demux->p_next, DEMUX_GET_META, p_meta) == VLC_SUCCESS)
                p_renderer->pf_set_meta( p_renderer->p_opaque, p_meta );
            else
                vlc_meta_Delete( p_meta );
81
        }
82 83
        if (demux_Control( demux->p_next, DEMUX_CAN_SEEK, &canSeek ) != VLC_SUCCESS)
            canSeek = false;
84 85 86 87 88 89 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

        int i_current_title;
        if( demux_Control( p_demux->p_next, DEMUX_GET_TITLE,
                           &i_current_title ) == VLC_SUCCESS )
        {
            input_title_t** pp_titles;
            int i_nb_titles, i_title_offset, i_chapter_offset;
            if( demux_Control( demux->p_next, DEMUX_GET_TITLE_INFO, &pp_titles,
                              &i_nb_titles, &i_title_offset,
                              &i_chapter_offset ) == VLC_SUCCESS )
            {
                int64_t i_longest_duration = 0;
                int i_longest_title = 0;
                bool b_is_interactive = false;
                for( int i = 0 ; i < i_nb_titles; ++i )
                {
                    if( pp_titles[i]->i_length > i_longest_duration )
                    {
                        i_longest_duration = pp_titles[i]->i_length;
                        i_longest_title = i;
                    }
                    if( i_current_title == i &&
                            pp_titles[i]->i_flags & INPUT_TITLE_INTERACTIVE )
                    {
                        b_is_interactive = true;
                    }
                    vlc_input_title_Delete( pp_titles[i] );
                }
                free( pp_titles );

                if( b_is_interactive == true )
                {
                    demux_Control( p_demux->p_next, DEMUX_SET_TITLE,
                                   i_longest_title );
                    p_demux->info.i_update = p_demux->p_next->info.i_update;
                }
            }
        }
122 123 124 125
    }

    ~demux_sys_t()
    {
126
        if( p_renderer )
127
            p_renderer->pf_set_meta( p_renderer->p_opaque, NULL );
128 129
    }

130 131 132 133 134
    void setPauseState(bool paused)
    {
        p_renderer->pf_set_pause_state( p_renderer->p_opaque, paused );
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148
    /**
     * @brief getPlaybackTime
     * @return the current playback time on the device or VLC_TS_INVALID if unknown
     */
    mtime_t getPlaybackTime()
    {
        return p_renderer->pf_get_time( p_renderer->p_opaque );
    }

    double getPlaybackPosition()
    {
        return p_renderer->pf_get_position( p_renderer->p_opaque );
    }

149 150 151 152 153
    void setCanSeek( bool canSeek )
    {
        this->canSeek = canSeek;
    }

154 155
    void setLength( mtime_t length )
    {
156
        this->i_length = length;
157 158 159
        p_renderer->pf_set_length( p_renderer->p_opaque, length );
    }

160 161
    int Demux()
    {
162 163 164
        if ( !m_enabled )
            return demux_Demux( p_demux->p_next );

165 166
        p_renderer->pf_pace( p_renderer->p_opaque );

167 168 169 170 171 172 173
        if( m_startTime == VLC_TS_INVALID )
        {
            if( demux_Control( p_demux->p_next, DEMUX_GET_TIME,
                               &m_startTime ) == VLC_SUCCESS )
                p_renderer->pf_set_initial_time( p_renderer->p_opaque,
                                                 m_startTime );
        }
174

175 176 177
        return demux_Demux( p_demux->p_next );
    }

178 179
    int Control( demux_t *p_demux_filter, int i_query, va_list args )
    {
180 181 182
        if( !m_enabled && i_query != DEMUX_FILTER_ENABLE )
            return demux_vaControl( p_demux_filter->p_next, i_query, args );

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 208 209 210 211 212 213 214 215 216 217 218 219 220
        switch (i_query)
        {
        case DEMUX_GET_POSITION:
            *va_arg( args, double * ) = getPlaybackPosition();
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
            *va_arg(args, int64_t *) = getPlaybackTime();
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
        {
            int ret;
            va_list ap;

            va_copy( ap, args );
            ret = demux_vaControl( p_demux_filter->p_next, i_query, args );
            if( ret == VLC_SUCCESS )
                setLength( *va_arg( ap, int64_t * ) );
            va_end( ap );
            return ret;
        }

        case DEMUX_CAN_SEEK:
        {
            int ret;
            va_list ap;

            va_copy( ap, args );
            ret = demux_vaControl( p_demux_filter->p_next, i_query, args );
            if( ret == VLC_SUCCESS )
                setCanSeek( *va_arg( ap, bool* ) );
            va_end( ap );
            return ret;
        }

        case DEMUX_SET_POSITION:
        {
Thomas Guillem's avatar
Thomas Guillem committed
221
            m_startTime = VLC_TS_INVALID;
222 223 224 225 226
            break;
        }

        case DEMUX_SET_TIME:
        {
Thomas Guillem's avatar
Thomas Guillem committed
227
            m_startTime = VLC_TS_INVALID;
228 229 230 231 232 233 234 235 236 237 238 239 240
            break;
        }
        case DEMUX_SET_PAUSE_STATE:
        {
            va_list ap;

            va_copy( ap, args );
            int paused = va_arg( ap, int );
            va_end( ap );

            setPauseState( paused != 0 );
            break;
        }
241 242 243 244 245 246 247 248 249
        case DEMUX_FILTER_ENABLE:
            p_renderer = static_cast<chromecast_common *>(
                        var_InheritAddress( p_demux, CC_SHARED_VAR_NAME ) );
            m_enabled = true;
            return VLC_SUCCESS;

        case DEMUX_FILTER_DISABLE:
            m_enabled = false;
            p_renderer = NULL;
250
            m_startTime = VLC_TS_INVALID;
251
            return VLC_SUCCESS;
252 253 254 255 256 257 258 259
        case DEMUX_CAN_PAUSE:
        case DEMUX_CAN_CONTROL_PACE:
        {
            int ret;
            va_list ap;

            va_copy( ap, args );
            ret = demux_vaControl( p_demux_filter->p_next, i_query, args );
260
            if( ret != VLC_SUCCESS )
261 262 263 264 265 266 267 268 269 270 271
                *va_arg( ap, bool* ) = false;
            va_end( ap );
            return VLC_SUCCESS;
        }
        case DEMUX_GET_PTS_DELAY:
        {
            int ret;
            va_list ap;

            va_copy( ap, args );
            ret = demux_vaControl( p_demux_filter->p_next, i_query, args );
272
            if( ret != VLC_SUCCESS )
273 274 275 276
                *va_arg( ap, int64_t* ) = 0;
            va_end( ap );
            return VLC_SUCCESS;
        }
277 278 279 280 281
        }

        return demux_vaControl( p_demux_filter->p_next, i_query, args );
    }

282 283
protected:
    demux_t     * const p_demux;
284
    chromecast_common  * p_renderer;
285 286
    mtime_t       i_length;
    bool          canSeek;
287
    bool          m_enabled;
288
    mtime_t       m_startTime;
289 290 291 292 293 294 295 296 297 298 299
};

static int Demux( demux_t *p_demux_filter )
{
    demux_sys_t *p_sys = p_demux_filter->p_sys;

    return p_sys->Demux();
}

static int Control( demux_t *p_demux_filter, int i_query, va_list args)
{
300 301
    demux_sys_t *p_sys = p_demux_filter->p_sys;

302
    return p_sys->Control( p_demux_filter, i_query, args );
303 304
}

305 306 307
int Open(vlc_object_t *p_this)
{
    demux_t *p_demux = reinterpret_cast<demux_t*>(p_this);
308
    chromecast_common *p_renderer = static_cast<chromecast_common *>(
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
                var_InheritAddress( p_demux, CC_SHARED_VAR_NAME ) );
    if ( p_renderer == NULL )
    {
        msg_Warn( p_this, "using Chromecast demuxer with no sout" );
        return VLC_ENOOBJ;
    }

    demux_sys_t *p_sys = new(std::nothrow) demux_sys_t( p_demux, p_renderer );
    if (unlikely(p_sys == NULL))
        return VLC_ENOMEM;

    p_demux->p_sys = p_sys;
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;

    return VLC_SUCCESS;
}

void Close(vlc_object_t *p_this)
{
    demux_t *p_demux = reinterpret_cast<demux_t*>(p_this);
    demux_sys_t *p_sys = p_demux->p_sys;

    delete p_sys;
}

vlc_module_begin ()
    set_shortname( "cc_demux" )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
339
    set_description( N_( "Chromecast demux wrapper" ) )
340 341 342 343
    set_capability( "demux_filter", 0 )
    add_shortcut( "cc_demux" )
    set_callbacks( Open, Close )
vlc_module_end ()