stream.c 12.2 KB
Newer Older
1 2 3
/*****************************************************************************
 * stream.c
 *****************************************************************************
4
 * Copyright (C) 1999-2004 VideoLAN
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
5
 * $Id: stream.c,v 1.10 2004/01/06 21:42:43 sigmunau Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <stdlib.h>
#include <vlc/vlc.h>
#include <vlc/input.h>

#include "ninput.h"

Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
30
#define MAX_LINE 1024
31 32 33 34 35 36
/****************************************************************************
 * stream_* :
 * XXX for now it's just a wrapper
 *
 ****************************************************************************/

37 38 39
/**
 * Handle to a stream.
 */
40 41 42 43
struct stream_t
{
    VLC_COMMON_MEMBERS

44
    /** pointer to the input thread */
45 46 47 48
    input_thread_t *p_input;

};

49 50 51
/**
 * Create a "stream_t *" from an "input_thread_t *".
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
52
stream_t *stream_OpenInput( input_thread_t *p_input )
53 54 55 56 57 58 59 60 61 62 63 64
{
    stream_t *s;

    s = vlc_object_create( p_input, sizeof( stream_t ) );
    if( s )
    {
        s->p_input = p_input;
    }

    return s;
}

65 66 67
/**
 * Destroy a previously created "stream_t *" instance.
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
68
void stream_Release( stream_t *s )
69 70 71 72
{
    vlc_object_destroy( s );
}

73
/**
Gildas Bazin's avatar
 
Gildas Bazin committed
74 75
 * Similar to #stream_Control(), but takes a va_list and not variable
 * arguments.
76
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
77
int stream_vaControl( stream_t *s, int i_query, va_list args )
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
    vlc_bool_t *p_b;
    int64_t    *p_i64, i64;

    switch( i_query )
    {
        case STREAM_GET_SIZE:
            p_i64 = (int64_t*) va_arg( args, int64_t * );

            vlc_mutex_lock( &s->p_input->stream.stream_lock );
            *p_i64 = s->p_input->stream.p_selected_area->i_size;
            vlc_mutex_unlock( &s->p_input->stream.stream_lock );
            return VLC_SUCCESS;

        case STREAM_CAN_SEEK:
            p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );

            vlc_mutex_lock( &s->p_input->stream.stream_lock );
            *p_b = s->p_input->stream.b_seekable;
            vlc_mutex_unlock( &s->p_input->stream.stream_lock );
            return VLC_SUCCESS;

        case STREAM_CAN_FASTSEEK:
            p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );

            vlc_mutex_lock( &s->p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
104 105
            *p_b = s->p_input->stream.b_seekable &&
                   s->p_input->stream.i_method == INPUT_METHOD_FILE;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            vlc_mutex_unlock( &s->p_input->stream.stream_lock );
            return VLC_SUCCESS;

        case STREAM_GET_POSITION:
            p_i64 = (int64_t*) va_arg( args, int64_t * );

            vlc_mutex_lock( &s->p_input->stream.stream_lock );
            *p_i64 = s->p_input->stream.p_selected_area->i_tell;
            vlc_mutex_unlock( &s->p_input->stream.stream_lock );
            return VLC_SUCCESS;

        case STREAM_SET_POSITION:
            i64 = (int64_t) va_arg( args, int64_t );

            vlc_mutex_lock( &s->p_input->stream.stream_lock );
            if( i64 < 0 ||
                ( s->p_input->stream.p_selected_area->i_size > 0 &&
                  s->p_input->stream.p_selected_area->i_size < i64 ) )
            {
                vlc_mutex_unlock( &s->p_input->stream.stream_lock );
126
                msg_Warn( s, "seek out of bound" );
127 128 129 130 131 132 133 134 135 136 137 138
                return VLC_EGENERIC;
            }
            vlc_mutex_unlock( &s->p_input->stream.stream_lock );

            if( i64 == s->p_input->stream.p_selected_area->i_tell )
            {
                return VLC_SUCCESS;
            }

            if( s->p_input->stream.b_seekable &&
                ( s->p_input->stream.i_method == INPUT_METHOD_FILE ||
                  i64 - s->p_input->stream.p_selected_area->i_tell < 0 ||
139
                  i64 - s->p_input->stream.p_selected_area->i_tell > 4096 ) )
140 141 142 143 144 145 146 147
            {
                input_AccessReinit( s->p_input );
                s->p_input->pf_seek( s->p_input, i64 );
                return VLC_SUCCESS;
            }

            if( i64 - s->p_input->stream.p_selected_area->i_tell > 0 )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
148 149
                data_packet_t *p_data;
                int i_skip = i64 - s->p_input->stream.p_selected_area->i_tell;
150

Laurent Aimar's avatar
Laurent Aimar committed
151 152 153 154
                if( i_skip > 1000 )
                {
                    msg_Warn( s, "will skip %d bytes, slow", i_skip );
                }
155 156 157 158 159

                while (i_skip > 0 )
                {
                    int i_read;

160
                    i_read = input_SplitBuffer( s->p_input, &p_data,
Gildas Bazin's avatar
 
Gildas Bazin committed
161
                                 __MIN( (int)s->p_input->i_bufsize, i_skip ) );
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                    if( i_read < 0 )
                    {
                        return VLC_EGENERIC;
                    }
                    i_skip -= i_read;

                    input_DeletePacket( s->p_input->p_method_data, p_data );
                    if( i_read == 0 && i_skip > 0 )
                    {
                        return VLC_EGENERIC;
                    }
                }
            }

            return VLC_SUCCESS;

        default:
            msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
}

184 185 186 187 188
/**
 * Use to control the "stream_t *". Look at #stream_query_e for
 * possible "i_query" value and format arguments.  Return VLC_SUCCESS
 * if ... succeed ;) and VLC_EGENERIC if failed or unimplemented
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
189
int stream_Control( stream_t *s, int i_query, ... )
190 191 192 193 194 195 196 197 198 199 200
{
    va_list args;
    int     i_result;

    va_start( args, i_query );
    i_result = stream_vaControl( s, i_query, args );
    va_end( args );

    return i_result;
}

201 202 203 204 205 206
/**
 * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
 * "p_read" is NULL then data are skipped instead of read.  The return
 * value is the real numbers of bytes read/skip. If this value is less
 * than i_read that means that it's the end of the stream.
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
207
int stream_Read( stream_t *s, void *p_data, int i_data )
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
    uint8_t       *p = (uint8_t*)p_data;
    data_packet_t *p_packet;

    int i_read = 0;

    if( p_data == NULL && i_data > 0 )
    {
        int64_t i_pos;

        stream_Control( s, STREAM_GET_POSITION, &i_pos );

        i_pos += i_data;
        if( stream_Control( s, STREAM_SET_POSITION, i_pos ) )
        {
            return 0;
        }
        return i_data;
    }

    while( i_data > 0 && !s->p_input->b_die )
    {
        int i_count;

232
        i_count = input_SplitBuffer( s->p_input, &p_packet,
Gildas Bazin's avatar
 
Gildas Bazin committed
233
                      __MIN( i_data, (int)s->p_input->i_bufsize ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
234

235 236
        if( i_count <= 0 )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
237 238 239
            if( i_count == 0 )
                input_DeletePacket( s->p_input->p_method_data, p_packet );

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            return i_read;
        }

        if( p )
        {
            memcpy( p, p_packet->p_payload_start, i_count );
            p += i_count;
        }

        input_DeletePacket( s->p_input->p_method_data, p_packet );

        i_data -= i_count;
        i_read += i_count;
    }

    return i_read;
}

258 259
/**
 * Store in pp_peek a pointer to the next "i_peek" bytes in the stream
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
260 261 262 263 264
 * \return The real numbers of valid bytes, if it's less
 * or equal to 0, *pp_peek is invalid.
 * \note pp_peek is a pointer to internal buffer and it will be invalid as
 * soons as other stream_* functions are called.
 * \note Due to input limitation, it could be less than i_peek without meaning
265 266 267
 * the end of the stream (but only when you have i_peek >=
 * p_input->i_bufsize)
 */
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
268
int stream_Peek( stream_t *s, uint8_t **pp_peek, int i_peek )
269
{
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
270
    return input_Peek( s->p_input, pp_peek, i_peek );
271 272
}

