video.c 30.8 KB
Newer Older
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
1 2 3
/*****************************************************************************
 * video.c: libvlc new API video functions
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2005-2010 VLC authors and VideoLAN
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
5
 *
6
 * $Id$
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
7
 *
8
 * Authors: Clément Stenac <zorglub@videolan.org>
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
9 10 11 12
 *          Filippo Carone <littlejohn@videolan.org>
 *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
 *          Damien Fouilleul <damienf a_t videolan dot org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
13 14 15
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
16 17 18 19
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
22
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
23 24 25
 * You should have received a copy of the GNU Lesser 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.
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
26 27
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
32
#include <vlc/libvlc.h>
33
#include <vlc/libvlc_renderer_discoverer.h>
34 35 36 37
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_player.h>

#include <vlc_common.h>
38
#include <vlc_modules.h>
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
39 40
#include <vlc_input.h>
#include <vlc_vout.h>
41
#include <vlc_url.h>
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
42

Steve Lhomme's avatar
Steve Lhomme committed
43
#include "libvlc_internal.h"
44
#include "media_player_internal.h"
45
#include <math.h>
46
#include <assert.h>
47

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
48
/*
49
 * Remember to release the returned vout_thread_t.
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
50
 */
51
static vout_thread_t **GetVouts( libvlc_media_player_t *p_mi, size_t *n )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
52
{
53
    input_thread_t *p_input = libvlc_get_input_thread( p_mi );
54
    if( !p_input )
55 56
    {
        *n = 0;
57
        return NULL;
58
    }
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
59

60 61
    vout_thread_t **pp_vouts;
    if (input_Control( p_input, INPUT_GET_VOUTS, &pp_vouts, n))
62
    {
63 64
        *n = 0;
        pp_vouts = NULL;
65
    }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    vlc_object_release (p_input);
    return pp_vouts;
}

static vout_thread_t *GetVout (libvlc_media_player_t *mp, size_t num)
{
    vout_thread_t *p_vout = NULL;
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (mp, &n);
    if (pp_vouts == NULL)
        goto err;

    if (num < n)
        p_vout = pp_vouts[num];

    for (size_t i = 0; i < n; i++)
        if (i != num)
            vlc_object_release (pp_vouts[i]);
    free (pp_vouts);

    if (p_vout == NULL)
err:
        libvlc_printerr ("Video output not active");
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
89 90 91 92 93 94 95
    return p_vout;
}

/**********************************************************************
 * Exported functions
 **********************************************************************/

96
void libvlc_set_fullscreen( libvlc_media_player_t *p_mi, int b_fullscreen )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
97
{
98 99
    /* This will work even if the video is not currently active */
    var_SetBool (p_mi, "fullscreen", !!b_fullscreen);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
100

101 102 103 104 105 106 107 108 109
    /* Apply to current video outputs (if any) */
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mi, &n);
    for (size_t i = 0; i < n; i++)
    {
        var_SetBool (pp_vouts[i], "fullscreen", b_fullscreen);
        vlc_object_release (pp_vouts[i]);
    }
    free (pp_vouts);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
110 111
}

112
int libvlc_get_fullscreen( libvlc_media_player_t *p_mi )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
113
{
114
    return var_GetBool (p_mi, "fullscreen");
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
115 116
}

117
void libvlc_toggle_fullscreen( libvlc_media_player_t *p_mi )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
118
{
119
    bool b_fullscreen = var_ToggleBool (p_mi, "fullscreen");
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
120

121 122 123 124 125 126
    /* Apply to current video outputs (if any) */
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mi, &n);
    for (size_t i = 0; i < n; i++)
    {
        vout_thread_t *p_vout = pp_vouts[i];
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
127

128 129 130 131
        var_SetBool (p_vout, "fullscreen", b_fullscreen);
        vlc_object_release (p_vout);
    }
    free (pp_vouts);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
132 133
}

134 135
void libvlc_video_set_key_input( libvlc_media_player_t *p_mi, unsigned on )
{
136
    var_SetBool (p_mi, "keyboard-events", !!on);
137 138 139 140
}

void libvlc_video_set_mouse_input( libvlc_media_player_t *p_mi, unsigned on )
{
141
    var_SetBool (p_mi, "mouse-events", !!on);
142 143
}

144 145 146 147
int
libvlc_video_take_snapshot( libvlc_media_player_t *p_mi, unsigned num,
                            const char *psz_filepath,
                            unsigned int i_width, unsigned int i_height )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
