]> git.sesse.net Git - vlc/blob - modules/demux/mkv/stream_io_callback.cpp
msg: simplify msg_GenericVa() macro
[vlc] / modules / demux / mkv / stream_io_callback.cpp
1 /*****************************************************************************
2  * stream_io_callback.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2004, 2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "stream_io_callback.hpp"
26
27 #include "matroska_segment.hpp"
28 #include "demux.hpp"
29
30 /*****************************************************************************
31  * Stream managment
32  *****************************************************************************/
33 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
34                        : s( s_), b_owner( b_owner_ )
35 {
36     mb_eof = false;
37 }
38
39 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
40 {
41     if( i_size <= 0 || mb_eof )
42         return 0;
43
44     return stream_Read( s, p_buffer, i_size );
45 }
46
47 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
48 {
49     int64_t i_pos, i_size;
50     int64_t i_current = stream_Tell( s );
51
52     switch( mode )
53     {
54         case seek_beginning:
55             i_pos = i_offset;
56             break;
57         case seek_end:
58             i_pos = stream_Size( s ) - i_offset;
59             break;
60         default:
61             i_pos= i_current + i_offset;
62             break;
63     }
64
65     if(i_pos == i_current)
66         return;
67
68     if( i_pos < 0 || ( ( i_size = stream_Size( s ) ) != 0 && i_pos >= i_size ) )
69     {
70         mb_eof = true;
71         return;
72     }
73
74     mb_eof = false;
75     if( stream_Seek( s, i_pos ) )
76     {
77         mb_eof = true;
78     }
79     return;
80 }
81
82 uint64 vlc_stream_io_callback::getFilePointer( void )
83 {
84     if ( s == NULL )
85         return 0;
86     return stream_Tell( s );
87 }
88
89 size_t vlc_stream_io_callback::write(const void *, size_t )
90 {
91     return 0;
92 }
93
94 uint64 vlc_stream_io_callback::toRead( void )
95 {
96     uint64_t i_size;
97
98     if( s == NULL)
99         return 0;
100
101     stream_Control( s, STREAM_GET_SIZE, &i_size );
102
103     if( i_size == 0 )
104         return UINT64_MAX;
105
106     return (uint64) i_size - stream_Tell( s );
107 }
108