Gildas Bazin's avatar
 
Gildas Bazin committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
/**
 * Read "i_size" bytes and store them in a block_t. If less than "i_size"
 * bytes are available then return what is left and if nothing is availble,
 * return NULL.
 */
block_t *stream_Block( stream_t *s, int i_size )
{
    block_t *p_block;

    if( i_size <= 0 ) return NULL;
    if( !(p_block = block_New( s->p_input, i_size ) ) ) return NULL;

    p_block->i_buffer = stream_Read( s, p_block->p_buffer, i_size );
    if( !p_block->i_buffer )
    {
        block_Release( p_block );
        p_block = NULL;
    }

    return p_block;
}

Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
/**
 * Read from the stream untill first newline.
 * \param s Stream handle to read from
 * \return A null-terminated string. This must be freed,
 */
char *stream_ReadLine( stream_t *s )
{
    uint8_t *p_data;
    char    *p_line;
    int      i_data;
    int      i = 0;
    i_data = stream_Peek( s, &p_data, MAX_LINE );
    msg_Dbg( s->p_input, "i_data %d", i_data );
    while( i < i_data && p_data[i] != '\n' )
    {
        i++;
    }
    if( i_data <= 0 )
    {
        return NULL;
    }
    else
    {
        p_line = malloc( i + 1 );
        if( p_line == NULL )
        {
            msg_Err( s->p_input, "Out of memory" );
            return NULL;
        }
        i = stream_Read( s, p_line, i + 1 );
        p_line[ i - 1 ] = '\0';
        msg_Dbg( s->p_input, "found %d chars long line", i );
        return p_line;
    }
}