148
{
149 150
    assert( psz_filepath );

151 152 153
    vout_thread_t *p_vout = GetVout (p_mi, num);
    if (p_vout == NULL)
        return -1;
154

155 156 157 158 159
    /* FIXME: This is not atomic. All parameters should be passed at once
     * (obviously _not_ with var_*()). Also, the libvlc object should not be
     * used for the callbacks: that breaks badly if there are concurrent
     * media players in the instance. */
    var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER );
160
    var_SetInteger( p_vout, "snapshot-width", i_width);
161
    var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER );
162
    var_SetInteger( p_vout, "snapshot-height", i_height );
163
    var_Create( p_vout, "snapshot-path", VLC_VAR_STRING );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
164
    var_SetString( p_vout, "snapshot-path", psz_filepath );
165
    var_Create( p_vout, "snapshot-format", VLC_VAR_STRING );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
166
    var_SetString( p_vout, "snapshot-format", "png" );
167 168
    var_TriggerCallback( p_vout, "video-snapshot" );
    vlc_object_release( p_vout );
169
    return 0;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
170 171
}

172 173
int libvlc_video_get_size( libvlc_media_player_t *p_mi, unsigned num,
                           unsigned *restrict px, unsigned *restrict py )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
174
{
175 176
    libvlc_media_track_info_t *info;
    int ret = -1;
Rafaël Carré's avatar
Rafaël Carré committed
177 178
    if (!p_mi->p_md)
        return ret;
179 180 181 182 183 184 185 186 187 188 189 190 191 192
    int infos = libvlc_media_get_tracks_info(p_mi->p_md, &info);
    if (infos <= 0)
        return ret;

    for (int i = 0; i < infos; i++)
        if (info[i].i_type == libvlc_track_video && num-- == 0) {
            *px = info[i].u.video.i_width;
            *py = info[i].u.video.i_height;
            ret = 0;
            break;
        }

    free(info);
    return ret;
193
}
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
194

195 196
int libvlc_video_get_height( libvlc_media_player_t *p_mi )
{
197
    unsigned width, height;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
198

199
    if (libvlc_video_get_size (p_mi, 0, &width, &height))
200
        return 0;
201
    return height;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
202 203
}

204
int libvlc_video_get_width( libvlc_media_player_t *p_mi )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
205
{
206
    unsigned width, height;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
207

208
    if (libvlc_video_get_size (p_mi, 0, &width, &height))
209
        return 0;
210
    return width;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
211 212
}

213
int libvlc_video_get_cursor( libvlc_media_player_t *mp, unsigned num,
214
                             int *restrict px, int *restrict py )
215 216 217 218 219
{
    vout_thread_t *p_vout = GetVout (mp, num);
    if (p_vout == NULL)
        return -1;

220
    var_GetCoords (p_vout, "mouse-moved", px, py);
221 222 223 224
    vlc_object_release (p_vout);
    return 0;
}

225
unsigned libvlc_media_player_has_vout( libvlc_media_player_t *p_mi )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
226
{
227 228 229 230 231 232
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mi, &n);
    for (size_t i = 0; i < n; i++)
        vlc_object_release (pp_vouts[i]);
    free (pp_vouts);
    return n;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
233 234
}

235
float libvlc_video_get_scale( libvlc_media_player_t *mp )
236
{
237
    float f_scale = var_GetFloat (mp, "zoom");
238
    if (var_GetBool (mp, "autoscale"))
239
        f_scale = 0.f;
240 241 242
    return f_scale;
}

243
void libvlc_video_set_scale( libvlc_media_player_t *p_mp, float f_scale )
244
{
245
    if (isfinite(f_scale) && f_scale != 0.f)
246
        var_SetFloat (p_mp, "zoom", f_scale);
247
    var_SetBool (p_mp, "autoscale", f_scale == 0.f);
248 249 250 251 252 253 254

    /* Apply to current video outputs (if any) */
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mp, &n);
    for (size_t i = 0; i < n; i++)
    {
        vout_thread_t *p_vout = pp_vouts[i];
255

256
        if (isfinite(f_scale) && f_scale != 0.f)
257
            var_SetFloat (p_vout, "zoom", f_scale);
258
        var_SetBool (p_vout, "autoscale", f_scale == 0.f);
259 260 261
        vlc_object_release (p_vout);
    }
    free (pp_vouts);
262 263
}

264
char *libvlc_video_get_aspect_ratio( libvlc_media_player_t *p_mi )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
265
{
266
    return var_GetNonEmptyString (p_mi, "aspect-ratio");
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
267 268
}

269
void libvlc_video_set_aspect_ratio( libvlc_media_player_t *p_mi,
270
                                    const char *psz_aspect )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
