upload tizen1.0 source
[framework/web/wrt-plugins-common.git] / src / modules / tizen / MMPlayer / Manager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include "Manager.h"
17 #include <mmf/mm.h>
18 #include <mm_sound.h>
19 #include <mm_player.h>
20 #include <WidgetDB/WidgetDBMgr.h>
21 #include <Commons/WrtAccess/WrtAccess.h>
22 #include <MMPlayer/EventOnStateChange.h>
23 #include "MMPlayer.h"
24
25 using namespace std;
26
27 namespace WrtDeviceApis {
28 namespace MMPlayer {
29
30 using namespace Api;
31
32 Manager::Manager()
33 {
34 }
35
36 Manager::~Manager()
37 {
38 }
39
40 void Manager::openFile(const EventOpenPtr& event)
41 {
42     EventRequestReceiver<EventOpen>::PostRequest(event);
43 }
44
45 void Manager::play(const EventPlayPtr& event)
46 {
47     EventRequestReceiver<EventPlay>::PostRequest(event);
48 }
49
50 void Manager::pause(const EventPausePtr& event)
51 {
52     EventRequestReceiver<EventPause>::PostRequest(event);
53 }
54
55 void Manager::resume(const EventResumePtr& event)
56 {
57     EventRequestReceiver<EventResume>::PostRequest(event);
58 }
59
60 void Manager::stop(const EventStopPtr& event)
61 {
62     EventRequestReceiver<EventStop>::PostRequest(event);
63 }
64
65 void Manager::setWindow(const EventSetWindowPtr& event)
66 {
67     EventRequestReceiver<EventSetWindow>::PostRequest(event);
68 }
69
70 void Manager::isPlaying(const EventIsPlayingPtr& event)
71 {
72     EventRequestReceiver<EventIsPlaying>::PostRequest(event);
73 }
74
75 void Manager::getVolume(const EventGetVolumePtr& event)
76 {
77     EventRequestReceiver<EventGetVolume>::PostRequest(event);
78 }
79
80 void Manager::OnRequestReceived(const EventOpenPtr& event)
81 {
82     LogDebug("Entered");
83
84     string fullName = event->getFilename();
85     MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
86     if (!player) {
87         LogError("MMPlayerPtr cast error");
88         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
89         return;
90     }
91
92     int err;
93     if (MM_PLAYER_STATE_NULL != player->getState()) {
94         err = mm_player_unrealize(player->getHandler());
95         if (MM_ERROR_NONE != err) {
96             LogError("Can't unrealize player. Error code: " << std::hex << err);
97             event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
98             return;
99         }
100     }
101
102     if (!fullName.length()) {
103         event->setExceptionCode(
104             Commons::ExceptionCodes::InvalidArgumentException);
105         return;
106     }
107
108     //Check if current installation path should be added
109     if ((fullName[0] != '/') && (fullName.find("://") == string::npos)) {
110         try {
111             using namespace WrtDeviceApis::Commons;
112             std::string installationPath;
113             int widgetId = WrtAccessSingleton::Instance().getWidgetId();
114
115             WidgetDB::Api::IWidgetDBPtr widgetDB = 
116                 WidgetDB::Api::getWidgetDB(widgetId);
117             installationPath = widgetDB->getConfigValue(
118                     WidgetDB::Api::ConfigAttribute::INSTALL_PATH);
119
120             LogInfo("Config attribute: " << installationPath);
121             fullName.insert(0, installationPath);
122         } catch (const Commons::Exception &ex) {
123             LogError("Obtaining config info failed");
124             event->setExceptionCode(ex.getCode());
125             return;
126         }
127     }
128
129     LogDebug("Opening file: " << fullName);
130     char *errorBuffer = NULL;
131     err = mm_player_set_attribute(
132             player->getHandler(), &errorBuffer, "profile_uri",
133             fullName.c_str(), fullName.length(), NULL);
134     if (MM_ERROR_NONE != err) {
135         LogError(
136             "Setting attribute error. Code: " << std::hex << err <<
137             ". Message: " << errorBuffer);
138         free(errorBuffer);
139         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
140         return;
141     }
142
143     err = mm_player_realize(player->getHandler());
144     if (MM_ERROR_NONE != err) {
145         LogError("Can't realize player. Error code:" << std::hex << err);
146         if (static_cast<int>(MM_ERROR_PLAYER_INVALID_URI) == err) {
147             LogDebug("x");
148             event->setExceptionCode(
149                 Commons::ExceptionCodes::InvalidArgumentException);
150         } else {
151             event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
152         }
153         return;
154     }
155
156     if (player->getEmitter()) {
157         EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
158                                                     EventOnStateChange::OPENED));
159         player->getEmitter()->emit(eventStateChanged);
160     }
161 }
162
163 void Manager::OnRequestReceived(const EventPlayPtr& event)
164 {
165     LogDebug("Entered");
166
167     MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
168     if (!player) {
169         LogError("MMPlayerPtr cast error");
170         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
171         return;
172     }
173
174     //Check if player currently plays media
175     if (MM_PLAYER_STATE_PLAYING == player->getState() || 0 ==
176         event->getRepeatTimes()) {
177         LogDebug("already playing");
178         return;
179     }
180     player->setRepeatTimes(event->getRepeatTimes() - 1);
181
182     int err = mm_player_start(player->getHandler());
183     if (MM_ERROR_NONE != err) {
184         LogError("Can't start play. Error code: " << std::hex << err);
185         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
186     }
187     //Event onStateChange will be emitted trough platform callback
188 }
189
190 void Manager::OnRequestReceived(const EventPausePtr& event)
191 {
192     LogDebug("Entered");
193
194     MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
195     if (!player) {
196         LogError("MMPlayerPtr cast error");
197         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
198         return;
199     }
200     if (MM_PLAYER_STATE_PLAYING != player->getState()) {
201         return;
202     }
203
204     int err = mm_player_pause(player->getHandler());
205     if (MM_ERROR_NONE != err) {
206         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
207     }
208     if (player->getEmitter()) {
209         EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
210                                                     EventOnStateChange::PAUSED));
211         player->getEmitter()->emit(eventStateChanged);
212     }
213 }
214
215 void Manager::OnRequestReceived(const EventResumePtr& event)
216 {
217     LogDebug("Entered");
218
219     MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
220     if (!player) {
221         LogError("MMPlayerPtr cast error");
222         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
223         return;
224     }
225     if (MM_PLAYER_STATE_PAUSED != player->getState()) {
226         return;
227     }
228
229     int err = mm_player_resume(player->getHandler());
230     if (MM_ERROR_NONE != err) {
231         LogError("Can't resume player. Error code: " << std::hex << err);
232         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
233     }
234     if (player->getEmitter()) {
235         EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
236                                                     EventOnStateChange::RESUMED));
237         player->getEmitter()->emit(eventStateChanged);
238     }
239 }
240
241 void Manager::OnRequestReceived(const EventStopPtr& event)
242 {
243     LogDebug("Entered");
244
245     MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
246     if (!player) {
247         LogError("MMPlayerPtr cast error");
248         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
249         return;
250     }
251     if (MM_PLAYER_STATE_PLAYING != player->getState() &&
252         MM_PLAYER_STATE_PAUSED != player->getState()) {
253         return;
254     }
255
256     int err = mm_player_stop(player->getHandler());
257     if (MM_ERROR_NONE != err) {
258         LogError("Can't stop player. Error code: " << std::hex << err);
259         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
260     }
261
262     if (player->getEmitter()) {
263         EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
264                                                     EventOnStateChange::STOPPED));
265         player->getEmitter()->emit(eventStateChanged);
266     }
267 }
268
269 void Manager::OnRequestReceived(const EventSetWindowPtr& /*event*/)
270 {
271     LogDebug("Entered");
272     LogError("Not implemented");
273     //TODO: set display parameter, when xID will be available
274 }
275
276 void Manager::OnRequestReceived(const EventIsPlayingPtr& event)
277 {
278     LogDebug("Entered");
279
280     MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
281     Try
282     {
283         if (!player) {
284             LogError("MMPlayerPtr cast error");
285             event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
286             return;
287         }
288         event->setIsPlaying(MM_PLAYER_STATE_PLAYING == player->getState());
289     }
290     Catch(Commons::PlatformException)
291     {
292         LogError("Can't get player status.");
293         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
294         return;
295     }
296 }
297
298 void Manager::OnRequestReceived(const EventGetVolumePtr& event)
299 {
300     LogDebug("Entered");
301     unsigned int volume = 0;
302     int steps = 0;
303
304     int err = mm_sound_volume_get_step(VOLUME_TYPE_MEDIA, &steps);
305     if (MM_ERROR_NONE != err) {
306         LogError(
307             "Can't get volume steps count. Error code: " << std::hex <<
308             err);
309         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
310         return;
311     }
312     LogInfo("Volume steps count: " << steps);
313     try
314     {
315         err = mm_sound_volume_get_value(VOLUME_TYPE_MEDIA, &volume);
316         if (MM_ERROR_NONE != err) {
317             LogError(
318                 "Can't get player's volume. Error code: " << std::hex <<
319                 err);
320             Throw(Commons::UnknownException);
321         }
322         LogInfo("Platform player's volume: " << volume);
323         if (steps > 1) {
324             event->setVolume((10.0 * volume) / (steps - 1));
325         } else {
326             LogError("no steps defined");
327             event->setVolume(0);
328         }
329     }
330     catch (const Commons::UnknownException &ex)
331     {
332         LogError("Can't scale the volume");
333         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
334     }
335 }
336
337 }
338 }
339