control.c 23.1 KB
Newer Older
Gildas Bazin's avatar
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * control.c
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
Gildas Bazin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * 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.
Gildas Bazin's avatar
Gildas Bazin committed
22 23
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
29 30 31

#include <stdio.h>
#include <stdlib.h>
Gildas Bazin's avatar
Gildas Bazin committed
32

Laurent Aimar's avatar
Laurent Aimar committed
33
#include "input_internal.h"
34

Gildas Bazin's avatar
Gildas Bazin committed
35

36 37
static void UpdateBookmarksOption( input_thread_t * );

Gildas Bazin's avatar
Gildas Bazin committed
38 39 40 41 42 43 44
/****************************************************************************
 * input_Control
 ****************************************************************************/
/**
 * Control function for inputs.
 * \param p_input input handle
 * \param i_query query type
45
 * \return VLC_SUCCESS if ok
Gildas Bazin's avatar
Gildas Bazin committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 */
int input_Control( input_thread_t *p_input, int i_query, ...  )
{
    va_list args;
    int     i_result;

    va_start( args, i_query );
    i_result = input_vaControl( p_input, i_query, args );
    va_end( args );

    return i_result;
}

int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
{
61
    seekpoint_t *p_bkmk, ***ppp_bkmk;
62 63
    int i_bkmk = 0;
    int *pi_bkmk;
64

65
    int i_int, *pi_int;
66
    bool b_bool, *pb_bool;
67 68
    double f, *pf;
    int64_t i_64, *pi_64;
Gildas Bazin's avatar
Gildas Bazin committed
69

70 71 72
    char *psz;
    vlc_value_t val;

Gildas Bazin's avatar
Gildas Bazin committed
73 74
    switch( i_query )
    {
75 76 77
        case INPUT_GET_POSITION:
            pf = (double*)va_arg( args, double * );
            *pf = var_GetFloat( p_input, "position" );
Laurent Aimar's avatar
Laurent Aimar committed
78 79
            return VLC_SUCCESS;

80 81
        case INPUT_SET_POSITION:
            f = (double)va_arg( args, double );
Laurent Aimar's avatar
Laurent Aimar committed
82
            return var_SetFloat( p_input, "position", f );
83 84 85 86

        case INPUT_GET_LENGTH:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = var_GetTime( p_input, "length" );
Laurent Aimar's avatar
Laurent Aimar committed
87 88
            return VLC_SUCCESS;

89 90 91
        case INPUT_GET_TIME:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = var_GetTime( p_input, "time" );
Laurent Aimar's avatar
Laurent Aimar committed
92 93
            return VLC_SUCCESS;

94 95
        case INPUT_SET_TIME:
            i_64 = (int64_t)va_arg( args, int64_t );
Laurent Aimar's avatar
Laurent Aimar committed
96
            return var_SetTime( p_input, "time", i_64 );
97 98 99 100

        case INPUT_GET_RATE:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = var_GetInteger( p_input, "rate" );
Laurent Aimar's avatar
Laurent Aimar committed
101 102
            return VLC_SUCCESS;

103 104
        case INPUT_SET_RATE:
            i_int = (int)va_arg( args, int );
Laurent Aimar's avatar
Laurent Aimar committed
105
            return var_SetInteger( p_input, "rate", i_int );
106 107 108 109

        case INPUT_GET_STATE:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = var_GetInteger( p_input, "state" );
Laurent Aimar's avatar
Laurent Aimar committed
110 111
            return VLC_SUCCESS;

112 113
        case INPUT_SET_STATE:
            i_int = (int)va_arg( args, int );
Laurent Aimar's avatar
Laurent Aimar committed
114
            return var_SetInteger( p_input, "state", i_int );
115

116 117 118 119
        case INPUT_GET_AUDIO_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = var_GetTime( p_input, "audio-delay" );
            return VLC_SUCCESS;
120

121 122 123 124
        case INPUT_GET_SPU_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = var_GetTime( p_input, "spu-delay" );
            return VLC_SUCCESS;
125

126 127 128
        case INPUT_SET_AUDIO_DELAY:
            i_64 = (int64_t)va_arg( args, int64_t );
            return var_SetTime( p_input, "audio-delay", i_64 );
129

130 131 132
        case INPUT_SET_SPU_DELAY:
            i_64 = (int64_t)va_arg( args, int64_t );
            return var_SetTime( p_input, "spu-delay", i_64 );
133 134

        case INPUT_ADD_INFO:
135
        {
136
            /* FIXME : Impossible to use input_item_AddInfo because of
137
             * the ... problem ? */
138 139 140 141
            char *psz_cat = (char *)va_arg( args, char * );
            char *psz_name = (char *)va_arg( args, char * );
            char *psz_format = (char *)va_arg( args, char * );

142
            info_category_t *p_cat;
143
            info_t *p_info;
144
            int i;
145

Clément Stenac's avatar
Clément Stenac committed
146 147
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            for( i = 0; i < p_input->p->input.p_item->i_categories; i++ )
148
            {
Clément Stenac's avatar
Clément Stenac committed
149
                if( !strcmp( p_input->p->input.p_item->pp_categories[i]->psz_name,
150
                             psz_cat ) ) break;
151 152
            }

Clément Stenac's avatar
Clément Stenac committed
153
            if( i == p_input->p->input.p_item->i_categories )
154 155
            {
                p_cat = malloc( sizeof( info_category_t ) );
156
                if( !p_cat )
157
                {
Rafaël Carré's avatar
Rafaël Carré committed
158
                    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
159
                    return VLC_EGENERIC;
160
                }
161 162 163
                p_cat->psz_name = strdup( psz_cat );
                p_cat->i_infos = 0;
                p_cat->pp_infos = NULL;
Clément Stenac's avatar
Clément Stenac committed
164 165 166
                INSERT_ELEM( p_input->p->input.p_item->pp_categories,
                             p_input->p->input.p_item->i_categories,
                             p_input->p->input.p_item->i_categories, p_cat );
167 168
            }

Clément Stenac's avatar
Clément Stenac committed
169
            p_cat = p_input->p->input.p_item->pp_categories[i];
170 171 172 173 174 175 176

            for( i = 0; i < p_cat->i_infos; i++ )
            {
                if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
                {
                    if( p_cat->pp_infos[i]->psz_value )
                        free( p_cat->pp_infos[i]->psz_value );
177
                    break;
178 179 180
                }
            }

181 182 183
            if( i == p_cat->i_infos )
            {
                p_info = malloc( sizeof( info_t ) );
184
                if( !p_info )
185
                {
Rafaël Carré's avatar
Rafaël Carré committed
186
                    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
187
                    return VLC_EGENERIC;
188 189
                }

190 191 192 193
                INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos,
                             p_cat->i_infos, p_info );
                p_info->psz_name = strdup( psz_name );
            }
194

195
            p_info = p_cat->pp_infos[i];
196 197
            if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
                p_info->psz_value = NULL;
198

Clément Stenac's avatar
Clément Stenac committed
199
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
200

201
            if( !p_input->b_preparsing )
202 203 204
            {
                vlc_event_t event;
                event.type = vlc_InputItemInfoChanged;
205
                vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
206
            }
207
        }
208
        return VLC_SUCCESS;
209

210 211 212 213 214 215
        case INPUT_DEL_INFO:
        {
            char *psz_cat = (char *)va_arg( args, char * );
            char *psz_name = (char *)va_arg( args, char * );

            info_category_t *p_cat = NULL;
216
            int i_cat;
217 218
            int i;

Clément Stenac's avatar
Clément Stenac committed
219
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
220
            for( i_cat = 0; i_cat < p_input->p->input.p_item->i_categories; i_cat++ )
221
            {
222
                if( !strcmp( p_input->p->input.p_item->pp_categories[i_cat]->psz_name,
223 224
                             psz_cat ) )
                {
225
                    p_cat = p_input->p->input.p_item->pp_categories[i_cat];
226 227 228 229 230
                    break;
                }
            }
            if( p_cat == NULL )
            {
Clément Stenac's avatar
Clément Stenac committed
231
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
232 233 234
                return VLC_EGENERIC;
            }

235
            if( psz_name )
236
            {
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                /* Remove a specific info */
                for( i = 0; i < p_cat->i_infos; i++ )
                {
                    if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
                    {
                        free( p_cat->pp_infos[i]->psz_name );
                        if( p_cat->pp_infos[i]->psz_value )
                            free( p_cat->pp_infos[i]->psz_value );
                        free( p_cat->pp_infos[i] );
                        REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
                        break;
                    }
                }
                if( i >= p_cat->i_infos )
                {
                    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
                    return VLC_EGENERIC;
                }
            }
            else
            {
                /* Remove the complete categorie */
                for( i = 0; i < p_cat->i_infos; i++ )
260
                {
261 262 263 264
                    free( p_cat->pp_infos[i]->psz_name );
                    if( p_cat->pp_infos[i]->psz_value )
                        free( p_cat->pp_infos[i]->psz_value );
                    free( p_cat->pp_infos[i] );
265
                }
266 267 268
                if( p_cat->pp_infos )
                    free( p_cat->pp_infos );
                REMOVE_ELEM( p_input->p->input.p_item->pp_categories, p_input->p->input.p_item->i_categories, i_cat );
269
            }
Clément Stenac's avatar
Clément Stenac committed
270
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
271

272
            if( !p_input->b_preparsing )
273 274 275
            {
                vlc_event_t event;
                event.type = vlc_InputItemInfoChanged;
276
                vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
277
            }
278
            return VLC_SUCCESS;
279 280
        }

281

282 283 284 285 286
        case INPUT_GET_INFO:
        {
            char *psz_cat = (char *)va_arg( args, char * );
            char *psz_name = (char *)va_arg( args, char * );
            char **ppsz_value = (char **)va_arg( args, char ** );
287
            int i_ret = VLC_EGENERIC;
288 289
            *ppsz_value = NULL;

290
            *ppsz_value = input_item_GetInfo( p_input->p->input.p_item,
291
                                                  psz_cat, psz_name );
292 293 294 295 296 297 298
            return i_ret;
        }

        case INPUT_SET_NAME:
        {
            char *psz_name = (char *)va_arg( args, char * );

299
            if( !psz_name ) return VLC_EGENERIC;
300

Clément Stenac's avatar
Clément Stenac committed
301 302 303 304 305
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( p_input->p->input.p_item->psz_name )
                free( p_input->p->input.p_item->psz_name );
            p_input->p->input.p_item->psz_name = strdup( psz_name );
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
306

307
            if( !p_input->b_preparsing )
308 309 310 311
            {
                vlc_event_t event;
                event.type = vlc_InputItemNameChanged;
                event.u.input_item_name_changed.new_name = psz_name;
312
                vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
313
            }
314
            return VLC_SUCCESS;
315 316
        }

Gildas Bazin's avatar
Gildas Bazin committed
317 318 319
        case INPUT_ADD_BOOKMARK:
            p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
            p_bkmk = vlc_seekpoint_Duplicate( p_bkmk );
320

Clément Stenac's avatar
Clément Stenac committed
321
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
Gildas Bazin's avatar
Gildas Bazin committed
322 323
            if( !p_bkmk->psz_name )
            {
324 325 326
                 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
                               p_input->p->i_bookmark ) == -1 )
                     p_bkmk->psz_name = NULL;