271
{
272 273 274
    if (psz_aspect == NULL)
        psz_aspect = "";
    var_SetString (p_mi, "aspect-ratio", psz_aspect);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
275

276 277 278
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mi, &n);
    for (size_t i = 0; i < n; i++)
279
    {
280 281 282 283
        vout_thread_t *p_vout = pp_vouts[i];

        var_SetString (p_vout, "aspect-ratio", psz_aspect);
        vlc_object_release (p_vout);
284
    }
285
    free (pp_vouts);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
286 287
}

288 289 290 291 292
libvlc_video_viewpoint_t *libvlc_video_new_viewpoint(void)
{
    libvlc_video_viewpoint_t *p_vp = malloc(sizeof *p_vp);
    if (unlikely(p_vp == NULL))
        return NULL;
293
    p_vp->f_yaw = p_vp->f_pitch = p_vp->f_roll = p_vp->f_field_of_view = 0.0f;
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    return p_vp;
}

int libvlc_video_update_viewpoint( libvlc_media_player_t *p_mi,
                                   const libvlc_video_viewpoint_t *p_viewpoint,
                                   bool b_absolute )
{
    vlc_viewpoint_t update = {
        .yaw   = p_viewpoint->f_yaw,
        .pitch = p_viewpoint->f_pitch,
        .roll  = p_viewpoint->f_roll,
        .fov   = p_viewpoint->f_field_of_view,
    };

    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
    if( p_input_thread != NULL )
    {
        if( input_UpdateViewpoint( p_input_thread, &update,
                                   b_absolute ) != VLC_SUCCESS )
        {
            vlc_object_release( p_input_thread );
            return -1;
        }
        vlc_object_release( p_input_thread );
318
        return 0;
319 320 321 322 323
    }

    /* Save the viewpoint in case the input is not created yet */
    if( !b_absolute )
    {
324 325 326 327
        p_mi->viewpoint.yaw += update.yaw;
        p_mi->viewpoint.pitch += update.pitch;
        p_mi->viewpoint.roll += update.roll;
        p_mi->viewpoint.fov += update.fov;
328 329 330 331
    }
    else
        p_mi->viewpoint = update;

332 333
    vlc_viewpoint_clip( &p_mi->viewpoint );

334 335 336
    return 0;
}

337
int libvlc_video_get_spu( libvlc_media_player_t *p_mi )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
338
{
339
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
340

341 342 343 344 345
    if( !p_input_thread )
    {
        libvlc_printerr( "No active input" );
        return -1;
    }
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
346

347
    int i_spu = var_GetInteger( p_input_thread, "spu-es" );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
348 349 350 351
    vlc_object_release( p_input_thread );
    return i_spu;
}

352
int libvlc_video_get_spu_count( libvlc_media_player_t *p_mi )
353
{
354
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
355
    int i_spu_count;
356 357

    if( !p_input_thread )
358
        return 0;
359

360
    i_spu_count = var_CountChoices( p_input_thread, "spu-es" );
361
    vlc_object_release( p_input_thread );
362
    return i_spu_count;
363 364 365
}

libvlc_track_description_t *
366
        libvlc_video_get_spu_description( libvlc_media_player_t *p_mi )
367
{
368
    return libvlc_get_track_description( p_mi, "spu-es" );
369 370
}

371
int libvlc_video_set_spu( libvlc_media_player_t *p_mi, int i_spu )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
372
{
373
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
374
    vlc_value_t list;
375
    int i_ret = -1;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
376

377
    if( !p_input_thread )
378
        return -1;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
379

380
    var_Change (p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &list, NULL);
381
    for (int i = 0; i < list.p_list->i_count; i++)
382
    {
383 384 385 386 387 388 389
        if( i_spu == list.p_list->p_values[i].i_int )
        {
            if( var_SetInteger( p_input_thread, "spu-es", i_spu ) < 0 )
                break;
            i_ret = 0;
            goto end;
        }
390
    }
391
    libvlc_printerr( "Track identifier not found" );
392
end:
393 394 395
    vlc_object_release (p_input_thread);
    var_FreeList (&list, NULL);
    return i_ret;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
396 397
}

398
int libvlc_video_set_subtitle_file( libvlc_media_player_t *p_mi,
399
                                    const char *psz_subtitle )
400
{
401
    input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
402 403 404 405
    bool b_ret = false;

    if( p_input_thread )
    {
406 407 408 409 410 411 412 413
        char* psz_mrl = vlc_path2uri( psz_subtitle, NULL );
        if( psz_mrl )
        {
            if( !input_AddSlave( p_input_thread, SLAVE_TYPE_SPU, psz_mrl,
                                 true, false ) )
                b_ret = true;
            free( psz_mrl );
        }
414 415 416 417 418
        vlc_object_release( p_input_thread );
    }
    return b_ret;
}

