a5f1b77337d925afe6ad326d8ceb6c1eeee33ad9
[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::getInstance(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::getInstance(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 // eStaticServiceMP3Info
61
62
63 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
64 // about unopened files.
65
66 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
67 // should have a database backend where ID3-files etc. are cached.
68 // this would allow listing the mp3 database based on certain filters.
69
70 DEFINE_REF(eStaticServiceMP3Info)
71
72 eStaticServiceMP3Info::eStaticServiceMP3Info()
73 {
74 }
75
76 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
77 {
78         name = "MP3 file: " + ref.path;
79         return 0;
80 }
81
82 // eServiceMP3
83
84 void eServiceMP3::test_end()
85 {
86         eDebug("end of mp3!");
87         stop();
88 }
89
90 eServiceMP3::eServiceMP3(const char *filename): filename(filename), test(eApp)
91 {
92         m_state = stIdle;
93         eDebug("SERVICEMP3 construct!");
94 }
95
96 eServiceMP3::~eServiceMP3()
97 {
98         eDebug("SERVICEMP3 destruct!");
99         if (m_state == stRunning)
100                 stop();
101 }
102
103 DEFINE_REF(eServiceMP3);        
104
105 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
106 {
107         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
108         return 0;
109 }
110
111 RESULT eServiceMP3::start()
112 {
113         assert(m_state == stIdle);
114         
115         m_state = stRunning;
116
117         printf("mp3 starts\n");
118         printf("MP3: %s start\n", filename.c_str());
119         test.start(1000, 1);
120         CONNECT(test.timeout, eServiceMP3::test_end);
121         m_event(this, evStart);
122         return 0;
123 }
124
125 RESULT eServiceMP3::stop()
126 {
127         assert(m_state != stIdle);
128         if (m_state == stStopped)
129                 return -1;
130         test.stop();
131         printf("MP3: %s stop\n", filename.c_str());
132         m_state = stStopped;
133         m_event(this, evEnd);
134         return 0;
135 }
136
137 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr) { ptr=this; return 0; }
138
139                 // iPausableService
140 RESULT eServiceMP3::pause() { printf("mp3 pauses!\n"); return 0; }
141 RESULT eServiceMP3::unpause() { printf("mp3 unpauses!\n"); return 0; }
142
143 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i) { i = this; return 0; }
144
145 RESULT eServiceMP3::getName(const eServiceReference &ref, std::string &name)
146 {
147         name = "MP3 File: " + filename;
148         return 0;
149 }
150
151 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");