From bbb6a92891071ebbe7faa049c3ad27f3699d0854 Mon Sep 17 00:00:00 2001 From: Michal Michalski Date: Tue, 7 May 2019 10:59:32 +0200 Subject: [PATCH] [mediacontroller][common] Use PlatformEnum in MediaController. Refactor enums in MediaController module using PlatformEnum template class. [Verification] Code compiles + MC TCT passed. Signed-off-by: Michal Michalski Change-Id: Ie861d1c0aca7651a2f9ce45b966773082abf6045 --- src/mediacontroller/mediacontroller_client.cc | 33 ++- src/mediacontroller/mediacontroller_server.cc | 38 ++-- src/mediacontroller/mediacontroller_types.cc | 198 +++++++----------- src/mediacontroller/mediacontroller_types.h | 36 ++-- 4 files changed, 119 insertions(+), 186 deletions(-) diff --git a/src/mediacontroller/mediacontroller_client.cc b/src/mediacontroller/mediacontroller_client.cc index 05d0bda7..513dffee 100644 --- a/src/mediacontroller/mediacontroller_client.cc +++ b/src/mediacontroller/mediacontroller_client.cc @@ -22,6 +22,7 @@ #include "common/logger.h" #include "common/scope_exit.h" +#include "common/tools.h" #include "mediacontroller/mediacontroller_types.h" @@ -153,10 +154,9 @@ PlatformResult MediaControllerClient::GetLatestServerInfo(picojson::value* serve } std::string state_str; - PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerServerState, - static_cast(state), &state_str); + PlatformResult result = types::MediaControllerServerStateEnum.getName(state, &state_str); if (!result) { - LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str()); + LoggerE("MediaControllerServerStateEnum.getName() failed, error: %s", result.message().c_str()); return result; } @@ -187,7 +187,7 @@ PlatformResult MediaControllerClient::GetPlaybackInfo(const std::string& server_ // playback state std::string state; - PlatformResult result = Types::ConvertPlaybackState(playback_h, &state); + PlatformResult result = types::ConvertPlaybackState(playback_h, &state); if (!result) { LoggerE("ConvertPlaybackState failed, error: %s", result.message().c_str()); return result; @@ -195,7 +195,7 @@ PlatformResult MediaControllerClient::GetPlaybackInfo(const std::string& server_ // playback position double position; - result = Types::ConvertPlaybackPosition(playback_h, &position); + result = types::ConvertPlaybackPosition(playback_h, &position); if (!result) { LoggerE("ConvertPlaybackPosition failed, error: %s", result.message().c_str()); return result; @@ -253,7 +253,7 @@ PlatformResult MediaControllerClient::GetMetadata(const std::string& server_name mc_metadata_destroy(metadata_h); }; - PlatformResult result = Types::ConvertMetadata(metadata_h, metadata); + PlatformResult result = types::ConvertMetadata(metadata_h, metadata); if (!result) { return result; } @@ -295,10 +295,9 @@ void MediaControllerClient::OnServerStatusUpdate(const char* server_name, mc_ser // server state std::string state_str; - PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerServerState, - static_cast(state), &state_str); + PlatformResult result = types::MediaControllerServerStateEnum.getName(state, &state_str); if (!result) { - LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str()); + LoggerE("MediaControllerServerStateEnum.getName() failed, error: %s", result.message().c_str()); return; } @@ -392,7 +391,7 @@ void MediaControllerClient::OnPlaybackUpdate(const char* server_name, mc_playbac // playback state std::string state; - PlatformResult result = Types::ConvertPlaybackState(playback, &state); + PlatformResult result = types::ConvertPlaybackState(playback, &state); if (!result) { LoggerE("ConvertPlaybackState failed, error: %s", result.message().c_str()); return; @@ -400,7 +399,7 @@ void MediaControllerClient::OnPlaybackUpdate(const char* server_name, mc_playbac // playback position double position; - result = Types::ConvertPlaybackPosition(playback, &position); + result = types::ConvertPlaybackPosition(playback, &position); if (!result) { LoggerE("ConvertPlaybackPosition failed, error: %s", result.message().c_str()); return; @@ -456,7 +455,7 @@ void MediaControllerClient::OnMetadataUpdate(const char* server_name, mc_metadat picojson::object& data_o = data.get(); picojson::value metadata = picojson::value(picojson::object()); - PlatformResult result = Types::ConvertMetadata(metadata_h, &metadata.get()); + PlatformResult result = types::ConvertMetadata(metadata_h, &metadata.get()); if (!result) { LoggerE("ConvertMetadata failed, error: %s", result.message().c_str()); return; @@ -551,11 +550,10 @@ void MediaControllerClient::OnCommandReply(const char* server_name, const char* PlatformResult MediaControllerClient::SendPlaybackState(const std::string& server_name, const std::string& state) { ScopeLogger(); - int state_e; // In Native API, since Tizen 5.0 an action instead of a state is sent to change the state of a // server. In Web API the names were not refactored. - PlatformResult result = - Types::StringToPlatformEnum(Types::kMediaControllerPlaybackAction, state, &state_e); + mc_playback_action_e action_e; + PlatformResult result = types::MediaControllerPlaybackActionEnum.getValue(state, &action_e); if (!result) { return result; } @@ -565,10 +563,7 @@ PlatformResult MediaControllerClient::SendPlaybackState(const std::string& serve SCOPE_EXIT { free(request_id); };*/ - int ret; - ret = mc_client_send_playback_action_cmd(handle_, server_name.c_str(), - static_cast(state_e), - /*&request_id*/ nullptr); + int ret = mc_client_send_playback_action_cmd(handle_, server_name.c_str(), action_e, nullptr); if (MEDIA_CONTROLLER_ERROR_NONE != ret) { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error sending playback state", ("mc_client_send_playback_action_cmd() error: %d, message: %s", ret, diff --git a/src/mediacontroller/mediacontroller_server.cc b/src/mediacontroller/mediacontroller_server.cc index 9c089cc1..df433136 100644 --- a/src/mediacontroller/mediacontroller_server.cc +++ b/src/mediacontroller/mediacontroller_server.cc @@ -74,27 +74,25 @@ PlatformResult MediaControllerServer::Init() { PlatformResult MediaControllerServer::SetPlaybackState(const std::string& state) { ScopeLogger(); - int state_int; - PlatformResult result = - Types::StringToPlatformEnum(Types::kMediaControllerPlaybackState, state, &state_int); - + mc_playback_states_e state_e; + PlatformResult result = types::MediaControllerPlaybackStateEnum.getValue(state, &state_e); if (!result) { return result; } - if (static_cast(state_int) == playback_state_) { + if (state_e == playback_state_) { LoggerD("No change in playback state requested, skipping"); return PlatformResult(ErrorCode::NO_ERROR); } - int ret = mc_server_set_playback_state(handle_, static_cast(state_int)); + int ret = mc_server_set_playback_state(handle_, state_e); if (MEDIA_CONTROLLER_ERROR_NONE != ret) { return LogAndCreateResult( ErrorCode::UNKNOWN_ERR, "Error setting playback state", ("mc_server_set_playback_state() error: %d, message: %s", ret, get_error_message(ret))); } - playback_state_ = static_cast(state_int); + playback_state_ = state_e; ret = mc_server_update_playback_info(handle_); if (MEDIA_CONTROLLER_ERROR_NONE != ret) { @@ -180,16 +178,14 @@ PlatformResult MediaControllerServer::SetRepeatMode(bool mode) { PlatformResult MediaControllerServer::SetMetadata(const picojson::object& metadata) { ScopeLogger(); - int attribute_int, ret; for (picojson::object::const_iterator i = metadata.begin(); i != metadata.end(); ++i) { - PlatformResult result = Types::StringToPlatformEnum(Types::kMediaControllerMetadataAttribute, - i->first, &attribute_int); + mc_meta_e attr_e; + PlatformResult result = types::MediaControllerMetadataAttributeEnum.getValue(i->first, &attr_e); if (!result) { return result; } - ret = mc_server_set_metadata(handle_, static_cast(attribute_int), - i->second.to_str().c_str()); + int ret = mc_server_set_metadata(handle_, attr_e, i->second.to_str().c_str()); if (MEDIA_CONTROLLER_ERROR_NONE != ret) { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error setting metadata", ("mc_server_set_metadata(%s) error: %d, message: %s", @@ -197,7 +193,7 @@ PlatformResult MediaControllerServer::SetMetadata(const picojson::object& metada } } - ret = mc_server_update_metadata(handle_); + int ret = mc_server_update_metadata(handle_); if (MEDIA_CONTROLLER_ERROR_NONE != ret) { return LogAndCreateResult( ErrorCode::UNKNOWN_ERR, "Error updating metadata", @@ -413,17 +409,17 @@ void MediaControllerServer::OnPlaybackActionCommand(const char* client_name, con MediaControllerServer* server = static_cast(user_data); // Here, we need to convert mc_playback_action_e enum to mc_playback_states_e enum. - std::string state; - PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerPlaybackAction, - static_cast(action), &state); + std::string action_str; + PlatformResult result = types::MediaControllerPlaybackActionEnum.getName(action, &action_str); if (!result) { - LoggerW("PlatformEnumToString failed, error: %s", result.message().c_str()); + LoggerW("MediaControllerPlaybackActionEnum.getName() failed, error: %s", result.message().c_str()); return; } - int state_e = 0; - result = Types::StringToPlatformEnum(Types::kMediaControllerPlaybackState, state, &state_e); + + mc_playback_states_e state_e; + result = types::MediaControllerPlaybackStateEnum.getValue(action_str, &state_e); if (!result) { - LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str()); + LoggerE("MediaControllerPlaybackStateEnum.getValue() failed, error: %s", result.message().c_str()); return; } @@ -436,7 +432,7 @@ void MediaControllerServer::OnPlaybackActionCommand(const char* client_name, con picojson::object& data_o = data.get(); data_o["action"] = picojson::value(std::string("onplaybackstaterequest")); - data_o["state"] = picojson::value(state); + data_o["state"] = picojson::value(action_str); server->change_request_playback_info_listener_(&data); } diff --git a/src/mediacontroller/mediacontroller_types.cc b/src/mediacontroller/mediacontroller_types.cc index 6c92df8a..78a32ed8 100644 --- a/src/mediacontroller/mediacontroller_types.cc +++ b/src/mediacontroller/mediacontroller_types.cc @@ -28,113 +28,69 @@ namespace mediacontroller { using common::PlatformResult; using common::ErrorCode; -const std::string Types::kMediaControllerServerState = "MediaControllerServerState"; -const std::string Types::kMediaControllerPlaybackState = "MediaControllerPlaybackState"; -const std::string Types::kMediaControllerPlaybackAction = "MediaControllerPlaybackAction"; -const std::string Types::kMediaControllerMetadataAttribute = "MediaControllerMetadataAttribute"; - -const PlatformEnumMap Types::platform_enum_map_ = {{kMediaControllerServerState, - {{"NONE", MC_SERVER_STATE_NONE}, - {"ACTIVE", MC_SERVER_STATE_ACTIVATE}, - {"INACTIVE", MC_SERVER_STATE_DEACTIVATE}}}, - {kMediaControllerPlaybackState, - {{"PLAY", MC_PLAYBACK_STATE_PLAYING}, - {"PAUSE", MC_PLAYBACK_STATE_PAUSED}, - {"STOP", MC_PLAYBACK_STATE_STOPPED}, - {"NEXT", MC_PLAYBACK_STATE_MOVING_TO_NEXT}, - {"PREV", MC_PLAYBACK_STATE_MOVING_TO_PREVIOUS}, - {"FORWARD", MC_PLAYBACK_STATE_FAST_FORWARDING}, - {"REWIND", MC_PLAYBACK_STATE_REWINDING}}}, - {kMediaControllerPlaybackAction, - {{"PLAY", MC_PLAYBACK_ACTION_PLAY}, - {"PAUSE", MC_PLAYBACK_ACTION_PAUSE}, - {"STOP", MC_PLAYBACK_ACTION_STOP}, - {"NEXT", MC_PLAYBACK_ACTION_NEXT}, - {"PREV", MC_PLAYBACK_ACTION_PREV}, - {"FORWARD", MC_PLAYBACK_ACTION_FAST_FORWARD}, - {"REWIND", MC_PLAYBACK_ACTION_REWIND}}}, - {kMediaControllerMetadataAttribute, - {{"title", MC_META_MEDIA_TITLE}, - {"artist", MC_META_MEDIA_ARTIST}, - {"album", MC_META_MEDIA_ALBUM}, - {"author", MC_META_MEDIA_AUTHOR}, - {"genre", MC_META_MEDIA_GENRE}, - {"duration", MC_META_MEDIA_DURATION}, - {"date", MC_META_MEDIA_DATE}, - {"copyright", MC_META_MEDIA_COPYRIGHT}, - {"description", MC_META_MEDIA_DESCRIPTION}, - {"trackNum", MC_META_MEDIA_TRACK_NUM}, - {"picture", MC_META_MEDIA_PICTURE}}}}; - -PlatformEnumReverseMap Types::platform_enum_reverse_map_ = {}; - -PlatformResult Types::GetPlatformEnumMap(const std::string& type, - std::map const** enum_map) { - ScopeLogger(); - - auto iter = platform_enum_map_.find(type); - if (iter == platform_enum_map_.end()) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - std::string("Undefined platform enum type ") + type); - } - - *enum_map = &iter->second; - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult Types::StringToPlatformEnum(const std::string& type, const std::string& value, - int* platform_enum) { - ScopeLogger(); - - std::map const* def = nullptr; - PlatformResult result = GetPlatformEnumMap(type, &def); - if (!result) { - return result; - } - - auto def_iter = def->find(value); - if (def_iter != def->end()) { - *platform_enum = def_iter->second; - return PlatformResult(ErrorCode::NO_ERROR); - } - - std::string message = "Platform enum value " + value + " not found for " + type; - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, message); -} - -PlatformResult Types::PlatformEnumToString(const std::string& type, int value, - std::string* platform_str) { - ScopeLogger(); - - if (platform_enum_reverse_map_.empty()) { - for (auto& def : platform_enum_map_) { - platform_enum_reverse_map_[def.first] = {}; - - for (auto& key : def.second) { - platform_enum_reverse_map_[def.first][key.second] = key.first; - } - } - } - - auto it = platform_enum_reverse_map_.find(type); - if (it == platform_enum_reverse_map_.end()) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - std::string("Undefined platform enum type ") + type); - } - - auto def = platform_enum_reverse_map_.at(type); - auto def_it = def.find(value); - if (def_it != def.end()) { - *platform_str = def_it->second; - return PlatformResult(ErrorCode::NO_ERROR); - } - - std::string message = "Platform enum value " + std::to_string(value) + " not found for " + type; - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, message); -} - -PlatformResult Types::ConvertPlaybackState(mc_playback_h playback_h, std::string* state) { +namespace types { + +const common::PlatformEnum MediaControllerServerStateEnum { + {"NONE", MC_SERVER_STATE_NONE}, + {"ACTIVE", MC_SERVER_STATE_ACTIVATE}, + {"INACTIVE", MC_SERVER_STATE_DEACTIVATE} +}; + +const common::PlatformEnum MediaControllerPlaybackStateEnum { + {"PLAY", MC_PLAYBACK_STATE_PLAYING}, + {"PAUSE", MC_PLAYBACK_STATE_PAUSED}, + {"STOP", MC_PLAYBACK_STATE_STOPPED}, + {"NEXT", MC_PLAYBACK_STATE_MOVING_TO_NEXT}, + {"PREV", MC_PLAYBACK_STATE_MOVING_TO_PREVIOUS}, + {"FORWARD", MC_PLAYBACK_STATE_FAST_FORWARDING}, + {"REWIND", MC_PLAYBACK_STATE_REWINDING} +}; + +const common::PlatformEnum MediaControllerPlaybackActionEnum { + {"PLAY", MC_PLAYBACK_ACTION_PLAY}, + {"PAUSE", MC_PLAYBACK_ACTION_PAUSE}, + {"STOP", MC_PLAYBACK_ACTION_STOP}, + {"NEXT", MC_PLAYBACK_ACTION_NEXT}, + {"PREV", MC_PLAYBACK_ACTION_PREV}, + {"FORWARD", MC_PLAYBACK_ACTION_FAST_FORWARD}, + {"REWIND", MC_PLAYBACK_ACTION_REWIND} +}; + +const common::PlatformEnum MediaControllerMetadataAttributeEnum { + {"title", MC_META_MEDIA_TITLE}, + {"artist", MC_META_MEDIA_ARTIST}, + {"album", MC_META_MEDIA_ALBUM}, + {"author", MC_META_MEDIA_AUTHOR}, + {"genre", MC_META_MEDIA_GENRE}, + {"duration", MC_META_MEDIA_DURATION}, + {"date", MC_META_MEDIA_DATE}, + {"copyright", MC_META_MEDIA_COPYRIGHT}, + {"description", MC_META_MEDIA_DESCRIPTION}, + {"trackNum", MC_META_MEDIA_TRACK_NUM}, + {"picture", MC_META_MEDIA_PICTURE} +}; + +const common::PlatformEnum MediaControllerRepeatModeEnum { + {"REPEAT_OFF", MC_REPEAT_MODE_OFF}, + {"REPEAT_ONE", MC_REPEAT_MODE_ONE_MEDIA}, + {"REPEAT_ALL", MC_REPEAT_MODE_ON} +}; + +const common::PlatformEnum MediaControllerContentAgeRatingEnum { + {"ALL", MC_CONTENT_RATING_ALL}, {"1", MC_CONTENT_RATING_1_PLUS}, + {"2", MC_CONTENT_RATING_2_PLUS}, {"3", MC_CONTENT_RATING_3_PLUS}, + {"4", MC_CONTENT_RATING_4_PLUS}, {"5", MC_CONTENT_RATING_5_PLUS}, + {"6", MC_CONTENT_RATING_6_PLUS}, {"7", MC_CONTENT_RATING_7_PLUS}, + {"8", MC_CONTENT_RATING_8_PLUS}, {"9", MC_CONTENT_RATING_9_PLUS}, + {"10", MC_CONTENT_RATING_10_PLUS}, {"11", MC_CONTENT_RATING_11_PLUS}, + {"12", MC_CONTENT_RATING_12_PLUS}, {"13", MC_CONTENT_RATING_13_PLUS}, + {"14", MC_CONTENT_RATING_14_PLUS}, {"15", MC_CONTENT_RATING_15_PLUS}, + {"16", MC_CONTENT_RATING_16_PLUS}, {"17", MC_CONTENT_RATING_17_PLUS}, + {"18", MC_CONTENT_RATING_18_PLUS}, {"19", MC_CONTENT_RATING_19_PLUS} +}; + + +PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state) { ScopeLogger(); int ret; @@ -149,17 +105,16 @@ PlatformResult Types::ConvertPlaybackState(mc_playback_h playback_h, std::string state_e = MC_PLAYBACK_STATE_STOPPED; } - PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerPlaybackState, - static_cast(state_e), state); + PlatformResult result = MediaControllerPlaybackStateEnum.getName(state_e, state); if (!result) { - LoggerE("PlatformEnumToString failed, error: %s", result.message().c_str()); + LoggerE("MediaControllerPlaybackStateEnum.getName() failed, error: %s", result.message().c_str()); return result; } return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult Types::ConvertPlaybackPosition(mc_playback_h playback_h, double* position) { +PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position) { ScopeLogger(); int ret; @@ -177,35 +132,30 @@ PlatformResult Types::ConvertPlaybackPosition(mc_playback_h playback_h, double* return PlatformResult(ErrorCode::NO_ERROR); } -PlatformResult Types::ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata) { +PlatformResult ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata) { ScopeLogger(); - std::map const* metadata_fields = nullptr; - PlatformResult result = - GetPlatformEnumMap(Types::kMediaControllerMetadataAttribute, &metadata_fields); - if (!result) { - LoggerE("GetPlatformEnumMap failed, error: %s", result.message().c_str()); - return result; - } - char* value = nullptr; SCOPE_EXIT { free(value); }; - for (auto& field : *metadata_fields) { - int ret = mc_metadata_get(metadata_h, static_cast(field.second), &value); + for (auto entry: MediaControllerMetadataAttributeEnum) { + int ret = mc_metadata_get(metadata_h, entry.second, &value); if (ret != MEDIA_CONTROLLER_ERROR_NONE) { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error getting metadata", - ("mc_metadata_get(%s) error: %d, message: %s", field.first.c_str(), + ("mc_metadata_get(%s) error: %d, message: %s", entry.first.c_str(), ret, get_error_message(ret))); } - (*metadata)[field.first] = picojson::value(std::string(value ? value : "")); + (*metadata)[entry.first] = picojson::value(std::string(value ? value : "")); } return PlatformResult(ErrorCode::NO_ERROR); } +} // types + + } // namespace mediacontroller } // namespace extension diff --git a/src/mediacontroller/mediacontroller_types.h b/src/mediacontroller/mediacontroller_types.h index 8aaa0f8d..b012cdc8 100644 --- a/src/mediacontroller/mediacontroller_types.h +++ b/src/mediacontroller/mediacontroller_types.h @@ -23,39 +23,31 @@ #include #include "common/platform_result.h" +#include "common/platform_enum.h" + namespace extension { namespace mediacontroller { -typedef std::map> PlatformEnumMap; -typedef std::map> PlatformEnumReverseMap; typedef std::function JsonCallback; -class Types { - public: - static const std::string kMediaControllerServerState; - static const std::string kMediaControllerPlaybackState; - static const std::string kMediaControllerPlaybackAction; - static const std::string kMediaControllerMetadataAttribute; - static common::PlatformResult GetPlatformEnumMap( - const std::string& type, std::map const** platform_str); +namespace types { - static common::PlatformResult StringToPlatformEnum(const std::string& type, - const std::string& value, int* platform_enum); +common::PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state); +common::PlatformResult ConvertContentAgeRating(mc_playback_h playback_h, std::string* state); +common::PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position); +common::PlatformResult ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata); - static common::PlatformResult PlatformEnumToString(const std::string& type, int value, - std::string* platform_str); +extern const common::PlatformEnum MediaControllerServerStateEnum; +extern const common::PlatformEnum MediaControllerPlaybackStateEnum; +extern const common::PlatformEnum MediaControllerPlaybackActionEnum; +extern const common::PlatformEnum MediaControllerMetadataAttributeEnum; +extern const common::PlatformEnum MediaControllerRepeatModeEnum; +extern const common::PlatformEnum MediaControllerContentAgeRatingEnum; - static common::PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state); - static common::PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position); - static common::PlatformResult ConvertMetadata(mc_metadata_h metadata_h, - picojson::object* metadata); +} // namespace types - private: - static const PlatformEnumMap platform_enum_map_; - static PlatformEnumReverseMap platform_enum_reverse_map_; -}; } // namespace mediacontroller } // namespace extension -- 2.34.1