mediacontrol_audio_video.c 9.17 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
 *****************************************************************************/

24 25 26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Clément Stenac's avatar
Clément Stenac committed
27
#include "mediacontrol_internal.h"
28
#include "libvlc_internal.h"
29
#include "media_player_internal.h"
30

Clément Stenac's avatar
Clément Stenac committed
31
#include <vlc/mediacontrol.h>
32
#include <vlc/libvlc.h>
33

Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_vout.h>
35
#include <vlc_input.h>
Clément Stenac's avatar
Clément Stenac committed
36
#include <vlc_osd.h>
37
#include <vlc_block.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51

#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
52 53 54
#ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#endif
55 56 57 58 59 60

mediacontrol_RGBPicture *
mediacontrol_snapshot( mediacontrol_Instance *self,
                       const mediacontrol_Position * a_position,
                       mediacontrol_Exception *exception )
{
61
    (void)a_position;
62
    vout_thread_t* p_vout;
63
    input_thread_t *p_input;
64
    mediacontrol_RGBPicture *p_pic;
65
    libvlc_exception_t ex;
66

67
    libvlc_exception_init( &ex );
68
    mediacontrol_exception_init( exception );
69

70 71 72 73 74
    p_input = libvlc_get_input_thread( self->p_media_player, &ex );
    if( ! p_input )
    {
        RAISE_NULL( mediacontrol_InternalException, "No input" );
    }
75

76
    p_vout = input_GetVout( p_input );
77
    vlc_object_release( p_input );
78 79
    if( ! p_vout )
    {
80
        RAISE_NULL( mediacontrol_InternalException, "No video output" );
81
    }
82

83 84
    block_t *p_image;
    video_format_t fmt;
85

86
    if( vout_GetSnapshot( p_vout, &p_image, NULL, &fmt, "png", 500*1000 ) )
87
    {
88 89
        RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
        return NULL;
90
    }
91

92 93 94
    /* */
    char *p_data = malloc( p_image->i_buffer );
    if( p_data )
95
    {
96 97 98 99 100 101 102
        memcpy( p_data, p_image->p_buffer, p_image->i_buffer );
        p_pic = private_mediacontrol_createRGBPicture( fmt.i_width,
                                                       fmt.i_height,
                                                       fmt.i_chroma,
                                                       p_image->i_pts,
                                                       p_data,
                                                       p_image->i_buffer );
103
    }
104
    else
105
    {
106
        p_pic = NULL;
107
    }
108 109 110 111
    block_Release( p_image );

    if( !p_pic )
        RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
112
    return p_pic;
113 114
}

115
static
116
int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
117
                           const char *psz_string, text_style_t *p_style,
118 119 120
                           int i_flags, int i_hmargin, int i_vmargin,
                           mtime_t i_start, mtime_t i_stop )
{
121
    return osd_ShowTextAbsolute( vout_GetSpu( p_vout ), i_channel,
122 123 124
                                 psz_string, p_style,
                                 i_flags, i_hmargin, i_vmargin,
                                 i_start, i_stop );
125 126 127 128 129 130 131 132 133 134 135
}


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;
136 137
    input_thread_t *p_input;
    libvlc_exception_t ex;
138

139 140 141
    libvlc_exception_init( &ex );
    mediacontrol_exception_init( exception );

142 143 144 145 146
    if( !message )
    {
        RAISE_VOID( mediacontrol_InternalException, "Empty text" );
    }

147 148
    p_input = libvlc_get_input_thread( self->p_media_player, &ex );
    if( ! p_input )
149
    {
150
        RAISE_VOID( mediacontrol_InternalException, "No input" );
151
    }
152
    p_vout = input_GetVout( p_input );
153 154
    if( ! p_vout )
    {
155 156 157
        RAISE_VOID( mediacontrol_InternalException, "No video output" );
    }

158 159 160 161 162
    if( begin->origin == mediacontrol_RelativePosition &&
        begin->value == 0 &&
        end->origin == mediacontrol_RelativePosition )
    {
        mtime_t i_duration = 0;
163
        mtime_t i_now = mdate();
164

165
        i_duration = 1000 * private_mediacontrol_unit_convert(
166 167 168 169
                                                              self->p_media_player,
                                                              end->key,
                                                              mediacontrol_MediaTime,
                                                              end->value );
170

171
        mediacontrol_showtext( p_vout, DEFAULT_CHAN, message, NULL,
172 173 174 175 176 177 178 179 180 181
                               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();
182

183
        i_debut = private_mediacontrol_position2microsecond( self->p_media_player,
184
                                            ( mediacontrol_Position* ) begin );
185 186
        i_debut += i_now;

187
        i_fin = private_mediacontrol_position2microsecond( self->p_media_player,
188
                                          ( mediacontrol_Position * ) end );
189 190
        i_fin += i_now;

191
        vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, message, NULL,
192 193 194 195 196 197 198 199 200 201 202
                               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 )
{
203 204
    libvlc_exception_t ex;
    int i_ret = 0;
205

206 207 208 209 210 211 212
    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;
213 214 215 216 217 218 219
}

void
mediacontrol_sound_set_volume( mediacontrol_Instance *self,
                               const unsigned short volume,
                               mediacontrol_Exception *exception )
{
220 221 222 223 224 225 226 227
    /* 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 );
228 229
}

230
int mediacontrol_set_visual( mediacontrol_Instance *self,
231 232
                                    WINDOWHANDLE visual_id,
                                    mediacontrol_Exception *exception )
233
{
234
    libvlc_exception_t ex;
235

236 237
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
238

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
239
    libvlc_media_player_set_drawable( self->p_media_player, (libvlc_drawable_t)visual_id, &ex );
240
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
241
    return true;
242
}
243 244 245

int
mediacontrol_get_rate( mediacontrol_Instance *self,
246
               mediacontrol_Exception *exception )
247
{
248 249 250 251 252 253
    libvlc_exception_t ex;
    int i_ret;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

254
    i_ret = libvlc_media_player_get_rate( self->p_media_player, &ex );
255
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
256

257
    return i_ret / 10;
258 259 260 261
}

void
mediacontrol_set_rate( mediacontrol_Instance *self,
262 263
               const int rate,
               mediacontrol_Exception *exception )
264
{
265 266 267 268
    libvlc_exception_t ex;

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

270
    libvlc_media_player_set_rate( self->p_media_player, rate * 10, &ex );
271
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
272 273 274 275
}

int
mediacontrol_get_fullscreen( mediacontrol_Instance *self,
276
                 mediacontrol_Exception *exception )
277
{
278
    libvlc_exception_t ex;
279 280
    int i_ret;

281 282 283
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

284
    i_ret = libvlc_get_fullscreen( self->p_media_player, &ex );
285 286 287
    HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );

    return i_ret;
288 289 290 291
}

void
mediacontrol_set_fullscreen( mediacontrol_Instance *self,
292 293
                 const int b_fullscreen,
                 mediacontrol_Exception *exception )
294
{
295
    libvlc_exception_t ex;
296

297 298
    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );
299

300
    libvlc_set_fullscreen( self->p_media_player, b_fullscreen, &ex );
301
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
302
}