Gildas Bazin's avatar
Gildas Bazin committed
327
            }
328

Clément Stenac's avatar
Clément Stenac committed
329
            TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
Gildas Bazin's avatar
Gildas Bazin committed
330 331 332 333

            /* Reflect the changes on the object var */
            var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
            {
334
                vlc_value_t val, text;
Gildas Bazin's avatar
Gildas Bazin committed
335
                int i;
336

Clément Stenac's avatar
Clément Stenac committed
337
                for( i = 0; i < p_input->p->i_bookmark; i++ )
Gildas Bazin's avatar
Gildas Bazin committed
338 339
                {
                    val.i_int = i;
Clément Stenac's avatar
Clément Stenac committed
340
                    text.psz_string = p_input->p->bookmark[i]->psz_name;
Gildas Bazin's avatar
Gildas Bazin committed
341 342 343 344
                    var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
                                &val, &text );
                }
            }
Clément Stenac's avatar
Clément Stenac committed
345
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
Gildas Bazin's avatar
Gildas Bazin committed
346

347 348
            UpdateBookmarksOption( p_input );

349
            return VLC_SUCCESS;
Gildas Bazin's avatar
Gildas Bazin committed
350

351
        case INPUT_CHANGE_BOOKMARK:
352 353 354
            p_bkmk = (seekpoint_t *)va_arg( args, seekpoint_t * );
            i_bkmk = (int)va_arg( args, int );

