-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
113 lines (85 loc) · 3.15 KB
/
index.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
const dgram = require("dgram")
class RtpSession {
constructor(opts) {
this._info = {
payload_type: opts.payload_type ? opts.payload_type : 0,
ssrc: opts.ssrc ? opts.ssrc : 0x1234678,
seq_num: 1,
time_stamp: 160,
}
this._preserve_headers = opts.preserve_headers || false
//console.log(this._info)
var version = 2
var padding = 0
var extension = 0
var csrc_count = 0
var marker = 0
this._hdr = Buffer.alloc(12)
this._hdr[0] = (version << 6 | padding << 5 | extension << 4 | csrc_count)
this._hdr[1] = (marker << 7 | this._info.payload_type)
this._hdr[2] = 0 // seq_num MSB
this._hdr[3] = 0 // seq_num LSB
this._hdr[4] = 0 // timestamp MSB
this._hdr[5] = 0 // timestamp
this._hdr[6] = 0 // timestamp
this._hdr[7] = 1 // timestamp LSB
this._hdr[8] = this._info.ssrc >>> 24
this._hdr[9] = this._info.ssrc >>> 16 & 0xFF
this._hdr[10] = this._info.ssrc >>> 8 & 0xFF
this._hdr[11] = this._info.ssrc & 0xFF
this._socket = dgram.createSocket("udp4")
}
set_socket(socket) {
if(this._socket != socket) {
this._socket.close()
}
this._socket = socket
this._info.local_ip = this._socket.address().address
this._info.local_port = this._socket.address().port
this._socket.on('message', (msg, rinfo) => {
if(rinfo.address != this._info.remote_ip || rinfo.port != this._info.remote_port) {
// ignore packet out of RTP session
return
}
// TODO: must check if message is really an RTP packet
var data = this._preserve_headers ? msg : msg.slice(12) // assume 12 bytes header for now
this._socket.emit('data', data)
})
}
set_local_end_point(ip, port) {
this._socket.bind(port, ip, (err) => {
if(err) return
this.set_socket(this._socket)
})
}
set_remote_end_point(ip, port) {
this._info.remote_ip = ip
this._info.remote_port = port
}
get info() {
return this._info
}
send_payload(payload, marker_bit, payload_type) {
var buf = Buffer.concat([this._hdr, payload])
//buf[1] = (marker_bit ? marker_bit : 0) << 7 | (payload_type ? payload_type : this._info.payload_type)
buf[1] = (this._info.seq_num ? 1 : 0) << 7 | (payload_type ? payload_type : this._info.payload_type)
var seq_num = this._info.seq_num
buf[2] = seq_num >>> 8
buf[3] = seq_num & 0xFF
this._info.seq_num++
var time_stamp = this._info.time_stamp
this._info.time_stamp += payload.length
buf[4] = time_stamp >>> 24
buf[5] = time_stamp >>> 16 & 0xFF
buf[6] = time_stamp >>> 8 & 0xFF
buf[7] = time_stamp & 0xFF
this._socket.send(buf, 0, buf.length, this._info.remote_port, this._info.remote_ip)
}
close() {
this._socket.close()
}
on(evt, cb) {
this._socket.on(evt, cb)
}
}
module.exports = RtpSession