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 "content/content_manager.h"
19 #include <metadata_extractor.h>
28 #include "common/converter.h"
29 #include "common/filesystem/filesystem_provider.h"
30 #include "common/logger.h"
31 #include "common/scope_exit.h"
32 #include "common/tools.h"
34 #include "content/content_filter.h"
37 using namespace common;
39 using common::tools::ReportSuccess;
40 using common::tools::ReportError;
46 static const std::string uri_prefix = "file://";
47 static const std::string uri_absolute_prefix = "file:///";
50 const std::map<std::string, media_content_orientation_e> orientationMap = {
51 {"NORMAL", MEDIA_CONTENT_ORIENTATION_NORMAL},
52 {"FLIP_HORIZONTAL", MEDIA_CONTENT_ORIENTATION_HFLIP},
53 {"ROTATE_180", MEDIA_CONTENT_ORIENTATION_ROT_180},
54 {"FLIP_VERTICAL", MEDIA_CONTENT_ORIENTATION_VFLIP},
55 {"TRANSPOSE", MEDIA_CONTENT_ORIENTATION_TRANSPOSE},
56 {"ROTATE_90", MEDIA_CONTENT_ORIENTATION_ROT_90},
57 {"TRANSVERSE", MEDIA_CONTENT_ORIENTATION_TRANSVERSE},
58 {"ROTATE_270", MEDIA_CONTENT_ORIENTATION_ROT_270},
61 std::string get_date(char* tmpStr) {
64 struct tm* result = (struct tm*)calloc(1, sizeof(struct tm));
65 if (nullptr != result) {
66 if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
70 time_t t = mktime(result); // + get_utc_offset() * 3600;
71 std::stringstream str_date;
74 return str_date.str();
81 void ContentToJson(media_info_h info, picojson::object& o) {
89 long long unsigned int tmpLong;
90 media_content_type_e type;
92 ret = media_info_get_media_type(info, &type);
94 if (ret != MEDIA_CONTENT_ERROR_NONE) {
95 LoggerE("Get media type failed: %d", ret);
96 type = MEDIA_CONTENT_TYPE_OTHERS;
99 if (type == MEDIA_CONTENT_TYPE_IMAGE) {
100 o["type"] = picojson::value(std::string("IMAGE"));
102 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_image(info, &img)) {
103 std::unique_ptr<std::remove_pointer<image_meta_h>::type, int (*)(image_meta_h)> img_ptr(
104 img, &image_meta_destroy); // automatically release the memory
105 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_date_taken(img, &tmpStr)) {
107 o["releaseDate"] = picojson::value(get_date(tmpStr));
112 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_width(img, &tmpInt)) {
113 o["width"] = picojson::value(static_cast<double>(tmpInt));
115 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_height(img, &tmpInt)) {
116 o["height"] = picojson::value(static_cast<double>(tmpInt));
118 picojson::object geo;
119 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble)) {
120 geo["latitude"] = picojson::value(tmpDouble);
122 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble)) {
123 geo["longitude"] = picojson::value(tmpDouble);
125 o["geolocation"] = picojson::value(geo);
127 media_content_orientation_e orientation;
128 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_orientation(img, &orientation)) {
129 switch (orientation) {
130 case MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE:
131 case MEDIA_CONTENT_ORIENTATION_NORMAL:
134 case MEDIA_CONTENT_ORIENTATION_HFLIP:
135 ori = "FLIP_HORIZONTAL";
137 case MEDIA_CONTENT_ORIENTATION_ROT_180:
140 case MEDIA_CONTENT_ORIENTATION_VFLIP:
141 ori = "FLIP_VERTICAL";
143 case MEDIA_CONTENT_ORIENTATION_TRANSPOSE:
146 case MEDIA_CONTENT_ORIENTATION_ROT_90:
149 case MEDIA_CONTENT_ORIENTATION_TRANSVERSE:
152 case MEDIA_CONTENT_ORIENTATION_ROT_270:
156 o["orientation"] = picojson::value(ori);
159 } else if (type == MEDIA_CONTENT_TYPE_VIDEO) {
160 o["type"] = picojson::value(std::string("VIDEO"));
162 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_video(info, &video)) {
163 std::unique_ptr<std::remove_pointer<video_meta_h>::type, int (*)(video_meta_h)> video_ptr(
164 video, &video_meta_destroy); // automatically release the memory
165 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_width(video, &tmpInt)) {
166 o["width"] = picojson::value(static_cast<double>(tmpInt));
169 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_height(video, &tmpInt)) {
170 o["height"] = picojson::value(static_cast<double>(tmpInt));
172 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_artist(video, &tmpStr)) {
173 picojson::array artists;
175 artists.push_back(picojson::value(std::string(tmpStr)));
179 o["artists"] = picojson::value(artists);
181 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_album(video, &tmpStr)) {
183 o["album"] = picojson::value(tmpStr);
188 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_duration(video, &tmpInt)) {
189 o["duration"] = picojson::value(static_cast<double>(tmpInt));
191 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_recorded_date(video, &tmpStr)) {
193 o["releaseDate"] = picojson::value(get_date(tmpStr));
199 picojson::object geo;
200 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble)) {
201 geo["latitude"] = picojson::value(tmpDouble);
203 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble)) {
204 geo["longitude"] = picojson::value(tmpDouble);
206 o["geolocation"] = picojson::value(geo);
207 } else if (type == MEDIA_CONTENT_TYPE_SOUND || type == MEDIA_CONTENT_TYPE_MUSIC) {
208 o["type"] = picojson::value(std::string("AUDIO"));
210 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(info, &audio)) {
211 std::unique_ptr<std::remove_pointer<audio_meta_h>::type, int (*)(audio_meta_h)> audio_ptr(
212 audio, &audio_meta_destroy); // automatically release the memory
213 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_recorded_date(audio, &tmpStr)) {
215 o["releaseDate"] = picojson::value(get_date(tmpStr));
220 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_album(audio, &tmpStr)) {
222 o["album"] = picojson::value(std::string(tmpStr));
227 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_artist(audio, &tmpStr)) {
229 picojson::array artists;
230 artists.push_back(picojson::value(std::string(tmpStr)));
231 o["artists"] = picojson::value(artists);
236 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_genre(audio, &tmpStr)) {
238 picojson::array genres;
239 genres.push_back(picojson::value(std::string(tmpStr)));
240 o["genres"] = picojson::value(genres);
245 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_composer(audio, &tmpStr)) {
247 picojson::array composers;
248 composers.push_back(picojson::value(std::string(tmpStr)));
249 o["composers"] = picojson::value(composers);
254 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_copyright(audio, &tmpStr)) {
256 o["copyright"] = picojson::value(std::string(tmpStr));
261 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_bit_rate(audio, &tmpInt)) {
262 o["bitrate"] = picojson::value(static_cast<double>(tmpInt));
264 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_track_num(audio, &tmpStr)) {
266 o["trackNumber"] = picojson::value(static_cast<double>(std::atoi(tmpStr)));
270 o["trackNumber"] = picojson::value();
273 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_duration(audio, &tmpInt)) {
274 o["duration"] = picojson::value(static_cast<double>(tmpInt));
278 o["type"] = picojson::value(std::string("OTHER"));
281 ret = media_info_get_media_id(info, &tmpStr);
282 if (ret == MEDIA_CONTENT_ERROR_NONE) {
284 o["id"] = picojson::value(std::string(tmpStr));
289 ret = media_info_get_display_name(info, &tmpStr);
290 if (ret == MEDIA_CONTENT_ERROR_NONE) {
292 o["name"] = picojson::value(std::string(tmpStr));
298 ret = media_info_get_mime_type(info, &tmpStr);
299 if (ret == MEDIA_CONTENT_ERROR_NONE) {
301 o["mimeType"] = picojson::value(std::string(tmpStr));
306 ret = media_info_get_title(info, &tmpStr);
307 if (ret == MEDIA_CONTENT_ERROR_NONE) {
309 o["title"] = picojson::value(std::string(tmpStr));
314 ret = media_info_get_file_path(info, &tmpStr);
315 if (ret == MEDIA_CONTENT_ERROR_NONE) {
317 o["contentURI"] = picojson::value(std::string(tmpStr));
322 ret = media_info_get_thumbnail_path(info, &tmpStr);
323 if (ret == MEDIA_CONTENT_ERROR_NONE) {
325 picojson::array thumbnails;
326 thumbnails.push_back(picojson::value(std::string(tmpStr)));
327 o["thumbnailURIs"] = picojson::value(thumbnails);
332 ret = media_info_get_description(info, &tmpStr);
333 if (ret == MEDIA_CONTENT_ERROR_NONE) {
335 o["description"] = picojson::value(std::string(tmpStr));
340 ret = media_info_get_rating(info, &tmpInt);
341 if (ret == MEDIA_CONTENT_ERROR_NONE) {
342 o["rating"] = picojson::value(static_cast<double>(tmpInt));
344 ret = media_info_get_size(info, &tmpLong);
345 if (ret == MEDIA_CONTENT_ERROR_NONE) {
346 o["size"] = picojson::value(static_cast<double>(tmpLong));
348 ret = media_info_get_favorite(info, &tmpBool);
349 if (ret == MEDIA_CONTENT_ERROR_NONE) {
350 o["isFavorite"] = picojson::value(tmpBool);
352 ret = media_info_get_modified_time(info, &tmpDate);
353 if (ret == MEDIA_CONTENT_ERROR_NONE) {
354 o["modifiedDate"] = picojson::value(static_cast<double>(tmpDate));
358 void ContentDirToJson(media_folder_h folder, picojson::object& o) {
362 media_content_storage_e storage_type;
366 ret = media_folder_get_folder_id(folder, &tmpStr);
367 if (ret == MEDIA_CONTENT_ERROR_NONE) {
369 o["id"] = picojson::value(std::string(tmpStr));
376 ret = media_folder_get_path(folder, &tmpStr);
377 if (ret == MEDIA_CONTENT_ERROR_NONE) {
379 o["directoryURI"] = picojson::value(std::string(tmpStr));
386 ret = media_folder_get_name(folder, &tmpStr);
387 if (ret == MEDIA_CONTENT_ERROR_NONE) {
389 o["title"] = picojson::value(std::string(tmpStr));
396 ret = media_folder_get_storage_type(folder, &storage_type);
397 if (ret == MEDIA_CONTENT_ERROR_NONE) {
398 if (storage_type == MEDIA_CONTENT_STORAGE_INTERNAL) {
399 o["storageType"] = picojson::value(std::string("INTERNAL"));
400 } else if (storage_type == MEDIA_CONTENT_STORAGE_EXTERNAL) {
401 o["storageType"] = picojson::value(std::string("EXTERNAL"));
402 } else if (storage_type == MEDIA_CONTENT_STORAGE_CLOUD) {
403 o["storageType"] = picojson::value(std::string("CLOUD"));
408 ret = media_folder_get_modified_time(folder, &tmpDate);
409 if (ret == MEDIA_CONTENT_ERROR_NONE) {
410 o["modifiedDate"] = picojson::value(static_cast<double>(tmpDate));
414 static int setContent(media_info_h media, const picojson::value& content) {
418 std::string name = content.get("name").to_str();
419 std::string description = content.get("description").to_str();
420 int rating = std::stoi(content.get("rating").to_str());
421 bool is_fav = content.get("isFavorite").get<bool>();
424 LoggerE("MEDIA_CONTENT_ERROR_DB_FAILED");
425 return MEDIA_CONTENT_ERROR_DB_FAILED;
428 media_content_type_e type;
429 ret = media_info_get_media_type(media, &type);
430 if (ret != MEDIA_CONTENT_ERROR_NONE) {
431 LoggerE("Failed: media_info_get_media_type()");
435 ret = media_info_set_display_name(media, name.c_str());
436 if (ret != MEDIA_CONTENT_ERROR_NONE) {
437 LoggerE("Updating name failed.");
440 ret = media_info_set_description(media, description.c_str());
441 if (ret != MEDIA_CONTENT_ERROR_NONE) {
442 LoggerE("Updating description failed.");
445 ret = media_info_set_rating(media, rating);
446 if (ret != MEDIA_CONTENT_ERROR_NONE) {
447 LoggerE("Updating rating failed.");
450 ret = media_info_set_favorite(media, is_fav);
451 if (ret != MEDIA_CONTENT_ERROR_NONE) {
452 LoggerE("Updating isFavorite failed.");
455 if (ret != MEDIA_CONTENT_ERROR_NONE) {
456 LoggerE("Updating favorite failed.");
459 if (type == MEDIA_CONTENT_TYPE_IMAGE) {
460 std::string orientation = content.get("orientation").to_str();
461 auto orientationToSet = orientationMap.find(orientation);
463 if (orientationToSet != orientationMap.end()) {
465 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_image(media, &img) &&
466 MEDIA_CONTENT_ERROR_NONE == image_meta_set_orientation(img, orientationToSet->second) &&
467 MEDIA_CONTENT_ERROR_NONE == image_meta_update_to_db(img)) {
468 LoggerD("orientation update was successful");
470 LoggerE("orientation update failed");
472 image_meta_destroy(img);
476 if (type == MEDIA_CONTENT_TYPE_IMAGE || type == MEDIA_CONTENT_TYPE_VIDEO) {
477 picojson::value geo = content.get("geolocation");
478 if (geo.evaluate_as_boolean()) {
479 LoggerD("geolocation is not null");
480 double latitude = atof(geo.get("latitude").to_str().c_str());
481 double longitude = atof(geo.get("longitude").to_str().c_str());
482 ret = media_info_set_latitude(media, latitude);
483 if (ret != MEDIA_CONTENT_ERROR_NONE) {
484 LoggerE("Updating geolocation is failed.");
486 ret = media_info_set_longitude(media, longitude);
487 if (ret != MEDIA_CONTENT_ERROR_NONE) {
488 LoggerD("Updating geolocation is failed.");
491 LoggerD("geolocation is null");
495 return MEDIA_CONTENT_ERROR_NONE;
498 static void FolderToJson(media_folder_h folder, picojson::object* out) {
505 media_content_storage_e storageType;
509 ret = media_folder_get_folder_id(folder, &id);
510 if (ret != MEDIA_CONTENT_ERROR_NONE) {
511 LogAndReportError(ContentManager::convertError(ret), out,
512 ("Failed: media_folder_get_folder_id"));
516 ret = media_folder_get_name(folder, &name);
517 if (ret != MEDIA_CONTENT_ERROR_NONE) {
518 LogAndReportError(ContentManager::convertError(ret), out, ("Failed: media_folder_get_name"));
523 ret = media_folder_get_path(folder, &path);
524 if (ret != MEDIA_CONTENT_ERROR_NONE) {
525 LogAndReportError(ContentManager::convertError(ret), out, ("Failed: media_folder_get_path"));
531 ret = media_folder_get_modified_time(folder, &date);
532 if (ret != MEDIA_CONTENT_ERROR_NONE) {
533 LogAndReportError(ContentManager::convertError(ret), out, ("Failed: media_folder_get_path"));
540 ret = media_folder_get_storage_type(folder, &storageType);
541 if (ret != MEDIA_CONTENT_ERROR_NONE) {
545 LogAndReportError(ContentManager::convertError(ret), out,
546 ("Failed: media_folder_get_storage_type"));
550 (*out)["id"] = picojson::value(std::string(id));
551 (*out)["directoryURI"] = picojson::value(std::string(path));
552 (*out)["title"] = picojson::value(std::string(name));
554 if (storageType == MEDIA_CONTENT_STORAGE_INTERNAL) {
555 (*out)["storageType"] = picojson::value(std::string("INTERNAL"));
556 } else if (storageType == MEDIA_CONTENT_STORAGE_EXTERNAL) {
557 (*out)["storageType"] = picojson::value(std::string("EXTERNAL"));
560 (*out)["modifiedDate"] = picojson::value(static_cast<double>(date));
567 static bool media_foreach_directory_cb(media_folder_h folder, void* user_data) {
569 picojson::array* array = static_cast<picojson::array*>(user_data);
570 picojson::object json;
571 FolderToJson(folder, &json);
572 array->push_back(picojson::value(json));
576 static bool media_foreach_content_cb(media_info_h media, void* user_data) {
578 picojson::value::array* contents = static_cast<picojson::value::array*>(user_data);
579 picojson::value::object o;
580 ContentToJson(media, o);
581 contents->push_back(picojson::value(o));
585 static bool playlist_foreach_cb(media_playlist_h playlist, void* user_data) {
587 picojson::value::array* playlists = static_cast<picojson::value::array*>(user_data);
588 picojson::value::object o;
589 if (playlist != NULL) {
591 char* thumb_path = NULL;
593 filter_h filter = NULL;
594 if (media_playlist_get_playlist_id(playlist, &id) == MEDIA_CONTENT_ERROR_NONE) {
595 std::stringstream str_id;
597 o["id"] = picojson::value(std::to_string(id));
599 LoggerD("Invalid ID for playlist.");
601 if (media_playlist_get_thumbnail_path(playlist, &thumb_path) == MEDIA_CONTENT_ERROR_NONE) {
602 if (thumb_path != NULL) {
603 std::string thumbnail_uri(thumb_path);
604 if (thumbnail_uri != " ") {
605 thumbnail_uri = uri_prefix + thumbnail_uri;
607 o["thumbnailURI"] = picojson::value(thumbnail_uri);
610 o["thumbnailURI"] = picojson::value(); // picojson::value(std::string(""));
613 LoggerD("Invalid thumbnail path for playlist.");
615 if (media_playlist_get_name(playlist, &name) == MEDIA_CONTENT_ERROR_NONE) {
616 o["name"] = picojson::value(std::string(name));
619 LoggerD("Invalid name for playlist.");
622 media_filter_create(&filter);
623 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
624 filter, &media_filter_destroy); // automatically release the memory
625 if (media_playlist_get_media_count_from_db(id, filter, &cnt) == MEDIA_CONTENT_ERROR_NONE) {
626 o["numberOfTracks"] = picojson::value(static_cast<double>(cnt));
628 LoggerE("Invalid count for playlist.");
630 playlists->push_back(picojson::value(o));
635 static bool playlist_content_member_cb(int playlist_member_id, media_info_h media,
638 picojson::value::array* contents = static_cast<picojson::value::array*>(user_data);
639 picojson::value::object o;
641 o["playlist_member_id"] = picojson::value(static_cast<double>(playlist_member_id));
642 ContentToJson(media, o);
643 contents->push_back(picojson::value(o));
647 void CreateThumbnailCallback(media_content_error_e err, const char* path, void* user_data) {
650 std::unique_ptr<CreateThumbnailCallbackData> callback_data_ptr(
651 (CreateThumbnailCallbackData*)user_data);
652 if (0 == callback_data_ptr->callback_id) {
653 LoggerD("Callback id is 0");
657 if (!(ContentManager::getInstance()->getContentInstance())) {
658 // There is not instance already
659 LoggerD("There is not instance now");
663 picojson::object out;
665 out["callbackId"] = picojson::value(callback_data_ptr->callback_id);
667 if (MEDIA_CONTENT_ERROR_NONE == err) {
668 out["result"] = picojson::value(std::string(path));
671 PlatformResult result = ContentManager::getInstance()->convertError(err);
672 LogAndReportError(result, &out, ("Failed to create a thumbnail"));
675 common::Instance::PostMessage(ContentManager::getInstance()->getContentInstance(),
676 picojson::value(out).serialize().c_str());
679 ContentManager::ContentManager() {
680 ScopeLogger("ContentManager called");
681 if (media_content_connect() == MEDIA_CONTENT_ERROR_NONE) {
682 m_dbConnected = true;
684 m_dbConnected = false;
686 m_contentInstance = nullptr;
689 ContentManager::~ContentManager() {
692 if (media_content_disconnect() == MEDIA_CONTENT_ERROR_NONE) {
693 m_dbConnected = false;
698 ContentManager* ContentManager::getInstance() {
700 static ContentManager instance;
704 ContentInstance* ContentManager::getContentInstance() {
706 return m_contentInstance;
709 void ContentManager::setContentInstance(ContentInstance* const content_instance) {
711 m_contentInstance = content_instance;
714 bool ContentManager::isConnected() {
716 return m_dbConnected;
719 void ContentManager::getDirectories(const std::shared_ptr<ReplyCallbackData>& user_data) {
722 filter_h filter = NULL;
723 ret = media_filter_create(&filter);
724 if (ret != MEDIA_CONTENT_ERROR_NONE) {
725 LoggerE("Failed: media_filter_create failed");
730 media_filter_destroy(filter);
733 std::string condition = "(FOLDER_STORAGE_TYPE = 0 OR FOLDER_STORAGE_TYPE = 1)";
734 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
736 picojson::array pico_dirs;
737 ret = media_folder_foreach_folder_from_db(filter, media_foreach_directory_cb, &pico_dirs);
738 if (ret != MEDIA_CONTENT_ERROR_NONE) {
739 PlatformResult err = LogAndCreateResult(
740 ErrorCode::UNKNOWN_ERR, "Getting the directories failed.",
741 ("Failed: Getting the directories failed %d (%s)", ret, get_error_message(ret)));
742 user_data->isSuccess = err;
746 user_data->result = picojson::value(pico_dirs);
749 void ContentManager::find(const std::shared_ptr<ReplyCallbackData>& user_data) {
756 picojson::value::array arrayContent;
757 filter_h filter = nullptr;
758 media_filter_create(&filter);
761 media_filter_destroy(filter);
765 if (!IsNull(user_data->args.get("filter"))) {
766 ContentFilter filterMechanism;
768 picojson::object argsObject = JsonCast<picojson::object>(user_data->args);
769 if (filterMechanism.BuildQuery(FromJson<picojson::object>(argsObject, "filter"), &query)) {
770 LoggerD("Filter query: %s", query.c_str());
771 ret = media_filter_set_condition(filter, query.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
772 if (MEDIA_CONTENT_ERROR_NONE != ret) {
773 LoggerE("Platform filter setting failed, error %d", ret);
778 if (user_data->args.contains("sortMode")) {
779 picojson::value vSortMode = user_data->args.get("sortMode");
781 if (vSortMode.is<picojson::object>()) {
782 std::string sortModeName, sortModeOrder;
784 ContentFilter::MapField(vSortMode.get("attributeName").to_str(), &sortModeName);
786 sortModeOrder = vSortMode.get("order").to_str();
787 if (!sortModeOrder.empty()) {
788 media_content_order_e order = MEDIA_CONTENT_ORDER_ASC;
790 if (sortModeOrder == "ASC") {
791 order = MEDIA_CONTENT_ORDER_ASC;
792 } else if (sortModeOrder == "DESC") {
793 order = MEDIA_CONTENT_ORDER_DESC;
796 ret = media_filter_set_order(filter, order, sortModeName.c_str(),
797 MEDIA_CONTENT_COLLATE_DEFAULT);
798 if (MEDIA_CONTENT_ERROR_NONE != ret) {
799 LoggerE("Platform SortMode setting failed, error: %d", ret);
805 if (!IsNull(user_data->args.get("count"))) {
806 count = static_cast<int>(user_data->args.get("count").get<double>());
810 if (!IsNull(user_data->args.get("offset"))) {
811 offset = static_cast<int>(user_data->args.get("offset").get<double>());
815 ret = media_filter_set_offset(filter, offset, count);
816 if (MEDIA_CONTENT_ERROR_NONE != ret) {
817 LoggerE("A platform error occurs in media_filter_set_offset: %d", ret);
819 if (!IsNull(user_data->args.get("directoryId"))) {
820 dirId = user_data->args.get("directoryId").get<std::string>();
821 ret = media_folder_foreach_media_from_db(dirId.c_str(), filter, media_foreach_content_cb,
822 static_cast<void*>(&arrayContent));
824 ret = media_info_foreach_media_from_db(filter, media_foreach_content_cb,
825 static_cast<void*>(&arrayContent));
828 if (ret == MEDIA_CONTENT_ERROR_NONE) {
829 user_data->result = picojson::value(arrayContent);
831 PlatformResult err = LogAndCreateResult(
832 ErrorCode::UNKNOWN_ERR, "The iteration failed in platform",
833 ("The iteration failed in platform: %d (%s)", ret, get_error_message(ret)));
834 user_data->isSuccess = err;
838 int ContentManager::scanFile(std::string& uri) {
840 return media_content_scan_file(uri.c_str());
843 PlatformResult ContentManager::scanDirectory(media_scan_completed_cb callback,
844 ReplyCallbackData* cbData) {
846 const std::string& contentDirURI = cbData->args.get("contentDirURI").get<std::string>();
847 std::string real_path = common::FilesystemProvider::Create().GetRealPath(contentDirURI);
848 const bool recursive = cbData->args.get("recursive").get<bool>();
850 int ret = media_content_scan_folder(real_path.c_str(), recursive, callback, (void*)cbData);
852 if (ret != MEDIA_CONTENT_ERROR_NONE) {
853 if (MEDIA_CONTENT_ERROR_INVALID_PARAMETER == ret) {
854 return LogAndCreateResult(
855 ErrorCode::INVALID_VALUES_ERR, "Scanning content directory failed",
856 ("Scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
859 return LogAndCreateResult(
860 ErrorCode::UNKNOWN_ERR, "Scanning content directory failed",
861 ("Scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
864 return PlatformResult(ErrorCode::NO_ERROR);
867 PlatformResult ContentManager::cancelScanDirectory(const std::string& content_dir_uri) {
870 int ret = media_content_cancel_scan_folder(content_dir_uri.c_str());
871 if (ret != MEDIA_CONTENT_ERROR_NONE) {
872 return LogAndCreateResult(
873 ErrorCode::UNKNOWN_ERR, "Cancel scan content directory failed",
874 ("Cancel scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
876 return PlatformResult(ErrorCode::NO_ERROR);
879 PlatformResult ContentManager::addChangeListener(media_content_noti_h* noti_handle,
880 media_content_db_update_cb callback,
884 int ret = media_content_add_db_updated_cb(callback, user_data, noti_handle);
886 if (MEDIA_CONTENT_ERROR_NONE != ret) {
887 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to add the listener.",
888 ("Failed to add the listener: %d (%s)", ret, get_error_message(ret)));
891 return PlatformResult(ErrorCode::NO_ERROR);
894 PlatformResult ContentManager::removeChangeListener(media_content_noti_h noti_handle) {
897 int ret = media_content_remove_db_updated_cb(noti_handle);
900 case MEDIA_CONTENT_ERROR_NONE:
901 return PlatformResult(ErrorCode::NO_ERROR);
902 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
903 // Trying to remove non-existent listener, ignoring
904 LoggerI("Failed to remove the listener: %d (%s)", ret, get_error_message(ret));
905 return PlatformResult(ErrorCode::NO_ERROR);
907 return LogAndCreateResult(
908 ErrorCode::ABORT_ERR, "Failed to remove the listener.",
909 ("Failed to remove the listener: %d (%s)", ret, get_error_message(ret)));
913 void ContentManager::createPlaylist(std::string name,
914 const std::shared_ptr<ReplyCallbackData>& user_data) {
916 media_playlist_h playlist = NULL;
918 int ret = media_playlist_insert_to_db(name.c_str(), &playlist);
919 std::unique_ptr<std::remove_pointer<media_playlist_h>::type, int (*)(media_playlist_h)>
920 playlist_ptr(playlist, &media_playlist_destroy); // automatically release the memory
921 if (ret != MEDIA_CONTENT_ERROR_NONE) {
922 // MEDIA_CONTENT_ERROR_DB_FAILED means that playlist probably already exists
923 PlatformResult err = LogAndCreateResult(
924 MEDIA_CONTENT_ERROR_DB_FAILED == ret ? ErrorCode::INVALID_VALUES_ERR
925 : ErrorCode::UNKNOWN_ERR,
926 "Creation of playlist has failed.",
927 ("Failed: creation of playlist is failed: %d (%s)", ret, get_error_message(ret)));
928 user_data->isSuccess = err;
931 picojson::value::object o;
933 if (playlist != NULL) {
935 char* thumb_path = NULL;
936 char* name_playlist = NULL;
937 filter_h filter = NULL;
938 if (media_playlist_get_playlist_id(playlist, &id) == MEDIA_CONTENT_ERROR_NONE) {
939 o["id"] = picojson::value(std::to_string(id));
942 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "loading of playlist is failed.");
943 user_data->isSuccess = err;
946 if (media_playlist_get_thumbnail_path(playlist, &thumb_path) == MEDIA_CONTENT_ERROR_NONE) {
947 if (thumb_path != NULL) {
948 o["thumbnailURI"] = picojson::value(std::string(thumb_path));
951 o["thumbnailURI"] = picojson::value();
954 LoggerE("Invalid thumbnail path for playlist.");
956 if (media_playlist_get_name(playlist, &name_playlist) == MEDIA_CONTENT_ERROR_NONE) {
957 o["name"] = picojson::value(std::string(name_playlist));
960 LoggerE("Invalid name for playlist.");
962 media_filter_create(&filter);
963 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
964 filter, &media_filter_destroy); // automatically release the memory
966 if (media_playlist_get_media_count_from_db(id, filter, &cnt) == MEDIA_CONTENT_ERROR_NONE) {
967 o["numberOfTracks"] = picojson::value(static_cast<double>(cnt));
969 LoggerE("Invalid count for playlist.");
973 user_data->result = picojson::value(o);
976 void ContentManager::getPlaylists(const std::shared_ptr<ReplyCallbackData>& user_data) {
979 filter_h filter = nullptr;
980 media_filter_create(&filter);
981 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
982 filter, &media_filter_destroy); // automatically release the memory
983 picojson::value::array playlists;
985 ret = media_playlist_foreach_playlist_from_db(filter, playlist_foreach_cb,
986 static_cast<void*>(&playlists));
988 if (ret != MEDIA_CONTENT_ERROR_NONE) {
989 PlatformResult err = LogAndCreateResult(
990 ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
991 ("Failed: Getting playlist is failed %d (%s)", ret, get_error_message(ret)));
992 user_data->isSuccess = err;
995 user_data->result = picojson::value(playlists);
998 void ContentManager::removePlaylist(std::string playlistId,
999 const std::shared_ptr<ReplyCallbackData>& user_data) {
1001 int id = std::atoi(playlistId.c_str());
1003 PlatformResult err = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "PlaylistId is wrong.");
1004 user_data->isSuccess = err;
1008 int ret = media_playlist_delete_from_db(id);
1009 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1010 PlatformResult err =
1011 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Removal of playlist is failed.");
1012 user_data->isSuccess = err;
1016 int ContentManager::update(picojson::value args) {
1020 picojson::value content = args.get("content");
1021 std::string id = content.get("id").to_str();
1023 media_info_h media = NULL;
1024 ret = media_info_get_media_from_db(id.c_str(), &media);
1025 if (ret == MEDIA_CONTENT_ERROR_NONE) {
1026 setContent(media, content);
1027 ret = media_info_update_to_db(media);
1028 media_info_destroy(media);
1034 int ContentManager::updateBatch(picojson::value args) {
1037 std::vector<picojson::value> contents = args.get("contents").get<picojson::array>();
1039 for (picojson::value::array::iterator it = contents.begin(); it != contents.end(); ++it) {
1040 picojson::value content = *it;
1041 std::string id = content.get("id").to_str();
1042 media_info_h media = NULL;
1043 ret = media_info_get_media_from_db(id.c_str(), &media);
1044 if (media != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
1045 ret = setContent(media, content);
1046 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1047 LoggerE("setContent failed");
1051 ret = media_info_update_to_db(media);
1052 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1053 LoggerE("update to db failed");
1055 media_info_destroy(media);
1063 int ContentManager::playlistAdd(std::string playlist_id, std::string content_id) {
1066 media_playlist_h playlist = NULL;
1067 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1069 if (playlist != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
1070 ret = media_playlist_add_media(playlist, content_id.c_str());
1071 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1072 LoggerE("The content(id:%s) can't add to playlist", content_id.c_str());
1075 ret = media_playlist_update_to_db(playlist);
1076 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1077 LoggerE("The content(id:%s) can't add to playlist", content_id.c_str());
1080 LoggerE("Playlist(id:%s) is not exist", playlist_id.c_str());
1083 media_playlist_destroy(playlist);
1087 int ContentManager::playlistRemove(std::string playlist_id, int member_id) {
1090 media_playlist_h playlist = NULL;
1091 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1092 if (playlist != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
1093 ret = media_playlist_remove_media(playlist, member_id);
1094 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1095 LoggerE("The content can't remove to playlist");
1098 ret = media_playlist_update_to_db(playlist);
1099 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1100 LoggerE("The content can't remove to playlist");
1103 LoggerE("Playlist(id:%s) is not exist", playlist_id.c_str());
1105 media_playlist_destroy(playlist);
1110 void ContentManager::playlistAddbatch(const std::shared_ptr<ReplyCallbackData>& user_data) {
1112 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1114 media_playlist_h playlist = NULL;
1115 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1117 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1118 PlatformResult err =
1119 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1120 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1121 user_data->isSuccess = err;
1125 std::vector<picojson::value> contents = user_data->args.get("contents").get<picojson::array>();
1126 for (picojson::value::array::iterator it = contents.begin(); it != contents.end(); ++it) {
1127 picojson::value content = *it;
1128 std::string id = content.get("id").to_str();
1129 ret = media_playlist_add_media(playlist, id.c_str());
1130 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1131 LoggerE("Adding Content(id:%s) is failed.", id.c_str());
1135 ret = media_playlist_update_to_db(playlist);
1136 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1137 PlatformResult err =
1138 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Adding playlist is failed.",
1139 ("Adding playlist is failed: %d (%s)", ret, get_error_message(ret)));
1140 user_data->isSuccess = err;
1142 media_playlist_destroy(playlist);
1145 void ContentManager::playlistGet(const std::shared_ptr<ReplyCallbackData>& user_data) {
1147 media_playlist_h playlist = NULL;
1148 media_content_order_e order = MEDIA_CONTENT_ORDER_ASC;
1149 const std::string playOrder("play_order");
1153 media_playlist_destroy(playlist);
1157 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1158 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1159 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1160 PlatformResult err =
1161 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1162 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1163 user_data->isSuccess = err;
1167 filter_h filter = NULL;
1168 ret = media_filter_create(&filter);
1169 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1170 PlatformResult err =
1171 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Creating a filter is failed.",
1172 ("Creating a filter is failed: %d (%s)", ret, get_error_message(ret)));
1173 user_data->isSuccess = err;
1177 int count = user_data->args.get("count").get<double>();
1178 int offset = user_data->args.get("offset").get<double>();
1179 ret = media_filter_set_offset(filter, offset, count);
1180 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1181 LoggerD("Setting a offset/count is failed.");
1183 ret = media_filter_set_order(filter, order, playOrder.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
1184 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1185 LoggerD("Setting a offset/count is failed.");
1188 picojson::value::array arrayContent;
1189 ret = media_playlist_foreach_media_from_db(std::stoi(playlist_id), filter,
1190 playlist_content_member_cb,
1191 static_cast<void*>(&arrayContent));
1193 media_filter_destroy(filter);
1194 if (ret == MEDIA_CONTENT_ERROR_NONE) {
1195 user_data->result = picojson::value(arrayContent);
1197 PlatformResult err =
1198 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Creating a filter is failed.",
1199 ("Creating a filter is failed: %d (%s)", ret, get_error_message(ret)));
1200 user_data->isSuccess = err;
1204 void ContentManager::playlistRemovebatch(const std::shared_ptr<ReplyCallbackData>& user_data) {
1206 media_playlist_h playlist = NULL;
1210 media_playlist_destroy(playlist);
1214 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1215 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1216 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1217 PlatformResult err =
1218 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1219 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1220 user_data->isSuccess = err;
1224 std::vector<picojson::value> members = user_data->args.get("members").get<picojson::array>();
1225 std::size_t members_size = members.size();
1226 for (std::size_t i = 0; i < members_size; ++i) {
1227 int member_id = static_cast<int>(members.at(i).get<double>());
1228 ret = media_playlist_remove_media(playlist, member_id);
1230 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1231 LoggerD("Removing a content is failed.");
1235 ret = media_playlist_update_to_db(playlist);
1236 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1237 PlatformResult err = LogAndCreateResult(
1238 ErrorCode::UNKNOWN_ERR, "Removing the contents is failed.",
1239 ("Removing the contents is failed: %d (%s)", ret, get_error_message(ret)));
1240 user_data->isSuccess = err;
1244 void ContentManager::playlistSetOrder(const std::shared_ptr<ReplyCallbackData>& user_data) {
1246 media_playlist_h playlist = NULL;
1250 media_playlist_destroy(playlist);
1254 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1255 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1256 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1257 PlatformResult err =
1258 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1259 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1260 user_data->isSuccess = err;
1265 std::vector<picojson::value> members = user_data->args.get("members").get<picojson::array>();
1267 ret = media_playlist_get_media_count_from_db(std::stoi(playlist_id), NULL, &cnt);
1268 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1269 LoggerE("Failed: media_playlist_get_media_count_from_db");
1270 PlatformResult err = convertError(ret);
1271 user_data->isSuccess = err;
1274 std::size_t members_size = members.size();
1275 if (cnt < 0 || static_cast<size_t>(cnt) != members_size) {
1276 PlatformResult err = LogAndCreateResult(
1277 ErrorCode::INVALID_VALUES_ERR,
1278 "The items array does not contain all items from the playlist.",
1279 ("Failed: The items array does not contain all items from the playlist: %d (%s)", ret,
1280 get_error_message(ret)));
1281 user_data->isSuccess = err;
1285 for (std::size_t i = 0; i < members_size; ++i) {
1286 int member_id = static_cast<int>(members.at(i).get<double>());
1287 ret = media_playlist_set_play_order(playlist, member_id, i);
1288 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1289 LoggerD("Removing a content is failed.");
1293 ret = media_playlist_update_to_db(playlist);
1294 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1295 PlatformResult err = LogAndCreateResult(
1296 ErrorCode::UNKNOWN_ERR, "Removing the contents is failed.",
1297 ("Removing the contents is failed: %d (%s)", ret, get_error_message(ret)));
1298 user_data->isSuccess = err;
1302 void ContentManager::playlistMove(const std::shared_ptr<ReplyCallbackData>& user_data) {
1304 media_playlist_h playlist = NULL;
1308 media_playlist_destroy(playlist);
1312 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1313 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1314 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1315 PlatformResult err =
1316 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1317 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1318 user_data->isSuccess = err;
1322 double member_id = user_data->args.get("memberId").get<double>();
1323 double delta = user_data->args.get("delta").get<double>();
1324 ret = media_playlist_get_play_order(playlist, static_cast<int>(member_id), &old_order);
1325 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1326 PlatformResult err = LogAndCreateResult(
1327 ErrorCode::UNKNOWN_ERR, "The content can't find form playlist.",
1328 ("The content can't find form playlist: %d (%s)", ret, get_error_message(ret)));
1329 user_data->isSuccess = err;
1332 int new_order = static_cast<int>(old_order) + static_cast<int>(delta);
1333 ret = media_playlist_set_play_order(playlist, static_cast<int>(member_id), new_order);
1334 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1335 PlatformResult err = LogAndCreateResult(
1336 ErrorCode::UNKNOWN_ERR, "The content can't update play_order.",
1337 ("The content can't update play_order: %d (%s)", ret, get_error_message(ret)));
1338 user_data->isSuccess = err;
1341 ret = media_playlist_update_to_db(playlist);
1342 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1343 PlatformResult err = LogAndCreateResult(
1344 ErrorCode::UNKNOWN_ERR, "Updateing play_order is failed.",
1345 ("Updateing play_order is failed: %d (%s)", ret, get_error_message(ret)));
1346 user_data->isSuccess = err;
1350 int ContentManager::getLyrics(const picojson::value& args, picojson::object& result) {
1353 int ret = METADATA_EXTRACTOR_ERROR_NONE;
1354 const std::string& contentURI = args.get("contentURI").to_str();
1355 if (contentURI.empty()) {
1356 LoggerE("contentURI empty - skipping media extractor");
1360 metadata_extractor_h extractor;
1361 ret = metadata_extractor_create(&extractor);
1362 if (METADATA_EXTRACTOR_ERROR_NONE != ret) {
1363 LoggerE("metadata_extractor_create failed, error: %d", ret);
1365 std::unique_ptr<std::remove_pointer<metadata_extractor_h>::type, int (*)(metadata_extractor_h)>
1366 extractor_ptr(extractor, &metadata_extractor_destroy); // automatically release the memory
1368 ret = metadata_extractor_set_path(extractor, contentURI.c_str());
1369 if (ret != METADATA_EXTRACTOR_ERROR_NONE) {
1370 LoggerE("metadata_extractor_set_path failed, error: %d", ret);
1373 picojson::array timestamps;
1374 picojson::array texts = picojson::array();
1375 char* strSyncTextNum = NULL;
1377 ret = metadata_extractor_get_metadata(extractor, METADATA_SYNCLYRICS_NUM, &strSyncTextNum);
1378 if (ret != METADATA_EXTRACTOR_ERROR_NONE) {
1379 LoggerE("Media extractor error %d", ret);
1383 int nSyncTextNum = 0;
1384 if (strSyncTextNum) {
1385 nSyncTextNum = atoi(strSyncTextNum);
1386 free(strSyncTextNum);
1387 strSyncTextNum = NULL;
1389 if (nSyncTextNum > 0) {
1390 result["type"] = picojson::value(std::string("SYNCHRONIZED"));
1391 for (int i = 0; i < nSyncTextNum; i++) {
1392 unsigned long time_info = 0;
1393 char* lyrics = NULL;
1394 ret = metadata_extractor_get_synclyrics(extractor, i, &time_info, &lyrics);
1395 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
1396 timestamps.push_back(picojson::value(static_cast<double>(time_info)));
1397 texts.push_back(picojson::value(std::string(lyrics)));
1401 result["texts"] = picojson::value(texts);
1402 result["timestamps"] = picojson::value(timestamps);
1403 ret = METADATA_EXTRACTOR_ERROR_NONE;
1405 char* unSyncText = nullptr;
1406 ret = metadata_extractor_get_metadata(extractor, METADATA_UNSYNCLYRICS, &unSyncText);
1407 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
1408 result["type"] = picojson::value(std::string("UNSYNCHRONIZED"));
1409 if (nullptr == unSyncText) {
1410 LoggerE("Unsynchronized lyrics text is NULL");
1412 texts.push_back(picojson::value(unSyncText ? unSyncText : ""));
1413 result["texts"] = picojson::value(texts);
1421 media_playlist_h getPlaylistHandle(int id) {
1423 media_playlist_h playlist_handle = nullptr;
1424 int ret_code = media_playlist_get_playlist_from_db(id, &playlist_handle);
1425 if (MEDIA_CONTENT_ERROR_NONE != ret_code || playlist_handle == nullptr) {
1426 LoggerE("could not get playlist handle for id: %d", id);
1430 return playlist_handle;
1433 void destroyMediaPlaylistHandle(media_playlist_h& playlist_handle) {
1435 if (playlist_handle) {
1436 int ret_code = media_playlist_destroy(playlist_handle);
1437 playlist_handle = nullptr;
1439 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1440 LoggerE("media_playlist_destroy failed");
1445 int ContentManager::getPlaylistName(int id, std::string* result) {
1447 media_playlist_h playlist_handle = getPlaylistHandle(id);
1448 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1450 char* tmp_playlist_name = nullptr;
1451 const int ret_code = media_playlist_get_name(playlist_handle, &tmp_playlist_name);
1453 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1454 LoggerE("media_playlist_get_name failed");
1455 return TIZEN_ERROR_UNKNOWN;
1458 std::string playlist_name;
1459 if (tmp_playlist_name) {
1460 playlist_name = tmp_playlist_name;
1461 free(tmp_playlist_name);
1462 tmp_playlist_name = nullptr;
1465 *result = playlist_name;
1466 return MEDIA_CONTENT_ERROR_NONE;
1469 int updatePlaylistInDB(media_playlist_h playlist_handle) {
1471 int ret_code = media_playlist_update_to_db(playlist_handle);
1472 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1473 LoggerE("media_playlist_update_to_db failed");
1476 return MEDIA_CONTENT_ERROR_NONE;
1479 int ContentManager::setPlaylistName(int id, const std::string& name) {
1482 LoggerE("Cannot set empty playlist name!");
1483 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1486 media_playlist_h playlist_handle = getPlaylistHandle(id);
1487 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1489 const int ret_code = media_playlist_set_name(playlist_handle, name.c_str());
1490 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1491 LoggerE("media_playlist_set_name failed");
1492 // Setting name that is used by other playlist does not return bad error code here.
1493 // MEDIA_CONTENT_ERROR_INVALID_OPERATION is being returned in updatePlaylistInDB
1494 return TIZEN_ERROR_UNKNOWN;
1497 int ret = updatePlaylistInDB(playlist_handle);
1498 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1499 LoggerE("Error while updating playlist: %d", ret);
1500 if (MEDIA_CONTENT_ERROR_DB_FAILED == ret) {
1501 // We could fetch list of playlists and check if other playlist is using this
1502 // name, but that seems to be to much work in synchronous method
1503 LoggerE("Playlist name: %s is probably already used", name.c_str());
1504 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1508 return MEDIA_CONTENT_ERROR_NONE;
1511 int ContentManager::getThumbnailUri(int id, std::string* result) {
1513 media_playlist_h playlist_handle = getPlaylistHandle(id);
1514 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1516 char* tmp_playlist_thb_path = nullptr;
1517 const int ret_code = media_playlist_get_thumbnail_path(playlist_handle, &tmp_playlist_thb_path);
1519 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1520 LoggerE("media_playlist_get_name failed");
1521 return TIZEN_ERROR_UNKNOWN;
1524 std::string playlist_thb_path;
1525 if (tmp_playlist_thb_path) {
1526 playlist_thb_path = tmp_playlist_thb_path;
1527 free(tmp_playlist_thb_path);
1528 tmp_playlist_thb_path = nullptr;
1531 if (playlist_thb_path != " ") {
1532 playlist_thb_path = uri_prefix + playlist_thb_path;
1535 *result = playlist_thb_path;
1536 return MEDIA_CONTENT_ERROR_NONE;
1539 int ContentManager::setThumbnailUri(int id, const std::string& thb_uri) {
1542 // Allow setting empty URI, unfortunately Core API does not allow to set empty
1543 // path so we need to set one empty space. This is probably issue of Core API.
1544 if (!thb_uri.empty() && " " != thb_uri) {
1545 if (thb_uri.find(uri_absolute_prefix) != 0) {
1546 LoggerE("thumbnail URI is not valid: [%s]", thb_uri.c_str());
1547 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1551 media_playlist_h playlist_handle = getPlaylistHandle(id);
1552 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1554 std::string real_path = common::FilesystemProvider::Create().GetRealPath(thb_uri);
1555 const int ret_code = media_playlist_set_thumbnail_path(playlist_handle, real_path.c_str());
1556 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1557 LoggerE("media_playlist_set_thumbnail_path failed");
1558 return TIZEN_ERROR_UNKNOWN;
1561 int ret = updatePlaylistInDB(playlist_handle);
1565 int ContentManager::getNumberOfTracks(int id, int* result) {
1569 const int ret_code = media_playlist_get_media_count_from_db(id, nullptr, &count);
1571 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1572 LoggerE("media_playlist_get_media_count_from_db failed");
1573 return TIZEN_ERROR_UNKNOWN;
1577 return MEDIA_CONTENT_ERROR_NONE;
1580 common::PlatformResult ContentManager::createThumbnail(const picojson::value& args) {
1583 std::unique_ptr<CreateThumbnailCallbackData> callback_data_ptr{new CreateThumbnailCallbackData};
1584 callback_data_ptr->callback_id = args.get("callbackId").get<double>();
1585 std::string id = args.get("id").get<std::string>();
1587 int ret = media_info_get_media_from_db(id.c_str(), &callback_data_ptr->media);
1588 if (MEDIA_CONTENT_ERROR_NONE != ret && nullptr == callback_data_ptr->media) {
1589 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Getting media is failed.",
1590 ("Getting media is failed: %d (%s)", ret, get_error_message(ret)));
1593 ret = media_info_create_thumbnail(callback_data_ptr->media, CreateThumbnailCallback,
1594 static_cast<void*>(callback_data_ptr.get()));
1596 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1597 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Creating thumbnail failed.",
1598 ("Creating thumbnail failed: %d (%s)", ret, get_error_message(ret)));
1600 callback_data_ptr.release();
1601 return PlatformResult(ErrorCode::NO_ERROR);
1604 PlatformResult ContentManager::convertError(int err) {
1605 char* error_msg = get_error_message(err);
1607 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
1608 return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, error_msg);
1609 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
1610 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1611 case MEDIA_CONTENT_ERROR_INVALID_OPERATION:
1612 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1613 case MEDIA_CONTENT_FILE_NO_SPACE_ON_DEVICE:
1614 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1615 case MEDIA_CONTENT_ERROR_PERMISSION_DENIED:
1616 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1617 case MEDIA_CONTENT_ERROR_DB_FAILED:
1618 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1619 case MEDIA_CONTENT_ERROR_DB_BUSY:
1620 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1621 case MEDIA_CONTENT_ERROR_NETWORK:
1622 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1623 case MEDIA_CONTENT_ERROR_UNSUPPORTED_CONTENT:
1624 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1626 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown error.");
1630 } // namespace content
1631 } // namespace extension