Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FG-2933: Add microphone capture (opus) to MCAP recording demo #1293

Open
wants to merge 4 commits into
base: fgwt202412/audio-recorder
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [main]
tags: ["releases/**", "go/mcap/*"]
pull_request:
branches: ["*"]
branches: ["**"]

jobs:
spellcheck:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [main]
pull_request:
branches: ["*"]
branches: ["**"]

jobs:
docs-home:
Expand Down
5 changes: 5 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,8 @@ overrides:
- filename: "docs/**"
words:
- plex

- filename: "tsconfig.json"
words:
- webcodecs
- mediacapture
2 changes: 2 additions & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"@mcap/core": "workspace:*",
"@mdx-js/react": "1.6.22",
"@tsconfig/docusaurus": "1.0.7",
"@types/dom-mediacapture-transform": "0.1.10",
"@types/dom-webcodecs": "0.1.13",
"@types/promise-queue": "2.2.0",
"buffer": "6.0.3",
"classnames": "2.3.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
margin: 0;
}

.videoContainer {
.mediaContainer {
background-color: #6f3be80f;
border: 1px solid #e0e0e0;
font-size: 0.8rem;
Expand All @@ -120,11 +120,11 @@
overflow: hidden;
}

[data-theme="dark"] .videoContainer {
[data-theme="dark"] .mediaContainer {
background-color: transparent;
}

.videoContainer video {
.mediaContainer video {
width: 100%;
height: 100%;
position: absolute;
Expand All @@ -133,7 +133,7 @@
z-index: 0;
}

.videoContainer .videoErrorContainer {
.mediaContainer .mediaErrorContainer {
width: 100%;
height: 100%;
position: absolute;
Expand All @@ -143,16 +143,16 @@
z-index: 1;
}

[data-theme="dark"] .videoContainer .videoErrorContainer {
[data-theme="dark"] .mediaContainer .mediaErrorContainer {
background-color: #17151ec4;
}

.videoPlaceholderText {
.mediaPlaceholderText {
font-weight: 600;
cursor: pointer;
}

.videoLoadingIndicator {
.mediaLoadingIndicator {
position: absolute;
top: 50%;
left: 50%;
Expand Down
119 changes: 111 additions & 8 deletions website/src/components/McapRecordingDemo/McapRecordingDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import {
Recorder,
toProtobufTime,
} from "./Recorder";
import {
startAudioStream,
CompressedAudioData,
startAudioCapture,
supportsOpusEncoding,
} from "./audioCapture";
import {
CompressedVideoFrame,
startVideoCapture,
Expand All @@ -37,6 +43,7 @@ type State = {
addPoseMessage: (msg: DeviceOrientationEvent) => void;
addJpegFrame: (blob: Blob) => void;
addVideoFrame: (frame: CompressedVideoFrame) => void;
addAudioData: (data: CompressedAudioData) => void;
closeAndRestart: () => Promise<Blob>;
};

Expand Down Expand Up @@ -70,6 +77,9 @@ const useStore = create<State>((set) => {
addVideoFrame(frame: CompressedVideoFrame) {
void recorder.addVideoFrame(frame);
},
addAudioData(data: CompressedAudioData) {
void recorder.addAudioData(data);
},
async closeAndRestart() {
return await recorder.closeAndRestart();
},
Expand Down Expand Up @@ -128,24 +138,33 @@ export function McapRecordingDemo(): JSX.Element {

const videoRef = useRef<HTMLVideoElement | undefined>();
const videoContainerRef = useRef<HTMLDivElement>(null);
const audioProgressRef = useRef<HTMLProgressElement>(null);
const [recordJpeg, setRecordJpeg] = useState(false);
const [recordH264, setRecordH264] = useState(false);
const [recordH265, setRecordH265] = useState(false);
const [recordVP9, setRecordVP9] = useState(false);
const [recordAV1, setRecordAV1] = useState(false);
const [recordOpus, setRecordOpus] = useState(false);
const [recordMouse, setRecordMouse] = useState(true);
const [recordOrientation, setRecordOrientation] = useState(true);
const [videoStarted, setVideoStarted] = useState(false);
const [videoError, setVideoError] = useState<Error | undefined>();
const [audioError, setAudioError] = useState<Error | undefined>();
const [showDownloadInfo, setShowDownloadInfo] = useState(false);

const { addJpegFrame, addVideoFrame, addMouseEventMessage, addPoseMessage } =
state;
const {
addJpegFrame,
addVideoFrame,
addMouseEventMessage,
addPoseMessage,
addAudioData,
} = state;

const { data: h264Support } = useAsync(supportsH264Encoding);
const { data: h265Support } = useAsync(supportsH265Encoding);
const { data: vp9Support } = useAsync(supportsVP9Encoding);
const { data: av1Support } = useAsync(supportsAV1Encoding);
const { data: opusSupport } = useAsync(supportsOpusEncoding);

const canStartRecording =
recordMouse ||
Expand All @@ -154,7 +173,8 @@ export function McapRecordingDemo(): JSX.Element {
(recordVP9 && !videoError) ||
(recordH265 && !videoError) ||
(recordH264 && !videoError) ||
(recordJpeg && !videoError);
(recordJpeg && !videoError) ||
(recordOpus && !audioError);

// Automatically pause recording after 30 seconds to avoid unbounded growth
useEffect(() => {
Expand Down Expand Up @@ -280,6 +300,56 @@ export function McapRecordingDemo(): JSX.Element {
recordJpeg,
]);

const [audioStream, setAudioStream] = useState<MediaStream | undefined>(
undefined,
);

const enableMicrophone = recordOpus;
useEffect(() => {
const progress = audioProgressRef.current;
if (!progress || !enableMicrophone) {
return;
}

const cleanup = startAudioStream({
progress,
onAudioStream: (stream) => {
setAudioStream(stream);
},
onError: (err) => {
setAudioError(err);
console.error(err);
},
});

return () => {
cleanup();
setAudioStream(undefined);
setAudioError(undefined);
};
}, [enableMicrophone, recordOpus]);

useEffect(() => {
if (!enableMicrophone || !recording || !audioStream) {
return;
}

const cleanup = startAudioCapture({
enableOpus: recordOpus,
stream: audioStream,
onAudioData: (data) => {
addAudioData(data);
},
onError: (error) => {
setAudioError(error);
},
});

return () => {
cleanup?.();
};
}, [addAudioData, enableMicrophone, recordOpus, audioStream, recording]);

const onRecordClick = useCallback(
(event: React.MouseEvent) => {
event.preventDefault();
Expand Down Expand Up @@ -418,6 +488,18 @@ export function McapRecordingDemo(): JSX.Element {
/>
Camera (JPEG)
</label>
{opusSupport === true && (
<label>
<input
type="checkbox"
checked={recordOpus}
onChange={(event) => {
setRecordOpus(event.target.checked);
}}
/>
Microphone (Opus)
</label>
)}
{!hasMouse && (
<label>
<input
Expand Down Expand Up @@ -546,22 +628,24 @@ export function McapRecordingDemo(): JSX.Element {
</div>
</div>
</div>
</div>

<div className={styles.recordingControls}>
<div className={styles.recordingControlsColumn}>
<div className={styles.videoContainer} ref={videoContainerRef}>
<div className={styles.mediaContainer} ref={videoContainerRef}>
{videoError ? (
<div className={cx(styles.error, styles.videoErrorContainer)}>
<div className={cx(styles.error, styles.mediaErrorContainer)}>
{videoError.toString()}
</div>
) : recordH264 || recordJpeg ? (
) : enableCamera ? (
<>
{!videoStarted && (
<progress className={styles.videoLoadingIndicator} />
<progress className={styles.mediaLoadingIndicator} />
)}
</>
) : (
<span
className={styles.videoPlaceholderText}
className={styles.mediaPlaceholderText}
onClick={() => {
if (av1Support?.supported === true) {
setRecordAV1(true);
Expand All @@ -579,6 +663,25 @@ export function McapRecordingDemo(): JSX.Element {
)}
</div>
</div>

<div className={styles.recordingControlsColumn}>
<div className={styles.mediaContainer}>
{audioError ? (
<div className={cx(styles.error, styles.mediaErrorContainer)}>
{audioError.toString()}
</div>
) : enableMicrophone ? (
<progress
className={styles.mediaLoadingIndicator}
ref={audioProgressRef}
/>
) : (
<span className={styles.mediaPlaceholderText}>
Enable “Microphone” to record audio
</span>
)}
</div>
</div>
</div>
</div>
</section>
Expand Down
Loading
Loading