Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changes/fix-desktop-recording.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"tauri-plugin-camera-kessdev": "patch"
"tauri-plugin-camera-kessdev-js": "patch"
---

Fix desktop video recording: keep the preview stream alive (reuse its video
track instead of stopping it), pick an explicit supported recording mime type
instead of falling back to WebM, and omit `bytesRecorded`/`bitrate` telemetry
until a real chunk arrives.
24 changes: 23 additions & 1 deletion .github/workflows/covector-version-or-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,29 @@ jobs:
with:
command: 'version-or-publish'
token: ${{ secrets.GITHUB_TOKEN }}
createRelease: true
# Disabled: the covector action crashes while serializing the injected
# createReleases generator function to YAML ("Tag not resolved for
# GeneratorFunction value"). Releases are created in the step below.
createRelease: 'false'

- name: create GitHub releases
if: steps.covector.outputs.commandRan == 'publish' && steps.covector.outputs.successfulPublish == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
RUST_VERSION="$(grep -m1 '^version' Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/')"
JS_VERSION="$(node -p "require('./package.json').version")"
SHA="$(git rev-parse HEAD)"
for entry in "tauri-plugin-camera-kessdev:${RUST_VERSION}" "tauri-plugin-camera-kessdev-js:${JS_VERSION}"; do
pkg="${entry%%:*}"; version="${entry##*:}"
tag="${pkg}-v${version}"
if gh release view "$tag" >/dev/null 2>&1; then
echo "release $tag already exists"
else
gh release create "$tag" --title "$pkg v$version" --target "$SHA" --notes "Automated release for $pkg v$version" || echo "failed to create release $tag"
fi
done

