( originalVideoStreamTrack: MediaStreamTrack )
| 20 | } |
| 21 | |
| 22 | export default function blurVideoTrack( |
| 23 | originalVideoStreamTrack: MediaStreamTrack |
| 24 | ): Observable<MediaStreamTrack> { |
| 25 | return new Observable((subscriber) => { |
| 26 | ;(async () => { |
| 27 | const segmenter = await loadSegmenter() |
| 28 | |
| 29 | const { height: h = 0, width: w = 0 } = |
| 30 | originalVideoStreamTrack.getSettings() |
| 31 | |
| 32 | const video = document.createElement('video') |
| 33 | video.height = h |
| 34 | video.width = w |
| 35 | // needed for iOS Safari to allow playing |
| 36 | video.muted = true |
| 37 | // needed for iOS Safari to allow playing |
| 38 | video.setAttribute('playsinline', '') |
| 39 | const loaded = new Promise((res) => |
| 40 | video.addEventListener('loadedmetadata', res, { once: true }) |
| 41 | ) |
| 42 | const mediaStream = new MediaStream() |
| 43 | mediaStream.addTrack(originalVideoStreamTrack) |
| 44 | video.srcObject = mediaStream |
| 45 | video.play() |
| 46 | await loaded |
| 47 | |
| 48 | const canvas = document.createElement('canvas') |
| 49 | // we need to create a context in order for this to work with firefox |
| 50 | const _contex = canvas.getContext('2d') |
| 51 | |
| 52 | canvas.height = h |
| 53 | canvas.width = w |
| 54 | |
| 55 | async function drawBlur() { |
| 56 | const segmentation = await segmenter.segmentPeople(video) |
| 57 | const foregroundThreshold = 0.6 |
| 58 | const backgroundBlurAmount = 12 |
| 59 | const edgeBlurAmount = 3 |
| 60 | const flipHorizontal = false |
| 61 | |
| 62 | await bodySegmentation.drawBokehEffect( |
| 63 | canvas, |
| 64 | video, |
| 65 | segmentation, |
| 66 | foregroundThreshold, |
| 67 | backgroundBlurAmount, |
| 68 | edgeBlurAmount, |
| 69 | flipHorizontal |
| 70 | ) |
| 71 | } |
| 72 | |
| 73 | const blurredTrack = canvas.captureStream().getVideoTracks()[0] |
| 74 | |
| 75 | let t = -1 |
| 76 | async function tick() { |
| 77 | await drawBlur() |
| 78 | t = window.setTimeout(tick, 1000 / 30) // 30fps |
| 79 | } |
nothing calls this directly
no test coverage detected