Clément Stenac's avatar
Clément Stenac committed
355 356
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( i_bkmk < p_input->p->i_bookmark )
357 358 359 360
            {
                vlc_value_t val, text;
                int i;

Clément Stenac's avatar
Clément Stenac committed
361
                p_input->p->bookmark[i_bkmk] = p_bkmk;
362 363 364

                /* Reflect the changes on the object var */
                var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
Clément Stenac's avatar
Clément Stenac committed
365
                for( i = 0; i < p_input->p->i_bookmark; i++ )
366 367
                {
                    val.i_int = i;
Clément Stenac's avatar
Clément Stenac committed
368
                    text.psz_string = p_input->p->bookmark[i]->psz_name;
369 370 371 372
                    var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
                                &val, &text );
                }
            }
Clément Stenac's avatar
Clément Stenac committed
373
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
374 375 376 377

            UpdateBookmarksOption( p_input );

            return VLC_SUCCESS;
378

Gildas Bazin's avatar
Gildas Bazin committed
379
        case INPUT_DEL_BOOKMARK:
380
            i_bkmk = (int)va_arg( args, int );
381

Clément Stenac's avatar
Clément Stenac committed
382 383
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( i_bkmk < p_input->p->i_bookmark )
Gildas Bazin's avatar
Gildas Bazin committed
384
            {
385
                vlc_value_t val, text;
Gildas Bazin's avatar
Gildas Bazin committed
386
                int i;
387

Clément Stenac's avatar
Clément Stenac committed
388 389
                p_bkmk = p_input->p->bookmark[i_bkmk];
                TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
390 391 392 393 394
                            p_bkmk );
                vlc_seekpoint_Delete( p_bkmk );

                /* Reflect the changes on the object var */
                var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
