2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <mm_player.h>
20 #include <WidgetDB/WidgetDBMgr.h>
21 #include <MMPlayer/EventOnStateChange.h>
26 namespace WrtDeviceApis {
39 void Manager::openFile(const EventOpenPtr& event)
41 EventRequestReceiver<EventOpen>::PostRequest(event);
44 void Manager::play(const EventPlayPtr& event)
46 EventRequestReceiver<EventPlay>::PostRequest(event);
49 void Manager::pause(const EventPausePtr& event)
51 EventRequestReceiver<EventPause>::PostRequest(event);
54 void Manager::resume(const EventResumePtr& event)
56 EventRequestReceiver<EventResume>::PostRequest(event);
59 void Manager::stop(const EventStopPtr& event)
61 EventRequestReceiver<EventStop>::PostRequest(event);
64 void Manager::setWindow(const EventSetWindowPtr& event)
66 EventRequestReceiver<EventSetWindow>::PostRequest(event);
69 void Manager::isPlaying(const EventIsPlayingPtr& event)
71 EventRequestReceiver<EventIsPlaying>::PostRequest(event);
74 void Manager::getVolume(const EventGetVolumePtr& event)
76 EventRequestReceiver<EventGetVolume>::PostRequest(event);
79 void Manager::OnRequestReceived(const EventOpenPtr& event)
83 string fullName = event->getFilename();
84 MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
86 LogError("MMPlayerPtr cast error");
87 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
92 if (MM_PLAYER_STATE_NULL != player->getState()) {
93 err = mm_player_unrealize(player->getHandler());
94 if (MM_ERROR_NONE != err) {
95 LogError("Can't unrealize player. Error code: " << std::hex << err);
96 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
101 if (!fullName.length()) {
102 event->setExceptionCode(
103 Commons::ExceptionCodes::InvalidArgumentException);
107 //Check if current installation path should be added
108 if ((fullName[0] != '/') && (fullName.find("://") == string::npos)) {
110 std::string installationPath;
111 Commons::IWrtWrapperPtr wrapper = event->getWrtWrapper();
113 LogError("Wrt API not accessible. Can't get installation path.");
114 event->setExceptionCode(
115 Commons::ExceptionCodes::PlatformException);
119 WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(
120 wrapper->getWidgetId());
121 installationPath = widgetDB->getConfigValue(
122 WidgetDB::Api::ConfigAttribute::INSTALL_PATH);
124 LogInfo("Config attribute: " << installationPath);
125 fullName.insert(0, installationPath);
126 } catch (const Commons::Exception &ex) {
127 LogError("Obtaining config info failed");
128 event->setExceptionCode(ex.getCode());
133 LogDebug("Opening file: " << fullName);
134 char *errorBuffer = NULL;
135 err = mm_player_set_attribute(
136 player->getHandler(), &errorBuffer, "profile_uri",
137 fullName.c_str(), fullName.length(), NULL);
138 if (MM_ERROR_NONE != err) {
140 "Setting attribute error. Code: " << std::hex << err <<
141 ". Message: " << errorBuffer);
143 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
147 err = mm_player_realize(player->getHandler());
148 if (MM_ERROR_NONE != err) {
149 LogError("Can't realize player. Error code:" << std::hex << err);
150 if (static_cast<int>(MM_ERROR_PLAYER_INVALID_URI) == err) {
152 event->setExceptionCode(
153 Commons::ExceptionCodes::InvalidArgumentException);
155 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
160 if (player->getEmitter()) {
161 EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
162 EventOnStateChange::OPENED));
163 player->getEmitter()->emit(eventStateChanged);
167 void Manager::OnRequestReceived(const EventPlayPtr& event)
171 MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
173 LogError("MMPlayerPtr cast error");
174 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
178 //Check if player currently plays media
179 if (MM_PLAYER_STATE_PLAYING == player->getState() || 0 ==
180 event->getRepeatTimes()) {
181 LogDebug("already playing");
184 player->setRepeatTimes(event->getRepeatTimes() - 1);
186 int err = mm_player_start(player->getHandler());
187 if (MM_ERROR_NONE != err) {
188 LogError("Can't start play. Error code: " << std::hex << err);
189 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
191 //Event onStateChange will be emitted trough platform callback
194 void Manager::OnRequestReceived(const EventPausePtr& event)
198 MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
200 LogError("MMPlayerPtr cast error");
201 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
204 if (MM_PLAYER_STATE_PLAYING != player->getState()) {
208 int err = mm_player_pause(player->getHandler());
209 if (MM_ERROR_NONE != err) {
210 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
212 if (player->getEmitter()) {
213 EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
214 EventOnStateChange::PAUSED));
215 player->getEmitter()->emit(eventStateChanged);
219 void Manager::OnRequestReceived(const EventResumePtr& event)
223 MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
225 LogError("MMPlayerPtr cast error");
226 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
229 if (MM_PLAYER_STATE_PAUSED != player->getState()) {
233 int err = mm_player_resume(player->getHandler());
234 if (MM_ERROR_NONE != err) {
235 LogError("Can't resume player. Error code: " << std::hex << err);
236 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
238 if (player->getEmitter()) {
239 EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
240 EventOnStateChange::RESUMED));
241 player->getEmitter()->emit(eventStateChanged);
245 void Manager::OnRequestReceived(const EventStopPtr& event)
249 MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
251 LogError("MMPlayerPtr cast error");
252 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
255 if (MM_PLAYER_STATE_PLAYING != player->getState() &&
256 MM_PLAYER_STATE_PAUSED != player->getState()) {
260 int err = mm_player_stop(player->getHandler());
261 if (MM_ERROR_NONE != err) {
262 LogError("Can't stop player. Error code: " << std::hex << err);
263 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
266 if (player->getEmitter()) {
267 EventOnStateChangePtr eventStateChanged(new EventOnStateChange(
268 EventOnStateChange::STOPPED));
269 player->getEmitter()->emit(eventStateChanged);
273 void Manager::OnRequestReceived(const EventSetWindowPtr& /*event*/)
276 LogError("Not implemented");
277 //TODO: set display parameter, when xID will be available
280 void Manager::OnRequestReceived(const EventIsPlayingPtr& event)
284 MMPlayerPtr player = DPL::DynamicPointerCast<MMPlayer>(event->getPlayer());
288 LogError("MMPlayerPtr cast error");
289 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
292 event->setIsPlaying(MM_PLAYER_STATE_PLAYING == player->getState());
294 Catch(Commons::PlatformException)
296 LogError("Can't get player status.");
297 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
302 void Manager::OnRequestReceived(const EventGetVolumePtr& event)
305 unsigned int volume = 0;
308 int err = mm_sound_volume_get_step(VOLUME_TYPE_MEDIA, &steps);
309 if (MM_ERROR_NONE != err) {
311 "Can't get volume steps count. Error code: " << std::hex <<
313 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
316 LogInfo("Volume steps count: " << steps);
319 err = mm_sound_volume_get_value(VOLUME_TYPE_MEDIA, &volume);
320 if (MM_ERROR_NONE != err) {
322 "Can't get player's volume. Error code: " << std::hex <<
324 Throw(Commons::UnknownException);
326 LogInfo("Platform player's volume: " << volume);
328 event->setVolume((10.0 * volume) / (steps - 1));
330 LogError("no steps defined");
334 catch (const Commons::UnknownException &ex)
336 LogError("Can't scale the volume");
337 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);