419 420 421 422 423 424 425
int64_t libvlc_video_get_spu_delay( libvlc_media_player_t *p_mi )
{
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
    int64_t val = 0;

    if( p_input_thread )
    {
426
        val = var_GetInteger( p_input_thread, "spu-delay" );
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        vlc_object_release( p_input_thread );
    }
    else
    {
        libvlc_printerr( "No active input" );
    }

    return val;
}

int libvlc_video_set_spu_delay( libvlc_media_player_t *p_mi,
                                int64_t i_delay )
{
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
    int ret = -1;

    if( p_input_thread )
    {
445
        var_SetInteger( p_input_thread, "spu-delay", i_delay );
446 447 448 449 450 451 452 453 454 455 456
        vlc_object_release( p_input_thread );
        ret = 0;
    }
    else
    {
        libvlc_printerr( "No active input" );
    }

    return ret;
}

457
libvlc_track_description_t *
458
        libvlc_video_get_title_description( libvlc_media_player_t *p_mi )
459
{
460
    return libvlc_get_track_description( p_mi, "title" );
461 462 463 464
}

libvlc_track_description_t *
        libvlc_video_get_chapter_description( libvlc_media_player_t *p_mi,
465
                                              int i_title )
466
{
467 468
    char psz_title[sizeof ("title ") + 3 * sizeof (int)];
    sprintf( psz_title,  "title %2u", i_title );
469
    return libvlc_get_track_description( p_mi, psz_title );
470 471
}

472
char *libvlc_video_get_crop_geometry (libvlc_media_player_t *p_mi)
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
473
{
474
    return var_GetNonEmptyString (p_mi, "crop");
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
475 476
}

477
void libvlc_video_set_crop_geometry( libvlc_media_player_t *p_mi,
478
                                     const char *psz_geometry )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
479
{
480 481
    if (psz_geometry == NULL)
        psz_geometry = "";
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
482

483
    var_SetString (p_mi, "crop", psz_geometry);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
484

485 486
    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mi, &n);
487

488
    for (size_t i = 0; i < n; i++)
489
    {
490
        vout_thread_t *p_vout = pp_vouts[i];
491

492 493
        var_SetString (p_vout, "crop", psz_geometry);
        vlc_object_release (p_vout);
494
    }
495
    free (pp_vouts);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
496 497
}

