]> git.vomp.tv Git - vompserver.git/blob - mediafile.c
Makefile changes as per VDR HISTORY file
[vompserver.git] / mediafile.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "mediafile.h"
22 #include "media.h"
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <iostream>
28 #include "log.h"
29
30
31
32
33 MediaFile::MediaFile(ULONG pid){
34   providerid=pid;
35 }
36
37 MediaFile::~MediaFile(){
38   };
39
40
41
42 static struct mtype{
43    const char* extension;
44    ULONG type;
45    } mediatypes[]= {
46      {".mp3",MEDIA_TYPE_AUDIO},
47      {".MP3",MEDIA_TYPE_AUDIO},
48      {".jpg",MEDIA_TYPE_PICTURE},
49      {".JPG",MEDIA_TYPE_PICTURE},
50      {".jpeg",MEDIA_TYPE_PICTURE},
51      {".JPEG",MEDIA_TYPE_PICTURE},
52      {".mpg",MEDIA_TYPE_VIDEO},
53      {".MPG",MEDIA_TYPE_VIDEO}
54      };
55 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
56 #define NUMTYPES 8
57
58 //helper from vdr tools.c
59 bool endswith(const char *s, const char *p)
60 {
61   const char *se = s + strlen(s) - 1;
62   const char *pe = p + strlen(p) - 1;
63   while (pe >= p) {
64         if (*pe-- != *se-- || (se < s && pe >= p))
65            return false;
66         }
67   return true;
68 }
69
70 MediaList* MediaFile::getRootList() {
71   //has to be implemented in the derived class
72   return NULL;
73 }
74
75 ULONG MediaFile::getMediaType(const char * filename) {
76  for (ULONG i=0;i<NUMTYPES;i++) {
77       if (endswith(filename,mediatypes[i].extension)) {
78         return mediatypes[i].type;
79         }
80      }
81  return MEDIA_TYPE_UNKNOWN;
82 }
83
84
85 Media * MediaFile::createMedia(const char * dirname, const char * filename, bool withURI) {
86   Media * rt=NULL;
87   char *buffer;
88   asprintf(&buffer, "%s/%s", dirname, filename);
89   struct stat st;
90   ULONG mtype=MEDIA_TYPE_UNKNOWN;
91   if (stat(buffer, &st) == 0) {
92     if (S_ISDIR(st.st_mode)) {
93        mtype=MEDIA_TYPE_DIR;
94     }
95     else {
96        mtype=getMediaType(filename);
97     }
98   }
99   //only consider entries we accept by type here
100   if (mtype != MEDIA_TYPE_UNKNOWN) {
101     rt =new Media();
102     rt->setMediaType(mtype);
103     rt->setFileName(filename);
104     rt->setTime(st.st_mtime);
105     Log::getInstance()->log("Media",Log::DEBUG,"created Media %s, type=%d",filename,mtype);
106     if (withURI) {
107       MediaURI u(providerid,buffer,NULL);
108       rt->setURI(&u);
109     }
110   }
111   free(buffer);
112   return rt;
113 }
114
115 MediaList* MediaFile::getMediaList(const MediaURI * parent){
116   ULONG mediaType=parent->getAllowedTypes();
117   Log::getInstance()->log("MediaFile::getMediaList",Log::DEBUG,"parent %s,types=0x%0lx",parent->getName(),mediaType);
118   MediaList *rt=NULL;
119   rt=new MediaList(parent);
120   const char *dirname=parent->getName();
121   //open the directory and read out the entries
122   DIR *d=opendir(dirname);
123   struct dirent *e;
124   union { // according to "The GNU C Library Reference Manual"
125     struct dirent d;
126     char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
127     } u;
128
129   while (d != NULL && (readdir_r(d,&u.d,&e) == 0) && e != NULL) {
130     const char * fname=e->d_name;
131     if ( fname == NULL) continue;
132     if (strcmp(fname,".") == 0) continue;
133     if (strcmp(fname,"..") == 0) continue;
134     Media *m=createMedia(dirname,fname);
135     if (m && ( m->getMediaType() & mediaType)) {
136       Log::getInstance()->log("Media",Log::DEBUG,"added entry %s, type=%d",fname,m->getMediaType());
137       rt->push_back(m);
138     }
139     else {
140       if (m) delete m;
141     }
142   }
143   if (d != NULL) closedir(d);
144   return rt;
145   }
146
147
148 int MediaFile::openMedium(ULONG channel, const MediaURI * uri, ULLONG * size, ULONG xsize, ULONG ysize) {
149   Log::getInstance()->log("Media::openMedium",Log::DEBUG,"fn=%s,chan=%u",uri->getName(),channel);
150   *size=0;
151   if (channel <0 || channel >= NUMCHANNELS) return -1;
152   struct ChannelInfo *info=&channels[channel];
153   if (info->file) info->reset();
154   FILE *fp=fopen(uri->getName(),"r");
155   if (! fp) {
156     Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
157     return -1;
158   }
159   struct stat st;
160   ULLONG mysize=0;
161   if ( fstat(fileno(fp),&st) == 0) mysize=st.st_size;
162   if (mysize == 0) {
163     Log::getInstance()->log("Media::openMedium",Log::ERR,"unable to open file fn=%s,chan=%u",uri->getName(),channel);
164     fclose(fp);
165     return -1;
166   }
167   info->setFilename(uri->getName());
168   info->file=fp;
169   info->size=mysize;
170   *size=mysize;
171   info->provider=providerid;
172   return 0;
173 }
174
175
176 int MediaFile::getMediaBlock(ULONG channel, ULLONG offset, ULONG len, ULONG * outlen,
177         unsigned char ** buffer) {
178   Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"chan=%u,offset=%llu,len=%lu",channel,offset,len);
179   *outlen=0;
180   if (channel <0 || channel >= NUMCHANNELS) return -1;
181   struct ChannelInfo *info=&channels[channel];
182   if (! info->file) {
183     Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"not open chan=%u",channel);
184     return -1;
185   }
186   ULLONG cpos=ftell(info->file);
187   if (offset != cpos) {
188     fseek(info->file,offset-cpos,SEEK_CUR);
189   }
190   if (offset != (ULLONG)ftell(info->file)) {
191     Log::getInstance()->log("Client", Log::DEBUG, "getMediaBlock pos = %llu not available", offset);
192     return -1;
193   }
194   if (*buffer == NULL) *buffer=(UCHAR *)malloc(len);
195   if (*buffer == NULL) {
196     Log::getInstance()->log("Media::getMediaBlock",Log::ERR,"uanble to allocate buffer");
197     return -1;
198   }
199   ULONG amount=fread(*buffer,1,len,info->file);
200   Log::getInstance()->log("Media::getMediaBlock",Log::DEBUG,"readlen=%lu",amount);
201   *outlen=amount;
202   return 0;
203 }
204
205
206 int MediaFile::closeMediaChannel(ULONG channel){
207   Log::getInstance()->log("Media::closeMediaChannel",Log::DEBUG,"chan=%u",channel);
208   if (channel <0 || channel >= NUMCHANNELS) return -1;
209   struct ChannelInfo *info=&channels[channel];
210   info->reset();
211   return 0;
212 }
213
214 //TODO: fill in more info
215 int MediaFile::getMediaInfo(ULONG channel, MediaInfo * result){
216   Log::getInstance()->log("Media::getMediaInfo",Log::DEBUG,"chan=%u",channel);
217   if (channel <0 || channel >= NUMCHANNELS) return -1;
218   struct ChannelInfo *info=&channels[channel];
219   if (! info->file) return -1;
220   result->size=info->size;
221   result->canPosition=true;
222   return 0;
223 }