VLCMedia.cpp 3.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * VLCMedia.cpp: Binding for libvlc_media
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *****************************************************************************/

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
23
#include "VLCMedia.h"
24
#include "VLCInstance.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
25

26 27 28
//Allow PRId64 to be defined:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
29
#include <cstdio>
30

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
31 32
using namespace LibVLCpp;

33
Media::Media(LibVLCpp::Instance* instance, const char *filename )
34
    : m_tracks( NULL )
35
    , m_nbTracks( 0 )
36
    , m_mrl( NULL )
37

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
38
{
39
    m_internalPtr = libvlc_media_new_location( *instance, filename);
40 41
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
42 43
Media::~Media()
{
44
    if ( m_tracks != NULL )
45
        libvlc_media_tracks_release( m_tracks, m_nbTracks );
46
    libvlc_media_release( m_internalPtr );
47
    free( m_mrl );
48 49
}

50 51
void
Media::addOption( const char* opt )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
52
{
53
    libvlc_media_add_option_flag( m_internalPtr, opt, libvlc_media_option_trusted );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
54 55
}

56 57
void
Media::setVideoLockCallback( void* callback )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
58 59
{
    char    param[64];
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
    sprintf( param, ":sout-smem-video-prerender-callback=%" PRIdPTR, (intptr_t)callback );
61
    addOption(param);
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
62 63
}

64 65
void
Media::setVideoUnlockCallback( void* callback )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
66 67
{
    char    param[64];
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
68
    sprintf( param, ":sout-smem-video-postrender-callback=%" PRIdPTR, (intptr_t)callback );
69
    addOption( param );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
70 71
}

72 73
void
Media::setAudioLockCallback( void* callback )
74 75
{
    char    param[64];
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
76
    sprintf( param, ":sout-smem-audio-prerender-callback=%" PRIdPTR, (intptr_t)callback );
77 78 79
    addOption(param);
}

80 81
void
Media::setAudioUnlockCallback( void* callback )
82 83
{
    char    param[64];
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
84
    sprintf( param, ":sout-smem-audio-postrender-callback=%" PRIdPTR, (intptr_t)callback );
85 86 87
    addOption( param );
}

88 89
void
Media::setVideoDataCtx( void* dataCtx )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
90 91
{
    char    param[64];
92

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
93
    sprintf( param, ":sout-smem-video-data=%" PRIdPTR, (intptr_t)dataCtx );
94
    addOption( param );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
95 96
}

97 98
void
Media::setAudioDataCtx( void* dataCtx )
99 100 101
{
    char    param[64];

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102
    sprintf( param, ":sout-smem-audio-data=%" PRIdPTR, (intptr_t)dataCtx );
103 104 105
    addOption( param );
}

106 107
const char*
Media::mrl()
108
{
109 110 111
    if ( m_mrl == NULL )
        m_mrl = libvlc_media_get_mrl( m_internalPtr );
    return m_mrl;
112
}
113 114 115 116 117 118 119 120 121 122

void
Media::parse()
{
    libvlc_media_parse( *this );
}

void
Media::fetchTrackInfo()
{
123
    m_nbTracks = libvlc_media_tracks_get( *this, &m_tracks );
124 125 126 127 128 129
}

unsigned int
Media::videoCodec() const
{
    for ( int i = 0; i < m_nbTracks; ++i )
130 131
        if ( m_tracks[i]->i_type == libvlc_track_video )
            return m_tracks[i]->i_codec;
132 133 134 135 136 137 138
    return 0;
}

unsigned int
Media::audioCodec() const
{
    for ( int i = 0; i < m_nbTracks; ++i )
139 140
        if ( m_tracks[i]->i_type == libvlc_track_video )
            return m_tracks[i]->i_codec;
141 142
    return 0;
}