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>
39 MediaFile::MediaFile(ULONG pid){
43 MediaFile::~MediaFile(){
49 const char* extension;
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}
61 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
64 //helper from vdr tools.c
65 bool endswith(const char *s, const char *p)
67 const char *se = s + strlen(s) - 1;
68 const char *pe = p + strlen(p) - 1;
70 if (*pe-- != *se-- || (se < s && pe >= p))
76 MediaList* MediaFile::getRootList() {
77 //has to be implemented in the derived class
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;
87 return MEDIA_TYPE_UNKNOWN;
91 Media * MediaFile::createMedia(const char * dirname, const char * filename, bool withURI) {
93 char *buffer=(char*)malloc(strlen(dirname)+strlen(filename)+2);
94 sprintf(buffer, "%s/%s", dirname, filename);
96 ULONG mtype=MEDIA_TYPE_UNKNOWN;
99 if (stat(buffer, &st) == 0) {
100 if (S_ISDIR(st.st_mode)) {
101 mtype=MEDIA_TYPE_DIR;
104 mtype=getMediaType(filename);
108 WIN32_FILE_ATTRIBUTE_DATA att;
109 GetFileAttributesEx(buffer,GetFileExInfoStandard,&att);
110 if (att.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
111 mtype=MEDIA_TYPE_DIR;
113 mtype=getMediaType(filename);
117 //only consider entries we accept by type here
118 if (mtype != MEDIA_TYPE_UNKNOWN) {
120 rt->setMediaType(mtype);
121 rt->setFileName(filename);
123 rt->setTime(st.st_mtime);
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);
130 Log::getInstance()->log("Media",Log::DEBUG,"created Media %s, type=%d",filename,mtype);
132 MediaURI u(providerid,buffer,NULL);
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);
144 rt=new MediaList(parent);
145 const char *dirname=parent->getName();
146 //open the directory and read out the entries
148 DIR *d=opendir(dirname);
150 union { // according to "The GNU C Library Reference Manual"
152 char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
154 while (d != NULL && (readdir_r(d,&u.d,&e) == 0) && e != NULL) {
155 const char * fname=e->d_name;
159 d=FindFirstFile(dirname,&e);
160 if (d==INVALID_HANDLE_VALUE) {
164 const char * fname=e.cFileName;
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());
179 if (d != NULL) closedir(d);
181 } while (FindNextFile(d,&e));
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);
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");
196 Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
203 if ( fstat(fileno(fp),&st) == 0) mysize=st.st_size;
205 mysize=_filelength(_fileno(fp));
208 Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
212 info->setFilename(uri->getName());
216 info->provider=providerid;
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);
225 if (channel <0 || channel >= NUMCHANNELS) return -1;
226 struct ChannelInfo *info=&channels[channel];
228 Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"not open chan=%u",channel);
231 ULLONG cpos=ftell(info->file);
232 if (offset != cpos) {
233 fseek(info->file,offset-cpos,SEEK_CUR);
235 if (offset != (ULLONG)ftell(info->file)) {
236 Log::getInstance()->log("Client", Log::DEBUG, "getMediaBlock pos = %llu not available", offset);
239 if (*buffer == NULL) *buffer=(UCHAR *)malloc(len);
240 if (*buffer == NULL) {
241 Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"uanble to allocate buffer");
244 ULONG amount=fread(*buffer,1,len,info->file);
245 Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"readlen=%lu",amount);
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];
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;