Clément Stenac's avatar
Clément Stenac committed
395
                for( i = 0; i < p_input->p->i_bookmark; i++ )
Gildas Bazin's avatar
Gildas Bazin committed
396
                {
397
                    val.i_int = i;
Clément Stenac's avatar
Clément Stenac committed
398
                    text.psz_string = p_input->p->bookmark[i]->psz_name;
399 400
                    var_Change( p_input, "bookmark", VLC_VAR_ADDCHOICE,
                                &val, &text );
Gildas Bazin's avatar
Gildas Bazin committed
401
                }
Clément Stenac's avatar
Clément Stenac committed
402
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
403 404 405

                UpdateBookmarksOption( p_input );

406
                return VLC_SUCCESS;
Gildas Bazin's avatar
Gildas Bazin committed
407
            }
Clément Stenac's avatar
Clément Stenac committed
408
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
409 410

            return VLC_EGENERIC;
Gildas Bazin's avatar
Gildas Bazin committed
411 412 413 414

        case INPUT_GET_BOOKMARKS:
            ppp_bkmk = (seekpoint_t ***)va_arg( args, seekpoint_t *** );
            pi_bkmk = (int *)va_arg( args, int * );
415

Clément Stenac's avatar
Clément Stenac committed
416 417
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( p_input->p->i_bookmark )
Gildas Bazin's avatar
Gildas Bazin committed
418 419 420
            {
                int i;

Clément Stenac's avatar
Clément Stenac committed
421
                *pi_bkmk = p_input->p->i_bookmark;
Gildas Bazin's avatar
Gildas Bazin committed
422
                *ppp_bkmk = malloc( sizeof(seekpoint_t *) *
Clément Stenac's avatar
Clément Stenac committed
423 424
                                    p_input->p->i_bookmark );
                for( i = 0; i < p_input->p->i_bookmark; i++ )
Gildas Bazin's avatar
Gildas Bazin committed
425 426
                {
                    (*ppp_bkmk)[i] =
Clément Stenac's avatar
Clément Stenac committed
427
                        vlc_seekpoint_Duplicate(p_input->p->bookmark[i]);
Gildas Bazin's avatar
Gildas Bazin committed
428
                }
429

Clément Stenac's avatar
Clément Stenac committed
430
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
431
                return VLC_SUCCESS;
Gildas Bazin's avatar
Gildas Bazin committed
432 433 434 435 436
            }
            else
            {
                *ppp_bkmk = NULL;
                *pi_bkmk = 0;
437

Clément Stenac's avatar
Clément Stenac committed
438
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
439
                return VLC_EGENERIC;
Gildas Bazin's avatar
Gildas Bazin committed
440 441 442 443
            }
            break;

        case INPUT_CLEAR_BOOKMARKS:
