2024-02-04 03:11:19 -07:00
|
|
|
import { ApolloClient, useApolloClient } from "@apollo/client";
|
2024-02-03 20:30:00 -07:00
|
|
|
import { useIsFocused } from "@react-navigation/native";
|
2024-02-04 03:11:19 -07:00
|
|
|
import * as gql from "railbird-gql";
|
|
|
|
import React, { useCallback, useEffect, useRef, useState } from "react";
|
2024-01-31 22:45:00 -07:00
|
|
|
import { Button, StyleSheet, Text, View } from "react-native";
|
2024-02-04 23:58:07 -07:00
|
|
|
import * as RNFS from "react-native-fs";
|
2024-01-31 22:45:00 -07:00
|
|
|
import {
|
2024-02-03 20:23:31 -07:00
|
|
|
Camera,
|
|
|
|
CameraRuntimeError,
|
|
|
|
Orientation,
|
2024-02-03 20:30:00 -07:00
|
|
|
PhotoFile,
|
|
|
|
VideoFile,
|
|
|
|
useCameraDevice,
|
|
|
|
useCameraFormat,
|
|
|
|
useCameraPermission,
|
2024-01-31 22:45:00 -07:00
|
|
|
} from "react-native-vision-camera";
|
|
|
|
import { RecordingButton } from "./capture-button";
|
|
|
|
import { useIsForeground } from "./is-foreground";
|
2024-01-31 16:55:33 -07:00
|
|
|
|
2024-02-04 03:11:19 -07:00
|
|
|
type Dictionary<KeyType extends string | number | symbol, ValueType> = {
|
|
|
|
[key in KeyType]: ValueType;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StreamUploadManager<TCacheShape> {
|
|
|
|
client: ApolloClient<TCacheShape>;
|
|
|
|
videoId: number;
|
2024-02-05 01:42:32 -07:00
|
|
|
nextUploadIdToRequest: number = 0;
|
|
|
|
highestUploadLinkObtained: number = -1;
|
|
|
|
prefetchedUploadLinks: Dictionary<number, string> = {};
|
|
|
|
uploadQueue: Array<() => Promise<void>> = [];
|
|
|
|
isUploading: boolean = false;
|
2024-02-04 03:11:19 -07:00
|
|
|
|
2024-02-05 01:42:32 -07:00
|
|
|
constructor(client: ApolloClient<TCacheShape>, videoId: number) {
|
2024-02-04 03:11:19 -07:00
|
|
|
this.client = client;
|
2024-02-05 01:42:32 -07:00
|
|
|
this.videoId = videoId;
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueueUploadTask(task: () => Promise<void>) {
|
|
|
|
this.uploadQueue.push(task);
|
|
|
|
this.processUploadQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
async processUploadQueue() {
|
|
|
|
if (this.isUploading || this.uploadQueue.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isUploading = true;
|
|
|
|
const task = this.uploadQueue.shift();
|
|
|
|
try {
|
2024-02-05 01:55:53 -07:00
|
|
|
if (task) {
|
|
|
|
await task();
|
|
|
|
}
|
2024-02-05 01:42:32 -07:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing upload task", error);
|
|
|
|
} finally {
|
|
|
|
this.isUploading = false;
|
|
|
|
this.processUploadQueue();
|
|
|
|
}
|
2024-02-04 03:11:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async uploadChunk({ filepath, index }: { filepath: string; index: number }) {
|
2024-02-05 01:42:32 -07:00
|
|
|
this.enqueueUploadTask(async () => {
|
|
|
|
const uploadUrl = await this.getUploadLink(index);
|
|
|
|
const uploadRequest = RNFS.uploadFiles({
|
|
|
|
toUrl: uploadUrl,
|
2024-02-05 01:58:19 -07:00
|
|
|
// @ts-ignore
|
2024-02-05 01:42:32 -07:00
|
|
|
files: [{ filepath: filepath }],
|
|
|
|
method: "PUT",
|
2024-02-05 01:52:37 -07:00
|
|
|
binaryStreamOnly: true,
|
2024-02-05 01:42:32 -07:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
},
|
|
|
|
begin: (res) => {
|
|
|
|
console.log("Start upload", res);
|
|
|
|
},
|
|
|
|
progress: (res) => {
|
|
|
|
console.log("Uploading", res);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
console.log(JSON.stringify(uploadRequest));
|
|
|
|
const result = await uploadRequest.promise;
|
|
|
|
if (result.statusCode === 200) {
|
|
|
|
console.log(`${filepath} Uploaded`);
|
|
|
|
} else {
|
|
|
|
console.error("SERVER ERROR");
|
|
|
|
}
|
2024-02-04 03:11:19 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUploadLink(chunkId: number): Promise<string> {
|
2024-02-05 01:42:32 -07:00
|
|
|
if (this.prefetchedUploadLinks[chunkId]) {
|
|
|
|
return this.prefetchedUploadLinks[chunkId];
|
|
|
|
}
|
2024-02-04 03:11:19 -07:00
|
|
|
return this.requestUploadLink(chunkId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async requestUploadLink(chunkId: number): Promise<string> {
|
2024-02-05 01:42:32 -07:00
|
|
|
console.log(`Requesting upload link for chunk ${chunkId}`);
|
2024-02-04 03:11:19 -07:00
|
|
|
const result = await this.client.mutate({
|
|
|
|
mutation: gql.GetUploadLinkDocument,
|
|
|
|
variables: { videoId: this.videoId, chunkIndex: chunkId },
|
|
|
|
});
|
2024-02-05 01:42:32 -07:00
|
|
|
this.prefetchedUploadLinks[chunkId] = result.data.getUploadLink.uploadUrl;
|
2024-02-04 03:11:19 -07:00
|
|
|
return result.data.getUploadLink.uploadUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
export default function CameraScreen({
|
|
|
|
route,
|
|
|
|
navigation,
|
|
|
|
}): React.ReactElement {
|
2024-02-04 03:11:19 -07:00
|
|
|
const apolloClient = useApolloClient();
|
|
|
|
const [createUpload, { data, loading, error }] =
|
|
|
|
gql.useCreateUploadStreamMutation();
|
|
|
|
|
|
|
|
const [uploadManager, setUploadManager] = useState(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
data &&
|
|
|
|
data.createUploadStream &&
|
|
|
|
data.createUploadStream.videoId &&
|
|
|
|
!uploadManager
|
|
|
|
) {
|
|
|
|
const newVideoId = data.createUploadStream.videoId;
|
|
|
|
console.log(`VideoId: ${newVideoId}`);
|
|
|
|
setUploadManager(new StreamUploadManager(apolloClient, newVideoId));
|
|
|
|
}
|
|
|
|
}, [data, uploadManager]);
|
2024-02-02 19:52:19 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const camera = useRef<Camera>(null);
|
|
|
|
const { hasPermission, requestPermission } = useCameraPermission();
|
|
|
|
const [isCameraInitialized, setIsCameraInitialized] =
|
|
|
|
useState<boolean>(false);
|
2024-02-02 19:52:19 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const isForeground = useIsForeground();
|
|
|
|
const isFocused = useIsFocused();
|
|
|
|
const isActive = isForeground && isFocused;
|
2024-01-30 01:15:23 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const onError = useCallback((error: CameraRuntimeError) => {
|
|
|
|
console.error(error);
|
|
|
|
}, []);
|
2024-01-30 15:08:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const onInitialized = useCallback(() => {
|
|
|
|
console.log("Camera initialized!");
|
|
|
|
setIsCameraInitialized(true);
|
2024-02-04 03:11:19 -07:00
|
|
|
createUpload({ variables: { videoName: "Test" } });
|
2024-02-03 20:23:31 -07:00
|
|
|
}, []);
|
2024-01-29 20:01:45 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const onMediaCaptured = useCallback((media: PhotoFile | VideoFile) => {
|
|
|
|
console.log(`Media captured! ${JSON.stringify(media)}`);
|
|
|
|
}, []);
|
2024-01-29 20:01:45 -07:00
|
|
|
|
2024-02-04 03:11:19 -07:00
|
|
|
const onVideoChunkReady = useCallback(
|
|
|
|
(event) => {
|
|
|
|
console.log(
|
|
|
|
`Chunk ready in react-native ${JSON.stringify(event.nativeEvent)}`,
|
|
|
|
);
|
|
|
|
uploadManager.uploadChunk(event.nativeEvent);
|
|
|
|
},
|
|
|
|
[uploadManager],
|
|
|
|
);
|
2024-01-29 20:01:45 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
if (!hasPermission) {
|
|
|
|
requestPermission();
|
|
|
|
// Error handling in case they refuse to give permission
|
|
|
|
}
|
2024-02-01 19:43:44 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const device = useCameraDevice("back");
|
|
|
|
const format = useCameraFormat(device, [
|
2024-02-05 01:42:32 -07:00
|
|
|
{ videoResolution: { width: 1920, height: 1080 } },
|
|
|
|
{ fps: 30 },
|
2024-02-04 03:11:19 -07:00
|
|
|
]);
|
2024-01-29 20:01:45 -07:00
|
|
|
|
2024-02-04 03:11:19 -07:00
|
|
|
// TODO(#60): setOrientation should be called when changes are detected
|
2024-02-03 20:23:31 -07:00
|
|
|
const [orientation, setOrientation] = useState<Orientation>("portrait");
|
2024-01-29 20:01:45 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const toggleOrientation = () => {
|
2024-02-04 03:11:19 -07:00
|
|
|
setOrientation((currentOrientation) =>
|
|
|
|
currentOrientation === "landscape-left" ? "portrait" : "landscape-left",
|
2024-02-03 20:23:31 -07:00
|
|
|
);
|
|
|
|
};
|
2024-01-30 17:35:11 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
// Replace with error handling
|
|
|
|
if (device === null) {
|
|
|
|
console.log(device);
|
|
|
|
return (
|
|
|
|
<Text>
|
|
|
|
Camera not available. Does user have permissions: {hasPermission}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
hasPermission && (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Camera
|
|
|
|
ref={camera}
|
|
|
|
style={StyleSheet.absoluteFill}
|
|
|
|
device={device}
|
|
|
|
format={format}
|
|
|
|
onInitialized={onInitialized}
|
|
|
|
onError={onError}
|
2024-02-03 21:06:47 -07:00
|
|
|
// @ts-ignore
|
2024-02-03 20:23:31 -07:00
|
|
|
onVideoChunkReady={onVideoChunkReady}
|
|
|
|
video={true}
|
2024-02-04 03:11:19 -07:00
|
|
|
orientation={orientation}
|
2024-02-03 20:23:31 -07:00
|
|
|
isActive={isActive}
|
|
|
|
/>
|
|
|
|
<View
|
|
|
|
style={
|
|
|
|
orientation === "portrait"
|
|
|
|
? styles.goBackPortrait
|
|
|
|
: styles.goBackLandscape
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button title="Go back" onPress={() => navigation.goBack()} />
|
|
|
|
</View>
|
|
|
|
<RecordingButton
|
|
|
|
style={[
|
|
|
|
styles.captureButton,
|
|
|
|
orientation === "portrait" ? styles.portrait : styles.landscape,
|
|
|
|
]}
|
|
|
|
camera={camera}
|
|
|
|
onMediaCaptured={onMediaCaptured}
|
|
|
|
enabled={isCameraInitialized}
|
|
|
|
/>
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.button,
|
|
|
|
orientation === "portrait"
|
|
|
|
? styles.togglePortrait
|
|
|
|
: styles.toggleLandscape,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
title="Toggle Orientation"
|
|
|
|
onPress={toggleOrientation}
|
|
|
|
color="#841584"
|
|
|
|
accessibilityLabel="Toggle camera orientation"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
);
|
2024-01-29 20:01:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2024-02-03 20:23:31 -07:00
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: "black",
|
|
|
|
},
|
|
|
|
captureButton: {
|
|
|
|
position: "absolute",
|
|
|
|
alignSelf: "center",
|
|
|
|
},
|
|
|
|
button: {
|
|
|
|
position: "absolute",
|
|
|
|
alignSelf: "center",
|
|
|
|
},
|
|
|
|
togglePortrait: {
|
|
|
|
bottom: 110, // needs refined
|
|
|
|
},
|
|
|
|
toggleLandscape: {
|
|
|
|
transform: [{ rotate: "90deg" }],
|
|
|
|
bottom: "43%", // Should come from SafeAreaProvider, hardcoded right now, should roughly appear above the button
|
|
|
|
left: 50, // needs refined
|
|
|
|
},
|
|
|
|
portrait: {
|
|
|
|
bottom: 20, // needs refined
|
|
|
|
},
|
|
|
|
landscape: {
|
|
|
|
bottom: "40%", // Should come from SafeAreaProvider
|
|
|
|
left: 20, // needs refined
|
|
|
|
},
|
|
|
|
goBackPortrait: {
|
|
|
|
position: "absolute",
|
|
|
|
top: 20, // or wherever you want the button to be positioned in portrait
|
|
|
|
left: 20, // or wherever you want the button to be positioned in portrait
|
|
|
|
},
|
|
|
|
goBackLandscape: {
|
|
|
|
position: "absolute",
|
|
|
|
top: 40,
|
|
|
|
right: 20,
|
|
|
|
transform: [{ rotate: "90deg" }],
|
|
|
|
},
|
2024-01-31 22:45:00 -07:00
|
|
|
});
|