498 499
int libvlc_video_get_teletext( libvlc_media_player_t *p_mi )
{
500
    return var_GetInteger (p_mi, "vbi-page");
501 502
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static void teletext_enable( input_thread_t *p_input_thread, bool b_enable )
{
    if( b_enable )
    {
        vlc_value_t list;
        if( !var_Change( p_input_thread, "teletext-es", VLC_VAR_GETCHOICES,
                         &list, NULL ) )
        {
            if( list.p_list->i_count > 0 )
                var_SetInteger( p_input_thread, "spu-es",
                                list.p_list->p_values[0].i_int );

            var_FreeList( &list, NULL );
        }
    }
    else
        var_SetInteger( p_input_thread, "spu-es", -1 );
}

522
void libvlc_video_set_teletext( libvlc_media_player_t *p_mi, int i_page )
523 524 525 526
{
    input_thread_t *p_input_thread;
    vlc_object_t *p_zvbi = NULL;
    int telx;
527
    bool b_key = false;
528

529 530
    if( i_page >= 0 && i_page < 1000 )
        var_SetInteger( p_mi, "vbi-page", i_page );
531
    else if( i_page >= 1000 )
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    {
        switch (i_page)
        {
            case libvlc_teletext_key_red:
            case libvlc_teletext_key_green:
            case libvlc_teletext_key_yellow:
            case libvlc_teletext_key_blue:
            case libvlc_teletext_key_index:
                b_key = true;
                break;
            default:
                libvlc_printerr("Invalid key action");
                return;
        }
    }
547 548 549 550 551
    else
    {
        libvlc_printerr("Invalid page number");
        return;
    }
552

553 554 555 556 557 558 559 560 561
    p_input_thread = libvlc_get_input_thread( p_mi );
    if( !p_input_thread ) return;

    if( var_CountChoices( p_input_thread, "teletext-es" ) <= 0 )
    {
        vlc_object_release( p_input_thread );
        return;
    }

562
    if( i_page == 0 )
563 564 565 566
    {
        teletext_enable( p_input_thread, false );
    }
    else
567
    {
568 569 570 571 572 573 574 575 576 577
        telx = var_GetInteger( p_input_thread, "teletext-es" );
        if( telx >= 0 )
        {
            if( input_GetEsObjects( p_input_thread, telx, &p_zvbi, NULL, NULL )
                == VLC_SUCCESS )
            {
                var_SetInteger( p_zvbi, "vbi-page", i_page );
                vlc_object_release( p_zvbi );
            }
        }
578
        else if (!b_key)
579
        {
580 581
            /* the "vbi-page" will be selected on es creation */
            teletext_enable( p_input_thread, true );
582
        }
583 584
        else
            libvlc_printerr("Key action sent while the teletext is disabled");
585 586 587 588
    }
    vlc_object_release( p_input_thread );
}

589
void libvlc_toggle_teletext( libvlc_media_player_t *p_mi )
590
{
591
    input_thread_t *p_input_thread;
592

593
    p_input_thread = libvlc_get_input_thread(p_mi);
594
    if( !p_input_thread ) return;
595

596
    if( var_CountChoices( p_input_thread, "teletext-es" ) <= 0 )
597
    {
598 599 600 601
        vlc_object_release( p_input_thread );
        return;
    }
    const bool b_selected = var_GetInteger( p_input_thread, "teletext-es" ) >= 0;
602
    teletext_enable( p_input_thread, !b_selected );
603
    vlc_object_release( p_input_thread );
604 605
}

606
int libvlc_video_get_track_count( libvlc_media_player_t *p_mi )
607
{
608
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
609
    int i_track_count;
610 611 612 613

    if( !p_input_thread )
        return -1;

614
    i_track_count = var_CountChoices( p_input_thread, "video-es" );
615

616
    vlc_object_release( p_input_thread );
617
    return i_track_count;
618 619 620
}

libvlc_track_description_t *
621
        libvlc_video_get_track_description( libvlc_media_player_t *p_mi )
622
{
623
    return libvlc_get_track_description( p_mi, "video-es" );
624 625
}

626
int libvlc_video_get_track( libvlc_media_player_t *p_mi )
627
{
628
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
629 630 631 632

    if( !p_input_thread )
        return -1;

633
    int id = var_GetInteger( p_input_thread, "video-es" );
634
    vlc_object_release( p_input_thread );
635
    return id;
636 637
}

638
int libvlc_video_set_track( libvlc_media_player_t *p_mi, int i_track )
639
{
640
    input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
641 642 643 644
    vlc_value_t val_list;
    int i_ret = -1;

    if( !p_input_thread )
645
        return -1;
646 647

    var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
648
    for( int i = 0; i < val_list.p_list->i_count; i++ )
649
    {
650
        if( i_track == val_list.p_list->p_values[i].i_int )
651
        {
652
            if( var_SetInteger( p_input_thread, "video-es", i_track ) < 0 )
653
                break;
654
            i_ret = 0;
655
            goto end;
656 657
        }
    }
658
    libvlc_printerr( "Track identifier not found" );
659
end:
660
    var_FreeList( &val_list, NULL );
661
    vlc_object_release( p_input_thread );
662
    return i_ret;
663
}
664 665 666 667

/******************************************************************************
 * libvlc_video_set_deinterlace : enable deinterlace
 *****************************************************************************/
668 669 670 671 672 673
void libvlc_video_set_deinterlace( libvlc_media_player_t *p_mi,
                                   const char *psz_mode )
{
    if (psz_mode == NULL)
        psz_mode = "";
    if (*psz_mode
Juha Jeronen's avatar
Juha Jeronen committed
674 675 676 677
     && strcmp (psz_mode, "blend")    && strcmp (psz_mode, "bob")
     && strcmp (psz_mode, "discard")  && strcmp (psz_mode, "linear")
     && strcmp (psz_mode, "mean")     && strcmp (psz_mode, "x")
     && strcmp (psz_mode, "yadif")    && strcmp (psz_mode, "yadif2x")
678
     && strcmp (psz_mode, "phosphor") && strcmp (psz_mode, "ivtc"))
679 680
        return;

681
    if (*psz_mode)
682
    {
683 684
        var_SetString (p_mi, "deinterlace-mode", psz_mode);
        var_SetInteger (p_mi, "deinterlace", 1);
685 686
    }
    else
687 688 689 690 691
        var_SetInteger (p_mi, "deinterlace", 0);

    size_t n;
    vout_thread_t **pp_vouts = GetVouts (p_mi, &n);
    for (size_t i = 0; i < n; i++)
692
    {
693
        vout_thread_t *p_vout = pp_vouts[i];
694

695 696 697 698 699 700 701 702 703 704
        if (*psz_mode)
        {
            var_SetString (p_vout, "deinterlace-mode", psz_mode);
            var_SetInteger (p_vout, "deinterlace", 1);
        }
        else
            var_SetInteger (p_vout, "deinterlace", 0);
        vlc_object_release (p_vout);
    }
    free (pp_vouts);
705
}
706

707 708 709 710
/* ************** */
/* module helpers */
/* ************** */

711 712 713
static int get_filter_str( vlc_object_t *p_parent, const char *psz_name,
                           bool b_add, const char **ppsz_filter_type,
                           char **ppsz_filter_value)
714 715 716 717 718 719 720 721
{
    char *psz_parser;
    char *psz_string;
    const char *psz_filter_type;

    module_t *p_obj = module_find( psz_name );
    if( !p_obj )
    {
722
        msg_Err( p_parent, "Unable to find filter module \"%s\".", psz_name );
723
        return VLC_EGENERIC;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    }

    if( module_provides( p_obj, "video filter" ) )
    {
        psz_filter_type = "video-filter";
    }
    else if( module_provides( p_obj, "sub source" ) )
    {
        psz_filter_type = "sub-source";
    }
    else if( module_provides( p_obj, "sub filter" ) )
    {
        psz_filter_type = "sub-filter";
    }
    else
    {
740
        msg_Err( p_parent, "Unknown video filter type." );
741
        return VLC_EGENERIC;
742 743
    }

744
    psz_string = var_GetString( p_parent, psz_filter_type );
745 746 747 748 749 750 751 752 753 754 755 756 757 758

    /* Todo : Use some generic chain manipulation functions */
    if( !psz_string ) psz_string = strdup("");

    psz_parser = strstr( psz_string, psz_name );
    if( b_add )
    {
        if( !psz_parser )
        {
            psz_parser = psz_string;
            if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
                          psz_string, psz_name ) == -1 )
            {
                free( psz_parser );
759
                return VLC_EGENERIC;
760 761 762 763 764 765
            }
            free( psz_parser );
        }
        else
        {
            free( psz_string );
766
            return VLC_EGENERIC;
767 768 769 770 771 772 773 774 775 776 777 778 779
        }
    }
    else
    {
        if( psz_parser )
        {
            memmove( psz_parser, psz_parser + strlen(psz_name) +
                            (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
                            strlen(psz_parser + strlen(psz_name)) + 1 );

            /* Remove trailing : : */
            if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
                *(psz_string+strlen(psz_string ) -1 ) = '\0';
Thomas Guillem's avatar
Thomas Guillem committed
780 781 782 783
        }
        else
        {
            free( psz_string );
784
            return VLC_EGENERIC;
Thomas Guillem's avatar
Thomas Guillem committed
785
        }
786 787
    }

788 789 790
    *ppsz_filter_type = psz_filter_type;
    *ppsz_filter_value = psz_string;
    return VLC_SUCCESS;
791 792
}

