]> git.vomp.tv Git - vompclient.git/blob - mediaplayer.cc
Display channel name, duration, resume point and size on recording info screen
[vompclient.git] / mediaplayer.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 #include "mediaplayer.h"
22 #include "media.h"
23 #include "log.h"
24
25 class MediaProviderHolder {
26   public:
27     MediaProvider *provider;
28     ULONG         id;
29     ULONG         range;
30     MediaProviderHolder(MediaProvider *p,ULONG i, ULONG r=1) {
31       provider=p;
32       range=r;
33       id=i;
34     }
35 };
36
37
38
39 MediaPlayer::MediaPlayer(){}
40 MediaPlayer::~MediaPlayer(){
41   for (Tplist::iterator it=plist.begin();it<plist.end();it++) {
42   delete *it;
43   }
44 }
45
46 /**
47   * get the root media list
48   * the returned list has to be destroyed by the caller
49   * if NULL is returned currently no media is available
50   */
51 MediaList* MediaPlayer::getRootList(){
52   Log::getInstance()->log("MediaPlayer::getRootList",Log::DEBUG,"numproviders %d",plist.size());
53   MediaList * rt=new MediaList(new MediaURI(0,NULL,NULL));
54   for (Tplist::iterator it=plist.begin();it<plist.end();it++) {
55     MediaList *cur=(*it)->provider->getRootList();
56     if (cur) {
57       //we take the entries away from the list - so don't delete them with the list
58       cur->setOwning(false);
59       Log::getInstance()->log("MediaPlayer::getRootList",Log::DEBUG,"got list for provider %p with %d items",(*it),cur->size());
60       for (MediaList::iterator mi=cur->begin();mi<cur->end();mi++) {
61         Media *m=*mi;
62         //always set the URI in the root list because we combine entries
63         //from different lists
64         if (! m->getURI()) {
65           MediaURI *u=cur->getURI(m);
66           m->setURI(u);
67           Log::getInstance()->log("MediaPlayer::getRootList",Log::DEBUG,"set uri n=%s,d=%s",u->getName(),u->getDisplayName());
68           delete u;
69         }
70         rt->push_back(m);
71         Log::getInstance()->log("MediaPlayer::getRootList",Log::DEBUG,"added item to list name=%s",m->getFileName());
72       }
73     }
74     delete cur;
75   }
76   return rt;
77 }
78
79 /**
80   * get a medialist for a given parent
81   * the returned list has to be destroyed by the caller
82   * NULL if no entries found
83   */
84 MediaList* MediaPlayer::getMediaList(const MediaURI * parent){
85   Log::getInstance()->log("MediaPlayer::getMediaList",Log::DEBUG,"numproviders %d,parent=%p",plist.size(),parent);
86   MediaProvider *p=providerById(parent->getProvider());
87   if (! p) {
88     return NULL;
89   }
90   return p->getMediaList(parent);
91 }
92
93 /**
94   * open a media uri
95   * afterwards getBlock or other functions must be possible
96   * currently only one medium is open at the same time
97   * for a given channel
98   * @param channel: channel id, NUMCHANNELS must be supported
99   * @param xsize,ysize: size of the screen
100   * @param size out: the size of the medium
101   * @return != 0 in case of error
102   * 
103   */
104 int MediaPlayer::openMedium(ULONG channel, const MediaURI * uri, ULLONG * size, ULONG xsize, ULONG ysize){
105   if ( channel >= NUMCHANNELS) return -1;
106   info[channel].provider=NULL;
107   *size= 0;
108   MediaProvider *p=providerById(uri->getProvider());
109   if (!p) {
110     return -1;
111   }
112   int rt=p->openMedium(channel,uri,size,xsize,ysize);
113   if (rt == 0) {
114     info[channel].providerId=uri->getProvider();
115     info[channel].provider=p;
116   }
117   return rt;
118
119 }
120
121 /**
122   * get a block for a channel
123   * @param offset - the offset
124   * @param len - the required len
125   * @param outlen out - the read len if 0 this is EOF
126   * @param buffer out the allocated buffer (must be freed with free!)
127   * @return != 0 in case of error
128   */           
129 int MediaPlayer::getMediaBlock(ULONG channel, ULLONG offset, ULONG len, ULONG * outlen,
130     unsigned char ** buffer) {
131   if ( channel >= NUMCHANNELS) return -1;
132   if ( info[channel].provider == NULL) return -1;
133   return info[channel].provider->getMediaBlock(channel,offset,len,outlen,buffer);
134 }
135
136
137 /**
138   * close a media channel
139   */
140 int MediaPlayer::closeMediaChannel(ULONG channel){
141   if ( channel >= NUMCHANNELS) return -1;
142   if ( info[channel].provider == NULL) return -1;
143   int rt=info[channel].provider->closeMediaChannel(channel);
144   info[channel].provider=NULL;
145   info[channel].providerId=0;
146   return rt;
147 }
148
149 /**
150   * return the media info for a given channel
151   * return != 0 on error
152   * the caller has to provide a pointer to an existing media info
153   */
154 int MediaPlayer::getMediaInfo(ULONG channel, struct MediaInfo * result){
155   if ( channel >= NUMCHANNELS) return -1;
156   if ( info[channel].provider == NULL) return -1;
157   return info[channel].provider->getMediaInfo(channel,result);
158 }
159
160
161
162 void MediaPlayer::registerMediaProvider(MediaProvider *p,ULONG providerId,ULONG range) {
163   if (! p) return;
164   MediaProviderHolder *h=new MediaProviderHolder(p,providerId,range);
165   Log::getInstance()->log("MediaPlayer::registerMediaProvider",Log::DEBUG,"p=%p",p);
166   plist.push_back(h);
167 }
168
169 MediaProvider * MediaPlayer::providerById(ULONG id) {
170   MediaProvider *rt=NULL;
171   for (Tplist::iterator it=plist.begin();it<plist.end();it++) {
172     MediaProviderHolder *h=*it;
173     if (id >= h->id && id < (h->id+h->range)) {
174       rt=h->provider;
175       break;
176     }
177   }
178   Log::getInstance()->log("MediaPlayer::providerById",Log::DEBUG,"id=%d,p=%p",id,rt);
179   return rt;
180 }
181
182 MediaPlayer* MediaPlayer::getInstance() {
183   return (MediaPlayer *) MediaPlayerRegister::getInstance();
184 }
185
186 MediaPlayerRegister* MediaPlayerRegister::getInstance() {
187   if ( ! instance) {
188     instance=new MediaPlayer();
189   }
190   return instance;
191 }
192 MediaPlayerRegister *MediaPlayerRegister::instance=NULL;
193
194
195
196
197