331 332 333 334 335 336
/**
 * Read "i_size" bytes and store them in a pes_packet_t.  Only fields
 * p_first, p_last, i_nb_data, and i_pes_size are set.  (Of course,
 * you need to fill i_dts, i_pts, ... ) If only less than "i_size"
 * bytes are available NULL is returned.
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
337
pes_packet_t *stream_PesPacket( stream_t *s, int i_data )
338 339 340 341 342 343 344 345 346 347 348 349 350
{
    pes_packet_t  *p_pes;
    data_packet_t *p_packet;


    if( !(p_pes = input_NewPES( s->p_input->p_method_data ) ) )
    {
        return NULL;
    }

    if( i_data <= 0 )
    {
        p_pes->p_first =
Gildas Bazin's avatar
 
Gildas Bazin committed
351
            p_pes->p_last = input_NewPacket( s->p_input->p_method_data, 0 );
352 353 354 355 356 357 358 359
        p_pes->i_nb_data = 1;
        return p_pes;
    }

    while( i_data > 0 )
    {
        int i_read;

360
        i_read = input_SplitBuffer( s->p_input, &p_packet,
Gildas Bazin's avatar
 
Gildas Bazin committed
361
                     __MIN( i_data, (int)s->p_input->i_bufsize ) );
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        if( i_read <= 0 )
        {
            /* should occur only with EOF and max allocation reached 
             * it safer to  return an error */
            /* free pes */
            input_DeletePES( s->p_input->p_method_data, p_pes );
            return NULL;
        }

        if( p_pes->p_first == NULL )
        {
            p_pes->p_first = p_packet;
        }
        else
        {
            p_pes->p_last->p_next = p_packet;
        }
        p_pes->p_last = p_packet;
        p_pes->i_nb_data++;
        p_pes->i_pes_size += i_read;
Gildas Bazin's avatar
 
Gildas Bazin committed
382
        i_data -= i_read;
383 384 385 386 387
    }

    return p_pes;
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/**
 * Read i_size into a data_packet_t. If b_force is not set, fewer bytes can
 * be returned. You should always set b_force, unless you know what you are
 * doing.
 */
data_packet_t *stream_DataPacket( stream_t *s, int i_size, vlc_bool_t b_force )
{
    data_packet_t *p_pk;
    int           i_read;

    if( i_size <= 0 )
    {
        p_pk = input_NewPacket( s->p_input->p_method_data, 0 );
        if( p_pk )
        {
            p_pk->p_payload_end = p_pk->p_payload_start;
        }
        return p_pk;
    }

    i_read = input_SplitBuffer( s->p_input, &p_pk, i_size );
    if( i_read <= 0 )
    {
        return NULL;
    }
413

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    /* Should be really rare, near 0 */
    if( i_read < i_size && b_force )
    {
        data_packet_t *p_old = p_pk;
        int           i_missing = i_size - i_read;

        p_pk = input_NewPacket( s->p_input->p_method_data, i_size );
        if( p_pk == NULL )
        {
            input_DeletePacket( s->p_input->p_method_data, p_old );
            return NULL;
        }
        p_pk->p_payload_end = p_pk->p_payload_start + i_size;
        memcpy( p_pk->p_payload_start, p_old->p_payload_start, i_read );
        input_DeletePacket( s->p_input->p_method_data, p_old );

Gildas Bazin's avatar
 
Gildas Bazin committed
430 431
        if( stream_Read( s, &p_pk->p_payload_start[i_read], i_missing )
            < i_missing )
432 433 434 435 436 437 438 439
        {
            input_DeletePacket( s->p_input->p_method_data, p_pk );
            return NULL;
        }
    }

    return p_pk;
}