444

Clément Stenac's avatar
Clément Stenac committed
445 446
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( p_input->p->i_bookmark )
Gildas Bazin's avatar
Gildas Bazin committed
447 448 449
            {
                int i;

Clément Stenac's avatar
Clément Stenac committed
450
                for( i = p_input->p->i_bookmark - 1; i >= 0; i-- )
Gildas Bazin's avatar
Gildas Bazin committed
451
                {
Clément Stenac's avatar
Clément Stenac committed
452 453
                    p_bkmk = p_input->p->bookmark[i];
                    TAB_REMOVE( p_input->p->i_bookmark, p_input->p->bookmark,
Gildas Bazin's avatar
Gildas Bazin committed
454 455 456
                                p_bkmk );
                    vlc_seekpoint_Delete( p_bkmk );
                }
457
                var_Change( p_input, "bookmark", VLC_VAR_CLEARCHOICES, 0, 0 );
Gildas Bazin's avatar
Gildas Bazin committed
458
            }
Clément Stenac's avatar
Clément Stenac committed
459
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
460 461 462

            UpdateBookmarksOption( p_input );

463
            return VLC_SUCCESS;
Gildas Bazin's avatar
Gildas Bazin committed
464 465 466

        case INPUT_SET_BOOKMARK:
            i_bkmk = (int)va_arg( args, int );
467

