summaryrefslogtreecommitdiff
path: root/modules/webrtc/WebRtcMessage.cc
blob: 62f31623416273f003a87c7e845288a5a03bb70d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "WebRtcMessage.h"

#include <json-glib/json-glib.h>

#include "aitt_internal.h"

namespace AittWebRTCNamespace {

WebRtcMessage::Type WebRtcMessage::getMessageType(const std::string &message)
{
    WebRtcMessage::Type type = WebRtcMessage::Type::UNKNOWN;
    JsonParser *parser = json_parser_new();
    if (!json_parser_load_from_data(parser, message.c_str(), -1, NULL)) {
        DBG("Unknown message '%s', ignoring", message.c_str());
        g_object_unref(parser);
        return type;
    }

    JsonNode *root = json_parser_get_root(parser);
    if (!JSON_NODE_HOLDS_OBJECT(root)) {
        DBG("Unknown json message '%s', ignoring", message.c_str());
        g_object_unref(parser);
        return type;
    }

    JsonObject *object = json_node_get_object(root);
    /* Check type of JSON message */

    if (json_object_has_member(object, "sdp")) {
        type = WebRtcMessage::Type::SDP;
    } else if (json_object_has_member(object, "ice")) {
        type = WebRtcMessage::Type::ICE;
    } else {
        DBG("%s:UNKNOWN", __func__);
    }

    g_object_unref(parser);
    return type;
}

bool WebRtcMessage::IsValidStreamInfo(const std::vector<uint8_t> &message)
{
    if (!flexbuffers::GetRoot(message).IsMap())
        return false;

    auto stream_info = flexbuffers::GetRoot(message).AsMap();

    return IsValidStream(stream_info);
}

bool WebRtcMessage::IsValidStream(const flexbuffers::Map &info)
{
    bool id_exist{info["id"].IsString()};
    bool peer_id_exist{info["peer_id"].IsString()};
    bool sdp_exist{info["sdp"].IsString()};
    bool ice_candidates_exist{info["ice_candidates"].IsVector()};

    return id_exist && peer_id_exist && sdp_exist && ice_candidates_exist;
}

}  // namespace AittWebRTCNamespace