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 "mediacontroller/mediacontroller_utils.h"
19 #include <media_controller_client.h>
21 #include "common/logger.h"
22 #include "common/platform_result.h"
23 #include "common/scope_exit.h"
26 namespace mediacontroller {
28 using common::PlatformResult;
29 using common::ErrorCode;
33 const common::PlatformEnum<mc_server_state_e> MediaControllerServerStateEnum{
34 {"NONE", MC_SERVER_STATE_NONE},
35 {"ACTIVE", MC_SERVER_STATE_ACTIVATE},
36 {"INACTIVE", MC_SERVER_STATE_DEACTIVATE}};
38 const common::PlatformEnum<mc_playback_states_e> MediaControllerPlaybackStateEnum{
39 {"PLAY", MC_PLAYBACK_STATE_PLAYING},
40 {"PAUSE", MC_PLAYBACK_STATE_PAUSED},
41 {"STOP", MC_PLAYBACK_STATE_STOPPED},
42 {"NEXT", MC_PLAYBACK_STATE_MOVING_TO_NEXT},
43 {"PREV", MC_PLAYBACK_STATE_MOVING_TO_PREVIOUS},
44 {"FORWARD", MC_PLAYBACK_STATE_FAST_FORWARDING},
45 {"REWIND", MC_PLAYBACK_STATE_REWINDING}};
47 const common::PlatformEnum<mc_playback_action_e> MediaControllerPlaybackActionEnum{
48 {"PLAY", MC_PLAYBACK_ACTION_PLAY}, {"PAUSE", MC_PLAYBACK_ACTION_PAUSE},
49 {"STOP", MC_PLAYBACK_ACTION_STOP}, {"NEXT", MC_PLAYBACK_ACTION_NEXT},
50 {"PREV", MC_PLAYBACK_ACTION_PREV}, {"FORWARD", MC_PLAYBACK_ACTION_FAST_FORWARD},
51 {"REWIND", MC_PLAYBACK_ACTION_REWIND}};
53 const common::PlatformEnum<mc_meta_e> MediaControllerMetadataAttributeEnum{
54 {"title", MC_META_MEDIA_TITLE},
55 {"artist", MC_META_MEDIA_ARTIST},
56 {"album", MC_META_MEDIA_ALBUM},
57 {"author", MC_META_MEDIA_AUTHOR},
58 {"genre", MC_META_MEDIA_GENRE},
59 {"duration", MC_META_MEDIA_DURATION},
60 {"date", MC_META_MEDIA_DATE},
61 {"copyright", MC_META_MEDIA_COPYRIGHT},
62 {"description", MC_META_MEDIA_DESCRIPTION},
63 {"trackNum", MC_META_MEDIA_TRACK_NUM},
64 {"picture", MC_META_MEDIA_PICTURE}};
66 const common::PlatformEnum<mc_repeat_mode_e> MediaControllerRepeatModeEnum{
67 {"REPEAT_OFF", MC_REPEAT_MODE_OFF},
68 {"REPEAT_ONE", MC_REPEAT_MODE_ONE_MEDIA},
69 {"REPEAT_ALL", MC_REPEAT_MODE_ON}};
71 const common::PlatformEnum<mc_content_age_rating_e> MediaControllerContentAgeRatingEnum{
72 {"ALL", MC_CONTENT_RATING_ALL}, {"1", MC_CONTENT_RATING_1_PLUS},
73 {"2", MC_CONTENT_RATING_2_PLUS}, {"3", MC_CONTENT_RATING_3_PLUS},
74 {"4", MC_CONTENT_RATING_4_PLUS}, {"5", MC_CONTENT_RATING_5_PLUS},
75 {"6", MC_CONTENT_RATING_6_PLUS}, {"7", MC_CONTENT_RATING_7_PLUS},
76 {"8", MC_CONTENT_RATING_8_PLUS}, {"9", MC_CONTENT_RATING_9_PLUS},
77 {"10", MC_CONTENT_RATING_10_PLUS}, {"11", MC_CONTENT_RATING_11_PLUS},
78 {"12", MC_CONTENT_RATING_12_PLUS}, {"13", MC_CONTENT_RATING_13_PLUS},
79 {"14", MC_CONTENT_RATING_14_PLUS}, {"15", MC_CONTENT_RATING_15_PLUS},
80 {"16", MC_CONTENT_RATING_16_PLUS}, {"17", MC_CONTENT_RATING_17_PLUS},
81 {"18", MC_CONTENT_RATING_18_PLUS}, {"19", MC_CONTENT_RATING_19_PLUS}};
83 const common::PlatformEnum<mc_content_type_e> MediaControllerContentTypeEnum{
84 {"IMAGE", MC_CONTENT_TYPE_IMAGE},
85 {"MUSIC", MC_CONTENT_TYPE_MUSIC},
86 {"VIDEO", MC_CONTENT_TYPE_VIDEO},
87 {"OTHER", MC_CONTENT_TYPE_OTHER},
88 {"UNDECIDED", MC_CONTENT_TYPE_UNDECIDED}};
90 const common::PlatformEnum<mc_search_category_e> MediaControllerSearchCategoryEnum{
91 {"NO_CATEGORY", MC_SEARCH_NO_CATEGORY},
92 {"TITLE", MC_SEARCH_TITLE},
93 {"ARTIST", MC_SEARCH_ARTIST},
94 {"ALBUM", MC_SEARCH_ALBUM},
95 {"GENRE", MC_SEARCH_GENRE},
96 {"TPO", MC_SEARCH_TPO}};
98 PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state) {
102 mc_playback_states_e state_e;
103 ret = mc_client_get_playback_state(playback_h, &state_e);
104 if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
105 return LogAndCreateResult(
106 ErrorCode::UNKNOWN_ERR, "Error getting playback state",
107 ("mc_client_get_playback_state() error: %d, message: %s", ret, get_error_message(ret)));
109 if (state_e == MC_PLAYBACK_STATE_NONE) {
110 state_e = MC_PLAYBACK_STATE_STOPPED;
113 PlatformResult result = MediaControllerPlaybackStateEnum.getName(state_e, state);
115 LoggerE("MediaControllerPlaybackStateEnum.getName() failed, error: %s",
116 result.message().c_str());
120 return PlatformResult(ErrorCode::NO_ERROR);
123 PlatformResult ConvertContentAgeRating(mc_playback_h playback_h, std::string* rating) {
126 mc_content_age_rating_e rating_e = MC_CONTENT_RATING_ALL;
127 int ret = mc_client_get_age_rating(playback_h, &rating_e);
128 if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
129 return LogAndCreateResult(
130 ErrorCode::UNKNOWN_ERR, "Error getting content age rating",
131 ("mc_client_get_age_rating() error: %d, message: %s", ret, get_error_message(ret)));
134 PlatformResult result = MediaControllerContentAgeRatingEnum.getName(rating_e, rating);
136 LoggerE("MediaControllerContentAgeRatingEnum.getName() failed, error: %s",
137 result.message().c_str());
141 return PlatformResult(ErrorCode::NO_ERROR);
144 PlatformResult ConvertContentType(mc_playback_h playback_h, std::string* contentType) {
146 mc_content_type_e content_type_e = MC_CONTENT_TYPE_UNDECIDED;
147 int ret = mc_client_get_playback_content_type(playback_h, &content_type_e);
148 if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
149 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error getting content type",
150 ("mc_client_get_playback_content_type() error: %d, message: %s", ret,
151 get_error_message(ret)));
154 PlatformResult result = MediaControllerContentTypeEnum.getName(content_type_e, contentType);
156 LoggerE("MediaControllerContentTypeEnum.getName() failed, error: %s", result.message().c_str());
160 return PlatformResult(ErrorCode::NO_ERROR);
163 PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position) {
168 unsigned long long pos;
169 ret = mc_client_get_playback_position(playback_h, &pos);
170 if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
171 return LogAndCreateResult(
172 ErrorCode::UNKNOWN_ERR, "Error getting playback position",
173 ("mc_client_get_playback_position() error: %d, message: %s", ret, get_error_message(ret)));
176 *position = static_cast<double>(pos);
178 return PlatformResult(ErrorCode::NO_ERROR);
181 PlatformResult ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata) {
184 char* value = nullptr;
189 for (auto entry : MediaControllerMetadataAttributeEnum) {
190 int ret = mc_metadata_get(metadata_h, entry.second, &value);
191 if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
192 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error getting metadata",
193 ("mc_metadata_get(%s) error: %d, message: %s", entry.first.c_str(),
194 ret, get_error_message(ret)));
197 (*metadata)[entry.first] = picojson::value(std::string(value ? value : ""));
200 return PlatformResult(ErrorCode::NO_ERROR);
205 PlatformResult utils::GetAllPlaylists(const std::string& app_id, picojson::array* playlists) {
208 auto OnPlaylists = [](mc_playlist_h playlist, void* user_data) -> bool {
210 char* name = nullptr;
216 int ret = mc_playlist_get_name(playlist, &name);
218 if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
222 auto playlists = static_cast<picojson::array*>(user_data);
224 picojson::value value = picojson::value(picojson::object());
225 picojson::object& obj = value.get<picojson::object>();
227 obj.insert(std::make_pair("name", picojson::value{name}));
228 playlists->push_back(value);
233 int ret = mc_playlist_foreach_playlist(app_id.c_str(), OnPlaylists, playlists);
235 if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
236 return LogAndCreateResult(
237 ErrorCode::UNKNOWN_ERR, "Error while get playlists",
238 ("mc_playlist_foreach_playlist() error: %d, message: %s", ret, get_error_message(ret)));
241 return PlatformResult(ErrorCode::NO_ERROR);
244 } // namespace mediacontroller
245 } // namespace extension