mediacontrol_audio_video.c 9.87 KB
Newer Older
zorglub's avatar
zorglub committed
1 2 3
/*****************************************************************************
 * audio_video.c: Audio/Video management : volume, snapshot, OSD
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
 *
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
zorglub's avatar
zorglub committed
22 23
 *****************************************************************************/

24 25 26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
zorglub's avatar
zorglub committed
27
#include "mediacontrol_internal.h"
28
#include "libvlc_internal.h"
29

zorglub's avatar
zorglub committed
30
#include <vlc/mediacontrol.h>
31
#include <vlc/libvlc.h>
zorglub's avatar
zorglub committed
32

zorglub's avatar
zorglub committed
33 34
#include <vlc_vout.h>
#include <vlc_osd.h>
zorglub's avatar
zorglub committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <errno.h>                                                 /* ENOMEM */
#include <stdio.h>
#include <ctype.h>

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
gbazin's avatar
gbazin committed
49 50 51
#ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#endif
zorglub's avatar
zorglub committed
52 53 54 55 56 57

mediacontrol_RGBPicture *
mediacontrol_snapshot( mediacontrol_Instance *self,
                       const mediacontrol_Position * a_position,
                       mediacontrol_Exception *exception )
{
58
    (void)a_position;
59 60
    vlc_object_t* p_cache;
    vout_thread_t* p_vout;
61
    input_thread_t *p_input;
62 63 64
    mediacontrol_RGBPicture *p_pic = NULL;
    char path[256];
    snapshot_t *p_snapshot;
65
    libvlc_exception_t ex;
zorglub's avatar
zorglub committed
66

67
    libvlc_exception_init( &ex );
68
    mediacontrol_exception_init( exception );
zorglub's avatar
zorglub committed
69

70 71 72 73 74 75
    p_input = libvlc_get_input_thread( self->p_media_player, &ex );
    if( ! p_input )
    {
        RAISE_NULL( mediacontrol_InternalException, "No input" );
    }
    p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
76 77
    if( ! p_vout )
    {
78
        RAISE_NULL( mediacontrol_InternalException, "No video output" );
79
    }
80
    p_cache = vlc_object_create( p_input, sizeof( vlc_object_t ) );
81
    if( p_cache == NULL )
zorglub's avatar
zorglub committed
82
    {
83
        vlc_object_release( p_vout );
84
        vlc_object_release( p_input );
85
        RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
zorglub's avatar
zorglub committed
86
    }
87
    snprintf( path, 255, "object:%ju", (uintmax_t)(uintptr_t)p_cache );
88 89
    var_SetString( p_vout, "snapshot-path", path );
    var_SetString( p_vout, "snapshot-format", "png" );
zorglub's avatar
zorglub committed
90

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
91
    vlc_object_lock( p_cache );
92
    vout_Control( p_vout, VOUT_SNAPSHOT );
93
    vlc_object_wait( p_cache );
94
    vlc_object_release( p_vout );
zorglub's avatar
zorglub committed
95

96
    p_snapshot = ( snapshot_t* ) p_cache->p_private;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
97
    vlc_object_unlock( p_cache );
98
    vlc_object_release( p_cache );
99
    vlc_object_release( p_input );
100

101
    if( p_snapshot )
zorglub's avatar
zorglub committed
102
    {
103 104
        /* Note: p_snapshot->p_data is directly used, not copied. Thus
           do not free it here. */
Olivier Aubert's avatar
Olivier Aubert committed
105
        p_pic = private_mediacontrol_createRGBPicture( p_snapshot->i_width,
106 107 108 109 110
                                                       p_snapshot->i_height,
                                                       VLC_FOURCC( 'p','n','g',' ' ),
                                                       p_snapshot->date,
                                                       p_snapshot->p_data,
                                                       p_snapshot->i_datasize );
111
        if( !p_pic )
112 113 114 115
        {
            free( p_snapshot );
            RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
        }
zorglub's avatar
zorglub committed
116
    }
117
    else
zorglub's avatar
zorglub committed
118
    {
119
        RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
zorglub's avatar
zorglub committed
120
    }
121
    return p_pic;
zorglub's avatar
zorglub committed
122 123
}

124
static
zorglub's avatar
zorglub committed
125 126 127 128 129
int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
                           char *psz_string, text_style_t *p_style,
                           int i_flags, int i_hmargin, int i_vmargin,
                           mtime_t i_start, mtime_t i_stop )
{
130 131 132 133
    return osd_ShowTextAbsolute( p_vout->p_spu, i_channel,
                                 psz_string, p_style,
                                 i_flags, i_hmargin, i_vmargin,
                                 i_start, i_stop );
zorglub's avatar
zorglub committed
134 135 136 137 138 139 140 141 142 143 144 145
}


