add EOF event
[enigma2.git] / lib / service / servicemp3.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/base/object.h>
3 #include <lib/base/ebase.h>
4 #include <string>
5 #include <lib/service/servicemp3.h>
6 #include <lib/service/service.h>
7 #include <lib/base/init_num.h>
8 #include <lib/base/init.h>
9
10 // eServiceFactoryMP3
11
12 eServiceFactoryMP3::eServiceFactoryMP3()
13 {
14         ePtr<eServiceCenter> sc;
15         
16         eServiceCenter::getPrivInstance(sc);
17         if (sc)
18                 sc->addServiceFactory(eServiceFactoryMP3::id, this);
19
20         m_service_info = new eStaticServiceMP3Info();
21 }
22
23 eServiceFactoryMP3::~eServiceFactoryMP3()
24 {
25         ePtr<eServiceCenter> sc;
26         
27         eServiceCenter::getPrivInstance(sc);
28         if (sc)
29                 sc->removeServiceFactory(eServiceFactoryMP3::id);
30 }
31
32 DEFINE_REF(eServiceFactoryMP3)
33
34         // iServiceHandler
35 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
36 {
37                 // check resources...
38         ptr = new eServiceMP3(ref.path.c_str());
39         return 0;
40 }
41
42 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
43 {
44         ptr=0;
45         return -1;
46 }
47
48 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
49 {
50         ptr=0;
51         return -1;
52 }
53
54 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
55 {
56         ptr = m_service_info;
57         return 0;
58 }
59
60 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
61 {
62         ptr = 0;
63         return -1;
64 }
65
66
67 // eStaticServiceMP3Info
68
69
70 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
71 // about unopened files.
72
73 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
74 // should have a database backend where ID3-files etc. are cached.
75 // this would allow listing the mp3 database based on certain filters.
76
77 DEFINE_REF(eStaticServiceMP3Info)
78
79 eStaticServiceMP3Info::eStaticServiceMP3Info()
80 {
81 }
82
83 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
84 {
85         name = "MP3 file: " + ref.path;
86         return 0;
87 }
88
89 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
90 {
91         return -1;
92 }
93
94 // eServiceMP3
95
96 void eServiceMP3::test_end()
97 {
98         eDebug("end of mp3!");
99         stop();
100 }
101
102 eServiceMP3::eServiceMP3(const char *filename): filename(filename), test(eApp)
103 {
104         m_state = stIdle;
105         eDebug("SERVICEMP3 construct!");
106 }
107
108 eServiceMP3::~eServiceMP3()
109 {
110         eDebug("SERVICEMP3 destruct!");
111         if (m_state == stRunning)
112                 stop();
113 }
114
115 DEFINE_REF(eServiceMP3);        
116
117 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
118 {
119         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
120         return 0;
121 }
122
123 RESULT eServiceMP3::start()
124 {
125         assert(m_state == stIdle);
126         
127         m_state = stRunning;
128
129         printf("mp3 starts\n");
130         printf("MP3: %s start\n", filename.c_str());
131         test.start(1000, 1);
132         CONNECT(test.timeout, eServiceMP3::test_end);
133         m_event(this, evStart);
134         return 0;
135 }
136
137 RESULT eServiceMP3::stop()
138 {
139         assert(m_state != stIdle);
140         if (m_state == stStopped)
141                 return -1;
142         test.stop();
143         printf("MP3: %s stop\n", filename.c_str());
144         m_state = stStopped;
145         m_event(this, evEnd);
146         return 0;
147 }
148
149 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
150 {
151         ptr=this;
152         return 0;
153 }
154
155 RESULT eServiceMP3::setSlowMotion(int ratio)
156 {
157         return -1;
158 }
159
160 RESULT eServiceMP3::setFastForward(int ratio)
161 {
162         return -1;
163 }
164   
165                 // iPausableService
166 RESULT eServiceMP3::pause()
167 {
168         printf("mp3 pauses!\n");
169         return 0;
170 }
171
172 RESULT eServiceMP3::unpause()
173 {
174         printf("mp3 unpauses!\n");
175         return 0;
176 }
177
178 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
179 {
180         i = this;
181         return 0;
182 }
183
184 RESULT eServiceMP3::getName(std::string &name)
185 {
186         name = "MP3 File: " + filename;
187         return 0;
188 }
189
190 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");