To achieve the functionality where both the microphone transcription and the last 2 seconds of video are sent to an endpoint when both mic and video are enabled, we'll enhance your existing VideoRoom component. We'll introduce a MediaRecorder to capture video chunks, maintain a rolling buffer of the last 2 seconds of video, and send both the transcription and the buffered video to your desired endpoint.
Here's a step-by-step guide along with the updated code:
-
Set Up MediaRecorder for Video Streaming:
- Initialize a
MediaRecorder for the video stream.
- Collect video chunks and maintain a buffer that holds the last 2 seconds of video.
-
Handle Transcription and Video Sending:
- When a final transcription is available (i.e.,
finalTranscript), check if both mic and video are enabled.
- If both are enabled, compile the last 2 seconds of video from the buffer and send it along with the transcription to the endpoint.
-
Ensure Resource Cleanup:
- Properly handle the start and stop of
MediaRecorder to prevent memory leaks.
- Clean up all media streams and event listeners when the component unmounts.
Below is the enhanced VideoRoom component incorporating these functionalities:
'use client'
import { useState, useEffect, useRef, useCallback } from 'react'
import { Mic, MicOff, Video, VideoOff, PhoneOff, RefreshCw } from 'lucide-react'
import ChatDisplay from '@/components/chatBox'
import { useRouter } from 'next/navigation'
import AudioStreamPlayer from '@/components/AudioStreamPlayer';
export default function VideoRoom() {
const router = useRouter()
const [localStream, setLocalStream] = useState(null)
const [isAudioMuted, setIsAudioMuted] = useState(true)
const [isVideoOff, setIsVideoOff] = useState(true)
const [isCameraLoading, setIsCameraLoading] = useState(false)
const [error, setError] = useState(null)
const [transcript, setTranscript] = useState('')
const [finalTranscript, setFinalTranscript] = useState('')
const [isTranscribing, setIsTranscribing] = useState(false)
const [audioLevel, setAudioLevel] = useState(0)
const [recognitionErrors, setRecognitionErrors] = useState(0)
const [isProcessing, setIsProcessing] = useState(false)
const [meetingData, setMeetingData] = useState({ meetingId: '', context: '' })
const localVideoRef = useRef(null)
const streamCleanupRef = useRef(null)
const recognitionRef = useRef(null)
const pauseTimeoutRef = useRef(null)
const audioContextRef = useRef(null)
const audioAnalyserRef = useRef(null)
const audioDataRef = useRef(new Uint8Array(1024))
const recognitionRetryTimeoutRef = useRef(null)
// Refs for MediaRecorder
const mediaRecorderRef = useRef(null)
const recordedChunksRef = useRef([])
const videoBufferRef = useRef([])
const BUFFER_DURATION_MS = 2000 // 2 seconds
const CHUNK_DURATION_MS = 500 // 0.5 seconds
const MAX_BUFFERED_CHUNKS = BUFFER_DURATION_MS / CHUNK_DURATION_MS
const MAX_RECOGNITION_RETRIES = 3
const NOISE_THRESHOLD = 30
const PAUSE_THRESHOLD = 1250
useEffect(() => {
try {
const userData = sessionStorage.getItem('userData')
if (userData) {
const parsedData = JSON.parse(userData)
setMeetingData({
meetingId: parsedData.meetingId || '',
context: parsedData.context || ''
})
}
} catch (err) {
console.error('Error accessing session storage:', err)
setError('Failed to load meeting data')
}
}, [])
const setupAudioAnalyser = useCallback((stream) => {
try {
if (!stream) return
// Cleanup existing audio context
if (audioContextRef.current) {
audioContextRef.current.close()
}
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
const analyser = audioContext.createAnalyser()
const source = audioContext.createMediaStreamSource(stream)
analyser.fftSize = 2048
analyser.smoothingTimeConstant = 0.8
source.connect(analyser)
audioContextRef.current = audioContext
audioAnalyserRef.current = analyser
// Start monitoring audio levels
const checkAudioLevel = () => {
if (audioAnalyserRef.current && !isAudioMuted) {
audioAnalyserRef.current.getByteFrequencyData(audioDataRef.current)
const average = audioDataRef.current.reduce((a, b) => a + b) / audioDataRef.current.length
setAudioLevel(average)
// Use audio level for intelligent pause detection
if (average < NOISE_THRESHOLD) {
startPauseDetection()
}
}
requestAnimationFrame(checkAudioLevel)
}
checkAudioLevel()
} catch (err) {
console.error('Error setting up audio analyser:', err)
}
}, [isAudioMuted])
const startPauseDetection = useCallback(() => {
if (pauseTimeoutRef.current) {
clearTimeout(pauseTimeoutRef.current)
}
pauseTimeoutRef.current = setTimeout(() => {
if (transcript.trim()) {
console.log('Natural pause detected, finalizing transcript:', transcript)
finalizeTranscript()
}
}, PAUSE_THRESHOLD)
}, [transcript])
const finalizeTranscript = useCallback(() => {
setFinalTranscript((prev) => prev + ' ' + transcript)
setTranscript('')
setIsProcessing(true)
if (!isAudioMuted && !isVideoOff) {
sendTranscriptionWithVideo(transcript)
} else {
sendTranscription(transcript)
}
}, [transcript, isAudioMuted, isVideoOff])
const errorMessages = {
NotAllowedError: 'Camera access denied. Please allow camera and microphone access in your browser settings.',
NotFoundError: 'No camera or microphone found. Please check your device connections.',
NotReadableError: 'Camera or microphone is already in use by another application.',
OverconstrainedError: 'Your device does not support the requested video quality.',
TypeError: 'Your browser does not support video calls. Please use a modern browser.',
SecurityError: 'Security error occurred. Please ensure you\'re using HTTPS.',
}
const initializeSpeechRecognition = useCallback(() => {
if (!('webkitSpeechRecognition' in window)) {
console.error('Speech recognition not supported')
setError('Speech recognition not supported in this browser')
return
}
const recognition = new window.webkitSpeechRecognition()
recognition.continuous = true
recognition.interimResults = true
recognition.lang = 'en-US'
recognition.onstart = () => {
setIsTranscribing(true)
setRecognitionErrors(0)
console.log('Speech recognition started')
}
recognition.onerror = (event) => {
console.error('Speech recognition error:', event.error)
setRecognitionErrors(prev => prev + 1)
if (event.error === 'not-allowed') {
setError('Microphone access denied for transcription')
} else if (event.error === 'audio-capture') {
setError('No microphone detected')
} else if (recognitionErrors < MAX_RECOGNITION_RETRIES) {
// Attempt to restart recognition after error
recognitionRetryTimeoutRef.current = setTimeout(() => {
if (!isAudioMuted && recognitionRef.current) {
console.log('Attempting to restart speech recognition')
recognition.start()
}
}, 1000)
} else {
setError('Speech recognition failed multiple times. Please refresh the page.')
}
}
recognition.onend = () => {
setIsTranscribing(false)
console.log('Speech recognition ended')
// Restart if not manually stopped and mic is still on
if (!isAudioMuted && recognitionRef.current && recognitionErrors < MAX_RECOGNITION_RETRIES) {
console.log('Restarting speech recognition')
try {
recognition.start()
} catch (err) {
console.error('Failed to restart recognition:', err)
}
}
}
recognition.onresult = (event) => {
let interimTranscript = ''
let finalTranscriptLocal = ''
// Process results with confidence scores
for (let i = event.resultIndex; i < event.results.length; i++) {
const transcriptText = event.results[i][0].transcript
const confidence = event.results[i][0].confidence
if (confidence > 0.7) { // Only accept high-confidence results
if (event.results[i].isFinal) {
finalTranscriptLocal += addPunctuation(transcriptText)
} else {
interimTranscript += transcriptText
}
}
}
const currentTranscript = finalTranscriptLocal || interimTranscript
setTranscript(currentTranscript)
if (finalTranscriptLocal) {
console.log('Final transcript:', finalTranscriptLocal)
finalizeTranscript()
}
}
recognitionRef.current = recognition
console.log('Speech recognition initialized')
// Start recognition if audio is not muted
if (!isAudioMuted) {
try {
recognition.start()
} catch (err) {
console.error('Failed to start recognition:', err)
}
}
}, [isAudioMuted, recognitionErrors, finalizeTranscript])
// Helper function to add basic punctuation
const addPunctuation = (text) => {
// Add period if sentence ends without punctuation
text = text.trim()
if (!/[.!?]$/.test(text)) {
text += '.'
}
// Capitalize first letter
return text.charAt(0).toUpperCase() + text.slice(1)
}
// Clean up function
const cleanupSpeechRecognition = useCallback(() => {
if (recognitionRef.current) {
recognitionRef.current.stop()
recognitionRef.current = null
}
if (recognitionRetryTimeoutRef.current) {
clearTimeout(recognitionRetryTimeoutRef.current)
}
if (pauseTimeoutRef.current) {
clearTimeout(pauseTimeoutRef.current)
}
if (audioContextRef.current) {
audioContextRef.current.close()
audioContextRef.current = null
}
setTranscript('')
setIsTranscribing(false)
setAudioLevel(0)
setRecognitionErrors(0)
}, [])
const stopMediaTracks = useCallback(() => {
console.log('Stopping media tracks')
if (streamCleanupRef.current) {
streamCleanupRef.current.getTracks().forEach(track => {
console.log(`Stopping track: ${track.kind}`)
track.stop()
})
streamCleanupRef.current = null
setLocalStream(null)
}
if (recognitionRef.current) {
console.log('Stopping speech recognition')
recognitionRef.current.stop()
recognitionRef.current = null
}
if (pauseTimeoutRef.current) {
console.log('Clearing pause timeout')
clearTimeout(pauseTimeoutRef.current)
}
// Stop MediaRecorder if active
if (mediaRecorderRef.current) {
console.log('Stopping MediaRecorder')
mediaRecorderRef.current.stop()
mediaRecorderRef.current = null
}
// Clear video buffer
videoBufferRef.current = []
}, [])
const getMediaConstraints = (includeVideo = true, includeAudio = true) => {
const constraints = {
video: includeVideo ? {
width: { ideal: 1280 },
height: { ideal: 720 },
facingMode: 'user',
frameRate: { ideal: 30 }
} : false,
audio: includeAudio ? {
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true
} : false
}
console.log('Media constraints:', constraints)
return constraints
}
useEffect(() => {
if (!localStream || !localVideoRef.current) return
console.log('Setting up video element with local stream')
const videoElement = localVideoRef.current
videoElement.srcObject = localStream
videoElement.muted = true
const playVideo = async () => {
try {
await videoElement.play()
console.log('Video playback started successfully')
} catch (err) {
console.error('Failed to play video:', err)
setError('Failed to play video stream')
}
}
videoElement.onloadedmetadata = playVideo
return () => {
console.log('Cleaning up video element')
videoElement.srcObject = null
}
}, [localStream])
// Effect for handling audio state changes
useEffect(() => {
if (!isAudioMuted) {
initializeSpeechRecognition()
if (localStream) {
setupAudioAnalyser(localStream)
}
} else {
cleanupSpeechRecognition()
}
return cleanupSpeechRecognition
}, [isAudioMuted, localStream, initializeSpeechRecognition, setupAudioAnalyser, cleanupSpeechRecognition])
const startLocalStream = useCallback(async (withVideo = true, withAudio = true) => {
console.log('Starting local stream:', { withVideo, withAudio })
if (!navigator.mediaDevices?.getUserMedia) {
console.error('getUserMedia not supported')
setError('Your browser does not support video calls')
return
}
try {
setIsCameraLoading(true)
setError(null)
stopMediaTracks()
if (withVideo || withAudio) {
console.log('Requesting media access')
const stream = await navigator.mediaDevices.getUserMedia(getMediaConstraints(withVideo, withAudio))
console.log('Media access granted')
streamCleanupRef.current = stream
setLocalStream(stream)
// Initialize MediaRecorder if video is enabled
if (withVideo) {
initializeMediaRecorder(stream)
}
} else {
console.log('No media requested, clearing stream')
streamCleanupRef.current = null
setLocalStream(null)
}
setIsVideoOff(!withVideo)
setIsAudioMuted(!withAudio)
} catch (error) {
console.error('Media access error:', error)
setError(errorMessages[error.name] || error.message || 'Failed to start media devices')
setIsVideoOff(true)
setIsAudioMuted(true)
streamCleanupRef.current = null
setLocalStream(null)
} finally {
setIsCameraLoading(false)
}
}, [stopMediaTracks])
// Initialize MediaRecorder and set up buffering
const initializeMediaRecorder = (stream) => {
if (mediaRecorderRef.current) {
console.warn('MediaRecorder already initialized')
return
}
try {
const options = { mimeType: 'video/webm; codecs=vp9' }
const mediaRecorder = new MediaRecorder(stream, options)
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
videoBufferRef.current.push(event.data)
if (videoBufferRef.current.length > MAX_BUFFERED_CHUNKS) {
videoBufferRef.current.shift() // Remove the oldest chunk
}
}
}
mediaRecorder.onstart = () => {
console.log('MediaRecorder started')
}
mediaRecorder.onstop = () => {
console.log('MediaRecorder stopped')
}
mediaRecorder.onerror = (event) => {
console.error('MediaRecorder error:', event.error)
setError('Video recording error')
}
mediaRecorder.start(CHUNK_DURATION_MS) // Start recording with chunk duration
mediaRecorderRef.current = mediaRecorder
console.log('MediaRecorder initialized and started')
} catch (err) {
console.error('Failed to initialize MediaRecorder:', err)
setError('Failed to record video')
}
}
useEffect(() => {
console.log('Initial stream setup')
startLocalStream(!isVideoOff, !isAudioMuted)
return stopMediaTracks
}, [startLocalStream, stopMediaTracks])
const toggleAudio = useCallback(() => {
console.log('Toggling audio:', { currentState: isAudioMuted })
startLocalStream(!isVideoOff, isAudioMuted)
}, [isVideoMuted, isAudioMuted, startLocalStream])
const toggleVideo = useCallback(() => {
console.log('Toggling video:', { currentState: isVideoOff })
startLocalStream(isVideoOff, !isAudioMuted)
}, [isVideoOff, isAudioMuted, startLocalStream])
// Function to send only transcription
const sendTranscription = async (transcriptText) => {
try {
const response = await fetch('/api/transcription-endpoint', { // Replace with your endpoint
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ transcript: transcriptText, meetingId: meetingData.meetingId })
})
if (!response.ok) {
throw new Error(`Transcription request failed: ${response.status}`)
}
console.log('Transcription sent successfully')
} catch (error) {
console.error('Failed to send transcription:', error)
setError('Failed to send transcription')
} finally {
setIsProcessing(false)
}
}
// Function to send transcription along with video
const sendTranscriptionWithVideo = async (transcriptText) => {
try {
if (!videoBufferRef.current.length) {
console.warn('No video available to send')
await sendTranscription(transcriptText) // Fallback to sending transcription only
return
}
// Compile the buffered video chunks into a single Blob
const videoBlob = new Blob(videoBufferRef.current, { type: 'video/webm' })
// Prepare FormData to send both transcript and video
const formData = new FormData()
formData.append('transcript', transcriptText)
formData.append('meetingId', meetingData.meetingId)
formData.append('video', videoBlob, 'video.webm')
const response = await fetch('/api/transcription-video-endpoint', { // Replace with your endpoint
method: 'POST',
body: formData
})
if (!response.ok) {
throw new Error(`Transcription with video request failed: ${response.status}`)
}
console.log('Transcription and video sent successfully')
} catch (error) {
console.error('Failed to send transcription with video:', error)
setError('Failed to send transcription with video')
} finally {
setIsProcessing(false)
}
}
return (
<div className="w-full h-screen bg-gradient-to-br from-gray-900 via-gray-800 to-gray-900 text-white relative overflow-hidden">
<div className="absolute top-4 right-4 z-10 flex gap-2">
{error && (
<button
onClick={() => {
console.log('Retrying media access')
startLocalStream(!isVideoOff, !isAudioMuted)
}}
className="bg-blue-500 hover:bg-blue-400 text-white p-2 rounded-full transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-blue-400"
aria-label="Retry camera"
>
<RefreshCw size={24} />
</button>
)}
</div>
<div className="flex flex-col h-full">
<div className="flex-grow grid grid-cols-1 md:grid-cols-2 gap-4 p-4">
<div className="relative rounded-2xl overflow-hidden shadow-lg bg-gradient-to-br from-blue-500 to-purple-600">
{isCameraLoading ? (
<div className="absolute inset-0 flex items-center justify-center bg-gray-800">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-white"></div>
</div>
) : error ? (
<div className="absolute inset-0 flex items-center justify-center bg-gray-800">
<div className="text-center p-4">
<p className="text-red-500 font-bold mb-2">Camera Error</p>
<p className="text-sm">{error}</p>
<button
onClick={() => startLocalStream(!isVideoOff, !isAudioMuted)}
className="mt-4 bg-blue-500 hover:bg-blue-400 text-white px-4 py-2 rounded-full text-sm"
>
Retry
</button>
</div>
</div>
) : isVideoOff ? (
<div className="absolute inset-0 flex items-center justify-center bg-gray-800">
<div className="w-32 h-32 rounded-full bg-gray-700 flex items-center justify-center text-4xl font-bold">
You
</div>
</div>
) : (
<video
ref={localVideoRef}
autoPlay
playsInline
muted
className="w-full h-full object-cover mirror"
/>
)}
<div className="absolute bottom-2 left-2 bg-black bg-opacity-50 px-3 py-1 rounded-full text-sm font-medium">
You
</div>
{isAudioMuted && (
<div className="absolute top-2 right-2 bg-red-500 p-1 rounded-full">
<MicOff size={16} />
</div>
)}
{!isAudioMuted && (
<div className="absolute bottom-12 left-0 right-0 bg-black bg-opacity-50 p-2 transition-opacity duration-300">
<p className="text-sm text-white">
{transcript || (isTranscribing ? 'Listening...' : 'Say something...')}
</p>
</div>
)}
</div>
<div className="relative rounded-2xl overflow-hidden shadow-lg bg-gradient-to-br from-green-500 to-teal-600">
<ChatDisplay userTranscript={transcript} isProcessing={isProcessing} />
<AudioStreamPlayer meetingId={meetingData.meetingId} context={meetingData.context} transcript={finalTranscript} />
</div>
</div>
<div className="bg-gray-800 bg-opacity-75 backdrop-blur-md p-6">
<div className="flex justify-center space-x-6">
<button
onClick={toggleAudio}
disabled={isCameraLoading}
className={`p-4 rounded-full transition-all duration-200 transform hover:scale-110 focus:outline-none focus:ring-2 focus:ring-blue-400 ${
isAudioMuted ? 'bg-red-500 hover:bg-red-600' : 'bg-blue-500 hover:bg-blue-600'
} ${isCameraLoading && 'opacity-50 cursor-not-allowed'}`}
aria-label={isAudioMuted ? 'Unmute audio' : 'Mute audio'}
>
{isAudioMuted ? <MicOff size={24} /> : <Mic size={24} />}
</button>
<button
onClick={toggleVideo}
disabled={isCameraLoading}
className={`p-4 rounded-full transition-all duration-200 transform hover:scale-110 focus:outline-none focus:ring-2 focus:ring-blue-400 ${
isVideoOff ? 'bg-red-500 hover:bg-red-600' : 'bg-blue-500 hover:bg-blue-600'
} ${isCameraLoading && 'opacity-50 cursor-not-allowed'}`}
aria-label={isVideoOff ? 'Turn on video' : 'Turn off video'}
>
{isVideoOff ? <VideoOff size={24} /> : <Video size={24} />}
</button>
<button
onClick={() => {
console.log('Ending call and navigating to home')
stopMediaTracks()
router.push('/')
}}
className="bg-red-500 hover:bg-red-600 text-white p-4 rounded-full transition-all duration-200 transform hover:scale-110 focus:outline-none focus:ring-2 focus:ring-red-400"
aria-label="End call"
>
<PhoneOff size={24} />
</button>
</div>
</div>
</div>
</div>
)
}
Additional Changes and Explanations
-
MediaRecorder Setup:
- Initialization: When the local stream is initialized with video (
withVideo is true), we initialize the MediaRecorder.
- Buffering Chunks: The
MediaRecorder captures video in chunks of 500ms. We maintain a buffer (videoBufferRef) that holds the last 2 seconds (i.e., the last 4 chunks) of video.
- Handling Data Available: Each time a chunk is available (
ondataavailable), it's pushed to the buffer. If the buffer exceeds the maximum allowed chunks, the oldest chunk is removed to maintain the 2-second window.
-
Sending Transcription with Video:
- Finalizing Transcript: When transcription is finalized (
finalizeTranscript), it checks if both audio is not muted and video is not off.
- Compiling Video Blob: The last 2 seconds of video are compiled into a single
Blob from the buffered chunks.
- FormData Submission: Both the transcription text and the video
Blob are sent together using FormData to the designated endpoint (/api/transcription-video-endpoint).
-
Sending Only Transcription:
- Fallback Mechanism: If only transcription needs to be sent (i.e., when video is off or audio is muted), it sends only the transcription to a different endpoint (
/api/transcription-endpoint).
-
Component Cleanup:
- Properly stops
MediaRecorder when media tracks are stopped to prevent memory leaks.
- Clears the video buffer when media tracks are stopped.
-
Error Handling:
- Comprehensive error handling to ensure that any issues during media access, transcription, or video recording are caught and appropriately handled.
-
UI Feedback:
- Visual indicators (like the spinner) to inform users of the ongoing processes.
- Error messages displayed within the UI for better user experience.
Backend Considerations
Ensure that your backend endpoints (/api/transcription-endpoint and /api/transcription-video-endpoint) are set up to handle the incoming data appropriately:
-
/api/transcription-endpoint: Should accept a JSON payload containing the transcription text and meetingId.
-
/api/transcription-video-endpoint: Should accept multipart/form-data with fields for the transcription text, meetingId, and the video file.
Final Notes
-
Browser Compatibility: The MediaRecorder API is widely supported in modern browsers, but always verify compatibility based on your target audience.
-
Performance: Continuously recording video can be resource-intensive. Ensure that this implementation does not degrade performance, especially on lower-end devices.
-
Security: Always validate and sanitize incoming data on the server side to prevent security vulnerabilities.
By following the above implementation, you should achieve a robust and seamless integration of both transcription and video capture functionalities, ensuring that your application remains performant and user-friendly.