2 * Copyright (c) 2015 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.
17 #include "sound/sound_instance.h"
21 #include "common/logger.h"
22 #include "common/picojson.h"
23 #include "common/platform_exception.h"
24 #include "common/tools.h"
25 #include "sound_manager.h"
26 #include "sound_manager_internal.h"
32 // The privileges that required in Sound API
33 const std::string kPrivilegeSound = "http://tizen.org/privilege/volume.set";
37 using namespace common;
38 using namespace extension::sound;
40 SoundInstance::SoundInstance() : manager_(*this) {
42 using std::placeholders::_1;
43 using std::placeholders::_2;
45 #define REGISTER_METHOD(M) RegisterSyncHandler(#M, std::bind(&SoundInstance::M, this, _1, _2))
46 REGISTER_METHOD(SoundManagerSetVolume);
47 REGISTER_METHOD(SoundManagerUnsetSoundModeChangeListener);
48 REGISTER_METHOD(SoundManagerGetVolume);
49 REGISTER_METHOD(SoundManagerUnsetVolumeChangeListener);
50 REGISTER_METHOD(SoundManagerSetSoundModeChangeListener);
51 REGISTER_METHOD(SoundManagerSetVolumeChangeListener);
52 REGISTER_METHOD(SoundManagerGetSoundMode);
53 REGISTER_METHOD(SoundManagerGetConnectedDeviceList);
54 REGISTER_METHOD(SoundManagerGetActivatedDeviceList);
55 REGISTER_METHOD(SoundManagerAddDeviceStateChangeListener);
56 REGISTER_METHOD(SoundManagerRemoveDeviceStateChangeListener);
57 #undef REGISTER_METHOD
60 SoundInstance::~SoundInstance() {
64 #define CHECK_EXIST(args, name, out) \
65 if (!args.contains(name)) { \
66 LogAndReportError(TypeMismatchException(name " is required argument"), out); \
70 void SoundInstance::SoundManagerGetSoundMode(const picojson::value& args, picojson::object& out) {
72 std::string sound_mode_type;
73 PlatformResult status = manager_.GetSoundMode(&sound_mode_type);
75 if (status.IsSuccess()) {
76 ReportSuccess(picojson::value(sound_mode_type), out);
78 LogAndReportError(status, &out);
82 void SoundInstance::SoundManagerSetVolume(const picojson::value& args, picojson::object& out) {
84 CHECK_PRIVILEGE_ACCESS(kPrivilegeSound, &out);
85 PlatformResult status = manager_.SetVolume(args.get<picojson::object>());
87 if (status.IsSuccess()) {
90 LogAndReportError(status, &out);
94 void SoundInstance::SoundManagerGetVolume(const picojson::value& args, picojson::object& out) {
97 PlatformResult status = manager_.GetVolume(args.get<picojson::object>(), &volume);
99 if (status.IsSuccess()) {
100 ReportSuccess(picojson::value(volume), out);
102 LogAndReportError(status, &out);
106 void SoundInstance::SoundManagerSetSoundModeChangeListener(const picojson::value& args,
107 picojson::object& out) {
109 PlatformResult status = manager_.SetSoundModeChangeListener(this);
111 if (status.IsSuccess()) {
114 LogAndReportError(status, &out);
118 void SoundInstance::SoundManagerUnsetSoundModeChangeListener(const picojson::value& args,
119 picojson::object& out) {
120 PlatformResult status = manager_.UnsetSoundModeChangeListener();
124 if (status.IsSuccess()) {
127 LogAndReportError(status, &out);
131 void SoundInstance::OnSoundModeChange(const std::string& newmode) {
133 if (current_sound_mode == newmode) {
134 LoggerD("New sound mode equals to current sound mode");
137 current_sound_mode = newmode;
138 picojson::value event = picojson::value(picojson::object());
139 picojson::object& obj = event.get<picojson::object>();
140 picojson::value result = picojson::value(newmode);
141 ReportSuccess(result, obj);
142 obj["listenerId"] = picojson::value("SoundModeChangeListener");
143 LoggerD("Posting: %s", event.serialize().c_str());
144 Instance::PostMessage(this, event.serialize().c_str());
147 void SoundInstance::SoundManagerSetVolumeChangeListener(const picojson::value& args,
148 picojson::object& out) {
150 PlatformResult status = manager_.SetVolumeChangeListener();
152 if (status.IsSuccess()) {
155 LogAndReportError(status, &out);
159 void SoundInstance::SoundManagerUnsetVolumeChangeListener(const picojson::value& args,
160 picojson::object& out) {
162 PlatformResult status = manager_.UnsetVolumeChangeListener();
164 if (status.IsSuccess()) {
167 LogAndReportError(status, &out);
171 void SoundInstance::SoundManagerGetConnectedDeviceList(const picojson::value& args,
172 picojson::object& out) {
174 manager_.GetDeviceList(SOUND_DEVICE_ALL_MASK, out);
177 void SoundInstance::SoundManagerGetActivatedDeviceList(const picojson::value& args,
178 picojson::object& out) {
181 "'Activated' concept is deprecated and replated with 'running' concept. "
182 "getActivatedDeviceList() is deprecated and will be removed from next release."
183 "Use getConnectedDeviceList() instead and check isRunning property to get running devices.",
185 manager_.GetDeviceList((sound_device_mask_e)SOUND_DEVICE_STATE_DEPRECATED_ACTIVATED_MASK, out);
188 void SoundInstance::SoundManagerAddDeviceStateChangeListener(const picojson::value& args,
189 picojson::object& out) {
191 PlatformResult result = manager_.AddDeviceStateChangeListener();
193 if (result.IsSuccess()) {
196 LogAndReportError(result, &out);
200 void SoundInstance::SoundManagerRemoveDeviceStateChangeListener(const picojson::value& args,
201 picojson::object& out) {
203 PlatformResult result = manager_.RemoveDeviceStateChangeListener();
205 if (result.IsSuccess()) {
208 LogAndReportError(result, &out);
215 } // namespace extension