forked from opdenkamp/xbmc
/
DVDInputStreamStack.cpp
178 lines (145 loc) · 3.78 KB
/
DVDInputStreamStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.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, 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 XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "DVDInputStreamStack.h"
#include "FileItem.h"
#include "filesystem/File.h"
#include "filesystem/StackDirectory.h"
#include "utils/log.h"
#include <limits.h>
using namespace XFILE;
using namespace boost;
using namespace std;
CDVDInputStreamStack::CDVDInputStreamStack() : CDVDInputStream(DVDSTREAM_TYPE_FILE)
{
m_eof = true;
m_pos = 0;
}
CDVDInputStreamStack::~CDVDInputStreamStack()
{
Close();
}
bool CDVDInputStreamStack::IsEOF()
{
return m_eof;
}
bool CDVDInputStreamStack::Open(const char* path, const std::string& content)
{
if (!CDVDInputStream::Open(path, content))
return false;
CStackDirectory dir;
CFileItemList items;
if(!dir.GetDirectory(path, items))
{
CLog::Log(LOGERROR, "CDVDInputStreamStack::Open - failed to get list of stacked items");
return false;
}
m_length = 0;
m_eof = false;
for(int index = 0; index < items.Size(); index++)
{
TFile file(new CFile());
if (!file->Open(items[index]->GetPath(), READ_TRUNCATED))
{
CLog::Log(LOGERROR, "CDVDInputStreamStack::Open - failed to open stack part '%s' - skipping", items[index]->GetPath().c_str());
continue;
}
TSeg segment;
segment.file = file;
segment.length = file->GetLength();
if(segment.length <= 0)
{
CLog::Log(LOGERROR, "CDVDInputStreamStack::Open - failed to get file length for '%s' - skipping", items[index]->GetPath().c_str());
continue;
}
m_length += segment.length;
m_files.push_back(segment);
}
if(m_files.size() == 0)
return false;
m_file = m_files[0].file;
m_eof = false;
return true;
}
// close file and reset everyting
void CDVDInputStreamStack::Close()
{
CDVDInputStream::Close();
m_files.clear();
m_file.reset();
m_eof = true;
}
int CDVDInputStreamStack::Read(BYTE* buf, int buf_size)
{
if(m_file == NULL || m_eof)
return 0;
unsigned int ret = m_file->Read(buf, buf_size);
if(ret > INT_MAX)
return -1;
if(ret == 0)
{
m_eof = true;
if(Seek(m_pos, SEEK_SET) < 0)
{
CLog::Log(LOGERROR, "CDVDInputStreamStack::Read - failed to seek into next file");
m_eof = true;
m_file.reset();
return -1;
}
}
m_pos += ret;
return (int)ret;
}
__int64 CDVDInputStreamStack::Seek(__int64 offset, int whence)
{
int64_t pos, len;
if (whence == SEEK_SET)
pos = offset;
else if(whence == SEEK_CUR)
pos = offset + m_pos;
else if(whence == SEEK_END)
pos = offset + m_length;
else
return -1;
len = 0;
for(TSegVec::iterator it = m_files.begin(); it != m_files.end(); it++)
{
if(len + it->length > pos)
{
TFile file = it->file;
int64_t file_pos = pos - len;
if(file->GetPosition() != file_pos)
{
if(file->Seek(file_pos, SEEK_SET) < 0)
return false;
}
m_file = file;
m_pos = pos;
m_eof = false;
return pos;
}
len += it->length;
}
return -1;
}
__int64 CDVDInputStreamStack::GetLength()
{
return m_length;
}