Clément Stenac's avatar
Clément Stenac committed
468 469
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( i_bkmk >= 0 && i_bkmk < p_input->p->i_bookmark )
Gildas Bazin's avatar
Gildas Bazin committed
470 471
            {
                vlc_value_t pos;
472 473
                int i_ret;

Clément Stenac's avatar
Clément Stenac committed
474
                if( p_input->p->bookmark[i_bkmk]->i_time_offset != -1 )
475
                {
Clément Stenac's avatar
Clément Stenac committed
476
                    pos.i_time = p_input->p->bookmark[i_bkmk]->i_time_offset;
477 478
                    i_ret = var_Set( p_input, "time", pos );
                }
Clément Stenac's avatar
Clément Stenac committed
479
                else if( p_input->p->bookmark[i_bkmk]->i_byte_offset != -1 )
Gildas Bazin's avatar
Gildas Bazin committed
480
                {
481
                    // don't crash on bookmarks in live streams
Clément Stenac's avatar
Clément Stenac committed
482
                    if( stream_Size( p_input->p->input.p_stream ) == 0 )
483
                    {
Clément Stenac's avatar
Clément Stenac committed
484
                        vlc_mutex_unlock( &p_input->p->input.p_item->lock );
485 486
                        return VLC_EGENERIC;
                    }
Clément Stenac's avatar
Clément Stenac committed
487 488 489
                    pos.f_float = !p_input->p->input.p_stream ? 0 :
                        p_input->p->bookmark[i_bkmk]->i_byte_offset /
                        stream_Size( p_input->p->input.p_stream );
Gildas Bazin's avatar
Gildas Bazin committed
490 491
                    i_ret = var_Set( p_input, "position", pos );
                }
492
                else
Gildas Bazin's avatar
Gildas Bazin committed
493
                {
494 495
                    pos.f_float = 0;
                    i_ret = var_Set( p_input, "position", pos );
Gildas Bazin's avatar
Gildas Bazin committed
496
                }
497

Clément Stenac's avatar
Clément Stenac committed
498
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
499
                return i_ret;
Gildas Bazin's avatar
Gildas Bazin committed
500 501 502
            }
            else
            {
Clément Stenac's avatar
Clément Stenac committed
503
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
504
                return VLC_EGENERIC;
Gildas Bazin's avatar
Gildas Bazin committed
505
            }
506 507 508 509 510

            break;

        case INPUT_ADD_OPTION:
        {
511 512 513
            const char *psz_option = va_arg( args, const char * );
            const char *psz_value = va_arg( args, const char * );
            char *str;
514 515
            int i;

516 517
            if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
                return VLC_ENOMEM;
518

519
            i = input_item_AddOpt( p_input->p->input.p_item, str,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
520
                                  VLC_INPUT_OPTION_UNIQUE );
521 522
            free( str );
            return i;
523 524 525 526
        }

        case INPUT_GET_BYTE_POSITION:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
Clément Stenac's avatar
Clément Stenac committed
527 528
            *pi_64 = !p_input->p->input.p_stream ? 0 :
                stream_Tell( p_input->p->input.p_stream );
529 530 531 532
            return VLC_SUCCESS;

        case INPUT_SET_BYTE_SIZE:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
Clément Stenac's avatar
Clément Stenac committed
533 534
            *pi_64 = !p_input->p->input.p_stream ? 0 :
                stream_Size( p_input->p->input.p_stream );
535
            return VLC_SUCCESS;
536

537 538 539 540 541 542 543 544 545 546 547 548
        case INPUT_GET_VIDEO_FPS:
        {
            int i;
            pf = (double*)va_arg( args, double * );
            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            *pf = p_input->p->input.f_fps;
            for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
                *pf = p_input->p->slave[i]->f_fps;
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
            return VLC_SUCCESS;
        }

549 550 551 552 553 554 555 556
        case INPUT_ADD_SLAVE:
            psz = (char*)va_arg( args, char * );
            if( psz && *psz )
            {
                val.psz_string = strdup( psz );
                input_ControlPush( p_input, INPUT_CONTROL_ADD_SLAVE, &val );
            }
            return VLC_SUCCESS;
Gildas Bazin's avatar
Gildas Bazin committed
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
        case INPUT_GET_ATTACHMENTS: /* arg1=input_attachment_t***, arg2=int*  res=can fail */
        {
            input_attachment_t ***ppp_attachment = (input_attachment_t***)va_arg( args, input_attachment_t *** );
            int *pi_attachment = (int*)va_arg( args, int * );
            int i;

            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            if( p_input->p->i_attachment <= 0 )
            {
                vlc_mutex_unlock( &p_input->p->input.p_item->lock );
                *ppp_attachment = NULL;
                *pi_attachment = 0;
                return VLC_EGENERIC;
            }
            *pi_attachment = p_input->p->i_attachment;
            *ppp_attachment = malloc( sizeof(input_attachment_t**) * p_input->p->i_attachment );
            for( i = 0; i < p_input->p->i_attachment; i++ )
                (*ppp_attachment)[i] = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );

            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