- name: create pull request
if: steps.covector.outputs.commandRan == 'version'
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ const withThumbnail = await stopRecording({ thumbnail: true })
await startRecording({ quality: 'fhd', fps: 30, bitrate: 2_000_000 }) // quality: 'sd' | 'hd' | 'fhd' | 'uhd'
```

On desktop the preview stays alive during recording: the recording reuses the
preview's video track (only the microphone is acquired separately), so the
`<video>` element bound to `getPreviewStream()` keeps playing. The recording
mime type is chosen from the browser's supported types (WebM or MP4).

### Video thumbnail

Pass `{ thumbnail: true }` to `stopRecording()` to include a base64-encoded JPEG
Expand Down
41 changes: 37 additions & 4 deletions guest-js/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,30 @@ describe('desktop recording telemetry', () => {
vi.spyOn(performance, 'now').mockImplementation(() => clock)
MockMediaRecorder.instances = []

const stream = { getTracks: () => [{ stop: vi.fn() }] }
function fakeStream(): unknown {
const video = { stop: vi.fn(), kind: 'video' }
const audio = { stop: vi.fn(), kind: 'audio' }
return {
getTracks: () => [video, audio],
getVideoTracks: () => [video],
getAudioTracks: () => [audio]
}
}
vi.stubGlobal('navigator', {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
mediaDevices: { getUserMedia: vi.fn().mockResolvedValue(stream) }
mediaDevices: {
getUserMedia: vi.fn().mockImplementation(() => Promise.resolve(fakeStream()))
}
})
vi.stubGlobal(
'MediaStream',
class {
tracks: unknown[]
constructor(tracks: unknown[] = []) {
this.tracks = tracks
}
}
)
vi.stubGlobal('document', {
createElement: (tag: string) => {
if (tag === 'canvas') {
Expand All @@ -349,7 +368,8 @@ describe('desktop recording telemetry', () => {
vi.stubGlobal('URL', { ...URL, createObjectURL: vi.fn(() => 'blob:mock') })
})

afterEach(() => {
afterEach(async () => {
await stopPreview()
vi.useRealTimers()
vi.restoreAllMocks()
vi.unstubAllGlobals()
Expand Down Expand Up @@ -415,11 +435,24 @@ describe('desktop recording telemetry', () => {
await startRecording({ quality: 'fhd', fps: 30, bitrate: 2000000 })
expect(getUserMedia).toHaveBeenCalledWith({
video: { height: { ideal: 1080 }, frameRate: { ideal: 30 } },
audio: true
audio: false
})
expect(getUserMedia).toHaveBeenCalledWith({ audio: true })
expect(MockMediaRecorder.instances[0].options).toEqual({ videoBitsPerSecond: 2000000 })
await stopRecording()
})

it('omits bytesRecorded until a chunk arrives', async () => {
const channel = fakeChannel()
await startRecording({ onProgress: channel as unknown as Channel<RecordingProgress> })
clock = 250
vi.advanceTimersByTime(250)

const first = channel.onmessage.mock.calls[0][0]
expect(first.bytesRecorded).toBeUndefined()
expect(first.bitrate).toBeUndefined()
await stopRecording()
})
})

describe('desktop capture config', () => {
Expand Down
78 changes: 58 additions & 20 deletions guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ function isMobile(): boolean {
let activeStream: MediaStream | null = null
let activeVideo: HTMLVideoElement | null = null
let mediaRecorder: MediaRecorder | null = null
let recordingAudioStream: MediaStream | null = null
let recordedChunks: Blob[] = []
let recordedBytes = 0
let recordingStartedAt = 0
Expand All @@ -209,10 +210,29 @@ const RECORDING_PROGRESS_INTERVAL_MS = 250
/** Ask MediaRecorder for data chunks at least this often so size is known live. */
const RECORDING_TIMESLICE_MS = 1000

/** Picks an explicit recording mime type supported by this browser, if any. */
function pickDesktopRecordingMimeType(): string | undefined {
if (typeof MediaRecorder === 'undefined' || typeof MediaRecorder.isTypeSupported !== 'function') {
return undefined
}
const candidates = [
'video/webm;codecs=vp9,opus',
'video/webm;codecs=vp8,opus',
'video/webm',
'video/mp4'
]
return candidates.find((type) => MediaRecorder.isTypeSupported(type))
}

function buildRecordingProgress(duration: number, bytes: number): RecordingProgress {
const progress: RecordingProgress = { duration, bytesRecorded: bytes }
if (duration > 0) {
progress.bitrate = Math.round((bytes * 8) / duration)
// Omit the size-derived fields until a real chunk has arrived, matching the
// mobile backends (do not report a misleading 0).
const progress: RecordingProgress = { duration }
if (bytes > 0) {
progress.bytesRecorded = bytes
if (duration > 0) {
progress.bitrate = Math.round((bytes * 8) / duration)
}
}
return progress
}
Expand Down Expand Up @@ -264,6 +284,8 @@ function teardownDesktopStream(): void {
mediaRecorder.stop()
}
mediaRecorder = null
recordingAudioStream?.getTracks().forEach((t) => t.stop())
recordingAudioStream = null
recordedChunks = []
recordedBytes = 0
if (activeStream) {
Expand Down Expand Up @@ -600,30 +622,43 @@ export async function startRecording(options?: StartRecordingOptions): Promise<v
return
}

if (activeStream) {
activeStream.getTracks().forEach((t) => t.stop())
activeStream = null
activeVideo = null
// Reuse the active preview video track when possible so the preview is not
// invalidated (which would freeze the app's <video> element).
let videoTrack = activeStream?.getVideoTracks()[0]
if (!videoTrack) {
const videoConstraints: MediaTrackConstraints = {}
if (options?.quality) videoConstraints.height = { ideal: qualityHeight(options.quality) }
if (options?.fps) videoConstraints.frameRate = { ideal: options.fps }
const videoStream = await navigator.mediaDevices.getUserMedia({
video: videoConstraints,
audio: false
})
activeStream = videoStream
videoTrack = videoStream.getVideoTracks()[0]
}
if (!videoTrack) {
throw new Error('No video track available for recording.')
}

const videoConstraints: MediaTrackConstraints = {}
if (options?.quality) videoConstraints.height = { ideal: qualityHeight(options.quality) }
if (options?.fps) videoConstraints.frameRate = { ideal: options.fps }
const stream = await navigator.mediaDevices.getUserMedia({ video: videoConstraints, audio: true })
activeStream = stream
if (!activeVideo) {
const video = document.createElement('video')
video.muted = true
video.playsInline = true
video.srcObject = activeStream
await video.play()
activeVideo = video
}

const video = document.createElement('video')
video.muted = true
video.playsInline = true
video.srcObject = stream
await video.play()
activeVideo = video
recordingAudioStream = await navigator.mediaDevices.getUserMedia({ audio: true })
const recordStream = new MediaStream([videoTrack, ...recordingAudioStream.getAudioTracks()])

recordedChunks = []
recordedBytes = 0
const recorderOptions: MediaRecorderOptions = {}
if (options?.bitrate) recorderOptions.videoBitsPerSecond = options.bitrate
mediaRecorder = new MediaRecorder(stream, recorderOptions)
const mimeType = pickDesktopRecordingMimeType()
if (mimeType) recorderOptions.mimeType = mimeType
mediaRecorder = new MediaRecorder(recordStream, recorderOptions)
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
recordedChunks.push(event.data)
Expand Down Expand Up @@ -666,9 +701,12 @@ export async function stopRecording(options?: StopRecordingOptions): Promise<Vid

return await new Promise<Video>((resolve, reject) => {
mediaRecorder!.onstop = () => {
const type = mediaRecorder!.mimeType || 'video/webm'
const mimeType = mediaRecorder!.mimeType || 'video/webm'
const type = mimeType.split(';')[0]
const blob = new Blob(recordedChunks, { type })
const url = URL.createObjectURL(blob)
recordingAudioStream?.getTracks().forEach((t) => t.stop())
recordingAudioStream = null
recordedChunks = []
recordedBytes = 0
mediaRecorder = null
Expand Down
Loading