|
| 1 | +const { pipelines } = window.mediaStreamLibrary |
| 2 | + |
| 3 | +// force auth |
| 4 | +const authorize = async host => { |
| 5 | + // Force a login by fetching usergroup |
| 6 | + const fetchOptions = { |
| 7 | + credentials: 'include', |
| 8 | + headers: { |
| 9 | + 'Axis-Orig-Sw': true, |
| 10 | + 'X-Requested-With': 'XMLHttpRequest', |
| 11 | + }, |
| 12 | + mode: 'no-cors', |
| 13 | + } |
| 14 | + try { |
| 15 | + await window.fetch(`http://${host}/axis-cgi/usergroup.cgi`, fetchOptions) |
| 16 | + } catch (err) { |
| 17 | + console.error(err) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +const play = host => { |
| 22 | + const initialTime = window.performance.now() |
| 23 | + // Setup a new pipeline |
| 24 | + const pipeline = new pipelines.MetadataPipeline({ |
| 25 | + ws: { |
| 26 | + uri: `ws://${host}/rtsp-over-websocket`, |
| 27 | + tokenUri: `http://${host}/axis-cgi/rtspwssession.cgi`, |
| 28 | + protocol: 'binary', |
| 29 | + timeout: 10000, |
| 30 | + }, |
| 31 | + rtsp: { |
| 32 | + uri: `rtsp://${host}/axis-media/media.amp?event=on&video=0&audio=0`, |
| 33 | + }, |
| 34 | + metadataHandler: msg => { |
| 35 | + const title = document.createElement('div') |
| 36 | + title.textContent = `+${window.performance.now() - initialTime}` |
| 37 | + title.classList.add('metadata-title') |
| 38 | + |
| 39 | + const content = document.createElement('div') |
| 40 | + content.textContent = new TextDecoder().decode(msg.data) |
| 41 | + content.classList.add('metadata-content') |
| 42 | + |
| 43 | + document.querySelector('#placeholder').prepend(title, content) |
| 44 | + }, |
| 45 | + }) |
| 46 | + pipeline.ready.then(() => { |
| 47 | + pipeline.rtsp.play() |
| 48 | + }) |
| 49 | + |
| 50 | + return pipeline |
| 51 | +} |
| 52 | + |
| 53 | +let pipeline |
| 54 | + |
| 55 | +// Each time a device ip is entered, authorize and then play |
| 56 | +const playButton = document.querySelector('#play') |
| 57 | +playButton.addEventListener('click', async e => { |
| 58 | + pipeline && pipeline.close() |
| 59 | + |
| 60 | + const device = document.querySelector('#device') |
| 61 | + const host = device.value || device.placeholder |
| 62 | + |
| 63 | + console.log(host) |
| 64 | + |
| 65 | + await authorize(host) |
| 66 | + |
| 67 | + pipeline = play(host) |
| 68 | +}) |
0 commit comments