This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathsipml5.js
267 lines (232 loc) · 10.1 KB
/
sipml5.js
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
/*
* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>
* License: BSD
* This file is part of Open Source sipML5 solution <http://www.sipml5.org>
*/
/** @mainpage Foreword
<p>This is the world's first open source HTML5 SIP client entirely written in javascript for integration in social networks (FaceBook, Twitter, Google+), online games, e-commerce sites... No extension, plugin or gateway is needed.<br />
The client can be used to connect to any SIP or IMS network from your preferred browser to make and receive audio/video calls and instant messages.<br />
The protocol parsers (SIP, SDP...) are highly optimized using <a href="http://www.complang.org/ragel/">Ragel</a> lookup tables and is suitable for embedded systems with limited memory and low computing power.
</p>
@image html architecture.png "sipML5 solution architecture"
<br />
This document has been written by us (<a href="http://www.doubango.org">Doubango Telecom</a>) to help developers to quickly create innovative multimedia applications
for the Windows, MAC OS X, Linux, iOS and Android platforms. If you are a developer and is looking for the best way to develop a NGN (VoIP, Messaging, Video Conferencing, ...) or rich application for these platforms
then your are at the right place. <br />
If you want to get help or have some feedbacks then please visit our website: <a href="http://code.google.com/p/sipml5/">http://code.google.com/p/sipml5/</a> or subscribe to our <a href="http://groups.google.com/group/doubango/"> developer mailing list</a>.
<h3>Quick start </h3>
- @ref page_create_sip_stack
- @ref page_login
- @ref page_sms_like
- @ref page_avcalls
- Presence
<br />
<h3>Checking out the source code</h3>
<br />
To check out the source code of the NGN library you will need a SVN client.<br />
Use this command to anonymously check out the last project source:
@code
svn checkout http://sipml5.googlecode.com/svn sipml5
@endcode
The source code of the library is under:
@code
sipml5/trunk/src
@endcode
the main page for testing is at:
@code
sipml5/trunk/src/call.htm
@endcode
you don't need to change anything in the code to start testing. Just drag and drop <b>call.htm</b> to your web browser and start enjoying.
@page Conding convention
As you probably now javascript is not a strongly typed language and it's bit hard to get the type of an object while programing. To make your life easier we adopted a convention in
the naming of the variables and functions.
<h2>Prefixes</h2>
- <b>e_</b>: Enumeration @code var e_state = states_e.START; @endcode
- <b>f_</b>: Floating point number @code f_density = 0.5; @endcode
- <b>i_</b>: Integer number @code var i_index = 1; @endcode
- <b>o_</b>: Object created using new() @code var o_stack = new tsip_stack_t(...); @endcode
- <b>ao_</b>: Array of Objects @code var ao_stacks = new Array(); @endcode
- <b>on_</b>: Callback function pointer @code o_stack.on_event = function(e){}; @endcode
- <b>s_</b>: String @code var s_uri = 'sip:[email protected]'; @endcode
- <b>as_</b>: Array of strings @code var ao_uris = new Array();
Please note that static variables are prefixes by "__" followed by the type e.g. @code var __i_timeout = 1800; @endcode
<h2>Sufixes</h2>
- <b>_t</b>: An object type which could be created using 'new' keyword.
- <b>_e</b>: Enumeration type.
- <b>_f</b>: Variable defining a function (like C/C++ function pointers or CSharp delegates)
*/
/**
@page page_create_sip_stack Create SIP Stack
In order to make and receive calls you need to create a SIP stack object. This page explain how to create such object and how to register for events.
@code
var s_realm = "doubango.org"; // your domain name
var s_impi = "alice"; // your authentication name
var s_impu = "sip:[email protected]"; // your SIP public address where to receive calls
var s_webrtc2sip_host = "sipml5.org"; // Proxy host or IP address of the WebRTC gateway used to convert WebSocket transport to UDP/TCP/TLS. You can use ours or replace it by yours if you're hosting your own Gateway.
var i_webrtc2sip_port = 4062; // WebRTC gateway port
// create the SIP stack
o_stack = new tsip_stack(s_realm, s_impi, s_impu, s_webrtc2sip_host, i_webrtc2sip_port,
tsip_stack.prototype.SetPassword("mysecret"),
tsip_stack.prototype.SetDisplayName("Alice"),
tsip_stack.prototype.SetHeader('User-Agent', 'MyUserAgent'),
tsip_stack.prototype.SetHeader('Organization', 'MyOrganization'));
// listen for events
o_stack.on_event_stack = function(evt){
// your code here
};
o_stack.on_event_dialog = function(evt){
// your code here
};
o_stack.on_event_invite = function(evt){
// your code here
};
o_stack.on_event_message = function(evt){
// your code here
};
// start the stack
var i_ret = o_stack.stop();
if(i_ret == 0){
tsk_utils_log_info('Stack starting');
// not that success case doesn't mean that the stack started. You have to listen for @b on_event_stack to get notified when the stack finish starting.
}
else{
tsk_utils_log_error('Failed to start the stack');
}
@endcode
<h4>Adding SIP outbound proxy</h4>
<br />
The SIP destination address is generated like this:
- The gateway performs a DNS <i>NAPTR</i> then <i>SRV</i> on the domain name ('doubango.org' in the above case) and try all records from the highest to the lowest priority
- If all records fails then, it tries to send the message directly to the domain name (DNS <i>A</i> and <i>AAAA</i>)
If you want to send the SIP messages to a fixed IP address or host name and bypass the process described above then, you can add an outbound proxy like this:
@code
o_stack.set(
tsip_stack.prototype.SetProxyOutBound('192.168.0.12', 5060, tsip_transport_type_e.UDP)
);
@endcode
<br />
Next Step: @ref page_login
*/
/**
@page page_login LogIn / LogOut
Before connecting to your SIP network (SIP REGISTER) you have to create a SIP stack as explained in @ref page_create_sip_stack section.
<h4>LogIn</h4>
@code
// Sends SIP REGISTER for connection as soon as the stack finish starting
var o_session_reg = null;
o_stack.on_event_stack = function(evt) {
tsk_utils_log_info(evt.s_phrase);
switch (evt.i_code) {
case tsip_event_code_e.STACK_STARTED:
{
// creates the session
o_session_reg = new tsip_session_register(o_stack,
tsip_session.prototype.SetExpires(200),
tsip_session.prototype.SetCaps("+g.oma.sip-im"));
// sends SIP REGISTER request
o_session_reg.register();
break;
}
case tsip_event_code_e.STACK_STOPPING:
case tsip_event_code_e.STACK_STOPPED:
case tsip_event_code_e.STACK_STARTING:
case tsip_event_code_e.STACK_FAILED_TO_START:
case tsip_event_code_e.STACK_FAILED_TO_STOP:
default:
{
break;
}
}
};
@endcode
<h4>LogOut</h4>
To disconnect from the SIP server:
@code
o_session_reg.unregister(); // not recommended
// or
o_stack.stop(); // recommended: hangup all calls, unpublish, unsubscribe... then unregister
@endcode
Disconnection events (Success, Failure...) will be reported to @ref tsip_stack::on_event_dialog.
*/
/**
@page page_sms_like Instant messaging (SMS-like)
Before sending or receiving SMS messages (SIP @b MESSAGE) you have to create a SIP stack as explained in @ref page_create_sip_stack section. <br />
To listen to the messaging events:
@code
o_stack.on_event_message = function (evt) {
tsk_utils_log_info(evt.s_phrase);
switch (evt.e_message_type) {
case tsip_event_message_type_e.I_MESSAGE:
{
evt.get_session().accept();
tsk_utils_log_info("Incoming message. content=%s from=" + evt.get_message().get_content_as_string(), evt.get_session().o_uri_from);
break;
}
case tsip_event_message_type_e.AO_MESSAGE:
{
if (evt.i_code >= 200 && evt.i_code <= 299) {
tsk_utils_log_info("sent");
}
else if (evt.i_code >= 300) {
tsk_utils_log_info("not sent (%d)", evt.i_code);
}
break;
}
}
}
@endcode
To send a message to @b 006:
@code
// creates an IM session
var o_session_im = new tsip_session_message(o_stack,
tsip_session.prototype.SetToStr("006"),
tsip_session.prototype.SetHeader("What", "IM"));
// sends a message
o_session_im.send(new String("Pêche à la moule"), "text/plain; charset=utf8");
@endcode
*/
/**
@page page_avcalls Audio/Video calls
Before making audio/video calls (SIP @b INVITE) you have to create a SIP stack as explained in @ref page_create_sip_stack section. <br />
To get notified for incoming calls and any event related to the @b INVITE dialog you should listen to @ref tsip_stack::on_event_invite callback.
To call @b bob:
@code
// create the session
o_session = new tsip_session_invite(oSipStack,
tsip_session.prototype.SetToStr('bob'),
tsip_session.prototype.SetHeader('MyHeader', 'My Header Value'),
);
// make the call
if (o_session.call(tmedia_type_e.AUDIO_VIDEO) != 0) {
tsk_utils_log_error('Failed to make call');
}
@endcode
To hangup the call:
@code
o_session.hangup();
@endcode
To transfer the call to @b alice:
@code
if (o_session.transfer('alice') != 0) {
tsk_utils_log_error('Failed to transfer the call');
}
@endcode
To hold the call:
@code
if(o_session.hold() != 0){
tsk_utils_log_error('Failed to hold the call');
}
@endcode
To resume the call:
@code
if(o_session.resume() != 0){
tsk_utils_log_error('Failed to resume the call');
}
@endcode
To send DTMF digit @b '#':
@code
if(o_session.dtmf('#') != 0){
tsk_utils_log_error('Failed to send DTMF');
}
@endcode
*/