Merge pull request #2774 from popcornmix/rpi-settings
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDInputStreams / DVDInputStreamFile.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "DVDInputStreamFile.h"
22 #include "filesystem/File.h"
23 #include "filesystem/IFile.h"
24 #include "settings/AdvancedSettings.h"
25 #include "utils/log.h"
26 #include "utils/URIUtils.h"
27
28 using namespace XFILE;
29
30 CDVDInputStreamFile::CDVDInputStreamFile() : CDVDInputStream(DVDSTREAM_TYPE_FILE)
31 {
32   m_pFile = NULL;
33   m_eof = true;
34 }
35
36 CDVDInputStreamFile::~CDVDInputStreamFile()
37 {
38   Close();
39 }
40
41 bool CDVDInputStreamFile::IsEOF()
42 {
43   return !m_pFile || m_eof;
44 }
45
46 bool CDVDInputStreamFile::Open(const char* strFile, const std::string& content)
47 {
48   if (!CDVDInputStream::Open(strFile, content))
49     return false;
50
51   m_pFile = new CFile();
52   if (!m_pFile)
53     return false;
54
55   unsigned int flags = READ_TRUNCATED | READ_BITRATE | READ_CHUNKED;
56
57   if ( g_advancedSettings.m_alwaysForceBuffer && 
58        !URIUtils::IsOnDVD(strFile) && 
59        !URIUtils::IsBluray(strFile) )
60     flags |= READ_CACHED; 
61
62
63   if (content == "video/mp4" || content == "video/x-msvideo" || content == "video/avi")
64     flags |= READ_MULTI_STREAM;
65
66   // open file in binary mode
67   if (!m_pFile->Open(strFile, flags))
68   {
69     delete m_pFile;
70     m_pFile = NULL;
71     return false;
72   }
73
74   if (m_pFile->GetImplemenation() && (content.empty() || content == "application/octet-stream"))
75     m_content = m_pFile->GetImplemenation()->GetContent();
76
77   m_eof = true;
78   return true;
79 }
80
81 // close file and reset everyting
82 void CDVDInputStreamFile::Close()
83 {
84   if (m_pFile)
85   {
86     m_pFile->Close();
87     delete m_pFile;
88   }
89
90   CDVDInputStream::Close();
91   m_pFile = NULL;
92   m_eof = true;
93 }
94
95 int CDVDInputStreamFile::Read(uint8_t* buf, int buf_size)
96 {
97   if(!m_pFile) return -1;
98
99   unsigned int ret = m_pFile->Read(buf, buf_size);
100
101   /* we currently don't support non completing reads */
102   if( ret <= 0 ) m_eof = true;
103
104   return (int)(ret & 0xFFFFFFFF);
105 }
106
107 int64_t CDVDInputStreamFile::Seek(int64_t offset, int whence)
108 {
109   if(!m_pFile) return -1;
110
111   if(whence == SEEK_POSSIBLE)
112     return m_pFile->IoControl(IOCTRL_SEEK_POSSIBLE, NULL);
113
114   int64_t ret = m_pFile->Seek(offset, whence);
115
116   /* if we succeed, we are not eof anymore */
117   if( ret >= 0 ) m_eof = false;
118
119   return ret;
120 }
121
122 int64_t CDVDInputStreamFile::GetLength()
123 {
124   if (m_pFile)
125     return m_pFile->GetLength();
126   return 0;
127 }
128
129 bool CDVDInputStreamFile::GetCacheStatus(XFILE::SCacheStatus *status)
130 {
131   if(m_pFile && m_pFile->IoControl(IOCTRL_CACHE_STATUS, status) >= 0)
132     return true;
133   else
134     return false;
135 }
136
137 BitstreamStats CDVDInputStreamFile::GetBitstreamStats() const
138 {
139   if (!m_pFile)
140     return m_stats; // dummy return. defined in CDVDInputStream
141
142   if(m_pFile->GetBitstreamStats())
143     return *m_pFile->GetBitstreamStats();
144   else
145     return m_stats;
146 }
147
148 int CDVDInputStreamFile::GetBlockSize()
149 {
150   if(m_pFile)
151     return m_pFile->GetChunkSize();
152   else
153     return 0;
154 }
155
156 void CDVDInputStreamFile::SetReadRate(unsigned rate)
157 {
158   unsigned maxrate = rate + 1024 * 1024 / 8;
159   if(m_pFile->IoControl(IOCTRL_CACHE_SETRATE, &maxrate) >= 0)
160     CLog::Log(LOGDEBUG, "CDVDInputStreamFile::SetReadRate - set cache throttle rate to %u bytes per second", maxrate);
161 }