[Sound] setVolume
authorRyszard Matuszyk <r.matuszyk@samsung.com>
Thu, 5 Feb 2015 13:19:07 +0000 (14:19 +0100)
committerRyszard Matuszyk <r.matuszyk@samsung.com>
Fri, 6 Feb 2015 12:03:32 +0000 (13:03 +0100)
[Verification] SoundManager_setVolume tct test should be pass

Change-Id: Id9946ed5c471585116bf48343888fb8c20420194
Signed-off-by: Ryszard Matuszyk <r.matuszyk@samsung.com>
src/sound/sound_instance.cc
src/sound/sound_manager.cc
src/sound/sound_manager.h

index b9fb10e..712ab82 100644 (file)
@@ -9,6 +9,7 @@
 #include "common/picojson.h"
 #include "common/logger.h"
 #include "common/platform_exception.h"
+#include "sound_manager.h"
 
 namespace extension {
 namespace sound {
@@ -61,19 +62,14 @@ void SoundInstance::SoundManagerGetSoundMode(const picojson::value& args, picojs
   // if error
   // ReportError(out);
 }
-void SoundInstance::SoundManagerSetVolume(const picojson::value& args, picojson::object& out) {
-  CHECK_EXIST(args, "volume", out)
 
-  double volume = args.get("volume").get<double>();
+void SoundInstance::SoundManagerSetVolume(const picojson::value& args,
+                                          picojson::object& out) {
+  manager_->SetVolume(args.get<picojson::object>());
 
-  // implement it
-
-
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  ReportSuccess(out);
 }
+
 void SoundInstance::SoundManagerGetVolume(const picojson::value& args, picojson::object& out) {
 
 
index 3145aef..aa10af1 100644 (file)
@@ -43,7 +43,7 @@ std::string SoundManager::PlatformEnumToStr(const sound_type_e value) {
   // TODO:  throw InvalidValuesException(message);
 }
 
-SoundManager::SoundManager() {}
+SoundManager::SoundManager() { FillMaxVolumeMap(); }
 
 SoundManager::~SoundManager() {}
 
@@ -52,9 +52,56 @@ SoundManager* SoundManager::GetInstance() {
   return &instance;
 }
 
+void SoundManager::FillMaxVolumeMap() {
+  int max = 100;
+  int ret;
+
+  for (auto& item : platform_enum_map_) {
+    max = 100;
+
+    ret = sound_manager_get_max_volume(item.second, &max);
+    if (ret != SOUND_MANAGER_ERROR_NONE) {
+      LoggerE("SoundManagerGetMaxVolumeFailed : %d", ret);
+    }
+
+    LoggerD("maxVolume: %d - %d", item.second, max);
+
+    max_volume_map_[item.second] = max;
+  }
+}
+
 int SoundManager::GetSoundMode() {}
 
-void SoundManager::SetVolume(const picojson::object& args) {}
+void SoundManager::SetVolume(const picojson::object& args) {
+  const std::string& type = FromJson<std::string>(args, "type");
+  double volume = FromJson<double>(args, "volume");
+
+  LoggerD("SoundType: %s", type.c_str());
+  LoggerD("volume: %f", volume);
+
+  if (volume > 1.0 || volume < 0.0) {
+    LoggerE("Volume should be the value between 0 and 1.");
+    // TODO: throw InvalidValuesException("Volume should be the value between 0
+    // and 1.");
+  }
+
+  auto it = max_volume_map_.find(SoundManager::StrToPlatformEnum(type));
+  if (it == max_volume_map_.end()) {
+    LoggerE("Failed to find maxVolume of type: %d", type.c_str());
+    // TODO: throw UnknownException("Failed to find maxVolume");
+  }
+
+  int max_volume = it->second;
+  int value = round(volume * max_volume);
+  LoggerD("volume: %lf, maxVolume: %d, value: %d", volume, max_volume, value);
+
+  int ret =
+      sound_manager_set_volume(SoundManager::StrToPlatformEnum(type), value);
+  if (ret != SOUND_MANAGER_ERROR_NONE) {
+    LoggerE("Failed to set volume: %d", ret);
+    // TODO: throw UnknownException("Failed to set volume");
+  }
+}
 
 double SoundManager::GetVolume(const picojson::object& args) {}
 
index becf94e..7d58b83 100644 (file)
@@ -28,8 +28,12 @@ class SoundManager {
   SoundManager();
   virtual ~SoundManager();
 
+  std::map<sound_type_e, int> max_volume_map_;
+
   static const std::map<std::string, sound_type_e> platform_enum_map_;
 
+  void FillMaxVolumeMap();
+
   static sound_type_e StrToPlatformEnum(const std::string& key);
   static std::string PlatformEnumToStr(const sound_type_e value);
 };