]> git.vomp.tv Git - vompclient.git/blob - mediafile.cc
Teletext
[vompclient.git] / mediafile.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, Andreas Vogel
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #ifndef WIN32
22 #include <dirent.h>
23 #else
24 #include "threadwin.h"
25 #include <io.h>
26 #endif
27
28 #include "mediafile.h"
29 #include "media.h"
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33 #include <iostream>
34 #include "log.h"
35
36
37
38
39 MediaFile::MediaFile(ULONG pid){
40   providerid=pid;
41 }
42
43 MediaFile::~MediaFile(){
44   };
45
46
47
48 static struct mtype{
49    const char* extension;
50    ULONG type;
51    } mediatypes[]= {
52      {".mp3",MEDIA_TYPE_AUDIO},
53      {".MP3",MEDIA_TYPE_AUDIO},
54      {".jpg",MEDIA_TYPE_PICTURE},
55      {".JPG",MEDIA_TYPE_PICTURE},
56      {".jpeg",MEDIA_TYPE_PICTURE},
57      {".JPEG",MEDIA_TYPE_PICTURE},
58      {".mpg",MEDIA_TYPE_VIDEO},
59      {".MPG",MEDIA_TYPE_VIDEO}
60      };
61 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
62 #define NUMTYPES 8
63
64 //helper from vdr tools.c
65 bool endswith(const char *s, const char *p)
66 {
67   const char *se = s + strlen(s) - 1;
68   const char *pe = p + strlen(p) - 1;
69   while (pe >= p) {
70         if (*pe-- != *se-- || (se < s && pe >= p))
71            return false;
72         }
73   return true;
74 }
75
76 MediaList* MediaFile::getRootList() {
77   //has to be implemented in the derived class
78   return NULL;
79 }
80
81 ULONG MediaFile::getMediaType(const char * filename) {
82  for (ULONG i=0;i<NUMTYPES;i++) {
83       if (endswith(filename,mediatypes[i].extension)) {
84         return mediatypes[i].type;
85         }
86      }
87  return MEDIA_TYPE_UNKNOWN;
88 }
89
90
91 Media * MediaFile::createMedia(const char * dirname, const char * filename, bool withURI) {
92   Media * rt=NULL;
93   char *buffer=(char*)malloc(strlen(dirname)+strlen(filename)+2);
94   sprintf(buffer, "%s/%s", dirname, filename);
95
96   ULONG mtype=MEDIA_TYPE_UNKNOWN;
97 #ifndef WIN32
98   struct stat st;
99   if (stat(buffer, &st) == 0) {
100     if (S_ISDIR(st.st_mode)) {
101        mtype=MEDIA_TYPE_DIR;
102     }
103     else {
104        mtype=getMediaType(filename);
105     }
106   }
107 #else 
108   WIN32_FILE_ATTRIBUTE_DATA att;
109   GetFileAttributesEx(buffer,GetFileExInfoStandard,&att);
110   if (att.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {    
111        mtype=MEDIA_TYPE_DIR;
112   } else {
113        mtype=getMediaType(filename);
114   }
115 #endif
116
117   //only consider entries we accept by type here
118   if (mtype != MEDIA_TYPE_UNKNOWN) {
119     rt =new Media();
120     rt->setMediaType(mtype);
121     rt->setFileName(filename);
122 #ifndef WIN32
123     rt->setTime(st.st_mtime);
124 #else
125     struct timespec targetTime;
126     targetTime.tv_sec=(*((__int64*) &att.ftCreationTime)-WINDOWS_TIME_BASE_OFFSET)/(10*1000*1000);
127     targetTime.tv_nsec=((*((__int64*)&att.ftCreationTime)-WINDOWS_TIME_BASE_OFFSET)%(10*1000*1000))*100;
128     rt->setTime(targetTime.tv_sec);
129 #endif
130     Log::getInstance()->log("Media",Log::DEBUG,"created Media %s, type=%d",filename,mtype);
131     if (withURI) {
132       MediaURI u(providerid,buffer,NULL);
133       rt->setURI(&u);
134     }
135   }
136   free(buffer);
137   return rt;
138 }
139
140 MediaList* MediaFile::getMediaList(const MediaURI * parent){
141   ULONG mediaType=parent->getAllowedTypes();
142   Log::getInstance()->log("MediaFile::getMediaList",Log::DEBUG,"parent %s,types=0x%0lx",parent->getName(),mediaType);
143   MediaList *rt=NULL;
144   rt=new MediaList(parent);
145   const char *dirname=parent->getName();
146   //open the directory and read out the entries
147 #ifndef WIN32
148   DIR *d=opendir(dirname);
149   struct dirent *e;
150   union { // according to "The GNU C Library Reference Manual"
151     struct dirent d;
152     char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
153     } u;
154   while (d != NULL && (readdir_r(d,&u.d,&e) == 0) && e != NULL) {
155       const char * fname=e->d_name;
156 #else
157   WIN32_FIND_DATA  e;
158   HANDLE d;
159   d=FindFirstFile(dirname,&e);
160   if (d==INVALID_HANDLE_VALUE) {
161       return rt;
162   }
163   do  {
164       const char * fname=e.cFileName;
165 #endif    
166     if ( fname == NULL) continue;
167     if (strcmp(fname,".") == 0) continue;
168     if (strcmp(fname,"..") == 0) continue;
169     Media *m=createMedia(dirname,fname);
170     if (m && ( m->getMediaType() & mediaType)) {
171       Log::getInstance()->log("Media",Log::DEBUG,"added entry %s, type=%d",fname,m->getMediaType());
172       rt->push_back(m);
173     }
174     else {
175       if (m) delete m;
176     }
177 #ifndef WIN32
178   }
179   if (d != NULL) closedir(d);
180 #else
181   } while (FindNextFile(d,&e));
182   FindClose(d);
183 #endif
184   return rt;
185 }
186
187
188 int MediaFile::openMedium(ULONG channel, const MediaURI * uri, ULLONG * size, ULONG xsize, ULONG ysize) {
189   Log::getInstance()->log("Media::openMedium",Log::DEBUG,"fn=%s,chan=%u",uri->getName(),channel);
190   *size=0;
191   if (channel <0 || channel >= NUMCHANNELS) return -1;
192   struct ChannelInfo *info=&channels[channel];
193   if (info->file) info->reset();
194   FILE *fp=fopen(uri->getName(),"r");
195   if (! fp) {
196     Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
197     return -1;
198   }
199   
200   ULLONG mysize=0;
201 #ifndef WIN32 
202   struct stat st;
203   if ( fstat(fileno(fp),&st) == 0) mysize=st.st_size;
204 #else 
205   mysize=_filelength(_fileno(fp));
206 #endif
207   if (mysize == 0) {
208     Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
209     fclose(fp);
210     return -1;
211   }
212   info->setFilename(uri->getName());
213   info->file=fp;
214   info->size=mysize;
215   *size=mysize;
216   info->provider=providerid;
217   return 0;
218 }
219
220
221 int MediaFile::getMediaBlock(ULONG channel, ULLONG offset, ULONG len, ULONG * outlen,
222         unsigned char ** buffer) {
223   Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"chan=%u,offset=%llu,len=%lu",channel,offset,len);
224   *outlen=0;
225   if (channel <0 || channel >= NUMCHANNELS) return -1;
226   struct ChannelInfo *info=&channels[channel];
227   if (! info->file) {
228     Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"not open chan=%u",channel);
229     return -1;
230   }
231   ULLONG cpos=ftell(info->file);
232   if (offset != cpos) {
233     fseek(info->file,offset-cpos,SEEK_CUR);
234   }
235   if (offset != (ULLONG)ftell(info->file)) {
236     Log::getInstance()->log("Client", Log::DEBUG, "getMediaBlock pos = %llu not available", offset);
237     return -1;
238   }
239   if (*buffer == NULL) *buffer=(UCHAR *)malloc(len);
240   if (*buffer == NULL) {
241     Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"uanble to allocate buffer");
242     return -1;
243   }
244   ULONG amount=fread(*buffer,1,len,info->file);
245   Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"readlen=%lu",amount);
246   *outlen=amount;
247   return 0;
248 }
249
250
251 int MediaFile::closeMediaChannel(ULONG channel){
252   Log::getInstance()->log("Media::closeMediaChannel",Log::DEBUG,"chan=%u",channel);
253   if (channel <0 || channel >= NUMCHANNELS) return -1;
254   struct ChannelInfo *info=&channels[channel];
255   info->reset();
256   return 0;
257 }
258
259 //TODO: fill in more info
260 int MediaFile::getMediaInfo(ULONG channel, MediaInfo * result){
261   Log::getInstance()->log("Media::getMediaInfo",Log::DEBUG,"chan=%u",channel);
262   if (channel <0 || channel >= NUMCHANNELS) return -1;
263   struct ChannelInfo *info=&channels[channel];
264   if (! info->file) return -1;
265   result->size=info->size;
266   result->canPosition=true;
267   return 0;
268 }