]> git.vomp.tv Git - vompserver.git/blob - media.c
Radio stream chunk size, stream end detection
[vompserver.git] / media.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "media.h"
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <iostream>
26
27
28
29
30 Media::Media(int type, const char * filename, int time){
31   _filename=NULL;
32   if (filename) {
33     int len=strlen(filename)+1;
34     _filename=new char[len];
35     strcpy(_filename,filename);
36     }
37   _type=type;
38   _time=time;
39   };
40
41 Media::~Media(){
42   delete _filename;
43   _filename=NULL;
44   };
45
46 const char * Media::getFilename() {
47   return _filename;
48   }
49
50 int Media::getType() {
51   return _type;
52   }
53 int Media::getTime() {
54   return _time;
55   }
56
57
58 static struct mtype{
59    const char* extension;
60    int type;
61    } mediatypes[]= {
62      {".mp3",MEDIA_TYPE_AUDIO},
63      {".MP3",MEDIA_TYPE_AUDIO},
64      {".jpg",MEDIA_TYPE_PICTURE},
65      {".JPG",MEDIA_TYPE_PICTURE},
66      {".jpeg",MEDIA_TYPE_PICTURE},
67      {".JPEG",MEDIA_TYPE_PICTURE}
68      };
69 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
70 #define NUMTYPES 6
71
72 //helper from vdr tools.c
73 bool endswith(const char *s, const char *p)
74 {
75   const char *se = s + strlen(s) - 1;
76   const char *pe = p + strlen(p) - 1;
77   while (pe >= p) {
78         if (*pe-- != *se-- || (se < s && pe >= p))
79            return false;
80         }
81   return true;
82 }
83
84
85 MediaList * MediaList::readList(Config * cfg,const char * dirname, int type) {
86   MediaList *rt=NULL;
87   if (dirname == NULL) {
88     //the configured Media List
89     //for the moment up to 10 entries [Media] Dir.1 ...Dir.10
90     for (int nr=1;nr<=10;nr++){
91       char buffer[20];
92       sprintf(buffer,"Dir.%d",nr);
93       const char * dn=cfg->getValueString("Media",buffer);
94       if (dn != NULL) {
95         if (rt == NULL) rt=new MediaList();
96         Media *m=new Media(MEDIA_TYPE_DIR,dn,0);
97         rt->push_back(m);
98         Log::getInstance()->log("Media",Log::DEBUG,"added base dir %s",dn);
99        }
100      }
101    }
102   if (rt != NULL) return rt;
103   //if nothing is configured, we start at /
104   if (dirname == NULL) dirname="/";
105   rt=new MediaList();
106   //open the directory and read out the entries
107   DIR *d=opendir(dirname);
108   struct dirent *e;
109   union { // according to "The GNU C Library Reference Manual"
110     struct dirent d;
111     char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
112     } u;
113
114   while (d != NULL && (readdir_r(d,&u.d,&e) == 0) && e != NULL) {
115     {
116     const char * fname=e->d_name;
117     if ( fname == NULL) continue;
118     if (strcmp(fname,".") == 0) continue;
119     char *buffer;
120     asprintf(&buffer, "%s/%s", dirname, e->d_name);
121     struct stat st;
122     int mtype=MEDIA_TYPE_UNKNOWN;
123     if (stat(buffer, &st) == 0) {
124       if (S_ISDIR(st.st_mode)) {
125          mtype=MEDIA_TYPE_DIR;
126    }
127       else {
128          for (int i=0;i<NUMTYPES;i++) {
129             if (endswith(fname,mediatypes[i].extension)) {
130               mtype=mediatypes[i].type;
131               break;
132               }
133            }
134          }
135       }
136     free(buffer);
137     //only consider entries we accept by type here
138     if (mtype & type) {
139      Media * m =new Media(mtype,fname,(int)(st.st_mtime));
140      rt->push_back(m);
141      Log::getInstance()->log("Media",Log::DEBUG,"added entry %s, type=%d",fname,mtype);
142      }
143     }
144   }
145   if (d != NULL) closedir(d);
146   return rt;
147   }
148
149 MediaList::~MediaList() {
150   MediaList::iterator it=this->begin();
151   while (it < this->end()) {
152     delete *it;
153     it++;
154   }
155 }
156
157