793
static bool find_sub_source_by_name( libvlc_media_player_t *p_mi, const char *restrict name )
794
{
795
    vout_thread_t *vout = GetVout( p_mi, 0 );
796 797
    if (!vout)
        return false;
798

799 800
    char *psz_sources = var_GetString( vout, "sub-source" );
    if( !psz_sources )
801
    {
802 803 804
        libvlc_printerr( "%s not enabled", name );
        vlc_object_release( vout );
        return false;
805
    }
806

807 808 809 810 811
    /* Find 'name'  */
    char *p = strstr( psz_sources, name );
    free( psz_sources );
    vlc_object_release( vout );
    return (p != NULL);
812 813
}

814
typedef const struct {
815
    const char name[20];
816
    unsigned type;
817
} opt_t;
818

819
static void
820 821
set_value( libvlc_media_player_t *p_mi, const char *restrict name,
           const opt_t *restrict opt, unsigned i_expected_type,
822
           const vlc_value_t *val, bool b_sub_source )
823
{
824 825
    if( !opt ) return;

826 827 828 829
    int i_type = opt->type;
    vlc_value_t new_val = *val;
    const char *psz_opt_name = opt->name;
    switch( i_type )
830
    {
831
        case 0: /* the enabler */
832
        {
833 834 835 836 837
            int i_ret = get_filter_str( VLC_OBJECT( p_mi ), opt->name, val->i_int,
                                        &psz_opt_name, &new_val.psz_string );
            if( i_ret != VLC_SUCCESS )
                return;
            i_type = VLC_VAR_STRING;
838
            break;
839
        }
840 841
        case VLC_VAR_INTEGER:
        case VLC_VAR_FLOAT:
842 843 844 845 846 847
        case VLC_VAR_STRING:
            if( i_expected_type != opt->type )
            {
                libvlc_printerr( "Invalid argument to %s", name );
                return;
            }
848 849
            break;
        default:
850
            libvlc_printerr( "Invalid argument to %s", name );
851
            return;
852
    }
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

    /* Set the new value to the media player. Next vouts created from this
     * media player will inherit this new value */
    var_SetChecked( p_mi, psz_opt_name, i_type, new_val );

    /* Set the new value to every loaded vouts */
    size_t i_vout_count;
    vout_thread_t **pp_vouts = GetVouts( p_mi, &i_vout_count );
    for( size_t i = 0; i < i_vout_count; ++i )
    {
        var_SetChecked( pp_vouts[i], psz_opt_name, i_type, new_val );
        if( b_sub_source )
            var_TriggerCallback( pp_vouts[i], "sub-source" );
        vlc_object_release( pp_vouts[i] );
    }

    if( opt->type == 0 )
        free( new_val.psz_string );
871 872 873
}

