2 Copyright 2004-2005 Chris Tallon, Andreas Vogel
4 This file is part of VOMP.
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.
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.
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.
24 #include "threadwin.h"
28 #include "mediafile.h"
31 #include <sys/types.h>
42 MediaFile::MediaFile(ULONG pid){
46 MediaFile::~MediaFile(){
52 const char* extension;
55 {".mp3",MEDIA_TYPE_AUDIO},
56 {".MP3",MEDIA_TYPE_AUDIO},
57 {".jpg",MEDIA_TYPE_PICTURE},
58 {".JPG",MEDIA_TYPE_PICTURE},
59 {".jpeg",MEDIA_TYPE_PICTURE},
60 {".JPEG",MEDIA_TYPE_PICTURE},
61 {".mpg",MEDIA_TYPE_VIDEO},
62 {".MPG",MEDIA_TYPE_VIDEO}
64 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
67 //helper from vdr tools.c
68 bool endswith(const char *s, const char *p)
70 const char *se = s + strlen(s) - 1;
71 const char *pe = p + strlen(p) - 1;
73 if (*pe-- != *se-- || (se < s && pe >= p))
79 MediaList* MediaFile::getRootList() {
80 //has to be implemented in the derived class
84 ULONG MediaFile::getMediaType(const char * filename) {
85 for (ULONG i=0;i<NUMTYPES;i++) {
86 if (endswith(filename,mediatypes[i].extension)) {
87 return mediatypes[i].type;
90 return MEDIA_TYPE_UNKNOWN;
94 Media * MediaFile::createMedia(const char * dirname, const char * filename, bool withURI) {
96 char *buffer=(char*)malloc(strlen(dirname)+strlen(filename)+2);
97 sprintf(buffer, "%s/%s", dirname, filename);
99 ULONG mtype=MEDIA_TYPE_UNKNOWN;
102 if (stat(buffer, &st) == 0) {
103 if (S_ISDIR(st.st_mode)) {
104 mtype=MEDIA_TYPE_DIR;
107 mtype=getMediaType(filename);
111 WIN32_FILE_ATTRIBUTE_DATA att;
112 GetFileAttributesEx(buffer,GetFileExInfoStandard,&att);
113 if (att.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
114 mtype=MEDIA_TYPE_DIR;
116 mtype=getMediaType(filename);
120 //only consider entries we accept by type here
121 if (mtype != MEDIA_TYPE_UNKNOWN) {
123 rt->setMediaType(mtype);
124 rt->setFileName(filename);
126 rt->setTime(st.st_mtime);
128 struct timespec targetTime;
129 targetTime.tv_sec=(*((__int64*) &att.ftCreationTime)-WINDOWS_TIME_BASE_OFFSET)/(10*1000*1000);
130 targetTime.tv_nsec=((*((__int64*)&att.ftCreationTime)-WINDOWS_TIME_BASE_OFFSET)%(10*1000*1000))*100;
131 rt->setTime(targetTime.tv_sec);
133 Log::getInstance()->log("Media",Log::DEBUG,"created Media %s, type=%d",filename,mtype);
135 MediaURI u(providerid,buffer,NULL);
143 MediaList* MediaFile::getMediaList(const MediaURI * parent){
144 ULONG mediaType=parent->getAllowedTypes();
145 Log::getInstance()->log("MediaFile::getMediaList",Log::DEBUG,"parent %s,types=0x%0lx",parent->getName(),mediaType);
147 rt=new MediaList(parent);
148 const char *dirname=parent->getName();
149 //open the directory and read out the entries
151 DIR *d=opendir(dirname);
153 union { // according to "The GNU C Library Reference Manual"
155 char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
157 while (d != NULL && (readdir_r(d,&u.d,&e) == 0) && e != NULL) {
158 const char * fname=e->d_name;
162 d=FindFirstFile(dirname,&e);
163 if (d==INVALID_HANDLE_VALUE) {
167 const char * fname=e.cFileName;
169 if ( fname == NULL) continue;
170 if (strcmp(fname,".") == 0) continue;
171 if (strcmp(fname,"..") == 0) continue;
172 Media *m=createMedia(dirname,fname);
173 if (m && ( m->getMediaType() & mediaType)) {
174 Log::getInstance()->log("Media",Log::DEBUG,"added entry %s, type=%d",fname,m->getMediaType());
182 if (d != NULL) closedir(d);
184 } while (FindNextFile(d,&e));
191 int MediaFile::openMedium(ULONG channel, const MediaURI * uri, ULLONG * size, ULONG xsize, ULONG ysize) {
192 Log::getInstance()->log("Media::openMedium",Log::DEBUG,"fn=%s,chan=%u",uri->getName(),channel);
194 if (channel <0 || channel >= NUMCHANNELS) return -1;
195 struct ChannelInfo *info=&channels[channel];
196 if (info->file) info->reset();
197 FILE *fp=fopen(uri->getName(),"r");
199 Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
206 if ( fstat(fileno(fp),&st) == 0) mysize=st.st_size;
208 mysize=_filelength(_fileno(fp));
211 Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
215 info->setFilename(uri->getName());
219 info->provider=providerid;
224 int MediaFile::getMediaBlock(ULONG channel, ULLONG offset, ULONG len, ULONG * outlen,
225 unsigned char ** buffer) {
226 Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"chan=%u,offset=%llu,len=%lu",channel,offset,len);
228 if (channel <0 || channel >= NUMCHANNELS) return -1;
229 struct ChannelInfo *info=&channels[channel];
231 Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"not open chan=%u",channel);
234 ULLONG cpos=ftell(info->file);
235 if (offset != cpos) {
236 fseek(info->file,offset-cpos,SEEK_CUR);
238 if (offset != (ULLONG)ftell(info->file)) {
239 Log::getInstance()->log("Client", Log::DEBUG, "getMediaBlock pos = %llu not available", offset);
242 if (*buffer == NULL) *buffer=(UCHAR *)malloc(len);
243 if (*buffer == NULL) {
244 Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"uanble to allocate buffer");
247 ULONG amount=fread(*buffer,1,len,info->file);
248 Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"readlen=%lu",amount);
254 int MediaFile::closeMediaChannel(ULONG channel){
255 Log::getInstance()->log("Media::closeMediaChannel",Log::DEBUG,"chan=%u",channel);
256 if (channel <0 || channel >= NUMCHANNELS) return -1;
257 struct ChannelInfo *info=&channels[channel];
262 //TODO: fill in more info
263 int MediaFile::getMediaInfo(ULONG channel, MediaInfo * result){
264 Log::getInstance()->log("Media::getMediaInfo",Log::DEBUG,"chan=%u",channel);
265 if (channel <0 || channel >= NUMCHANNELS) return -1;
266 struct ChannelInfo *info=&channels[channel];
267 if (! info->file) return -1;
268 result->size=info->size;
269 result->canPosition=true;