| 1 |
using Gee; |
| 2 |
using Xmpp; |
| 3 |
using Xmpp.Xep; |
| 4 |
|
| 5 |
public class Xmpp.Xep.JingleRtp.Parameters : Jingle.ContentParameters, Object { |
| 6 |
|
| 7 |
public signal void stream_created(Stream stream); |
| 8 |
public signal void connection_ready(); |
| 9 |
|
| 10 |
public string media { get; private set; } |
| 11 |
public string? ssrc { get; private set; } |
| 12 |
public bool rtcp_mux { get; private set; } |
| 13 |
|
| 14 |
public string? bandwidth { get; private set; } |
| 15 |
public string? bandwidth_type { get; private set; } |
| 16 |
|
| 17 |
public bool encryption_required { get; private set; default = false; } |
| 18 |
public PayloadType? agreed_payload_type { get; private set; } |
| 19 |
public Gee.List<PayloadType> payload_types = new ArrayList<PayloadType>(PayloadType.equals_func); |
| 20 |
public Gee.List<HeaderExtension> header_extensions = new ArrayList<HeaderExtension>(); |
| 21 |
public Gee.List<Crypto> remote_cryptos = new ArrayList<Crypto>(); |
| 22 |
public Crypto? local_crypto = null; |
| 23 |
public Crypto? remote_crypto = null; |
| 24 |
|
| 25 |
public bool rtp_ready { get; private set; default=false; } |
| 26 |
public bool rtcp_ready { get; private set; default=false; } |
| 27 |
|
| 28 |
public weak Stream? stream { get; private set; } |
| 29 |
|
| 30 |
private Module parent; |
| 31 |
|
| 32 |
public Parameters(Module parent, |
| 33 |
string media, Gee.List<PayloadType> payload_types, |
| 34 |
string? ssrc = null, bool rtcp_mux = false, |
| 35 |
string? bandwidth = null, string? bandwidth_type = null, |
| 36 |
bool encryption_required = false, Crypto? local_crypto = null |
| 37 |
) { |
| 38 |
this.parent = parent; |
| 39 |
this.media = media; |
| 40 |
this.ssrc = ssrc; |
| 41 |
this.rtcp_mux = true; |
| 42 |
this.bandwidth = bandwidth; |
| 43 |
this.bandwidth_type = bandwidth_type; |
| 44 |
this.encryption_required = encryption_required; |
| 45 |
this.payload_types = payload_types; |
| 46 |
this.local_crypto = local_crypto; |
| 47 |
} |
| 48 |
|
| 49 |
public Parameters.from_node(Module parent, StanzaNode node) throws Jingle.IqError { |
| 50 |
this.parent = parent; |
| 51 |
this.media = node.get_attribute("media"); |
| 52 |
this.ssrc = node.get_attribute("ssrc"); |
| 53 |
this.rtcp_mux = node.get_subnode("rtcp-mux") != null; |
| 54 |
StanzaNode? encryption = node.get_subnode("encryption"); |
| 55 |
if (encryption != null) { |
| 56 |
this.encryption_required = encryption.get_attribute_bool("required", this.encryption_required); |
| 57 |
foreach (StanzaNode crypto in encryption.get_subnodes("crypto")) { |
| 58 |
this.remote_cryptos.add(Crypto.parse(crypto)); |
| 59 |
} |
| 60 |
} |
| 61 |
foreach (StanzaNode payloadType in node.get_subnodes(PayloadType.NAME)) { |
| 62 |
this.payload_types.add(PayloadType.parse(payloadType)); |
| 63 |
} |
| 64 |
foreach (StanzaNode subnode in node.get_subnodes(HeaderExtension.NAME, HeaderExtension.NS_URI)) { |
| 65 |
this.header_extensions.add(HeaderExtension.parse(subnode)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public async void handle_proposed_content(XmppStream stream, Jingle.Session session, Jingle.Content content) { |
| 70 |
agreed_payload_type = yield parent.pick_payload_type(media, payload_types); |
| 71 |
if (agreed_payload_type == null) { |
| 72 |
debug("no usable payload type"); |
| 73 |
content.reject(); |
| 74 |
return; |
| 75 |
} |
| 76 |
// Drop unsupported header extensions |
| 77 |
var iter = header_extensions.iterator(); |
| 78 |
while(iter.next()) { |
| 79 |
if (!parent.is_header_extension_supported(media, iter.@get())) iter.remove(); |
| 80 |
} |
| 81 |
remote_crypto = parent.pick_remote_crypto(remote_cryptos); |
| 82 |
if (local_crypto == null && remote_crypto != null) { |
| 83 |
local_crypto = parent.pick_local_crypto(remote_crypto); |
| 84 |
} |
| 85 |
if ((local_crypto == null || remote_crypto == null) && encryption_required) { |
| 86 |
debug("no usable encryption, but encryption required"); |
| 87 |
content.reject(); |
| 88 |
return; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public void accept(XmppStream stream, Jingle.Session session, Jingle.Content content) { |
| 93 |
debug("[%p] Jingle RTP on_accept", stream); |
| 94 |
|
| 95 |
Jingle.DatagramConnection rtp_datagram = (Jingle.DatagramConnection) content.get_transport_connection(1); |
| 96 |
Jingle.DatagramConnection rtcp_datagram = (Jingle.DatagramConnection) content.get_transport_connection(2); |
| 97 |
|
| 98 |
ulong rtcp_ready_handler_id = 0; |
| 99 |
rtcp_ready_handler_id = rtcp_datagram.notify["ready"].connect((rtcp_datagram, _) => { |
| 100 |
this.stream.on_rtcp_ready(); |
| 101 |
this.rtcp_ready = true; |
| 102 |
|
| 103 |
((Jingle.DatagramConnection)rtcp_datagram).disconnect(rtcp_ready_handler_id); |
| 104 |
rtcp_ready_handler_id = 0; |
| 105 |
}); |
| 106 |
|
| 107 |
ulong rtp_ready_handler_id = 0; |
| 108 |
rtp_ready_handler_id = rtp_datagram.notify["ready"].connect((rtp_datagram, _) => { |
| 109 |
this.stream.on_rtp_ready(); |
| 110 |
this.rtp_ready = true; |
| 111 |
if (rtcp_mux) { |
| 112 |
this.stream.on_rtcp_ready(); |
| 113 |
this.rtcp_ready = true; |
| 114 |
} |
| 115 |
connection_ready(); |
| 116 |
|
| 117 |
((Jingle.DatagramConnection)rtp_datagram).disconnect(rtp_ready_handler_id); |
| 118 |
rtp_ready_handler_id = 0; |
| 119 |
}); |
| 120 |
|
| 121 |
ulong session_state_handler_id = 0; |
| 122 |
session_state_handler_id = session.notify["state"].connect((obj, _) => { |
| 123 |
Jingle.Session session2 = (Jingle.Session) obj; |
| 124 |
if (session2.state == Jingle.Session.State.ENDED) { |
| 125 |
if (rtcp_ready_handler_id != 0) rtcp_datagram.disconnect(rtcp_ready_handler_id); |
| 126 |
if (rtp_ready_handler_id != 0) rtp_datagram.disconnect(rtp_ready_handler_id); |
| 127 |
if (session_state_handler_id != 0) { |
| 128 |
session2.disconnect(session_state_handler_id); |
| 129 |
} |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
if (remote_crypto == null || local_crypto == null) { |
| 134 |
if (encryption_required) { |
| 135 |
warning("Encryption required but not provided in both directions"); |
| 136 |
return; |
| 137 |
} |
| 138 |
remote_crypto = null; |
| 139 |
local_crypto = null; |
| 140 |
} |
| 141 |
if (remote_crypto != null && local_crypto != null) { |
| 142 |
var content_encryption = new Xmpp.Xep.Jingle.ContentEncryption("", "SRTP", local_crypto.key, remote_crypto.key); |
| 143 |
content.encryptions[content_encryption.encryption_name] = content_encryption; |
| 144 |
} |
| 145 |
|
| 146 |
this.stream = parent.create_stream(content); |
| 147 |
this.stream.weak_ref(() => this.stream = null); |
| 148 |
rtp_datagram.datagram_received.connect(this.stream.on_recv_rtp_data); |
| 149 |
rtcp_datagram.datagram_received.connect(this.stream.on_recv_rtcp_data); |
| 150 |
this.stream.on_send_rtp_data.connect(rtp_datagram.send_datagram); |
| 151 |
this.stream.on_send_rtcp_data.connect(rtcp_datagram.send_datagram); |
| 152 |
this.stream_created(this.stream); |
| 153 |
this.stream.create(); |
| 154 |
} |
| 155 |
|
| 156 |
public void handle_accept(XmppStream stream, Jingle.Session session, Jingle.Content content, StanzaNode description_node) { |
| 157 |
rtcp_mux = description_node.get_subnode("rtcp-mux") != null; |
| 158 |
Gee.List<StanzaNode> payload_type_nodes = description_node.get_subnodes("payload-type"); |
| 159 |
if (payload_type_nodes.size == 0) { |
| 160 |
warning("Counterpart didn't include any payload types"); |
| 161 |
return; |
| 162 |
} |
| 163 |
PayloadType preferred_payload_type = PayloadType.parse(payload_type_nodes[0]); |
| 164 |
if (!payload_types.contains(preferred_payload_type)) { |
| 165 |
warning("Counterpart's preferred content type doesn't match any of our sent ones"); |
| 166 |
} |
| 167 |
agreed_payload_type = preferred_payload_type; |
| 168 |
|
| 169 |
Gee.List<StanzaNode> crypto_nodes = description_node.get_deep_subnodes("encryption", "crypto"); |
| 170 |
if (crypto_nodes.size == 0) { |
| 171 |
debug("Counterpart didn't include any cryptos"); |
| 172 |
if (encryption_required) { |
| 173 |
return; |
| 174 |
} |
| 175 |
} else { |
| 176 |
Crypto preferred_crypto = Crypto.parse(crypto_nodes[0]); |
| 177 |
if (local_crypto.crypto_suite != preferred_crypto.crypto_suite) { |
| 178 |
warning("Counterpart's crypto suite doesn't match any of our sent ones"); |
| 179 |
} |
| 180 |
remote_crypto = preferred_crypto; |
| 181 |
} |
| 182 |
|
| 183 |
accept(stream, session, content); |
| 184 |
} |
| 185 |
|
| 186 |
public void terminate(bool we_terminated, string? reason_name, string? reason_text) { |
| 187 |
if (stream != null) parent.close_stream(stream); |
| 188 |
} |
| 189 |
|
| 190 |
public StanzaNode get_description_node() { |
| 191 |
StanzaNode ret = new StanzaNode.build("description", NS_URI) |
| 192 |
.add_self_xmlns() |
| 193 |
.put_attribute("media", media); |
| 194 |
|
| 195 |
if (agreed_payload_type != null) { |
| 196 |
ret.put_node(agreed_payload_type.to_xml()); |
| 197 |
} else { |
| 198 |
foreach (PayloadType payload_type in payload_types) { |
| 199 |
ret.put_node(payload_type.to_xml()); |
| 200 |
} |
| 201 |
} |
| 202 |
foreach (HeaderExtension ext in header_extensions) { |
| 203 |
ret.put_node(ext.to_xml()); |
| 204 |
} |
| 205 |
if (local_crypto != null) { |
| 206 |
ret.put_node(new StanzaNode.build("encryption", NS_URI) |
| 207 |
.put_node(local_crypto.to_xml())); |
| 208 |
} |
| 209 |
if (rtcp_mux) { |
| 210 |
ret.put_node(new StanzaNode.build("rtcp-mux", NS_URI)); |
| 211 |
} |
| 212 |
return ret; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
public class Xmpp.Xep.JingleRtp.HeaderExtension { |
| 217 |
public const string NS_URI = "urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"; |
| 218 |
public const string NAME = "rtp-hdrext"; |
| 219 |
|
| 220 |
public uint8 id { get; private set; } |
| 221 |
public string uri { get; private set; } |
| 222 |
|
| 223 |
public HeaderExtension(uint8 id, string uri) { |
| 224 |
this.id = id; |
| 225 |
this.uri = uri; |
| 226 |
} |
| 227 |
|
| 228 |
public static HeaderExtension parse(StanzaNode node) { |
| 229 |
return new HeaderExtension((uint8) node.get_attribute_int("id"), node.get_attribute("uri")); |
| 230 |
} |
| 231 |
|
| 232 |
public StanzaNode to_xml() { |
| 233 |
return new StanzaNode.build(NAME, NS_URI) |
| 234 |
.add_self_xmlns() |
| 235 |
.put_attribute("id", id.to_string()) |
| 236 |
.put_attribute("uri", uri); |
| 237 |
} |
| 238 |
} |