-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a `capture` method to the pipelines that produce MP4 data. It stores all data in a buffer at start of movie, and passes it to the provided callback when capture ends. The latter can be triggered by calling the returned trigger function, or automatically when the buffer is full. Co-authored-by: Rikard Tegnander <[email protected]> Co-authored-by: Victor Ingvarsson <[email protected]>
- Loading branch information
1 parent
8114f37
commit 0f40d48
Showing
10 changed files
with
244 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Example streaming MP4 video from Axis camera</title> | ||
</head> | ||
|
||
<body> | ||
<head> | ||
<title>Example streaming MP4 video from Axis camera</title> | ||
</head> | ||
|
||
<body> | ||
<div> | ||
<div> | ||
<div> | ||
<label for="device">Device IP:</label> | ||
<input id="device" type="text" placeholder="192.168.0.90" /> | ||
</div> | ||
<button id="play">Play</button> | ||
</div> | ||
<div style="position: fixed; width: 1280px; height: 720px"> | ||
<video | ||
style="position: absolute; width: 100%; height: 100%" | ||
autoplay | ||
muted | ||
controls | ||
></video> | ||
<label for="device">Device IP:</label> | ||
<input id="device" type="text" placeholder="192.168.0.90" /> | ||
</div> | ||
<script src="/media-stream-library.min.js"></script> | ||
<script src="simple-mp4-player.js"></script> | ||
</body> | ||
<button id="play">Play</button> | ||
<button id="startCapture">Start capture</button> | ||
<button id="stopCapture">Stop capture</button> | ||
</div> | ||
<div> | ||
<video style="width: 100%; max-width: 1280px" autoplay muted controls></video> | ||
</div> | ||
<script src="/media-stream-library.min.js"></script> | ||
<script src="simple-mp4-player.js"></script> | ||
</body> | ||
|
||
</html> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { logInfo as logDebug } from './log' | ||
|
||
import { IsomMessage } from './components/types' | ||
import { encode } from './components/utils/bytes' | ||
|
||
const MAX_BUFFER = 225000000 // 5 min at a rate of 6 Mbit/s | ||
|
||
// Detect the start of the movie by detecting an ftyp box. | ||
const magicHeader = encode('ftyp') | ||
function isFtypIsom(box: Uint8Array): boolean { | ||
const header = box.subarray(4, 8) | ||
return magicHeader.every((byte, i) => byte === header[i]) | ||
} | ||
|
||
/** Given a callback and max buffer size, returns two functions, one that takes | ||
* MP4 data (as ISOM message) and stores that data whenever it detects the start | ||
* of a movie, and a function that triggers the end of data storage. The trigger | ||
* is called automatically if the buffer is full. */ | ||
export function setupMp4Capture( | ||
cb: (bytes: Uint8Array) => void, | ||
bufferSize = MAX_BUFFER | ||
): { | ||
capture: (msg: IsomMessage) => void | ||
triggerEnd: () => void | ||
} { | ||
let active = true | ||
let buffer = new Uint8Array(bufferSize) | ||
let bufferOffset = 0 | ||
let startOfMovie = false | ||
|
||
const triggerEnd = () => { | ||
active = false | ||
logDebug(`stop MP4 capture, collected ${bufferOffset} bytes`) | ||
try { | ||
cb(buffer.subarray(0, bufferOffset)) | ||
} catch (err) { | ||
console.error('capture callback failed:', err) | ||
} | ||
} | ||
|
||
const capture = (msg: IsomMessage) => { | ||
if (!active) { | ||
return | ||
} | ||
|
||
// Arrival of ISOM with MIME type indicates new movie, start recording if active. | ||
if (!startOfMovie) { | ||
if (isFtypIsom(msg.data)) { | ||
startOfMovie = true | ||
logDebug('detected start of movie, proceeding with MP4 capture') | ||
} else { | ||
return | ||
} | ||
} | ||
|
||
// If movie started, record all ISOM (MP4) boxes | ||
if (bufferOffset < buffer.byteLength - msg.data.byteLength) { | ||
buffer.set(msg.data, bufferOffset) | ||
bufferOffset += msg.data.byteLength | ||
} else { | ||
triggerEnd() | ||
} | ||
} | ||
|
||
return { | ||
capture, | ||
triggerEnd, | ||
} | ||
} |
Oops, something went wrong.