diff options
author | Farshid Monhaseri <monhaseri.f@gmail.com> | 2020-01-02 21:37:11 +0330 |
---|---|---|
committer | Farshid Monhaseri <monhaseri.f@gmail.com> | 2020-01-11 09:06:30 +0330 |
commit | 7051b6f8422fdb742bf70077eacfd1249ac21d21 (patch) | |
tree | 6e2149f9e5c22f3506ae664cb1b553edcc059665 /binding/media-api.c | |
parent | ee883c3f23b4a4b49730396e7522646b9c224ab4 (diff) |
Add scan-type(audio,video,image,all) to scanner
Bug-AGL: SPEC-3096
User can pass 'types' as a request property with 'audio','video',
'image','all' values as a single 'string' value or as an array of
'string' values along 'media_result','subscribe','unsubscribe'
verbs to scan or register the events with those scan-types.
If the user doesn't provide the 'types' request property, the API
will function as before with 'audio' and 'video' scan-types.
Change-Id: I8ff49f89e2696f600dfb2935403d8b0c8246c07d
Signed-off-by: Farshid Monhaseri <monhaseri.f@gmail.com>
Diffstat (limited to 'binding/media-api.c')
-rw-r--r-- | binding/media-api.c | 113 |
1 files changed, 108 insertions, 5 deletions
diff --git a/binding/media-api.c b/binding/media-api.c index d02ad93..05fbba2 100644 --- a/binding/media-api.c +++ b/binding/media-api.c @@ -29,6 +29,72 @@ static afb_event_t media_added_event; static afb_event_t media_removed_event; +static gint get_scan_type(afb_req_t request, json_object *jtype) { + gint ret = 0; + const *stype = NULL; + if(!json_object_is_type(jtype,json_type_string)) { + afb_req_fail(request,"failed", "invalid scan-type type"); + return ret; + } + stype = json_object_get_string(jtype); + + if(!strcasecmp(stype,MEDIA_ALL)) { + ret = LMS_ALL_SCAN; + } else if(!strcasecmp(stype,MEDIA_AUDIO)) { + ret = LMS_AUDIO_SCAN; + } else if(!strcasecmp(stype,MEDIA_VIDEO)) { + ret = LMS_VIDEO_SCAN; + } else if(!strcasecmp(stype,MEDIA_IMAGE)) { + ret = LMS_IMAGE_SCAN; + } else { + afb_req_fail(request,"failed","invalid scan-type value"); + } + return ret; +} + +static gint get_scan_types(afb_req_t request) { + + json_object *jtypes = NULL; + gint ret = 0; + + if(!afb_req_is_valid(request)) { + afb_req_fail(request, "failed", "invalid request"); + return -1; + } + + if(!json_object_object_get_ex(afb_req_json(request),"types",&jtypes)) { + /* + * Considering 'audio' & 'video' scan types as default + * if the user doesn't provide 'types' property, + * for sake of compability with previeus versions + */ + ret = (LMS_AUDIO_SCAN | LMS_VIDEO_SCAN); + return ret; + } + + if(json_object_is_type(jtypes,json_type_array)) { + const gint len = json_object_array_length(jtypes); + size_t i; + + if(len > LMS_SCAN_COUNT) { + afb_req_fail(request, "failed", "too many scan-types"); + return -1; + } + + for(i=0; i<len; ++i){ + gint t = get_scan_type(request, json_object_array_get_idx(jtypes,i)); + if(t != 0) { ret |= t; } else { return -1;} + } + return ret; + } else if(json_object_is_type(jtypes,json_type_string)) { + gint t = get_scan_type(request,jtypes); + if(t != 0) {return t;} else {return -1;} + } else { + afb_req_fail(request, "failed", "invalid scan-types format"); + return -1; + } +} + /* * @brief Subscribe for an event * @@ -38,6 +104,8 @@ static afb_event_t media_removed_event; static void subscribe(afb_req_t request) { const char *value = afb_req_value(request, "value"); + gint scantype = 0; + if(value) { if(!strcasecmp(value, "media_added")) { afb_req_subscribe(request, media_added_event); @@ -48,7 +116,13 @@ static void subscribe(afb_req_t request) return; } } - afb_req_success(request, NULL, NULL); + + //Get scan types & append them to scan config + scantype = get_scan_types(request); + if(scantype < 0) return; + + ScanTypeAppend(scantype); + afb_req_success(request, NULL, NULL); } /* @@ -59,7 +133,26 @@ static void subscribe(afb_req_t request) */ static void unsubscribe(afb_req_t request) { - const char *value = afb_req_value(request, "value"); + json_object *jrequest = afb_req_json(request); + char *value = NULL; + + if( json_object_object_get_ex(jrequest,"value",NULL) && + json_object_object_get_ex(jrequest,"types",NULL) ) { + //If 'types' is provided, We just remove the specified types. + gint scantype = get_scan_types(request); + if(scantype < 0) return; + + /* + * If any scan type remained, we escape unsubscribe the event + * otherwise continue to unsubscribe the event + */ + if(ScanTypeRemove(scantype)) { + afb_req_success(request, NULL, NULL); + return; + } + } + + value = afb_req_value(request, "value"); if(value) { if(!strcasecmp(value, "media_added")) { afb_req_unsubscribe(request, media_added_event); @@ -88,7 +181,7 @@ static json_object *new_json_object_from_device(GList *list) jstring = json_object_new_string(item->path); json_object_object_add(jdict, "path", jstring); - jstring = json_object_new_string(lms_scan_types[item->type]); + jstring = json_object_new_string(item->type); json_object_object_add(jdict, "type", jstring); if (item->metadata.title) { @@ -131,10 +224,20 @@ static void media_results_get (afb_req_t request) { GList *list = NULL; json_object *jresp = NULL; + gint scan_type = 0; + + scan_type = get_scan_types(request); + if(scan_type < 0) return; ListLock(); - list = media_lightmediascanner_scan(list, NULL, LMS_AUDIO_SCAN); - list = media_lightmediascanner_scan(list, NULL, LMS_VIDEO_SCAN); + + if(scan_type & LMS_AUDIO_SCAN) + list = media_lightmediascanner_scan(list, NULL, LMS_AUDIO_SCAN); + if(scan_type & LMS_VIDEO_SCAN) + list = media_lightmediascanner_scan(list, NULL, LMS_VIDEO_SCAN); + if(scan_type & LMS_IMAGE_SCAN) + list = media_lightmediascanner_scan(list, NULL, LMS_IMAGE_SCAN); + if (list == NULL) { afb_req_fail(request, "failed", "media scan error"); ListUnlock(); |