#include "common/logger.h"
#include "common/scope_exit.h"
+#include "common/tools.h"
#include "mediacontroller/mediacontroller_types.h"
}
std::string state_str;
- PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerServerState,
- static_cast<int>(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;
}
// 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;
// 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;
mc_metadata_destroy(metadata_h);
};
- PlatformResult result = Types::ConvertMetadata(metadata_h, metadata);
+ PlatformResult result = types::ConvertMetadata(metadata_h, metadata);
if (!result) {
return result;
}
// server state
std::string state_str;
- PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerServerState,
- static_cast<int>(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;
}
// 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;
// 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;
picojson::object& data_o = data.get<picojson::object>();
picojson::value metadata = picojson::value(picojson::object());
- PlatformResult result = Types::ConvertMetadata(metadata_h, &metadata.get<picojson::object>());
+ PlatformResult result = types::ConvertMetadata(metadata_h, &metadata.get<picojson::object>());
if (!result) {
LoggerE("ConvertMetadata failed, error: %s", result.message().c_str());
return;
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;
}
SCOPE_EXIT {
free(request_id);
};*/
- int ret;
- ret = mc_client_send_playback_action_cmd(handle_, server_name.c_str(),
- static_cast<mc_playback_action_e>(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,
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<mc_playback_states_e>(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<mc_playback_states_e>(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<mc_playback_states_e>(state_int);
+ playback_state_ = state_e;
ret = mc_server_update_playback_info(handle_);
if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
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<mc_meta_e>(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",
}
}
- 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",
MediaControllerServer* server = static_cast<MediaControllerServer*>(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<int>(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;
}
picojson::object& data_o = data.get<picojson::object>();
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);
}
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<std::string, int> 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<std::string, int> 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<mc_server_state_e> MediaControllerServerStateEnum {
+ {"NONE", MC_SERVER_STATE_NONE},
+ {"ACTIVE", MC_SERVER_STATE_ACTIVATE},
+ {"INACTIVE", MC_SERVER_STATE_DEACTIVATE}
+};
+
+const common::PlatformEnum<mc_playback_states_e> 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<mc_playback_action_e> 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<mc_meta_e> 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<mc_repeat_mode_e> MediaControllerRepeatModeEnum {
+ {"REPEAT_OFF", MC_REPEAT_MODE_OFF},
+ {"REPEAT_ONE", MC_REPEAT_MODE_ONE_MEDIA},
+ {"REPEAT_ALL", MC_REPEAT_MODE_ON}
+};
+
+const common::PlatformEnum<mc_content_age_rating_e> 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;
state_e = MC_PLAYBACK_STATE_STOPPED;
}
- PlatformResult result = Types::PlatformEnumToString(Types::kMediaControllerPlaybackState,
- static_cast<int>(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;
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<std::string, int> 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<mc_meta_e>(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