webrtc_source: Refactor audio/video mute functions 96/274296/6 accepted/tizen/unified/20220428.162650 submit/tizen/20220427.075236
authorSangchul Lee <sc11.lee@samsung.com>
Tue, 26 Apr 2022 04:03:37 +0000 (13:03 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Wed, 27 Apr 2022 02:49:02 +0000 (11:49 +0900)
__validate_audio_source_for_mute() and __validate_video_source_for_mute()
are added to reduce duplicate codes.

Null parameter checks are added.

[Version] 0.3.97
[Issue Type] Refactoring

Change-Id: I7bf5438d7da93f6b5b2727b537822b3189950879
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
packaging/capi-media-webrtc.spec
src/webrtc.c
src/webrtc_source.c

index 2e6ac64c0ba39aef043583c8e65bfc09c229fa74..fb159c9a7d0b7b0b62132a74881106ff97906a68 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-webrtc
 Summary:    A WebRTC library in Tizen Native API
-Version:    0.3.96
+Version:    0.3.97
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index e4eb730506495e86b4d9b90dd04000be2834563b..34ea1e41e3a6c5f16f4b5f9281ab0ebefbf1158b 100644 (file)
@@ -428,6 +428,7 @@ int webrtc_media_source_get_mute(webrtc_h webrtc, unsigned int source_id, webrtc
        webrtc_s *_webrtc = (webrtc_s *)webrtc;
 
        RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
+       RET_VAL_IF(muted == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "muted is NULL");
 
        g_mutex_lock(&_webrtc->mutex);
 
index 36da3e62b3d025aa15bd2aa9776640feb8122c09..f4318642ab9f254dbcbe3130546bdb69adf1c7e8 100644 (file)
@@ -3650,80 +3650,105 @@ static int __mute_videotestsrc(webrtc_gst_slot_s *source, bool mute)
        return WEBRTC_ERROR_NONE;
 }
 
-int _set_video_mute(webrtc_s *webrtc, unsigned int source_id, bool mute)
+static int __validate_video_source_for_mute(webrtc_s *webrtc, unsigned int source_id, webrtc_gst_slot_s **source)
 {
-       int ret = WEBRTC_ERROR_NONE;
-       webrtc_gst_slot_s *source = NULL;
+       webrtc_gst_slot_s *_source;
 
        RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
        RET_VAL_IF(source_id == 0, WEBRTC_ERROR_INVALID_PARAMETER, "source_id is 0");
-       RET_VAL_IF((source = _get_slot_by_id(webrtc->gst.source_slots, source_id)) == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "could not find source");
+       RET_VAL_IF((_source = _get_slot_by_id(webrtc->gst.source_slots, source_id)) == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "could not find source");
 
-       if (!(source->media_types & MEDIA_TYPE_VIDEO)) {
-               LOG_ERROR("invalid media_type for source[media_types:0x%x, id:%u]", source->media_types, source_id);
+       if (!(_source->media_types & MEDIA_TYPE_VIDEO)) {
+               LOG_ERROR("invalid media_type for source[media_types:0x%x, id:%u]", _source->media_types, source_id);
                return WEBRTC_ERROR_INVALID_PARAMETER;
        }
 
-       LOG_DEBUG("webrtc[%p] source_id[%u] mute[%d]", webrtc, source_id, mute);
+       if (_source->type != WEBRTC_MEDIA_SOURCE_TYPE_VIDEOTEST &&
+               _source->type != WEBRTC_MEDIA_SOURCE_TYPE_CAMERA &&
+               _source->type != WEBRTC_MEDIA_SOURCE_TYPE_SCREEN) {
+               LOG_ERROR_IF_REACHED("type(%d)", _source->type);
+               return WEBRTC_ERROR_INVALID_PARAMETER;
+       }
 
-       switch (source->type) {
-       case WEBRTC_MEDIA_SOURCE_TYPE_VIDEOTEST:
-               ret = __mute_videotestsrc(source, mute);
-               break;
+       if (source)
+               *source = _source;
 
-       case WEBRTC_MEDIA_SOURCE_TYPE_CAMERA:
-               ret = __mute_camerasrc(source, mute);
-               break;
+       return WEBRTC_ERROR_NONE;
+}
 
-       case WEBRTC_MEDIA_SOURCE_TYPE_SCREEN:
-               ret = __mute_videosrc(source, mute);
-               break;
+typedef int (*videosrc_mute_func)(webrtc_gst_slot_s *source, bool mute);
 
-       default:
-               LOG_ERROR_IF_REACHED("type(%d)", source->type);
-               return WEBRTC_ERROR_INVALID_PARAMETER;
-       }
+static videosrc_mute_func videosrc_mute_funcs[] = {
+       [WEBRTC_MEDIA_SOURCE_TYPE_VIDEOTEST] = __mute_videotestsrc,
+       [WEBRTC_MEDIA_SOURCE_TYPE_CAMERA] = __mute_camerasrc,
+       [WEBRTC_MEDIA_SOURCE_TYPE_SCREEN] = __mute_videosrc
+};
 
-       if (ret == WEBRTC_ERROR_NONE)
+int _set_video_mute(webrtc_s *webrtc, unsigned int source_id, bool mute)
+{
+       int ret;
+       webrtc_gst_slot_s *source;
+
+       RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
+       RET_VAL_IF(source_id == 0, WEBRTC_ERROR_INVALID_PARAMETER, "source_id is 0");
+       if ((ret = __validate_video_source_for_mute(webrtc, source_id, &source)) != WEBRTC_ERROR_NONE)
+               return ret;
+
+       LOG_DEBUG("webrtc[%p] source_id[%u] mute[%d]", webrtc, source_id, mute);
+
+       if ((ret = (videosrc_mute_funcs[source->type])(source, mute)) == WEBRTC_ERROR_NONE)
                source->video_muted = mute;
 
        return ret;
 }
 
 //LCOV_EXCL_START
-int _set_audio_mute(webrtc_s *webrtc, unsigned int source_id, bool mute)
+static int __validate_audio_source_for_mute(webrtc_s *webrtc, unsigned int source_id, webrtc_gst_slot_s **source)
 {
-       webrtc_gst_slot_s *source = NULL;
-       GstElement *volume = NULL;
+       webrtc_gst_slot_s *_source;
+       GstElement *volume;
 
        RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
        RET_VAL_IF(source_id == 0, WEBRTC_ERROR_INVALID_PARAMETER, "source_id is 0");
-       RET_VAL_IF((source = _get_slot_by_id(webrtc->gst.source_slots, source_id)) == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "could not find source");
+       RET_VAL_IF((_source = _get_slot_by_id(webrtc->gst.source_slots, source_id)) == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "could not find source");
 
-       if (!(source->media_types & MEDIA_TYPE_AUDIO)) {
-               LOG_ERROR("invalid media_type for source[media_types:0x%x, id:%u]", source->media_types, source_id);
+       if (!(_source->media_types & MEDIA_TYPE_AUDIO)) {
+               LOG_ERROR("invalid media_type for source[media_types:0x%x, id:%u]", _source->media_types, source_id);
                return WEBRTC_ERROR_INVALID_PARAMETER;
        }
 
-       switch (source->type) {
-       case WEBRTC_MEDIA_SOURCE_TYPE_AUDIOTEST:
-       case WEBRTC_MEDIA_SOURCE_TYPE_MIC:
-               volume = gst_bin_get_by_name(source->bin, ELEMENT_NAME_VOLUME);
-               RET_VAL_IF(volume == NULL, WEBRTC_ERROR_INVALID_OPERATION, "volume is NULL");
-
-               if (!g_object_class_find_property(G_OBJECT_GET_CLASS(volume), "mute")) {
-                       LOG_ERROR("there is no mute property");
-                       return WEBRTC_ERROR_INVALID_OPERATION;
-               }
+       if (_source->type != WEBRTC_MEDIA_SOURCE_TYPE_AUDIOTEST &&
+               _source->type != WEBRTC_MEDIA_SOURCE_TYPE_MIC) {
+               LOG_ERROR_IF_REACHED("type(%d)", _source->type);
+               return WEBRTC_ERROR_INVALID_PARAMETER;
+       }
 
-               g_object_set(G_OBJECT(volume), "mute", mute, NULL);
-               break;
+       volume = gst_bin_get_by_name(_source->bin, ELEMENT_NAME_VOLUME);
+       RET_VAL_IF(volume == NULL, WEBRTC_ERROR_INVALID_OPERATION, "volume is NULL");
 
-       default:
-               LOG_ERROR_IF_REACHED("type(%d)", source->type);
-               return WEBRTC_ERROR_INVALID_PARAMETER;
+       if (!g_object_class_find_property(G_OBJECT_GET_CLASS(volume), "mute")) {
+               LOG_ERROR("there is no mute property");
+               return WEBRTC_ERROR_INVALID_OPERATION;
        }
 
+       if (source)
+               *source = _source;
+
+       return WEBRTC_ERROR_NONE;
+}
+
+int _set_audio_mute(webrtc_s *webrtc, unsigned int source_id, bool mute)
+{
+       int ret;
+       webrtc_gst_slot_s *source;
+
+       RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
+       RET_VAL_IF(source_id == 0, WEBRTC_ERROR_INVALID_PARAMETER, "source_id is 0");
+       if ((ret = __validate_audio_source_for_mute(webrtc, source_id, &source)) != WEBRTC_ERROR_NONE)
+               return ret;
+
+       g_object_set(gst_bin_get_by_name(source->bin, ELEMENT_NAME_VOLUME), "mute", mute, NULL);
+
        LOG_INFO("webrtc[%p] source_id[%u] mute[%d]", webrtc, source_id, mute);
 
        return WEBRTC_ERROR_NONE;
@@ -3732,28 +3757,16 @@ int _set_audio_mute(webrtc_s *webrtc, unsigned int source_id, bool mute)
 
 int _get_video_mute(webrtc_s *webrtc, unsigned int source_id, bool *muted)
 {
-       const webrtc_gst_slot_s *source;
+       int ret;
+       webrtc_gst_slot_s *source = NULL;
 
        RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
        RET_VAL_IF(source_id == 0, WEBRTC_ERROR_INVALID_PARAMETER, "source_id is 0");
-       RET_VAL_IF((source = _get_slot_by_id(webrtc->gst.source_slots, source_id)) == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "could not find source");
-
-       if (!(source->media_types & MEDIA_TYPE_VIDEO)) {
-               LOG_ERROR("invalid media_type for source[media_types:0x%x, id:%u]", source->media_types, source_id);
-               return WEBRTC_ERROR_INVALID_PARAMETER;
-       }
+       RET_VAL_IF(muted == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "muted is NULL");
+       if ((ret = __validate_video_source_for_mute(webrtc, source_id, &source)) != WEBRTC_ERROR_NONE)
+               return ret;
 
-       switch (source->type) {
-       case WEBRTC_MEDIA_SOURCE_TYPE_VIDEOTEST:
-       case WEBRTC_MEDIA_SOURCE_TYPE_CAMERA:
-       case WEBRTC_MEDIA_SOURCE_TYPE_SCREEN:
-               *muted = source->video_muted;
-               break;
-
-       default:
-               LOG_ERROR_IF_REACHED("type(%d)", source->type);
-               return WEBRTC_ERROR_INVALID_PARAMETER;
-       }
+       *muted = source->video_muted;
 
        LOG_INFO("webrtc[%p] source_id[%u] muted[%d]", webrtc, source_id, *muted);
 
@@ -3763,38 +3776,21 @@ int _get_video_mute(webrtc_s *webrtc, unsigned int source_id, bool *muted)
 //LCOV_EXCL_START
 int _get_audio_mute(webrtc_s *webrtc, unsigned int source_id, bool *muted)
 {
-       const webrtc_gst_slot_s *source;
-       GstElement *volume = NULL;
+       int ret;
+       webrtc_gst_slot_s *source;
+       gboolean _muted;
 
        RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
        RET_VAL_IF(source_id == 0, WEBRTC_ERROR_INVALID_PARAMETER, "source_id is 0");
-       RET_VAL_IF((source = _get_slot_by_id(webrtc->gst.source_slots, source_id)) == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "could not find source");
-
-       if (!(source->media_types & MEDIA_TYPE_AUDIO)) {
-               LOG_ERROR("invalid media_type for source[media_types:0x%x, id:%u]", source->media_types, source_id);
-               return WEBRTC_ERROR_INVALID_PARAMETER;
-       }
-
-       switch (source->type) {
-       case WEBRTC_MEDIA_SOURCE_TYPE_AUDIOTEST:
-       case WEBRTC_MEDIA_SOURCE_TYPE_MIC:
-               volume = gst_bin_get_by_name(source->bin, ELEMENT_NAME_VOLUME);
-               RET_VAL_IF(volume == NULL, WEBRTC_ERROR_INVALID_OPERATION, "volume is NULL");
-
-               if (!g_object_class_find_property(G_OBJECT_GET_CLASS(volume), "mute")) {
-                       LOG_ERROR("there is no mute property");
-                       return WEBRTC_ERROR_INVALID_OPERATION;
-               }
+       RET_VAL_IF(muted == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "muted is NULL");
+       if ((ret = __validate_audio_source_for_mute(webrtc, source_id, &source)) != WEBRTC_ERROR_NONE)
+               return ret;
 
-               g_object_get(G_OBJECT(volume), "mute", muted, NULL);
-               break;
+       g_object_get(gst_bin_get_by_name(source->bin, ELEMENT_NAME_VOLUME), "mute", &_muted, NULL);
 
-       default:
-               LOG_ERROR_IF_REACHED("type(%d)", source->type);
-               return WEBRTC_ERROR_INVALID_PARAMETER;
-       }
+       *muted = (bool)_muted;
 
-       LOG_DEBUG("webrtc[%p] source_id[%u] muted[%d]", webrtc, source_id, *muted);
+       LOG_INFO("webrtc[%p] source_id[%u] muted[%d]", webrtc, source_id, *muted);
 
        return WEBRTC_ERROR_NONE;
 }