[droid] add new internal player for amlogic based SoCs
[vuplus_xbmc] / xbmc / cores / amlplayer / FileURLProtocol.cpp
1 /*
2  *      Copyright (C) 2011-2012 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, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "FileURLProtocol.h"
23 #include "filesystem/File.h"
24 #include "utils/log.h"
25 #include "FileItem.h"
26
27 #define URL_RDONLY 1  /**< read-only */
28 #define URL_WRONLY 2  /**< write-only */
29 #define URL_RDWR   (URL_RDONLY|URL_WRONLY)  /**< read-write */
30
31 #define AVSEEK_SIZE  0x10000
32 #define AVSEEK_FORCE 0x20000
33
34 //========================================================================
35 int CFileURLProtocol::Open(AML_URLContext *h, const char *filename, int flags)
36 {
37   if (flags != URL_RDONLY)
38   {
39     CLog::Log(LOGDEBUG, "CFileURLProtocol::Open: Only read-only is supported");
40     return -EINVAL;
41   }
42
43   CStdString url = filename;
44   if (url.Left(strlen("xb-http://")).Equals("xb-http://"))
45   {
46     url = url.Right(url.size() - strlen("xb-"));
47   }
48   else if (url.Left(strlen("xb-https://")).Equals("xb-https://"))
49   {
50     url = url.Right(url.size() - strlen("xb-"));
51   }
52   else if (url.Left(strlen("xb-ftp://")).Equals("xb-ftp://"))
53   {
54     url = url.Right(url.size() - strlen("xb-"));
55   }
56   else if (url.Left(strlen("xb-ftps://")).Equals("xb-ftps://"))
57   {
58     url = url.Right(url.size() - strlen("xb-"));
59   }
60
61   CLog::Log(LOGDEBUG, "CFileURLProtocol::Open filename2(%s)", url.c_str());
62   // open the file, always in read mode, calc bitrate
63   unsigned int cflags = READ_BITRATE;
64   XFILE::CFile *cfile = new XFILE::CFile();
65
66   if (CFileItem(url, true).IsInternetStream())
67     cflags |= READ_CACHED;
68
69   // open file in binary mode
70   if (!cfile->Open(url, cflags))
71   {
72     delete cfile;
73     return -EIO;
74   }
75
76   h->priv_data = (void *)cfile;
77
78   return 0;
79 }
80
81 //========================================================================
82 int CFileURLProtocol::Read(AML_URLContext *h, unsigned char *buf, int size)
83 {
84   XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
85
86   int readsize = cfile->Read(buf, size);
87   //CLog::Log(LOGDEBUG, "CFileURLProtocol::Read size(%d), readsize(%d)", size, readsize);
88
89   return readsize;
90 }
91
92 //========================================================================
93 int CFileURLProtocol::Write(AML_URLContext *h, unsigned char *buf, int size)
94 {
95   //CLog::Log(LOGDEBUG, "CFileURLProtocol::Write size(%d)", size);
96   return 0;
97 }
98
99 //========================================================================
100 int64_t CFileURLProtocol::Seek(AML_URLContext *h, int64_t pos, int whence)
101 {
102   //CLog::Log(LOGDEBUG, "CFileURLProtocol::Seek1 pos(%lld), whence(%d)", pos, whence);
103   XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
104   whence &= ~AVSEEK_FORCE;
105
106   // seek to the end of file
107   if (pos == -1 || whence == AVSEEK_SIZE)
108     pos = cfile->GetLength();
109   else
110     pos = cfile->Seek(pos, whence);
111
112   //CLog::Log(LOGDEBUG, "CFileURLProtocol::Seek2 pos(%lld), whence(%d)", pos, whence);
113
114   return pos;
115 }
116
117 //========================================================================
118 int64_t CFileURLProtocol::SeekEx(AML_URLContext *h, int64_t pos, int whence)
119 {
120   //CLog::Log(LOGDEBUG, "CFileURLProtocol::SeekEx1 pos(%lld), whence(%d)", pos, whence);
121   XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
122   whence &= ~AVSEEK_FORCE;
123
124   // seek to the end of file
125   if (pos == -1 || whence == AVSEEK_SIZE)
126     pos = cfile->GetLength();
127   else
128     pos = cfile->Seek(pos, whence);
129
130   //CLog::Log(LOGDEBUG, "CFileURLProtocol::SeekEx2 pos(%lld), whence(%d)", pos, whence);
131
132   return pos;
133 }
134
135 //========================================================================
136 int CFileURLProtocol::Close(AML_URLContext *h)
137 {
138   CLog::Log(LOGDEBUG, "CFileURLProtocol::Close");
139   XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
140   cfile->Close();
141   delete cfile;
142
143   return 0;
144 }