static int
874 875
get_int( libvlc_media_player_t *p_mi, const char *restrict name,
         const opt_t *restrict opt )
876 877
{
    if( !opt ) return 0;
878

879
    switch( opt->type )
880
    {
881 882
        case 0: /* the enabler */
        {
883 884
            bool b_enabled = find_sub_source_by_name( p_mi, name );
            return b_enabled ? 1 : 0;
885
        }
886
    case VLC_VAR_INTEGER:
887
        return var_GetInteger(p_mi, opt->name);
888 889
    case VLC_VAR_FLOAT:
        return lroundf(var_GetFloat(p_mi, opt->name));
890
    default:
891 892
        libvlc_printerr( "Invalid argument to %s in %s", name, "get int" );
        return 0;
893 894 895
    }
}

896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
static float
get_float( libvlc_media_player_t *p_mi, const char *restrict name,
            const opt_t *restrict opt )
{
    if( !opt ) return 0.0;

    if( opt->type != VLC_VAR_FLOAT )
    {
        libvlc_printerr( "Invalid argument to %s in %s", name, "get float" );
        return 0.0;
    }

    return var_GetFloat( p_mi, opt->name );
}

911
static char *
912 913
get_string( libvlc_media_player_t *p_mi, const char *restrict name,
            const opt_t *restrict opt )
914 915 916
{
    if( !opt ) return NULL;

917
    if( opt->type != VLC_VAR_STRING )
918
    {
919 920
        libvlc_printerr( "Invalid argument to %s in %s", name, "get string" );
        return NULL;
921 922
    }

923 924
    return var_GetString( p_mi, opt->name );
}
925 926

static const opt_t *
927
marq_option_bynumber(unsigned option)
928
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
929
    static const opt_t optlist[] =
930 931 932 933 934 935 936 937 938 939 940 941 942
    {
        { "marq",          0 },
        { "marq-marquee",  VLC_VAR_STRING },
        { "marq-color",    VLC_VAR_INTEGER },
        { "marq-opacity",  VLC_VAR_INTEGER },
        { "marq-position", VLC_VAR_INTEGER },
        { "marq-refresh",  VLC_VAR_INTEGER },
        { "marq-size",     VLC_VAR_INTEGER },
        { "marq-timeout",  VLC_VAR_INTEGER },
        { "marq-x",        VLC_VAR_INTEGER },
        { "marq-y",        VLC_VAR_INTEGER },
    };
    enum { num_opts = sizeof(optlist) / sizeof(*optlist) };
943

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
944
    const opt_t *r = option < num_opts ? optlist+option : NULL;
945 946 947
    if( !r )
        libvlc_printerr( "Unknown marquee option" );
    return r;
948 949 950
}

/*****************************************************************************
951
 * libvlc_video_get_marquee_int : get a marq option value
952
 *****************************************************************************/
953
int libvlc_video_get_marquee_int( libvlc_media_player_t *p_mi,
954
                                  unsigned option )
955
{
956
    return get_int( p_mi, "marq", marq_option_bynumber(option) );
957
}
958

959 960 961 962
/*****************************************************************************
 * libvlc_video_get_marquee_string : get a marq option value
 *****************************************************************************/
char * libvlc_video_get_marquee_string( libvlc_media_player_t *p_mi,
963
                                        unsigned option )
964
{
965
    return get_string( p_mi, "marq", marq_option_bynumber(option) );
966
}
967

968 969 970 971
/*****************************************************************************
 * libvlc_video_set_marquee_int: enable, disable or set an int option
 *****************************************************************************/
void libvlc_video_set_marquee_int( libvlc_media_player_t *p_mi,
972
                         unsigned option, int value )
