mediacontrol_audio_video.c 10.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * audio_video.c: Audio/Video management : volume, snapshot, OSD
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5
 * $Id$
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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

Clément Stenac's avatar
Clément Stenac committed
24
#include "mediacontrol_internal.h"
25

Clément Stenac's avatar
Clément Stenac committed
26
#include <vlc/mediacontrol.h>
27
#include <vlc/libvlc.h>
28

Clément Stenac's avatar
Clément Stenac committed
29
#include <vlc_playlist.h>
30

Clément Stenac's avatar
Clément Stenac committed
31
#include <vlc_aout.h>
32

Clément Stenac's avatar
Clément Stenac committed
33 34
#include <vlc_vout.h>
#include <vlc_osd.h>
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
49 50 51
#ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#endif
52 53 54 55 56 57

mediacontrol_RGBPicture *
mediacontrol_snapshot( mediacontrol_Instance *self,
                       const mediacontrol_Position * a_position,
                       mediacontrol_Exception *exception )
{
58 59 60 61 62
    vlc_object_t* p_cache;
    vout_thread_t* p_vout;
    mediacontrol_RGBPicture *p_pic = NULL;
    char path[256];
    snapshot_t *p_snapshot;
63 64 65

    exception=mediacontrol_exception_init( exception );

66 67 68
    p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
    if( ! p_vout )
    {
69
        RAISE_NULL( mediacontrol_InternalException, "No video output" );
70
    }
71 72
    p_cache = vlc_object_create( self->p_playlist, VLC_OBJECT_GENERIC );
    if( p_cache == NULL )
73
    {
74
        vlc_object_release( p_vout );
75
        RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
76
    }
77 78 79
    snprintf( path, 255, "object:%d", p_cache->i_object_id );
    var_SetString( p_vout, "snapshot-path", path );
    var_SetString( p_vout, "snapshot-format", "png" );
80

81 82 83
    vlc_mutex_lock( &p_cache->object_lock );
    vout_Control( p_vout, VOUT_SNAPSHOT );
    vlc_cond_wait( &p_cache->object_wait, &p_cache->object_lock );
84
    vlc_object_release( p_vout );
85

86 87
    p_snapshot = ( snapshot_t* ) p_cache->p_private;
    vlc_object_destroy( p_cache );
88

89
    if( p_snapshot )
90
    {
91 92
        p_pic = _mediacontrol_createRGBPicture( p_snapshot->i_width,
                                                p_snapshot->i_height,
93
                                                VLC_FOURCC( 'p','n','g',' ' ),
94 95 96 97
                                                p_snapshot->date,
                                                p_snapshot->p_data,
                                                p_snapshot->i_datasize );
        if( !p_pic )
98 99 100 101 102
        {
            free( p_snapshot->p_data );
            free( p_snapshot );
            RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
        }
103
    }
104
    else
105
    {
106
        RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
107
    }
108
    return p_pic;
109 110 111 112 113 114 115 116
}

mediacontrol_RGBPicture **
mediacontrol_all_snapshots( mediacontrol_Instance *self,
                            mediacontrol_Exception *exception )
{
    exception=mediacontrol_exception_init( exception );

117
    RAISE_NULL( mediacontrol_InternalException, "unsupported method" );
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 170 171 172 173 174 175 176 177
}

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 )
{
    subpicture_t *p_spu;
    video_format_t fmt;

    if( !psz_string ) return VLC_EGENERIC;

    p_spu = spu_CreateSubpicture( p_vout->p_spu );
    if( !p_spu ) return VLC_EGENERIC;

    /* Create a new subpicture region */
    memset( &fmt, 0, sizeof(video_format_t) );
    fmt.i_chroma = VLC_FOURCC('T','E','X','T');
    fmt.i_aspect = 0;
    fmt.i_width = fmt.i_height = 0;
    fmt.i_x_offset = fmt.i_y_offset = 0;
    p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
    if( !p_spu->p_region )
    {
        msg_Err( p_vout, "cannot allocate SPU region" );
        spu_DestroySubpicture( p_vout->p_spu, p_spu );
        return VLC_EGENERIC;
    }

    p_spu->p_region->psz_text = strdup( psz_string );
    p_spu->i_start = i_start;
    p_spu->i_stop = i_stop;
    p_spu->b_ephemer = VLC_FALSE;
    p_spu->b_absolute = VLC_FALSE;

    p_spu->i_x = i_hmargin;
    p_spu->i_y = i_vmargin;
    p_spu->i_flags = i_flags;
    p_spu->i_channel = i_channel;

    spu_DisplaySubpicture( p_vout->p_spu, p_spu );

    return VLC_SUCCESS;
}


