This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jingleMedia.lua
317 lines (278 loc) · 10.6 KB
/
jingleMedia.lua
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package.path = "./?.lua;./sdp-jingle-table/src/?.lua;" .. package.path;
local Jingle = require("basejingle");
local xmlns_jingle = "urn:xmpp:jingle:1";
local xmlns_jingle_rtp_info = "urn:xmpp:jingle:apps:rtp:info:1";
local jingletolua = require("jingletolua");
jingletolua.init();
local helpers = require "helpers"
local utils = require "utils"
local JingleMedia = Jingle:new();
function JingleMedia:onSessionAccept(req)
self.client:send(verse.reply(req));
local jingle_tag = req:get_child('jingle', xmlns_jingle);
local sdp, intermediate = jingletolua.toIncomingAnswerSDP(jingle_tag);
self.remote_state = intermediate;
self.isPending = false;
self.client:event("jingle/session-accept-sdp", sdp, self.peer, self.sid);
return true;
end
function JingleMedia:extractMSIDFromJingle(jingle)
-- May change due to Firefox and https://github.com/otalk/sdp-jingle-json/issues/7
local msid = ""
for content in helpers.childtags(jingle, "content") do
-- We don't need to check the description from a 'data' content, which would have an xmlns of 'http://talky.io/ns/datachannel'
local description = content:get_child("description", "urn:xmpp:jingle:apps:rtp:1")
if description then
for source in helpers.childtags(description, "source") do
for parameter in helpers.childtags(source, "parameter") do
if parameter.attr.name and parameter.attr.name == "msid" then
local value = parameter.attr.value
local parts = utils.split(value, " ")
if #parts > 0 then
msid = parts[1]
end
end
end
end
end
end
return msid
end
function JingleMedia:onSessionInitiate(req)
self.client:send(verse.reply(req));
local jingle_tag = req:get_child('jingle', xmlns_jingle);
local msid = self:extractMSIDFromJingle(jingle_tag)
local sdp, intermediate = jingletolua.toIncomingOfferSDP(jingle_tag);
self.remote_state = intermediate;
self.isPending = true;
self.client:event("jingle/session-initiate-sdp", sdp, self.peer, self.sid, msid);
return true;
end
function JingleMedia:acceptSDP(sdp)
--print("acceptSDP: " .. sdp)
local jingle, intermediate = jingletolua.toOutgoingAnswerJingle(sdp);
--print("acceptSDP jingle:")
--print(jingle)
self.local_state = intermediate;
self.isPending = false;
jingle.attr.initiator = self.peer;
jingle.attr.responder = self.client.full;
jingle.attr.action = 'session-accept';
jingle.attr.sid = self.sid;
local iq = self.verse.iq({
to = self.peer,
from = self.client.full,
type = "set",
});
iq:add_child(jingle);
self.client:send(iq);
end
function JingleMedia:initiateSDP(sdp)
local jingle, intermediate = jingletolua.toOutgoingOfferJingle(sdp);
self.local_state = intermediate;
self.isPending = true;
jingle.attr.responder = self.peer;
jingle.attr.initiator = self.client.full;
jingle.attr.action = 'session-initiate';
jingle.attr.sid = self.sid;
local iq = self.verse.iq({
to = self.peer,
from = self.client.full,
type = "set",
});
iq:add_child(jingle);
self.client:send(iq);
end
function JingleMedia:removeSourceSDP(sdp)
local iq, jingle = self:createSetSDP('source-remove', sdp);
self.client:send(iq);
end
function JingleMedia:addSourceSDP(sdp)
local iq, jingle = self.createSetSDP('source-add', sdp);
self.client:send(iq);
end
function JingleMedia:addCandidate(mid, mline, candidate)
--print("Yay ICE!")
candidate = "a=" .. candidate
local candidateTable = jingletolua.toCandidateTable(candidate)
local audio = { name = mid,
transport = {
candidates = {
candidateTable
}
}
}
local jingleTable = { contents = { audio }}
local jingle = jingletolua.toJingle(jingleTable, 'initiator');
jingle.attr.initiator = self.peer;
jingle.attr.responder = self.client.full;
jingle.attr.action = 'transport-info';
jingle.attr.sid = self.sid;
local iq = self.verse.iq({
to = self.peer,
from = self.client.full,
type = "set",
});
iq:add_child(jingle);
self.client:send(iq);
end
function JingleMedia:onTransportInfo(req)
self.client:send(verse.reply(req));
--TODO something something something
local jingle_tag = req:get_child('jingle', xmlns_jingle);
---[[
local sdp, jingleTable = jingletolua.toSDP(jingle_tag)
--print("onTransportInfo:\n" .. sdp)
for _, content in pairs(jingleTable.contents) do
if (content.transport) then
for _, candidate in pairs(content.transport.candidates) do
-- toSDP
local sdp = jingletolua.toCandidateSDP(candidate)
-- Drop a=
sdp = string.sub(sdp, 3)
-- emit mid, mline, sdp
local mline = 0
for i, oldContent in ipairs(self.remote_state.contents) do
if oldContent.name == content.name then
mline = i - 1
break
end
end
self.client:event("jingle/transport-candidate", content.name, mline, sdp, self.peer, self.sid);
end
end
end
--]]
return true
end
function JingleMedia:onSourceAdd(req)
--print("onSourceAdd")
self.client:send(verse.reply(req));
local jingle_tag = req:get_child('jingle', xmlns_jingle);
local changesTable = jingletolua.jingleToTable(jingle_tag);
local sourceAdded = false
for i, content in ipairs(self.remote_state.contents) do
--print("remote_state contents isn't empty in Lua land")
local desc = content.description
local ssrcs = desc.sources or {}
local groups = desc.sourceGroups or {}
for _, newContent in ipairs(changesTable.contents) do
--print("there are new contents in Lua land")
--print("content stuff: " .. newContent.creator .. ", " .. newContent.senders)
--print("name: " .. content.name .. " - " .. newContent.name)
if (content.name == newContent.name) then
--print("names match in Lua land")
local newContentsDesc = newContent.description
local newSSRCs = newContentsDesc.sources or {}
for _, newSSRC in pairs(newSSRCs) do
--print("A source was added in Lua land")
sourceAdded = true
table.insert(ssrcs, newSSRC)
end
local newGroups = newContentsDesc.sourceGroups or {}
for _, newGroup in pairs(newGroups) do
--print("A source group was added in Lua land")
table.insert(groups, newGroup)
end
self.remote_state.contents[i].description.sources = ssrcs
self.remote_state.contents[i].description.sourceGroups = groups
end
end
end
if sourceAdded then
local sdp = jingletolua.toIncomingSDPOffer(self.remote_state);
self.client:event("jingle/source-add-sdp", sdp, self.peer, self.sid);
end
return true;
end
function JingleMedia:onSourceRemove(req)
self.client:send(verse.reply(req));
local jingle_tag = req:get_child('jingle', xmlns_jingle);
local changesTable = jingletolua.jingleToTable(jingle_tag);
local sourceRemoved = false
for i, content in ipairs(self.remote_state.contents) do
local desc = content.description
local ssrcs = desc.sources or {}
local groups = desc.sourceGroups or {}
for _, newContent in ipairs(changesTable.contents) do
if (content.name == newContent.name) then
local newContentsDesc = newContent.description
local newSSRCs = newContentsDesc.sources or {}
local newGroups = newContentsDesc.sourceGroups or {}
for _, newSSRC in ipairs(newSSRCs) do
for j=#ssrcs,1,-1 do
if ssrcs[j].ssrc == newSSRC.ssrc then
sourceRemoved = true
table.remove(ssrcs, j)
end
end
end
for l, newGroup in ipairs(newGroups) do
for m=#groups,1,-1 do
local group = groups[m]
local sources = groups.sources or {}
local newSources = newGroups.sources or {}
if newGroup.semantics == group.semantics and #newSources == #sources then
local same = true;
for n, newSource in ipairs(newSources) do
if newSource ~= sources[n] then
same = false
end
end
if (same) then
table.remove(groups, m)
end
end
end
end
end
self.remote_state.contents[i].description.sources = ssrcs
self.remote_state.contents[i].description.sourceGroups = groups
end
end
if sourceRemoved then
local sdp = jingletolua.toIncomingSDPOffer(self.remote_state)
self.client:event("jingle/source-remove-sdp", sdp, self.peer, self.sid);
end
return true
end
function JingleMedia:sendMediaInfo(tagname, attr)
--<hold xmlns='urn:xmpp:jingle:apps:rtp:info:1'/>
local iq, jingle = self:createInfo();
local info = self.verse.stanza(tagname, {xmlns = xmlns_jingle_rtp_info});
for attr_name, attr_value in pairs(attr) do
info.attr[attr_name] = attr_value;
end
jingle:add_child(info);
self.client:send(iq);
end
function JingleMedia:onSessionInfo(req)
self.client:send(verse.reply(req));
local jingle = req:get_child('jingle', xmlns_jingle);
for child in jingle:children() do
if child.attr.xmlns == xmlns_jingle_rtp_info then
local name = child.attr.name and child.attr.name or ""
self.client:event("jingle/media-info/"..child.name, name, self.peer, self.sid);
end
end
return true;
end
function JingleMedia:hold()
self:sendMediaInfo('hold');
end
function JingleMedia:resume()
self:sendMediaInfo('resume');
end
function JingleMedia:active()
self:sendMediaInfo('resume');
end
function JingleMedia:ring()
self:sendMediaInfo('ring');
end
function JingleMedia:mute(creator, media)
self:sendMediaInfo('mute', {name = media, creator = creator});
end
function JingleMedia:unmute(creator, media)
self:sendMediaInfo('unmute', {name = media, creator = creator});
end
return JingleMedia;