578
            return VLC_SUCCESS;
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
        }

        case INPUT_GET_ATTACHMENT:  /* arg1=input_attachment_t**, arg2=char*  res=can fail */
        {
            input_attachment_t **pp_attachment = (input_attachment_t**)va_arg( args, input_attachment_t ** );
            const char *psz_name = (const char*)va_arg( args, const char * );
            int i;

            vlc_mutex_lock( &p_input->p->input.p_item->lock );
            for( i = 0; i < p_input->p->i_attachment; i++ )
            {
                if( !strcmp( p_input->p->attachment[i]->psz_name, psz_name ) )
                {
                    *pp_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i] );
                    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
                    return VLC_SUCCESS;
                }
            }
            *pp_attachment = NULL;
            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
            return VLC_EGENERIC;
        }

602 603 604 605 606 607 608 609 610
        case INPUT_SET_RECORD_STATE:
            b_bool = (bool)va_arg( args, int );
            var_SetBool( p_input, "record", b_bool );
            return VLC_SUCCESS;

        case INPUT_GET_RECORD_STATE:
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = var_GetBool( p_input, "record" );
            return VLC_SUCCESS;
611

612 613 614 615 616
        case INPUT_RESTART_ES:
            val.i_int = (int)va_arg( args, int );
            input_ControlPush( p_input, INPUT_CONTROL_RESTART_ES, &val );
            return VLC_SUCCESS;

Gildas Bazin's avatar
Gildas Bazin committed
617 618
        default:
            msg_Err( p_input, "unknown query in input_vaControl" );
Laurent Aimar's avatar
Laurent Aimar committed
619
            return VLC_EGENERIC;
Gildas Bazin's avatar
Gildas Bazin committed
620 621
    }
}
622 623 624 625 626 627

static void UpdateBookmarksOption( input_thread_t *p_input )
{
    int i, i_len = 0;
    char *psz_value = NULL, *psz_next = NULL;

Clément Stenac's avatar
Clément Stenac committed
628
    vlc_mutex_lock( &p_input->p->input.p_item->lock );
Rémi Duraffort's avatar
Rémi Duraffort committed
629
    if( p_input->p->i_bookmark > 0 )
630
    {
Rémi Duraffort's avatar
Rémi Duraffort committed
631 632 633 634 635 636 637 638
        for( i = 0; i < p_input->p->i_bookmark; i++ )
        {
            i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
                               p_input->p->bookmark[i]->psz_name,
                               p_input->p->bookmark[i]->i_byte_offset,
                               p_input->p->bookmark[i]->i_time_offset/1000000 );
        }
        psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
639

Rémi Duraffort's avatar
Rémi Duraffort committed
640
        for( i = 0; i < p_input->p->i_bookmark; i++ )
641
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
642 643 644 645 646 647 648 649
            sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
                     p_input->p->bookmark[i]->psz_name,
                     p_input->p->bookmark[i]->i_byte_offset,
                     p_input->p->bookmark[i]->i_time_offset/1000000 );

            psz_next += strlen( psz_next );
            if( i < p_input->p->i_bookmark - 1)
                *psz_next = ','; psz_next++;
650 651
        }
    }
Clément Stenac's avatar
Clément Stenac committed
652
    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
653

654 655
    input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
                   psz_value ? psz_value : "" );
Rémi Duraffort's avatar
Rémi Duraffort committed
656
    free( psz_value );
657
}
Rémi Duraffort's avatar
Rémi Duraffort committed
658