Add MediaVolume rule in Media Plugin
authorJinWang An <jinwang.an@samsung.com>
Tue, 19 Nov 2019 07:39:11 +0000 (16:39 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 19 Mar 2020 04:30:37 +0000 (13:30 +0900)
media/MediaAction.h
media/MediaFactory.cpp
media/MediaFactory.h
media/MediaPlugin.cpp
media/MediaVolume.cpp [new file with mode: 0644]
media/MediaVolume.h [new file with mode: 0644]
media/tizen_media_rule.xml
unittests/mdsp_test_media.cpp

index 04e1eb3..77927e6 100644 (file)
@@ -33,6 +33,10 @@ public:
        {
                return MODES_ERROR_NOT_SUPPORTED;
        }
+       virtual int set(int val)
+       {
+               return MODES_ERROR_NOT_SUPPORTED;
+       }
 };
 
 MODES_NAMESPACE_END
index 7402c78..a54aa10 100644 (file)
 #include "MediaFactory.h"
 #include "plugin-log.h"
 #include "MediaPlayer.h"
+#include "MediaVolume.h"
 
 MODES_NAMESPACE_USE;
 
 MediaFactory::MediaFactory()
 {
        actionMap[MediaPlayer::NAME] = PLAYER;
+       actionMap[MediaVolume::NAME] = VOLUME;
 }
 
 MediaAction* MediaFactory::createAction(const std::string &key)
@@ -37,6 +39,9 @@ MediaAction* MediaFactory::createAction(const std::string &key)
        case PLAYER:
                action = new MediaPlayer();
                break;
+       case VOLUME:
+               action = new MediaVolume();
+               break;
        default:
                action = nullptr;
                break;
index 6d94487..2e76df3 100644 (file)
@@ -30,7 +30,8 @@ public:
        void destroyAction(MediaAction *action);
 private:
        enum actionKey{
-               PLAYER
+               PLAYER,
+               VOLUME
        };
 
        std::map<std::string, enum actionKey> actionMap;
index b9ecc55..25cc538 100644 (file)
@@ -29,7 +29,10 @@ public:
        ~MediaPlugin();
 
        int set(const std::string &key, const std::string &val, PluginAction **piAction) override;
+       int set(const std::string &key, int val, PluginAction **piAction) override;
        PluginAction* getUndoAction(const std::string &key, const std::string &info) override;
+
+       void undo(PluginAction *piAction) override;
 private:
        MediaFactory mediaFactory;
 };
@@ -58,10 +61,35 @@ int MediaPlugin::set(const std::string &key, const std::string &val, PluginActio
        MediaAction *action = mediaFactory.createAction(key);
        RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str());
 
+       int ret = action->set(val);
+       if (ret != MODES_ERROR_NONE) {
+               ERR("Action(%s) set(%s) Fail(%d)", key.c_str(), val.c_str(), ret);
+               mediaFactory.destroyAction(action);
+               return ret;
+       }
        DBG("Action(%s) set(%s)", key.c_str(), val.c_str());
 
+       if (piAction)
+               *piAction = action;
+       else
+               mediaFactory.destroyAction(action);
+       return ret;
+}
+
+int MediaPlugin::set(const std::string &key, int val, PluginAction **piAction)
+{
+       MediaAction *action = mediaFactory.createAction(key);
+       RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str());
+
        int ret = action->set(val);
-       if ((ret == MODES_ERROR_NONE) && piAction)
+       if (ret != MODES_ERROR_NONE) {
+               ERR("Action(%s) set(%d) Fail(%d)", key.c_str(), val, ret);
+               mediaFactory.destroyAction(action);
+               return ret;
+       }
+       DBG("Action(%s) set(%d)", key.c_str(), val);
+
+       if (piAction)
                *piAction = action;
        else
                mediaFactory.destroyAction(action);
@@ -82,3 +110,14 @@ PluginAction* MediaPlugin::getUndoAction(const std::string &key, const std::stri
 
        return action;
 }
