mediacontrol_util.c 8.62 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
/*****************************************************************************
 * util.c: Utility functions and exceptions management
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
zorglub's avatar
zorglub committed
22
23
 *****************************************************************************/

zorglub's avatar
zorglub committed
24
#include <vlc/mediacontrol.h>
zorglub's avatar
zorglub committed
25

zorglub's avatar
zorglub committed
26
27
28
#include <vlc_interface.h>
#include <vlc_aout.h>
#include <vlc_input.h>
zorglub's avatar
zorglub committed
29
30
#include <vlc_demux.h>

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

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

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

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
gbazin's avatar
gbazin committed
48
49
50
#ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#endif
zorglub's avatar
zorglub committed
51

zorglub's avatar
zorglub committed
52
53
54
/* FIXME: Need to stop accessing private input structures !! */
#include "input/input_internal.h"

55
vlc_int64_t private_mediacontrol_unit_convert( input_thread_t *p_input,
56
57
58
                                       mediacontrol_PositionKey from,
                                       mediacontrol_PositionKey to,
                                       vlc_int64_t value )
zorglub's avatar
zorglub committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
    if( to == from )
        return value;

    /* For all conversions, we need data from p_input */
    if( !p_input )
        return 0;

    switch( from )
    {
    case mediacontrol_MediaTime:
        if( to == mediacontrol_ByteCount )
        {
            /* FIXME */
            /* vlc < 0.8 API */
            /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
            return 0;
        }
        if( to == mediacontrol_SampleCount )
        {
            double f_fps;

zorglub's avatar
zorglub committed
81
            if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
zorglub's avatar
zorglub committed
82
83
84
85
86
87
88
89
90
91
92
93
                return 0;
            else
                return( value * f_fps / 1000.0 );
        }
        /* Cannot happen */
        /* See http://catb.org/~esr/jargon/html/entry/can't-happen.html */
        break;

    case mediacontrol_SampleCount:
    {
        double f_fps;

zorglub's avatar
zorglub committed
94
        if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) ||
zorglub's avatar
zorglub committed
95
                                f_fps < 0.1 )
zorglub's avatar
zorglub committed
96
97
98
99
100
101
            return 0;

        if( to == mediacontrol_ByteCount )
        {
            /* FIXME */
            /* vlc < 0.8 API */
102
/*             return ( vlc_int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
zorglub's avatar
zorglub committed
103
104
105
106
            return 0;
        }

        if( to == mediacontrol_MediaTime )
107
            return( vlc_int64_t )( value * 1000.0 / ( double )f_fps );
zorglub's avatar
zorglub committed
108
109
110
111
112
113
114
115
116
117
118

        /* Cannot happen */
        break;
    }
    case mediacontrol_ByteCount:
        /* FIXME */
        return 0;
/* vlc < 0.8 API: */

//         if( p_input->stream.i_mux_rate == 0 )
//             return 0;
119
//
zorglub's avatar
zorglub committed
120
121
122
123
124
//         /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
//            The 50 hardcoded constant comes from the definition of i_mux_rate :
//            i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
//            0 if undef */
//         if( to == mediacontrol_MediaTime )
125
//             return ( vlc_int64_t )( 1000 * value / 50 / p_input->stream.i_mux_rate );
126
//
zorglub's avatar
zorglub committed
127
128
129
130
131
132
//         if( to == mediacontrol_SampleCount )
//         {
//             double f_fps;
//             if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
//                 return 0;
//             else
133
//                 return ( vlc_int64_t )( value * f_fps / 50 / p_input->stream.i_mux_rate );
zorglub's avatar
zorglub committed
134
135
136
137
138
139
140
141
142
143
//         }
        /* Cannot happen */
        break;
    }
    /* Cannot happen */
    return 0;
}

/* Converts a mediacontrol_Position into a time in microseconds in
   movie clock time */
