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