Repository navigation
Expand file tree
/
Copy pathcamera.js
More file actions
299 lines (249 loc) · 8.4 KB
/
Copy pathcamera.js
File metadata and controls
299 lines (249 loc) · 8.4 KB
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
const hap = require("hap-nodejs")
const crypto = require("crypto")
const ip = require("ip")
const child_process = require("child_process")
const [Accessory, CameraController, StreamRequestTypes, H264Level, H264Profile, SRTPCryptoSuites] = [
hap.Accessory,
hap.CameraController,
hap.StreamRequestTypes,
hap.H264Level,
hap.H264Profile,
hap.SRTPCryptoSuites
]
const STREAM_URL = "rtsp://192.168.1.59"
const FFMPEGH264ProfileNames = [
"baseline",
"main",
"high"
]
const FFMPEGH264LevelNames = [
"3.1",
"3.2",
"4.0"
]
const ExampleCamera = (function () {
function ExampleCamera() {
this.pendingSessions = []
this.ongoingSessions = []
this.running = false
}
ExampleCamera.prototype.handleSnapshotRequest = function(request, callback) {
const e = new Error("not implemented yet")
callback(e)
}
// called when iOS request rtp setup
ExampleCamera.prototype.prepareStream = function(request, callback) {
let response = {}
let sessionInfo = {}
const sessionId = request.sessionID
const video = request.video
const audio = request.audio
const targetAddress = request.targetAddress
if (video) {
const videoPort = video.port
const videoCryptoSuite = video.srtpCryptoSuite // could be used to support multiple crypto suite (or support no suite for debugging)
const videoSrtpKey = video.srtp_key
const videoSrtpSalt = video.srtp_salt
const videoSSRC = CameraController.generateSynchronisationSource()
// // SSRC is a 32 bit integer that is unique per stream
// let ssrcSource = crypto.randomBytes(4)
// ssrcSource[0] = 0
// const ssrc = ssrcSource.readInt32BE(0)
sessionInfo = {
address: targetAddress,
videoPort: videoPort,
videoCryptoSuite: videoCryptoSuite,
videoSRTP: Buffer.concat([videoSrtpKey, videoSrtpSalt]),
videoSSRC: videoSSRC
}
const currentAddress = ip.address("public", request.addressVersion); // ipAddress version must match
response = {
address: currentAddress,
video: {
port: videoPort,
ssrc: videoSSRC,
srtp_key: videoSrtpKey,
srtp_salt: videoSrtpSalt
}
}
}
// const audioInfo = request["audio"]
// if (audioInfo) {
// const targetPort = audioInfo["port"]
// const srtp_key = audioInfo["srtp_key"]
// const srtp_salt = audioInfo["srtp_salt"]
// // SSRC is a 32 bit integer that is unique per stream
// let ssrcSource = crypto.randomBytes(4)
// ssrcSource[0] = 0
// const ssrc = ssrcSource.readInt32BE(0)
// response["audio"] = {
// port: targetPort,
// srtp_key: srtp_key,
// srtp_salt: srtp_salt,
// ssrc: ssrc
// }
// sessionInfo["audio_port"] = targetPort
// sessionInfo["audio_srtp"] = Buffer.concat([srtp_key, srtp_salt])
// sessionInfo["audio_ssrc"] = ssrc
// }
// const currentAddress = ip.address()
// response["address"] = {
// address: currentAddress,
// type: ip.isV4Format(currentAddress) ? "v4" : "v6"
// }
// sessionInfo["address"] = request["targetAddress"]
// const sessionId = request["sessionId"]
// const sessionIdentifier = uuid.unparse(sessionId)
this.pendingSessions[sessionId] = sessionInfo
callback(null, response)
}
// called when iOS device asks stream to start/stop/reconfigure
ExampleCamera.prototype.handleStreamRequest = function(request, callback) {
const sessionId = request.sessionID
switch (request.type) {
case StreamRequestTypes.START:
const sessionInfo = this.pendingSessions[sessionId]
if (sessionInfo) {
let width = 1024
let height = 768
let fps = 30
let bitrate = 300
let pt = 99
let mtu = 1378
const video = request.video
if (video) {
width = video.width
height = video.height
bitrate = video.max_bit_rate
pt = video.pt
mtu = video.mtu
fps = Math.max(video.fps, fps)
}
const address = sessionInfo.address
const port = sessionInfo.videoPort
const srtp = sessionInfo.videoSRTP
const ssrc = sessionInfo.videoSSRC
const cryptoSuite = sessionInfo.videoCryptoSuite
const key = srtp.toString("hex")
let gst = 'uridecodebin uri="{{location}}" ! videoconvert ! videoscale ! videorate ! video/x-raw, width={{width}}, height={{height}}, framerate={{fps}}/1 ! v4l2h264enc ! rtph264pay pt={{pt}} mtu={{mtu}} ssrc={{ssrc}} ! srtpenc key={{key}} ! udpsink host={{address}} port={{port}}'
gst = gst.replace(/{{location}}/, STREAM_URL)
gst = gst.replace(/{{width}}/, width)
gst = gst.replace(/{{height}}/, height)
gst = gst.replace(/{{fps}}/, fps)
gst = gst.replace(/{{pt}}/, pt)
gst = gst.replace(/{{mtu}}/, mtu)
gst = gst.replace(/{{ssrc}}/, ssrc)
gst = gst.replace(/{{key}}/, key)
gst = gst.replace(/{{address}}/, address)
gst = gst.replace(/{{port}}/, port)
gst = gst.replace(/{{bitrate}}/g, bitrate)
console.log('gst-launch-1.0', gst)
const arguments = gst.split(' ')
const process = child_process.spawn('gst-launch-1.0', arguments)
process.stderr.on('data', (data) => {
const e = data.toString()
callback(e)
})
process.on('exit', (code, signal) => {
console.log(code, signal)
if (this.running) {
this.controller.forceStopStreamingSession(sessionId)
}
})
process.on('uncaughtException', (err) => {
console.log(err)
})
callback() // do not forget to execute callback once set up
this.ongoingSessions[sessionId] = process
delete this.pendingSessions[sessionId]
}
break
case StreamRequestTypes.RECONFIGURE:
// not supported by this example
console.log("Received (unsupported) request to reconfigure to: " + JSON.stringify(request.video))
callback()
break
case StreamRequestTypes.STOP:
const ongoingSession = this.ongoingSessions[sessionId]
if (!ongoingSession) {
callback()
break
}
try {
ongoingSession.kill("SIGKILL")
} catch (e) {
console.log("Error occurred terminating the video process!")
console.log(e)
}
delete this.ongoingSessions[sessionId]
console.log("Stopped streaming session!")
callback()
break
}
}
ExampleCamera.prototype.setController = function(controller) {
this.controller = controller
}
return ExampleCamera
}());
const streamDelegate = new ExampleCamera()
const cameraControllerOptions = {
cameraStreamCount: 2,
delegate: streamDelegate,
streamingOptions: {
// srtp: true, // legacy option which will just enable AES_CM_128_HMAC_SHA1_80 (can still be used though)
supportedCryptoSuites: [
SRTPCryptoSuites.NONE,
SRTPCryptoSuites.AES_CM_128_HMAC_SHA1_80
],
video: {
codec: {
profiles: [
H264Profile.BASELINE,
H264Profile.MAIN,
H264Profile.HIGH
],
levels: [
H264Level.LEVEL3_1,
H264Level.LEVEL3_2,
H264Level.LEVEL4_0
] // Enum, please refer StreamController.VideoCodecParamLevelTypes
},
resolutions: [
[1920, 1080, 30], // width, height, framerate
[1280, 960, 30],
[1280, 720, 30],
[1024, 768, 30],
[640, 480, 30],
[640, 360, 30],
[480, 360, 30],
[480, 270, 30],
[320, 240, 30],
[320, 240, 15], // Apple Watch requires this configuration (Apple Watch also seems to required OPUS @16K)
[320, 180, 30],
]
},
/* audio option is omitted, as it is not supported in this example; HAP-NodeJS will fake an appropriate audio codec
audio: {
comfort_noise: false,
codecs: [
{
type: "OPUS",
samplerate: 24 // 8, 16, 24 KHz
},
{
type: "AAC-eld",
samplerate: 16
}
]
}
*/
}
}
module.exports = {
configureController: (accessory) => {
const cameraController = new CameraController(cameraControllerOptions)
streamDelegate.setController(cameraController)
accessory.configureController(cameraController)
}
}