144
vlc_int64_t
145
private_mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
zorglub's avatar
zorglub committed
146
147
148
149
{
    switch( pos->origin )
    {
    case mediacontrol_AbsolutePosition:
150
        return ( 1000 * private_mediacontrol_unit_convert( p_input,
zorglub's avatar
zorglub committed
151
152
153
154
155
156
                                                   pos->key, /* from */
                                                   mediacontrol_MediaTime,  /* to */
                                                   pos->value ) );
        break;
    case mediacontrol_RelativePosition:
    {
157
        vlc_int64_t l_pos;
zorglub's avatar
zorglub committed
158
159
160
161
162
163
164
165
        vlc_value_t val;

        val.i_time = 0;
        if( p_input )
        {
            var_Get( p_input, "time", &val );
        }

166
        l_pos = 1000 * private_mediacontrol_unit_convert( p_input,
zorglub's avatar
zorglub committed
167
168
169
170
171
172
173
174
                                                  pos->key,
                                                  mediacontrol_MediaTime,
                                                  pos->value );
        return val.i_time + l_pos;
        break;
    }
    case mediacontrol_ModuloPosition:
    {
175
        vlc_int64_t l_pos;
zorglub's avatar
zorglub committed
176
177
178
179
180
181
182
183
184
185
        vlc_value_t val;

        val.i_time = 0;
        if( p_input )
        {
            var_Get( p_input, "length", &val );
        }

        if( val.i_time > 0)
        {
186
            l_pos = ( 1000 * private_mediacontrol_unit_convert( p_input,
zorglub's avatar
zorglub committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
                                                        pos->key,
                                                        mediacontrol_MediaTime,
                                                        pos->value ) );
        }
        else
            l_pos = 0;

        return l_pos % val.i_time;
        break;
    }
    }
    return 0;
}

mediacontrol_RGBPicture*
Olivier Aubert's avatar
Olivier Aubert committed
202
private_mediacontrol_RGBPicture__alloc( int datasize )
zorglub's avatar
zorglub committed
203
204
205
206
207
208
209
210
{
    mediacontrol_RGBPicture* pic;

    pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
    if( ! pic )
        return NULL;

    pic->size = datasize;
211
    pic->data = ( char* )malloc( datasize * sizeof( char ) );
zorglub's avatar
zorglub committed
212
213
214
215
216
217
218
    return pic;
}

void
mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
{
    if( pic )
219
    {
zorglub's avatar
zorglub committed
220
        free( pic->data );
221
222
        free( pic );
    }
zorglub's avatar
zorglub committed
223
224
225
}

mediacontrol_PlaylistSeq*
Olivier Aubert's avatar
Olivier Aubert committed
226
private_mediacontrol_PlaylistSeq__alloc( int size )
zorglub's avatar
zorglub committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{
    mediacontrol_PlaylistSeq* ps;

    ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
    if( ! ps )
        return NULL;

    ps->size = size;
    ps->data = ( char** )malloc( size * sizeof( char* ) );
    return ps;
}

void
mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
{
    if( ps )
    {
        int i;
        for( i = 0 ; i < ps->size ; i++ )
            free( ps->data[i] );
247
248
        free( ps->data );
        free( ps );
zorglub's avatar
zorglub committed
249
250
251
    }
}

252
253
254
255
256
257
258
259
260
261
262
void
mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
{
  if( p_si )
  {
      free( p_si->url );
      free( p_si );
  }
}


zorglub's avatar
zorglub committed
263
mediacontrol_Exception*
264
265
266
mediacontrol_exception_create( void )
{
    mediacontrol_Exception* exception;
267
 
268
269
270
271
272
273
    exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
    mediacontrol_exception_init( exception );
    return exception;
}

void
zorglub's avatar
zorglub committed
274
275
mediacontrol_exception_init( mediacontrol_Exception *exception )
{
276
    if( exception )
277
    {
278
279
        exception->code = 0;
        exception->message = NULL;
280
    }
zorglub's avatar
zorglub committed
281
282
283
}

void
284
mediacontrol_exception_cleanup( mediacontrol_Exception *exception )
zorglub's avatar
zorglub committed
285
{
286
287
288
    if( exception )
        free( exception->message );
}
zorglub's avatar
zorglub committed
289

290
291
292
293
void
mediacontrol_exception_free( mediacontrol_Exception *exception )
{
    mediacontrol_exception_cleanup( exception );
zorglub's avatar
zorglub committed
294
295
296
297
    free( exception );
}

mediacontrol_RGBPicture*
Olivier Aubert's avatar
Olivier Aubert committed
298
private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, vlc_int64_t l_date,
zorglub's avatar
zorglub committed
299
300
301
302
                                char* p_data, int i_datasize )
{
    mediacontrol_RGBPicture *retval;

Olivier Aubert's avatar
Olivier Aubert committed
303
    retval = private_mediacontrol_RGBPicture__alloc( i_datasize );
zorglub's avatar
zorglub committed
304
305
306
307
308
309
310
311
312
313
314
    if( retval )
    {
        retval->width  = i_width;
        retval->height = i_height;
        retval->type   = i_chroma;
        retval->date   = l_date;
        retval->size   = i_datasize;
        memcpy( retval->data, p_data, i_datasize );
    }
    return retval;
}