973
{
974
    set_value( p_mi, "marq", marq_option_bynumber(option), VLC_VAR_INTEGER,
975
               &(vlc_value_t) { .i_int = value }, true );
976
}
977

978 979 980 981
/*****************************************************************************
 * libvlc_video_set_marquee_string: set a string option
 *****************************************************************************/
void libvlc_video_set_marquee_string( libvlc_media_player_t *p_mi,
982
                unsigned option, const char * value )
983
{
984
    set_value( p_mi, "marq", marq_option_bynumber(option), VLC_VAR_STRING,
985
               &(vlc_value_t){ .psz_string = (char *)value }, true );
986 987 988
}


989
/* logo module support */
990

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
991
static const opt_t *
992
logo_option_bynumber( unsigned option )
993
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
994 995
    static const opt_t vlogo_optlist[] =
    /* depends on libvlc_video_logo_option_t */
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    {
        { "logo",          0 },
        { "logo-file",     VLC_VAR_STRING },
        { "logo-x",        VLC_VAR_INTEGER },
        { "logo-y",        VLC_VAR_INTEGER },
        { "logo-delay",    VLC_VAR_INTEGER },
        { "logo-repeat",   VLC_VAR_INTEGER },
        { "logo-opacity",  VLC_VAR_INTEGER },
        { "logo-position", VLC_VAR_INTEGER },
    };
    enum { num_vlogo_opts = sizeof(vlogo_optlist) / sizeof(*vlogo_optlist) };

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1008
    const opt_t *r = option < num_vlogo_opts ? vlogo_optlist+option : NULL;
1009
    if( !r )
1010
        libvlc_printerr( "Unknown logo option" );
1011 1012 1013 1014
    return r;
}

void libvlc_video_set_logo_string( libvlc_media_player_t *p_mi,
1015
                                   unsigned option, const char *psz_value )
1016
{
1017
    set_value( p_mi,"logo",logo_option_bynumber(option), VLC_VAR_STRING,
1018
               &(vlc_value_t){ .psz_string = (char *)psz_value }, true );
1019 1020 1021 1022
}


void libvlc_video_set_logo_int( libvlc_media_player_t *p_mi,
1023
                                unsigned option, int value )
1024
{
1025
    set_value( p_mi, "logo", logo_option_bynumber(option), VLC_VAR_INTEGER,
1026
               &(vlc_value_t) { .i_int = value }, true );
1027 1028 1029 1030
}


int libvlc_video_get_logo_int( libvlc_media_player_t *p_mi,
1031
                               unsigned option )
1032
{
1033
    return get_int( p_mi, "logo", logo_option_bynumber(option) );
1034
}
1035 1036


1037 1038 1039
/* adjust module support */


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1040
static const opt_t *
1041 1042 1043 1044
adjust_option_bynumber( unsigned option )
{
    static const opt_t optlist[] =
    {
1045 1046 1047
        { "adjust",     0 },
        { "contrast",   VLC_VAR_FLOAT },
        { "brightness", VLC_VAR_FLOAT },
1048
        { "hue",        VLC_VAR_FLOAT },
1049 1050
        { "saturation", VLC_VAR_FLOAT },
        { "gamma",      VLC_VAR_FLOAT },
1051 1052 1053
    };
    enum { num_opts = sizeof(optlist) / sizeof(*optlist) };

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1054
    const opt_t *r = option < num_opts ? optlist+option : NULL;
1055 1056 1057 1058 1059 1060 1061 1062 1063
    if( !r )
        libvlc_printerr( "Unknown adjust option" );
    return r;
}


void libvlc_video_set_adjust_int( libvlc_media_player_t *p_mi,
                                  unsigned option, int value )
{
1064
    set_value( p_mi, "adjust", adjust_option_bynumber(option), VLC_VAR_INTEGER,
1065
               &(vlc_value_t) { .i_int = value }, false );
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
}


int libvlc_video_get_adjust_int( libvlc_media_player_t *p_mi,
                                 unsigned option )
{
    return get_int( p_mi, "adjust", adjust_option_bynumber(option) );
}


void libvlc_video_set_adjust_float( libvlc_media_player_t *p_mi,
                                    unsigned option, float value )
{
1079
    set_value( p_mi, "adjust", adjust_option_bynumber(option), VLC_VAR_FLOAT,
1080
               &(vlc_value_t) { .f_float = value }, false );
1081 1082 1083 1084 1085 1086 1087 1088
}


float libvlc_video_get_adjust_float( libvlc_media_player_t *p_mi,
                                     unsigned option )
{
    return get_float( p_mi, "adjust", adjust_option_bynumber(option) );
}