void
mediacontrol_display_text( mediacontrol_Instance *self,
                           const char * message,
                           const mediacontrol_Position * begin,
                           const mediacontrol_Position * end,
                           mediacontrol_Exception *exception )
{
    vout_thread_t *p_vout = NULL;
    char* psz_message;
146 147
    input_thread_t *p_input;
    libvlc_exception_t ex;
zorglub's avatar
zorglub committed
148

149 150 151 152 153
    libvlc_exception_init( &ex );
    mediacontrol_exception_init( exception );

    p_input = libvlc_get_input_thread( self->p_media_player, &ex );
    if( ! p_input )
zorglub's avatar
zorglub committed
154
    {
155
        RAISE_VOID( mediacontrol_InternalException, "No input" );
zorglub's avatar
zorglub committed
156
    }
157
    p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
zorglub's avatar
zorglub committed
158 159
    if( ! p_vout )
    {
160 161 162 163 164 165 166
        RAISE_VOID( mediacontrol_InternalException, "No video output" );
    }

    psz_message = strdup( message );
    if( !psz_message )
    {
        RAISE_VOID( mediacontrol_InternalException, "no more memory" );
zorglub's avatar
zorglub committed
167 168 169 170 171 172 173
    }

    if( begin->origin == mediacontrol_RelativePosition &&
        begin->value == 0 &&
        end->origin == mediacontrol_RelativePosition )
    {
        mtime_t i_duration = 0;
174
        mtime_t i_now = mdate();
zorglub's avatar
zorglub committed
175

176
        i_duration = 1000 * private_mediacontrol_unit_convert(
177 178 179 180
                                                              self->p_media_player,
                                                              end->key,
                                                              mediacontrol_MediaTime,
                                                              end->value );
zorglub's avatar
zorglub committed
181 182 183 184 185 186 187 188 189 190 191 192

        mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
                               OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
                               i_now, i_now + i_duration );
    }
    else
    {
        mtime_t i_debut, i_fin, i_now;

        /* FIXME */
        /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
        i_now = mdate();
193

194
        i_debut = private_mediacontrol_position2microsecond( self->p_media_player,
zorglub's avatar
zorglub committed
195
                                            ( mediacontrol_Position* ) begin );
zorglub's avatar
zorglub committed
196 197
        i_debut += i_now;

198
        i_fin = private_mediacontrol_position2microsecond( self->p_media_player,
zorglub's avatar
zorglub committed
199
                                          ( mediacontrol_Position * ) end );
zorglub's avatar
zorglub committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
        i_fin += i_now;

        vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
                               OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
                               i_debut, i_fin );
    }

    vlc_object_release( p_vout );
}

unsigned short
mediacontrol_sound_get_volume( mediacontrol_Instance *self,
                               mediacontrol_Exception *exception )
{
214 215
    libvlc_exception_t ex;
    int i_ret = 0;
zorglub's avatar
zorglub committed
216

217 218 219 220 221 222 223
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
    /* FIXME: Normalize in [0..100] */
    return (unsigned short)i_ret;
zorglub's avatar
zorglub committed
224 225 226 227 228 229 230
}

void
mediacontrol_sound_set_volume( mediacontrol_Instance *self,
                               const unsigned short volume,
                               mediacontrol_Exception *exception )
{
231 232 233 234 235 236 237 238
    /* FIXME: Normalize in [0..100] */
    libvlc_exception_t ex;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    libvlc_audio_set_volume( self->p_instance, volume, &ex );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
zorglub's avatar
zorglub committed
239 240
}

241
int mediacontrol_set_visual( mediacontrol_Instance *self,
242 243
                                    WINDOWHANDLE visual_id,
                                    mediacontrol_Exception *exception )
zorglub's avatar
zorglub committed
244
{
245
    libvlc_exception_t ex;
zorglub's avatar
zorglub committed
246

247 248
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
249

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
250
    libvlc_media_player_set_drawable( self->p_media_player, (libvlc_drawable_t)visual_id, &ex );
251
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
252
    return true;
zorglub's avatar
zorglub committed
253
}
254 255 256

int
mediacontrol_get_rate( mediacontrol_Instance *self,
257
               mediacontrol_Exception *exception )
258
{
259 260 261 262 263 264
    libvlc_exception_t ex;
    int i_ret;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

265
    i_ret = libvlc_media_player_get_rate( self->p_media_player, &ex );
266
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
267

268
    return i_ret / 10;
269 270 271 272
}

void
mediacontrol_set_rate( mediacontrol_Instance *self,
273 274
               const int rate,
               mediacontrol_Exception *exception )
275
{
276 277 278 279
    libvlc_exception_t ex;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
280

281
    libvlc_media_player_set_rate( self->p_media_player, rate * 10, &ex );
282
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
283 284 285 286
}

int
mediacontrol_get_fullscreen( mediacontrol_Instance *self,
287
                 mediacontrol_Exception *exception )
288
{
289
    libvlc_exception_t ex;
290 291
    int i_ret;

292 293 294
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

295
    i_ret = libvlc_get_fullscreen( self->p_media_player, &ex );
296 297 298
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );

    return i_ret;
299 300 301 302
}

void
mediacontrol_set_fullscreen( mediacontrol_Instance *self,
303 304
                 const int b_fullscreen,
                 mediacontrol_Exception *exception )
305
{
306
    libvlc_exception_t ex;
307

308 309
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
310

311
    libvlc_set_fullscreen( self->p_media_player, b_fullscreen, &ex );
312
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
313
}