-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathTakeSnapshot.tsx
198 lines (185 loc) · 4.7 KB
/
TakeSnapshot.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os from 'os';
import {
ClientRoleType,
ErrorCodeType,
RtcConnection,
} from 'agora-electron-sdk';
import React, {
ReactElement,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import {
AgoraButton,
AgoraDivider,
AgoraDropdown,
AgoraImage,
AgoraStyle,
} from '../../../components/ui';
import { arrayToItems } from '../../../utils';
import * as log from '../../../utils/log';
import { BaseComponent } from '../components/BaseComponent';
import BaseRenderChannel from '../components/BaseRenderChannel';
import BaseRenderUsers from '../components/BaseRenderUsers';
import useInitRtcEngine from '../hooks/useInitRtcEngine';
export default function TakeSnapshot() {
const [enableVideo] = useState<boolean>(true);
const {
channelId,
setChannelId,
token,
uid,
joinChannelSuccess,
remoteUsers,
engine,
} =
/**
* Step 1: initRtcEngine
*/
useInitRtcEngine(enableVideo);
const [targetUid, setTargetUid] = useState<number>(0);
const [osFilePath] = useState<string>(os.homedir());
const timestamp = useRef<number>(0);
const [takeSnapshot, setTakeSnapshot] = useState<boolean>(false);
/**
* Step 2: joinChannel
*/
const joinChannel = () => {
if (!channelId) {
log.error('channelId is invalid');
return;
}
if (uid < 0) {
log.error('uid is invalid');
return;
}
// start joining channel
// 1. Users can only see each other after they join the
// same channel successfully using the same app id.
// 2. If app certificate is turned on at dashboard, token is needed
// when joining channel. The channel name and uid used to calculate
// the token has to match the ones used for channel join
engine.current.joinChannel(token, channelId, uid, {
// Make myself as the broadcaster to send stream to remote
clientRoleType: ClientRoleType.ClientRoleBroadcaster,
});
};
/**
* Step 3: takeSnapshot
*/
const _takeSnapshot = () => {
if (!osFilePath) {
log.error('filePath is invalid');
return;
}
timestamp.current = new Date().getTime();
engine.current.takeSnapshot(
targetUid,
`${osFilePath}/${targetUid}-${timestamp.current}.jpg`
);
setTakeSnapshot(false);
};
/**
* Step 4: leaveChannel
*/
const leaveChannel = () => {
engine.current.leaveChannel();
};
const onSnapshotTaken = useCallback(
(
connection: RtcConnection,
uid: number,
filePath: string,
width: number,
height: number,
errCode: number
) => {
log.info(
'onSnapshotTaken',
'connection',
connection,
'uid',
uid,
'filePath',
filePath,
'width',
width,
'height',
height,
'errCode',
errCode
);
if (filePath === `${osFilePath}/${targetUid}-${timestamp.current}.jpg`) {
setTakeSnapshot(errCode === ErrorCodeType.ErrOk);
}
},
[osFilePath, targetUid]
);
useEffect(() => {
engine.current.addListener('onSnapshotTaken', onSnapshotTaken);
const engineCopy = engine.current;
return () => {
engineCopy.removeListener('onSnapshotTaken', onSnapshotTaken);
};
}, [engine, onSnapshotTaken]);
return (
<BaseComponent
name={'TakeSnapshot'}
renderConfiguration={renderConfiguration}
renderChannel={() => (
<BaseRenderChannel
channelId={channelId}
joinChannel={joinChannel}
leaveChannel={leaveChannel}
joinChannelSuccess={joinChannelSuccess}
onChannelIdChange={setChannelId}
/>
)}
renderUsers={() => (
<BaseRenderUsers
enableVideo={enableVideo}
joinChannelSuccess={joinChannelSuccess}
remoteUsers={remoteUsers}
/>
)}
renderAction={renderAction}
/>
);
function renderConfiguration(): ReactElement | undefined {
return (
<>
<AgoraDropdown
title={'TakeSnapshot'}
items={arrayToItems([0, ...remoteUsers])}
value={targetUid}
onValueChange={(value) => {
setTargetUid(value);
setTakeSnapshot(false);
}}
/>
{takeSnapshot ? (
<>
<AgoraDivider />
<AgoraImage
style={AgoraStyle.image}
source={`file://${osFilePath}/${targetUid}-${timestamp.current}.jpg`}
/>
</>
) : undefined}
</>
);
}
function renderAction(): ReactElement | undefined {
return (
<>
<AgoraButton
disabled={!joinChannelSuccess}
title={`take Snapshot`}
onPress={_takeSnapshot}
/>
</>
);
}
}