forked from christian-fei/homebridge-image-to-camera
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CameraSource.js
154 lines (131 loc) · 4.6 KB
/
CameraSource.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
const ip = require('ip')
const getImage = require('./get-image')
const https = require('https');
const http = require('http');
module.exports = CameraSource
function CameraSource(hap, conf, log) {
this.hap = hap
this.conf = conf
this.log = log
this.services = []
this.streamControllers = []
const options = {
proxy: false, // Requires RTP/RTCP MUX Proxy
disable_audio_proxy: false, // If proxy = true, you can opt out audio proxy via this
srtp: true, // Supports SRTP AES_CM_128_HMAC_SHA1_80 encryption
video: {
resolutions: [
[1920, 1080, 30], // Width, Height, framerate
[320, 240, 15], // Apple Watch requires this configuration
[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, 180, 30]
],
codec: {
profiles: [0, 1, 2], // Enum, please refer StreamController.VideoCodecParamProfileIDTypes
levels: [0, 1, 2] // Enum, please refer StreamController.VideoCodecParamLevelTypes
}
},
audio: {
comfort_noise: false,
codecs: [
{
type: 'OPUS', // Audio Codec
samplerate: 24 // 8, 16, 24 KHz
},
{
type: 'AAC-eld',
samplerate: 16
}
]
}
}
this._createStreamControllers(2, options)
}
CameraSource.prototype.handleSnapshotRequest = function (request, callback) {
const log = this.log
log('CameraSource handleSnapshotRequest', this.conf)
if (this.conf.url) {
if (String(this.conf.url).startsWith("https://")) {
let myURL = new URL(this.conf.url, 'https://www.w3.org/');
let body = [];
https.request(myURL, res => {
// XXX verify HTTP 200 response
res.on('data', chunk => {
body.push(chunk)
});
res.on('end', () => {
log('Got image!')
callback(null, Buffer.concat(body))
});
}).end()
} else if (String(this.conf.url).startsWith("http://")) {
let myURL = new URL(this.conf.url, 'http://www.w3.org/');
let body = [];
http.request(myURL, res => {
// XXX verify HTTP 200 response
res.on('data', chunk => {
body.push(chunk)
});
res.on('end', () => {
log('Got image!')
callback(null, Buffer.concat(body))
});
}).end()
}
} else {
return getImage(this.conf.path)
.then(img => {
log('Got image!', img)
callback(null, img)
})
.catch(reason => {
log(reason)
callback(reason)
})
}
}
CameraSource.prototype.createCameraControlService = function () {
this.log('CameraSource createCameraControlService', this.conf)
const controlService = new this.hap.Service.CameraControl()
this.services.push(controlService)
}
CameraSource.prototype.handleStreamRequest = function (request) {
this.log('CameraSource handleStreamRequest', this.conf)
}
CameraSource.prototype.prepareStream = function (request, callback) {
this.log('CameraSource prepareStream', this.conf)
const sessionInfo = {}
sessionInfo.address = request.targetAddress
const response = {}
const currentAddress = ip.address()
const addressResp = {
address: currentAddress
}
if (ip.isV4Format(currentAddress)) {
addressResp.type = 'v4'
} else {
addressResp.type = 'v6'
}
response.address = addressResp
this.log('CameraSource prepareStream', response)
callback(response)
}
CameraSource.prototype.handleCloseConnection = function (connectionID) {
this.log('CameraSource handleCloseConnection', this.conf)
}
CameraSource.prototype._createStreamControllers = function (maxStreams, options) {
this.log('CameraSource _createStreamControllers', this.conf)
let self = this
for (let i = 0; i < maxStreams; i += 1) {
var streamController = new this.hap.StreamController(i, options, self)
self.services.push(streamController.service)
self.streamControllers.push(streamController)
}
}