Exemplo n.º 1
0
static void SysCleanup( stream_sys_t *p_sys )
{
    if ( p_sys->sms.i_size )
    {
        FOREACH_ARRAY( sms_stream_t *sms, p_sys->sms );
            sms_Free( sms );
        FOREACH_END();
        ARRAY_RESET( p_sys->sms );
    }
    ARRAY_RESET( p_sys->sms_selected );
    if ( p_sys->playback.init.p_datachunk )
        chunk_Free( p_sys->playback.init.p_datachunk );
    free( p_sys->download.base_url );
}
Exemplo n.º 2
0
static void Close( vlc_object_t *p_this )
{
    stream_t *s = (stream_t*)p_this;
    stream_sys_t *p_sys = s->p_sys;

    vlc_mutex_lock( &p_sys->download.lock_wait );
    p_sys->b_close = true;
    /* Negate the condition variable's predicate */
    for( int i = 0; i < 3; i++ )
        p_sys->download.lead[i] = 0;
    p_sys->playback.toffset = 0;
    vlc_cond_signal(&p_sys->download.wait);
    vlc_mutex_unlock( &p_sys->download.lock_wait );

    vlc_join( p_sys->thread, NULL );
    vlc_mutex_destroy( &p_sys->download.lock_wait );
    vlc_cond_destroy( &p_sys->download.wait );

    /* Free sms streams */
    sms_stream_t *sms;
    for( int i = 0; i < vlc_array_count( p_sys->sms_streams ); i++ )
    {
        sms = vlc_array_item_at_index( p_sys->sms_streams, i );
        if( sms )
            sms_Free( sms );
    }
    /* Free downloaded chunks */
    chunk_t *chunk;
    for( int i = 0; i < vlc_array_count( p_sys->init_chunks ); i++ )
    {
        chunk = vlc_array_item_at_index( p_sys->init_chunks, i );
        chunk_Free( chunk );
    }

    sms_queue_free( p_sys->bws );
    vlc_array_destroy( p_sys->sms_streams );
    vlc_array_destroy( p_sys->selected_st );
    vlc_array_destroy( p_sys->download.chunks );
    vlc_array_destroy( p_sys->init_chunks );

    free( p_sys->base_url );
    free( p_sys );
}
Exemplo n.º 3
0
static void SysCleanup( stream_sys_t *p_sys )
{
    if ( p_sys->sms_streams )
    {
        for ( int i=0; i< p_sys->sms_streams->i_count ; i++ )
            sms_Free( p_sys->sms_streams->pp_elems[i] );
        vlc_array_destroy( p_sys->sms_streams );
    }
    vlc_array_destroy( p_sys->selected_st );
    vlc_array_destroy( p_sys->download.chunks );
    if ( p_sys->init_chunks )
    {
        for ( int i=0; i< p_sys->init_chunks->i_count ; i++ )
            chunk_Free( p_sys->init_chunks->pp_elems[i] );
        vlc_array_destroy( p_sys->init_chunks );
    }
    sms_queue_free( p_sys->bws );
    free( p_sys->base_url );
}
Exemplo n.º 4
0
static int parse_Manifest( stream_t *s )
{
    stream_sys_t *p_sys = s->p_sys;
    xml_reader_t *vlc_reader = NULL;
    int type = UNKNOWN_ES;
    const char *name, *value;
    stream_t *st = s->p_source;
    msg_Dbg( s, "Manifest parsing\n" );

    vlc_reader = xml_ReaderCreate( st, st );
    if( !vlc_reader )
    {
        msg_Err( s, "Failed to open source for parsing" );
        return VLC_EGENERIC;
    }

    const char *node;
    uint8_t *WaveFormatEx;
    sms_stream_t *sms = NULL;
    quality_level_t *ql = NULL;
    custom_attrs_t *cp = NULL;
    int64_t start_time = 0, duration = 0;
    int64_t computed_start_time = 0, computed_duration = 0;
    unsigned next_track_id = 1;
    int loop_count = 0;
    bool b_weird = false;
    int ret = VLC_SUCCESS;

#define TIMESCALE 10000000
    while( (type = xml_ReaderNextNode( vlc_reader, &node )) > 0 )
    {
        switch( type )
        {
            case XML_READER_STARTELEM:

                if( !strcmp( node, "SmoothStreamingMedia" ) )
                {
                    while( (name = xml_ReaderNextAttr( vlc_reader, &value )) )
                    {
                        if( !strcmp( name, "Duration" ) )
                            p_sys->vod_duration = strtoull( value, NULL, 10 );
                        else if( !strcmp( name, "TimeScale" ) )
                            p_sys->timescale = strtoul( value, NULL, 10 );
                        else if ( !strcmp( name, "LookAheadFragmentCount" ) )
                            p_sys->download.lookahead_count = strtoul( value, NULL, 10 );
                    }
                    if( !p_sys->timescale )
                        p_sys->timescale = TIMESCALE;
                }
                else if( !strcmp( node, "StreamIndex" ) )
                {
                    sms_Free( sms );
                    sms = sms_New();
                    if( unlikely( !sms ) )
                    {
                        ret = VLC_ENOMEM;
                        goto cleanup;
                    }
                    sms->id = next_track_id;
                    next_track_id++;

                    while( (name = xml_ReaderNextAttr( vlc_reader, &value )) )
                    {
                        if( !strcmp( name, "Type" ) )
                        {
                            if( !strcmp( value, "video" ) )
                                sms->type = VIDEO_ES;
                            else if( !strcmp( value, "audio" ) )
                                sms->type = AUDIO_ES;
                            else if( !strcmp( value, "text" ) )
                                sms->type = SPU_ES;
                        }

                        else if( !strcmp( name, "Name" ) )
                            sms->name = strdup( value );
                        else if( !strcmp( name, "TimeScale" ) )
                            sms->timescale = strtoull( value, NULL, 10 );
                        else if( !strcmp( name, "FourCC" ) )
                            sms->default_FourCC =
                                VLC_FOURCC( value[0], value[1], value[2], value[3] );

                        else if( !strcmp( name, "Chunks" ) )
                        {
                            sms->vod_chunks_nb = strtoul( value, NULL, 10 );
                            if( sms->vod_chunks_nb == 0 ) /* live */
                                sms->vod_chunks_nb = UINT32_MAX;
                        }

                        else if( !strcmp( name, "QualityLevels" ) )
                            sms->qlevel_nb = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "Url" ) )
                            sms->url_template = strdup(value);
                    }

                    if( !sms->timescale )
                        sms->timescale = TIMESCALE;

                    if( !sms->name )
                    {
                        if( sms->type == VIDEO_ES )
                            sms->name = strdup( "video" );
                        else if( sms->type == AUDIO_ES )
                            sms->name = strdup( "audio" );
                        else if( sms->type == SPU_ES )
                            sms->name = strdup( "text" );
                    }
                }
                else if ( !strcmp( node, "CustomAttributes" ) )
                {
                    if (!sms || !ql || cp)
                        break;
                    cp = (custom_attrs_t *) calloc( 1, sizeof(*cp) );
                    if( unlikely( !cp ) )
                    {
                        ret = VLC_ENOMEM;
                        goto cleanup;
                    }
                }
                else if ( !strcmp( node, "Attribute" ) )
                {
                    if (!sms || !ql || !cp)
                        break;
                    while( (name = xml_ReaderNextAttr( vlc_reader, &value )) )
                    {
                        if( !strcmp( name, "Name" ) && !cp->psz_key )
                            cp->psz_key = strdup( value );
                        else
                        if( !strcmp( name, "Value" ) && !cp->psz_value )
                            cp->psz_value = strdup( value );
                    }
                }
                else if( !strcmp( node, "QualityLevel" ) )
                {
                    if ( !sms )
                        break;

                    ql = ql_New();
                    if( !ql )
                    {
                        ret = VLC_ENOMEM;
                        goto cleanup;
                    }

                    while( (name = xml_ReaderNextAttr( vlc_reader, &value )) )
                    {
                        if( !strcmp( name, "Index" ) )
                            ql->Index = strtol( value, NULL, 10 );
                        else if( !strcmp( name, "Bitrate" ) )
                            ql->Bitrate = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "PacketSize" ) )
                            ql->nBlockAlign = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "FourCC" ) )
                            ql->FourCC = VLC_FOURCC( value[0], value[1],
                                                     value[2], value[3] );
                        else if( !strcmp( name, "CodecPrivateData" ) )
                            ql->CodecPrivateData = strdup( value );
                        else if( !strcmp( name, "WaveFormatEx" ) )
                        {
                            WaveFormatEx = decode_string_hex_to_binary( value );
                            uint16_t data_len = ((uint16_t *)WaveFormatEx)[8];
                            ql->CodecPrivateData = strndup( value + 36, data_len * 2 );

                            uint16_t wf_tag = ((uint16_t *)WaveFormatEx)[0];
                            wf_tag_to_fourcc( wf_tag, &ql->FourCC, NULL );

                            ql->Channels = ((uint16_t *)WaveFormatEx)[1];
                            ql->SamplingRate = ((uint32_t *)WaveFormatEx)[1];
                            ql->nBlockAlign = ((uint16_t *)WaveFormatEx)[6];
                            ql->BitsPerSample = ((uint16_t *)WaveFormatEx)[7];
                            free( WaveFormatEx );
                        }
                        else if( !strcmp( name, "MaxWidth" ) || !strcmp( name, "Width" ) )
                            ql->MaxWidth = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "MaxHeight" ) || !strcmp( name, "Height" ) )
                            ql->MaxHeight = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "Channels" ) )
                            ql->Channels = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "SamplingRate" ) )
                            ql->SamplingRate = strtoul( value, NULL, 10 );
                        else if( !strcmp( name, "BitsPerSample" ) )
                            ql->BitsPerSample = strtoul( value, NULL, 10 );
                    }

                    ARRAY_APPEND( sms->qlevels, ql );
                }
                else if ( !strcmp( node, "Content" ) && sms && !sms->url_template )
                {
                    /* empty(@Url) && ./Content == manifest embedded content */
                    sms_Free( sms );
                    sms = NULL;
                }
                else if( !strcmp( node, "c" ) )
                {
                    if ( !sms )
                        break;
                    loop_count++;
                    start_time = duration = -1;
                    while( (name = xml_ReaderNextAttr( vlc_reader, &value )) )
                    {
                        if( !strcmp( name, "t" ) )
                            start_time = strtoll( value, NULL, 10 );
                        if( !strcmp( name, "d" ) )
                            duration = strtoll( value, NULL, 10 );
                    }
                    if( start_time == -1 )
                    {
                        assert( duration != -1 );
                        computed_start_time += computed_duration;
                        computed_duration = duration;
                    }
                    else if( duration == -1 )
                    {
                        assert( start_time != -1 );
                        /* Handle weird Manifests which give only the start time
                         * of the first segment. In those cases, we have to look
                         * at the start time of the second segment to compute
                         * the duration of the first one. */
                        if( loop_count == 1 )
                        {
                            b_weird = true;
                            computed_start_time = start_time;
                            continue;
                        }

                        computed_duration = start_time - computed_start_time;
                        if( !b_weird )
                            computed_start_time = start_time;
                    }
                    else
                    {
                        if( b_weird )
                            computed_duration = start_time - computed_start_time;
                        else
                        {
                            computed_start_time = start_time;
                            computed_duration = duration;
                        }
                    }

                    if( unlikely( chunk_AppendNew( sms, computed_duration,
                                        computed_start_time ) == NULL ) )
                    {
                        ret = VLC_ENOMEM;
                        goto cleanup;
                    }
                    if( b_weird && start_time != -1 )
                        computed_start_time = start_time;
                }
                break;

            case XML_READER_ENDELEM:
                if ( !strcmp( node, "CustomAttributes" ) )
                {
                    if ( cp )
                    {
                        ARRAY_APPEND(ql->custom_attrs, cp);
                        cp = NULL;
                    }
                }
                else if ( !strcmp( node, "Attribute" ) )
                {
                    if( !cp->psz_key || !cp->psz_value )
                    {
                        cleanup_attributes( &cp );
                    }
                }
                else if( strcmp( node, "StreamIndex" ) )
                    break;
                else if ( sms )
                {
                    ARRAY_APPEND( p_sys->sms, sms );

                    computed_start_time = 0;
                    computed_duration = 0;
                    loop_count = 0;
                    if( b_weird && !chunk_AppendNew( sms, computed_duration, computed_start_time ) )
                    {
                        ret = VLC_ENOMEM;
                        goto cleanup;
                    }

                    b_weird = false;

                    if( sms->qlevel_nb == 0 )
                        sms->qlevel_nb = sms->qlevels.i_size;

                    sms = NULL;
                }
                break;

            case XML_READER_TEXT:
                break;
            default:
                ret = VLC_EGENERIC;
                goto cleanup;
        }
    }
#undef TIMESCALE

cleanup:
    cleanup_attributes( &cp );
    sms_Free( sms );
    xml_ReaderDelete( vlc_reader );

    return ret;
}