From 399105d9618464f486947468aca2e76ef3eb918f Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 9 Mar 2016 10:04:44 +0100 Subject: [PATCH] Add event handler for m.call.candidates --- .gitignore | 1 + src/Makefile.am | 1 + src/matrix-event-call-candidates.vala | 174 ++++++++++++++++++++++++++ src/matrix-event-types.c | 3 + 4 files changed, 179 insertions(+) create mode 100644 src/matrix-event-call-candidates.vala diff --git a/.gitignore b/.gitignore index 6aefbc2..b1144f2 100644 --- a/.gitignore +++ b/.gitignore @@ -76,3 +76,4 @@ Makefile.in /src/matrix-event-room-redaction.c /src/matrix-event-room-third-party-invite.c /src/matrix-event-call-invite.c +/src/matrix-event-call-candidates.c diff --git a/src/Makefile.am b/src/Makefile.am index 2ac2cbb..59908b8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,6 +46,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-redaction.vala \ matrix-event-room-third-party-invite.vala \ matrix-event-call-invite.vala \ + matrix-event-call-candidates.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-event-call-candidates.vala b/src/matrix-event-call-candidates.vala new file mode 100644 index 0000000..dd9016b --- /dev/null +++ b/src/matrix-event-call-candidates.vala @@ -0,0 +1,174 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with matrix-glib-sdk. If not, see + * . + */ + +/** + * This event is sent by callers after sending an invite and by the + * callee after answering. Its purpose is to give the other party + * additional ICE candidates to try using to communicate. + */ +public class Matrix.Event.CallCandidates : Matrix.Event.Room { + public struct Candidate { + string? sdp_mid; /// The SDP media type this candidate is + /// intended for. + int? sdp_line_index; /// The index of the SDP 'm' line this + /// candidate is intended for. + string? candidate; /// The SDP 'a' line of the candidate. + } + + /** + * The ID of the call this event relates to. + */ + public string? call_id { get; set; default = null; } + + /** + * The list of candidates. + */ + public List? candidates { + get { + return _candidates; + } + + set { + _candidates = value.copy(); + } + + default = null; + } + + public int? version { get; set; default = null; } + + private List? _candidates; + + protected override void + from_json(Json.Node json_data) + throws Matrix.Error + { + var content_root = json_data.get_object() + .get_member("content").get_object(); + Json.Node? node; + + if ((node = content_root.get_member("call_id")) != null) { + _call_id = node.get_string(); + } else { + warning("content.call_id is missing from a m.call.candidates event"); + } + + if ((node = content_root.get_member("candidates")) != null) { + node.get_array().foreach_element((ary, idx, cand_node) => { + var cand_root = cand_node.get_object(); + var cand = Candidate(); + + if ((node = cand_root.get_member("sdpMid")) != null) { + cand.sdp_mid = node.get_string(); + } else { + warning("sdpMid is missing from a candidate of a m.call.candidates event"); + } + + if ((node = cand_root.get_member("sdpMLineIndex")) != null) { + cand.sdp_line_index = (int)node.get_int(); + } else { + warning("sdpMLineIndex is missing from a candidate of a m.call.candidates event"); + } + + if ((node = cand_root.get_member("candidate")) != null) { + cand.candidate = node.get_string(); + } else { + warning("candidate is missing from a candidate of a m.call.candidates event"); + } + + _candidates.prepend(cand); + }); + } else { + warning("content.candidates is missing from a m.call.candidates event"); + } + + if ((node = content_root.get_member("version")) != null) { + _version = (int)node.get_int(); + } else { + warning("content.version is missing from a m.call.candidates event"); + } + + base.from_json(json_data); + } + + protected override void + to_json(Json.Node json_data) + throws Matrix.Error + { + if (_call_id == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event without call_id"); + } + + if (_version == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event without version"); + } + + if ((_candidates == null) || (_candidates.length() < 1)) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event without candidates"); + } + + var content_root = json_data.get_object() + .get_member("content").get_object(); + + content_root.set_string_member("call_id", _call_id); + content_root.set_int_member("version", _version); + + var cands = new Json.Array(); + + foreach (var entry in _candidates) { + if (entry.sdp_mid == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event with a missing sdpMid for candidates"); + } + + if (entry.sdp_line_index == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event with a missing sdpMLineIndex for candidates"); + } + + if (entry.candidate == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event with a missing candidate for candidates"); + } + + var cand_obj = new Json.Object(); + var cand_node = new Json.Node(Json.NodeType.OBJECT); + cand_node.set_object(cand_obj); + + cand_obj.set_string_member("sdpMid", entry.sdp_mid); + cand_obj.set_int_member("sdpMLineIndex", entry.sdp_line_index); + cand_obj.set_string_member("candidate", entry.candidate); + + cands.add_element(cand_node); + } + + if (cands.get_length() < 1) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.call.candidates event with empty candidates list"); + } + + var cands_node = new Json.Node(Json.NodeType.ARRAY); + cands_node.set_array(cands); + content_root.set_member("candidates", cands_node); + + base.to_json(json_data); + } +} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index c2a9449..04a65ae 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -212,6 +212,9 @@ matrix_event_types_ctor(void) matrix_event_register_type("m.call.invite", MATRIX_EVENT_TYPE_CALL_INVITE, NULL); + matrix_event_register_type("m.call.candidates", + MATRIX_EVENT_TYPE_CALL_CANDIDATES, + NULL); } void