+
+void MediaPlugin::undo(PluginAction *piAction)
+{
+       MediaAction *action = static_cast<MediaAction*>(piAction);
+       RET_IF(nullptr == piAction);
+
+       DBG("Action(%s) undo", action->getName().c_str());
+
+       action->undo();
+       mediaFactory.destroyAction(action);
+}
diff --git a/media/MediaVolume.cpp b/media/MediaVolume.cpp
new file mode 100644 (file)
index 0000000..ed2725f
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "MediaVolume.h"
+#include <string>
+#include <sstream>
+#include <modes_errors.h>
+#include <player.h>
+#include "plugin-log.h"
+
+MODES_NAMESPACE_USE;
+
+const std::string MediaVolume::NAME = "volume";
+
+MediaVolume::MediaVolume()
+       : MediaAction(NAME), oldVal(-1), volumeCallbackID(0), cb(NULL), cbData(NULL), requestVal(-1)
+{
+}
+
+void MediaVolume::volumeChangedCB(sound_type_e type, unsigned int volume, void *user_data)
+{
+       MediaVolume *action = (MediaVolume*)user_data;
+       RET_IF(NULL == user_data);
+
+       if (action->requestVal != (int)volume)
+               action->cb(action->cbData);
+}
+
+int MediaVolume::set(int val)
+{
+       requestVal = val;
+       int ret = sound_manager_get_volume(SOUND_TYPE_MEDIA, &oldVal);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               ERR("sound_manager_get_volume() Fail(%s)", get_error_message(ret));
+               return MODES_ERROR_SYSTEM;
+       }
+
+       if (val == oldVal) {
+               INFO("Already media volume is [%d]", val);
+               return MODES_ERROR_NONE;
+       }
+
+       ret = sound_manager_set_volume(SOUND_TYPE_MEDIA, val);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               ERR("sound_manager_set_volume() Fail(%s)", get_error_message(ret));
+               return MODES_ERROR_SYSTEM;
+       }
+
+       return MODES_ERROR_NONE;
+}
+
+void MediaVolume::undo()
+{
+       int ret = sound_manager_set_volume(SOUND_TYPE_MEDIA, oldVal);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               ERR("sound_manager_set_volume() Fail(%s)", get_error_message(ret));
+       }
+}
+
+std::string MediaVolume::serialize()
+{
+       std::ostringstream ostr;
+
+       ostr << oldVal;
+       return ostr.str();
+}
+
+int MediaVolume::parse(const std::string &archive)
+{
+       std::istringstream iss(archive);
+       iss >> oldVal;
+
+       return MODES_ERROR_NONE;
+}
+
+int MediaVolume::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       int ret = sound_manager_add_volume_changed_cb(volumeChangedCB, this, &volumeCallbackID);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               ERR("sound_manager_add_volume_changed_cb() Fail(%s)", get_error_message(ret));
+               return MODES_ERROR_SYSTEM;
+       }
+       cb = callback;
+       cbData = userData;
+       return MODES_ERROR_NONE;
+}
+
+void MediaVolume::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       int ret = sound_manager_remove_volume_changed_cb(volumeCallbackID);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               ERR("sound_manager_add_volume_changed_cb() Fail(%s)", get_error_message(ret));
+       }
+       cb = NULL;
+       cbData = NULL;
+}
diff --git a/media/MediaVolume.h b/media/MediaVolume.h
new file mode 100644 (file)
index 0000000..b9da085
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <string>
+#include <sound_manager.h>
+#include "plugin-def.h"
+#include "MediaAction.h"
+
+MODES_NAMESPACE_BEGIN
+
+class MediaVolume : public MediaAction {
+public:
+       static const std::string NAME;
+       MediaVolume();
+       ~MediaVolume() = default;
+
+       int set(int val) override;
+       void undo() override;
+       std::string serialize() override;
+       int parse(const std::string &archive) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+private:
+       static void volumeChangedCB(sound_type_e type, unsigned int volume, void *user_data);
+       int oldVal;
+       int volumeCallbackID;
+       valueChangedCB cb;
+       void *cbData;
+       int requestVal;
+};
+
+MODES_NAMESPACE_END
index fd152dd..e796bce 100644 (file)
@@ -5,5 +5,10 @@
       <desc>Audio/Video player</desc>
       <domain>Multimedia</domain>
     </rule>
+    <rule name="media.volume" type="int" since="6.0" plugin="media">
+      <alias name="MEDIA_MUTE">0</alias>
+      <desc>Media volume</desc>
+      <domain>Multimedia</domain>
+    </rule>
   </actionRule>
 </tizenModes>
index f598274..89073ee 100644 (file)
@@ -41,7 +41,7 @@ protected:
                plugin = NULL;
        }
 
-       static gboolean mediaPluginSetUndoTimeout(gpointer data)
+       static gboolean mediaPlaySetUndoTimeout(gpointer data)
        {
                PluginAction *action = (PluginAction*)data;
                plugin->undo(action);
@@ -49,7 +49,7 @@ protected:
                return false;
        }
 
-       static gboolean mediaPluginSetUndoIdler(gpointer data)
+       static gboolean mediaPlaySetUndoIdler(gpointer data)
        {
                PluginAction *action;
                result = plugin->set("player", std::string("/opt/usr/data/settings/Ringtones/ringtone_sdk.mp3"), &action);
@@ -59,13 +59,26 @@ protected:
                EXPECT_EQ(MODES_ERROR_NONE, result);
 
                if (MODES_ERROR_NONE == result)
-                       g_timeout_add(1000, mediaPluginSetUndoTimeout, action);
+                       g_timeout_add(1000, mediaPlaySetUndoTimeout, action);
                else
                        g_main_loop_quit(loop);
 
                return G_SOURCE_REMOVE;
        }
 
+       static gboolean mediaVolumeSetUndoIdler(gpointer data)
+       {
+               PluginAction *action;
+               result = plugin->set("volume", 5, &action);
+               EXPECT_EQ(MODES_ERROR_NONE, result);
+
+               plugin->undo(action);
+
+               g_main_loop_quit(loop);
+
+               return G_SOURCE_REMOVE;
+       }
+
        static int result;
        static GMainLoop *loop;
        static Plugin *plugin;
@@ -75,9 +88,15 @@ int MediaPluginTest::result = 0;
 Plugin *MediaPluginTest::plugin = NULL;
 GMainLoop *MediaPluginTest::loop = NULL;
 
-TEST_F(MediaPluginTest, setUndoPluginmedia)
+TEST_F(MediaPluginTest, setUndoMediaPlay)
+{
+       g_idle_add(mediaPlaySetUndoIdler, plugin);
+       g_main_loop_run(loop);
+}
+
+TEST_F(MediaPluginTest, setUndoMediaVolume)
 {
-       g_idle_add(mediaPluginSetUndoIdler, plugin);
+       g_idle_add(mediaVolumeSetUndoIdler, plugin);
        g_main_loop_run(loop);
 }