void
mediacontrol_display_text( mediacontrol_Instance *self,
                           const char * message,
                           const mediacontrol_Position * begin,
                           const mediacontrol_Position * end,
                           mediacontrol_Exception *exception )
{
    input_thread_t *p_input = NULL;
    vout_thread_t *p_vout = NULL;
    char* psz_message;

    psz_message = strdup( message );
    if( !psz_message )
    {
178
        RAISE_VOID( mediacontrol_InternalException, "no more memory" );
179 180 181 182 183
    }

    p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
    if( ! p_vout )
    {
184
        RAISE_VOID( mediacontrol_InternalException, "no video output" );
185 186 187 188 189 190 191
    }

    if( begin->origin == mediacontrol_RelativePosition &&
        begin->value == 0 &&
        end->origin == mediacontrol_RelativePosition )
    {
        mtime_t i_duration = 0;
192
        mtime_t i_now = mdate();
193

194 195 196 197 198
        i_duration = 1000 * mediacontrol_unit_convert(
                                                self->p_playlist->p_input,
                                                end->key,
                                                mediacontrol_MediaTime,
                                                end->value );
199 200 201 202 203 204 205 206 207 208 209 210 211

        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;

        p_input = self->p_playlist->p_input;
        if( ! p_input )
        {
            vlc_object_release( p_vout );
212
            RAISE_VOID( mediacontrol_InternalException, "No input" );
213 214 215 216 217
        }

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

219
        i_debut = mediacontrol_position2microsecond( p_input,
220
                                            ( mediacontrol_Position* ) begin );
221 222 223
        i_debut += i_now;

        i_fin = mediacontrol_position2microsecond( p_input,
224
                                          ( mediacontrol_Position * ) end );
225 226 227 228 229 230 231 232 233 234 235 236 237 238
        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 )
{
239 240
    libvlc_exception_t ex;
    int i_ret = 0;
241

242 243 244 245 246 247 248
    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;
249 250 251 252 253 254 255
}

void
mediacontrol_sound_set_volume( mediacontrol_Instance *self,
                               const unsigned short volume,
                               mediacontrol_Exception *exception )
{
256 257 258 259 260 261 262 263
    /* 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 );
264 265
}

266
vlc_bool_t mediacontrol_set_visual( mediacontrol_Instance *self,
267 268
                                    WINDOWHANDLE visual_id,
                                    mediacontrol_Exception *exception )
269
{
270
    libvlc_exception_t ex;
271

272 273
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
274

275 276 277
    libvlc_video_set_parent( self->p_instance, visual_id, &ex );
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
    return VLC_TRUE;
278
}
279 280 281 282 283

int
mediacontrol_get_rate( mediacontrol_Instance *self,
		       mediacontrol_Exception *exception )
{
284 285 286 287 288 289 290 291 292 293 294 295 296
    libvlc_exception_t ex;
    libvlc_input_t* p_input;
    int i_ret;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    p_input = libvlc_playlist_get_input( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );

    i_ret = libvlc_input_get_rate( p_input, &ex );
    libvlc_input_free( p_input );
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
297

298
    return i_ret / 10;
299 300 301 302 303 304 305
}

void
mediacontrol_set_rate( mediacontrol_Instance *self,
		       const int rate,
		       mediacontrol_Exception *exception )
{
306 307 308 309 310
    libvlc_exception_t ex;
    libvlc_input_t* p_input;

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

312 313 314 315 316 317
    p_input = libvlc_playlist_get_input( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );

    libvlc_input_set_rate( p_input, rate * 10, &ex );
    libvlc_input_free( p_input );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
318 319 320 321 322 323
}

int
mediacontrol_get_fullscreen( mediacontrol_Instance *self,
			     mediacontrol_Exception *exception )
{
324 325
    libvlc_exception_t ex;
    libvlc_input_t* p_input;
326 327
    int i_ret;

328 329 330 331 332 333 334 335 336 337 338
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    p_input = libvlc_playlist_get_input( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );

    i_ret = libvlc_get_fullscreen( p_input, &ex );
    libvlc_input_free( p_input );
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );

    return i_ret;
339 340 341 342 343 344 345
}

void
mediacontrol_set_fullscreen( mediacontrol_Instance *self,
			     const int b_fullscreen,
			     mediacontrol_Exception *exception )
{
346 347
    libvlc_exception_t ex;
    libvlc_input_t* p_input;
348

349 350
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
351

352 353
    p_input = libvlc_playlist_get_input( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
354

355 356 357
    libvlc_set_fullscreen( p_input, b_fullscreen, &ex );
    libvlc_input_free( p_input );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
358
}