-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathApp.js
316 lines (277 loc) · 13.5 KB
/
App.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Sample for Pili react-native SDK
*/
import merge from 'merge'
import React, { Component } from 'react'
import { SafeAreaView, Text, StatusBar, ScrollView, View, Button, Platform, PermissionsAndroid, TextInput } from 'react-native'
import { consts, Streaming } from 'pili-streaming-react-native'
import { FileInput, AvCodecTypeInput, CameraResolutionInput, CameraFocusModeInput, CameraVideoOrientationInput, MicrophoneSampleRateInput, MicrophoneChannelInput, SwitchInput, VideoEncodeOrientationInput, VideoH264ProfileInput, BitrateAdjustModeInput, EncoderRCModeInput, CameraInput, NumberInput } from './components/Input'
const isAndroid = Platform.OS === 'android'
export default class App extends Component {
state = {
androidPermissionGranted: false,
state: null,
streamInfo: null,
audioMixProgress: null,
streamingConfigInput: '',
streamingConfigError: null,
streamingConfig: {
rtmpURL: 'rtmp://pili-publish.qnsdk.com/sdk-live/111',
camera: 'back',
muted: false,
zoom: 0,
focus: false,
started: true,
faceBeautyEnable: false,
faceBeautySetting: {
beautyLevel: 1,
whiten: 1,
redden: 0.8,
},
watermarkSetting: {
src: null, // or `''`?
alpha: 122,
position: {
x: 0,
y: 0
},
size: {
width: 50,
height: 20
},
},
pictureStreamingFile: null,
pictureStreamingEnable: false,
torchEnable: false,
previewMirrorEnable: false,
encodingMirrorEnable: false,
audioMixFile: {
filePath: null, // or `''`?
loop: true,
},
playMixAudio: false,
audioMixVolume: {
micVolume: 0.5,
musicVolume: 0.5,
},
playbackEnable: false,
profile: {
videoStreamingSetting: {
fps: 30,
bps: 1000 * 1024,
maxFrameInterval: 60,
encodeOrientation: consts.videoEncodeOrientations.portrait,
h264Profile: (
isAndroid
? consts.videoH264Profiles_android.baseline
: consts.videoH264Profiles_iOS.baseline31
),
customVideoEncodeSize: {
width: 1024,
height: 720
}
},
audioStreamingSetting: {
rate: 44100,
bitrate: 96 * 1024,
},
encodingSize: consts.videoEncodings.e480,
avCodecType: (
isAndroid
? consts.avCodecTypes_android.SW_VIDEO_WITH_SW_AUDIO_CODEC
: consts.avCodecTypes_iOS.PLH264EncoderType_AVFoundation
),
cameraStreamingSetting: {
resolution: (
isAndroid
? consts.cameraResolutions_android.MEDIUM_RATIO_16_9
: consts.cameraResolutions_iOS.AVCaptureSessionPresetMedium
),
focusMode: consts.cameraFocusModes.continuousVideo,
videoOrientation: consts.cameraVideoOrientations.portrait
},
microphoneSteamingSetting: {
sampleRate: consts.microphoneSampleRates.r44100,
channel: consts.microphoneChannels.mono,
isAecEnable: false
},
quicEnable: false,
bitrateAdjustMode: consts.bitrateAdjustModes.auto,
adaptiveBitrateRange: {
minBitrate: 1024,
maxBitrate: 1024*1024,
},
encoderRCMode: consts.encoderRCModes.bitratePriority,
streamInfoUpdateInterval: 5,
},
},
}
handleStateChange = state => this.setState({ state })
handleStreamInfoChange = streamInfo => this.setState({ streamInfo })
handleAudioMixProgress = audioMixProgress => this.setState({ audioMixProgress })
handleStreamingConfigInputChange = text => this.setState({ streamingConfigInput: text })
handleStreamingConfigInputSubmit = () => {
this.setState({ streamingConfigError: null })
try {
const toMerge = JSON.parse(this.state.streamingConfigInput)
const streamingConfig = merge.recursive(true, this.state.streamingConfig, toMerge)
this.setState({ streamingConfig })
} catch (e) {
this.setState({ streamingConfigError: e && e.message })
}
}
useStateOfPath = keyPath => {
const toMerge = {}
const keys = keyPath.split('.')
const lastKey = keys[keys.length - 1]
const lastObj = keys.slice(0, -1).reduce((obj, key) => {
return obj[key] = {}
}, toMerge)
const value = keys.reduce((obj, key) => obj[key], this.state)
const onChange = value => {
lastObj[lastKey] = value
const newState = merge.recursive(true, this.state, toMerge)
this.setState(newState)
}
return [value, onChange]
}
bindStateOfPath = keyPath => {
const [value, onChange] = this.useStateOfPath(keyPath)
return { value, onChange }
}
componentDidMount() {
if (isAndroid) {
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION
]).then(() => {
this.setState({ androidPermissionGranted: true })
})
}
}
render() {
const {
androidPermissionGranted,
state,
streamInfo,
audioMixProgress,
streamingConfigInput,
streamingConfigError,
streamingConfig
} = this.state
if (isAndroid && !androidPermissionGranted) {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={{ display: 'flex', flex: 1, backgroundColor: '#fff' }}>
<Text>Permission not granted</Text>
</SafeAreaView>
</>
)
}
const streamingConfigErrorText = (
streamingConfigError != null
? <Text style={{ color: 'red' }}>{streamingConfigError}</Text>
: null
)
const stateText = state != null ? state : 'none'
const streamInfoText = streamInfo != null ? JSON.stringify(streamInfo) : 'none'
const audioMixProgressText = audioMixProgress != null ? JSON.stringify(audioMixProgress) : 'none'
const props = {
...streamingConfig,
// TODO: 后续不再需要
profile: {
...streamingConfig.profile,
video: streamingConfig.profile.videoStreamingSetting,
audio: streamingConfig.profile.audioStreamingSetting,
},
onStateChange: this.handleStateChange,
onStreamInfoChange: this.handleStreamInfoChange,
onAudioMixProgress: this.handleAudioMixProgress,
style: {
width: '100%',
height: 200,
backgroundColor: 'transparent',
borderBottomColor: '#333',
borderBottomWidth: 1,
},
}
const streamingConfigText = JSON.stringify(streamingConfig, null, 2)
this.state.streamingConfig.audioMixFile.filePath
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={{ display: 'flex', flex: 1 }}>
<Streaming {...props} />
<ScrollView style={{ flex: 1, backgroundColor : 'white', padding: 10 }}>
<View style={{ display: 'none' }}>
<TextInput
multiline
numberOfLines={4}
value={streamingConfigInput}
onChangeText={this.handleStreamingConfigInputChange}
placeholder={'请输入 JSON 格式的配置,如 { "camera": "front" }'}
style={{ backgroundColor: '#f0f0f0', lineHeight: 30, height: 120 }}
/>
{streamingConfigErrorText}
<Button title="提交" onPress={this.handleStreamingConfigInputSubmit} />
</View>
<SwitchInput label="开始推流" {...this.bindStateOfPath('streamingConfig.started')} />
<SwitchInput label="静音" {...this.bindStateOfPath('streamingConfig.muted')} />
<SwitchInput label="手动对焦" {...this.bindStateOfPath('streamingConfig.focus')} />
<CameraInput {...this.bindStateOfPath('streamingConfig.camera')} />
<AvCodecTypeInput {...this.bindStateOfPath('streamingConfig.profile.avCodecType')} />
<CameraResolutionInput {...this.bindStateOfPath('streamingConfig.profile.cameraStreamingSetting.resolution')} />
<CameraFocusModeInput {...this.bindStateOfPath('streamingConfig.profile.cameraStreamingSetting.focusMode')} />
<CameraVideoOrientationInput {...this.bindStateOfPath('streamingConfig.profile.cameraStreamingSetting.videoOrientation')} />
<MicrophoneSampleRateInput {...this.bindStateOfPath('streamingConfig.profile.microphoneSteamingSetting.sampleRate')} />
<MicrophoneChannelInput {...this.bindStateOfPath('streamingConfig.profile.microphoneSteamingSetting.channel')} />
<SwitchInput label="回音消除" {...this.bindStateOfPath('streamingConfig.profile.microphoneSteamingSetting.isAecEnable')} />
<VideoEncodeOrientationInput {...this.bindStateOfPath('streamingConfig.profile.videoStreamingSetting.encodeOrientation')} />
<VideoH264ProfileInput {...this.bindStateOfPath('streamingConfig.profile.videoStreamingSetting.h264Profile')} />
<NumberInput label="自定义视频编码分辨率宽度" {...this.bindStateOfPath('streamingConfig.profile.videoStreamingSetting.customVideoEncodeSize.width')} />
<NumberInput label="自定义视频编码分辨率高度" {...this.bindStateOfPath('streamingConfig.profile.videoStreamingSetting.customVideoEncodeSize.height')} />
<NumberInput label="音频采样率" {...this.bindStateOfPath('streamingConfig.profile.audioStreamingSetting.rate')} />
<NumberInput label="音频码率" {...this.bindStateOfPath('streamingConfig.profile.audioStreamingSetting.bitrate')} />
<SwitchInput label="使用 QUIC 协议" {...this.bindStateOfPath('streamingConfig.profile.quicEnable')} />
<BitrateAdjustModeInput {...this.bindStateOfPath('streamingConfig.profile.bitrateAdjustMode')} />
<NumberInput label="自适应码率调节范围下限" {...this.bindStateOfPath('streamingConfig.profile.adaptiveBitrateRange.minBitrate')} />
<NumberInput label="自适应码率调节范围上限" {...this.bindStateOfPath('streamingConfig.profile.adaptiveBitrateRange.maxBitrate')} />
<EncoderRCModeInput {...this.bindStateOfPath('streamingConfig.profile.encoderRCMode')} />
<NumberInput label="streamInfo 更新间隔" {...this.bindStateOfPath('streamingConfig.profile.streamInfoUpdateInterval')} />
<SwitchInput label="内置美颜" {...this.bindStateOfPath('streamingConfig.faceBeautyEnable')} />
<NumberInput label="内置美颜磨皮程度" {...this.bindStateOfPath('streamingConfig.faceBeautySetting.beautyLevel')} />
<NumberInput label="内置美颜美白程度" {...this.bindStateOfPath('streamingConfig.faceBeautySetting.whiten')} />
<NumberInput label="内置美颜红润程度" {...this.bindStateOfPath('streamingConfig.faceBeautySetting.redden')} />
<FileInput label="水印文件" {...this.bindStateOfPath('streamingConfig.watermarkSetting.src')} initialFromUrl="http://oyojsr1f8.bkt.clouddn.com/qiniu_logo.png" />
<NumberInput label="水印文件透明度" {...this.bindStateOfPath('streamingConfig.watermarkSetting.alpha')} />
<NumberInput label="水印水平位置" {...this.bindStateOfPath('streamingConfig.watermarkSetting.position.x')} />
<NumberInput label="水印垂直位置" {...this.bindStateOfPath('streamingConfig.watermarkSetting.position.y')} />
<NumberInput label="水印宽度" {...this.bindStateOfPath('streamingConfig.watermarkSetting.size.width')} />
<NumberInput label="水印高度" {...this.bindStateOfPath('streamingConfig.watermarkSetting.size.height')} />
<SwitchInput label="图片推流" {...this.bindStateOfPath('streamingConfig.pictureStreamingEnable')} />
<FileInput label="图片推流文件" {...this.bindStateOfPath('streamingConfig.pictureStreamingFile')} initialFromUrl="http://oyojsr1f8.bkt.clouddn.com/pause_publish.png" />
<SwitchInput label="开启闪光灯" {...this.bindStateOfPath('streamingConfig.torchEnable')} />
<SwitchInput label="预览镜像设置" {...this.bindStateOfPath('streamingConfig.previewMirrorEnable')} />
<SwitchInput label="编码镜像设置" {...this.bindStateOfPath('streamingConfig.encodingMirrorEnable')} />
<SwitchInput label="播放混音文件" {...this.bindStateOfPath('streamingConfig.playMixAudio')} />
<FileInput label="混音文件" {...this.bindStateOfPath('streamingConfig.audioMixFile.filePath')} initialFromUrl="http://oyojsr1f8.bkt.clouddn.com/Lovestoned-Thursdays.mp3" />
<SwitchInput label="混音文件循环播放" {...this.bindStateOfPath('streamingConfig.audioMixFile.loop')} />
<NumberInput label="混音音量 micVolume" {...this.bindStateOfPath('streamingConfig.audioMixVolume.micVolume')} />
<NumberInput label="混音音量 musicVolume" {...this.bindStateOfPath('streamingConfig.audioMixVolume.musicVolume')} />
<SwitchInput label="返听功能" {...this.bindStateOfPath('streamingConfig.playbackEnable')} />
<Text>Pili@ReactNative</Text>
<Text>State: {stateText}</Text>
<Text>StreamInfo: {streamInfoText}</Text>
<Text>AudioMixProgress: {audioMixProgressText}</Text>
<Text>streamingConfig: </Text>
<Text>{streamingConfigText}</Text>
</ScrollView>
</SafeAreaView>
</>
)
}
}