-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1449 from ecency/feature/video-recording
Video recorder
- Loading branch information
Showing
9 changed files
with
352 additions
and
16 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
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
39 changes: 39 additions & 0 deletions
39
src/common/components/video-upload-threespeak/video-upload-recorder-actions.tsx
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,39 @@ | ||
import { circleSvg, rectSvg } from "../../img/svg"; | ||
import React, { useState } from "react"; | ||
|
||
interface Props { | ||
noPermission: boolean; | ||
mediaRecorder?: MediaRecorder; | ||
} | ||
|
||
export function VideoUploadRecorderActions({ noPermission, mediaRecorder }: Props) { | ||
const [recordStarted, setRecordStarted] = useState(false); | ||
|
||
return ( | ||
<div className="actions"> | ||
{recordStarted ? ( | ||
<div | ||
aria-disabled={noPermission} | ||
className="record-btn" | ||
onClick={() => { | ||
mediaRecorder?.stop(); | ||
setRecordStarted(false); | ||
}} | ||
> | ||
{rectSvg} | ||
</div> | ||
) : ( | ||
<div | ||
aria-disabled={noPermission} | ||
className="record-btn" | ||
onClick={() => { | ||
mediaRecorder?.start(); | ||
setRecordStarted(true); | ||
}} | ||
> | ||
{circleSvg} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/common/components/video-upload-threespeak/video-upload-recorder-no-permission.tsx
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,10 @@ | ||
import { _t } from "../../i18n"; | ||
import React from "react"; | ||
|
||
export function VideoUploadRecorderNoPermission() { | ||
return ( | ||
<div className="no-permission"> | ||
<p>{_t("video-upload.no-record-permission")}</p> | ||
</div> | ||
); | ||
} |
159 changes: 159 additions & 0 deletions
159
src/common/components/video-upload-threespeak/video-upload-recorder.tsx
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,159 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import useMount from "react-use/lib/useMount"; | ||
import { VideoUploadRecorderActions } from "./video-upload-recorder-actions"; | ||
import { VideoUploadRecorderNoPermission } from "./video-upload-recorder-no-permission"; | ||
import { Button } from "react-bootstrap"; | ||
import { _t } from "../../i18n"; | ||
import { useThreeSpeakVideoUpload } from "../../api/threespeak"; | ||
import { error } from "../feedback"; | ||
import { v4 } from "uuid"; | ||
import { useUnmount } from "react-use"; | ||
|
||
interface Props { | ||
setVideoUrl: (v: string) => void; | ||
setFilevName: (v: string) => void; | ||
setFilevSize: (v: number) => void; | ||
setSelectedFile: (v: string) => void; | ||
onReset: () => void; | ||
} | ||
|
||
export function VideoUploadRecorder({ | ||
setVideoUrl, | ||
setFilevName, | ||
setFilevSize, | ||
onReset, | ||
setSelectedFile | ||
}: Props) { | ||
const [stream, setStream] = useState<MediaStream>(); | ||
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder>(); | ||
const [recordedVideoSrc, setRecordedVideoSrc] = useState<string>(); | ||
const [recordedBlob, setRecordedBlob] = useState<Blob>(); | ||
const [noPermission, setNoPermission] = useState(false); | ||
|
||
const ref = useRef<HTMLVideoElement | null>(null); | ||
|
||
const { | ||
mutateAsync: uploadVideo, | ||
completed, | ||
isLoading, | ||
isSuccess | ||
} = useThreeSpeakVideoUpload("video"); | ||
|
||
useMount(async () => { | ||
try { | ||
const stream = await navigator.mediaDevices.getUserMedia({ | ||
video: true, | ||
audio: true | ||
}); | ||
const mediaRecorder = new MediaRecorder(stream, { | ||
mimeType: "video/webm" | ||
}); | ||
|
||
setMediaRecorder(mediaRecorder); | ||
setStream(stream); | ||
|
||
mediaRecorder.addEventListener("dataavailable", (event) => { | ||
if (event.data.size > 0) { | ||
setRecordedVideoSrc(URL.createObjectURL(event.data)); | ||
setRecordedBlob(event.data); | ||
stream.getTracks().forEach((track) => track.stop()); | ||
} | ||
}); | ||
} catch (e) { | ||
setNoPermission(true); | ||
} | ||
}); | ||
|
||
useUnmount(() => { | ||
stream?.getTracks().forEach((track) => track.stop()); | ||
}); | ||
|
||
useEffect(() => { | ||
if (stream && ref.current) { | ||
// @ts-ignore | ||
ref.current?.srcObject = stream; | ||
} | ||
}, [stream, ref]); | ||
|
||
return ( | ||
<div className="video-upload-recorder"> | ||
{recordedBlob ? ( | ||
<Button className="reset-btn" variant="link" onClick={() => onReset()}> | ||
{_t("video-upload.reset")} | ||
</Button> | ||
) : ( | ||
<></> | ||
)} | ||
|
||
{!noPermission && !recordedVideoSrc ? ( | ||
<VideoUploadRecorderActions noPermission={noPermission} mediaRecorder={mediaRecorder} /> | ||
) : ( | ||
<></> | ||
)} | ||
|
||
{noPermission ? ( | ||
<VideoUploadRecorderNoPermission /> | ||
) : ( | ||
<> | ||
<video | ||
hidden={!recordedVideoSrc} | ||
controls={true} | ||
src={recordedVideoSrc} | ||
autoPlay={false} | ||
playsInline={true} | ||
id="videoRecorded" | ||
/> | ||
<video | ||
hidden={!!recordedVideoSrc} | ||
ref={ref} | ||
muted={true} | ||
autoPlay={true} | ||
playsInline={true} | ||
id="videoLive" | ||
/> | ||
{recordedVideoSrc ? ( | ||
<div className="d-flex align-items-center justify-content-center mt-3"> | ||
{recordedBlob && isSuccess ? ( | ||
<div className="bg-success text-white p-3 text-sm rounded-pill w-100"> | ||
{_t("video-upload.uploaded")} | ||
</div> | ||
) : ( | ||
<Button | ||
disabled={isLoading} | ||
onClick={async () => { | ||
if (!recordedBlob) { | ||
return; | ||
} | ||
|
||
try { | ||
const file = new File([recordedBlob], `ecency-recorder-${v4()}.webm`, { | ||
type: "video/webm" | ||
}); | ||
const result = await uploadVideo({ | ||
file | ||
}); | ||
if (result) { | ||
setVideoUrl(result.fileUrl); | ||
setFilevName(result.fileName); | ||
setFilevSize(result.fileSize); | ||
setSelectedFile(URL.createObjectURL(file)); | ||
} | ||
} catch (e) { | ||
error(e); | ||
} | ||
}} | ||
> | ||
{isLoading | ||
? _t("video-upload.uploading", { n: completed, total: 100 }) | ||
: _t("video-upload.confirm-and-upload")} | ||
</Button> | ||
)} | ||
</div> | ||
) : ( | ||
<></> | ||
)} | ||
</> | ||
)} | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.