-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
187 lines (148 loc) · 4.8 KB
/
index.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
import React from 'react'
import { requireNativeComponent, Text, View, UIManager, findNodeHandle, AppState, Platform } from 'react-native'
const PropTypes = require('prop-types')
export const GstState = {
VOID_PENDING: 0,
NULL: 1,
READY: 2,
PAUSED: 3,
PLAYING: 4
}
export default class GstPlayer extends React.Component {
currentGstState = undefined
appState = "active"
isInitialized = false
componentDidMount() {
this.playerHandle = findNodeHandle(this.playerViewRef)
AppState.addEventListener('change', this.appStateChanged)
}
componentWillUnmount() {
AppState.removeEventListener('change', this.appStateChanged)
}
appStateChanged = (nextAppState) => {
if (this.appState.match(/inactive|background/) && nextAppState === 'active') {
// On iOS we need to recreate video sink to bypass a bug with vtdec when video freezes
if (Platform.OS === 'ios') {
this.recreateView()
}
this.play()
} else {
this.stop()
}
this.appState = nextAppState
}
// Callbacks
onPlayerInit() {
this.isInitialized = true
if (this.props.onPlayerInit)
this.props.onPlayerInit()
}
onStateChanged(_message) {
const { old_state, new_state } = _message.nativeEvent
this.currentGstState = new_state
if (old_state === GstState.PAUSED && new_state === GstState.READY) {
// On iOS we need to recreate video sink to bypass a bug with vtdec when video freezes
if (Platform.OS === 'ios')
this.recreateView()
}
if (this.props.onStateChanged)
this.props.onStateChanged(old_state, new_state)
}
onVolumeChanged(_message) {
const { rms, peak, decay } = _message.nativeEvent
if (this.props.onVolumeChanged)
this.props.onVolumeChanged(rms, peak, decay)
}
onUriChanged(_message) {
const { new_uri } = _message.nativeEvent
if (this.props.onUriChanged) {
this.props.onUriChanged(new_uri)
}
if (this.props.autoPlay) {
this.play()
}
}
onEOS() {
if (this.props.onEOS)
this.props.onEOS()
}
onElementError(_message) {
const { source, message, debug_info } = _message.nativeEvent
if (this.props.onElementError)
this.props.onElementError(source, message, debug_info)
}
shouldComponentUpdate() {
return true
}
// Methods
setGstState(state) {
UIManager.dispatchViewManagerCommand(
this.playerHandle,
UIManager.RCTGstPlayer.Commands.setState,
[state]
)
}
// Player state shortcuts
play() {
this.setGstState(GstState.PLAYING)
}
pause() {
this.setGstState(GstState.PAUSED)
}
stop() {
this.setGstState(GstState.READY)
}
// Helper methods
recreateView() {
UIManager.dispatchViewManagerCommand(
this.playerHandle,
UIManager.RCTGstPlayer.Commands.recreateView,
[]
)
}
render() {
return (
<RCTGstPlayer
autoPlay={this.props.autoPlay}
uri={this.props.uri || undefined}
audioLevelRefreshRate={this.props.audioLevelRefreshRate !== undefined ? this.props.audioLevelRefreshRate : 100}
isDebugging={this.props.isDebugging !== undefined ? this.props.isDebugging : false}
onPlayerInit={this.onPlayerInit.bind(this)}
onStateChanged={this.onStateChanged.bind(this)}
onVolumeChanged={this.onVolumeChanged.bind(this)}
onUriChanged={this.onUriChanged.bind(this)}
onEOS={this.onEOS.bind(this)}
onElementError={this.onElementError.bind(this)}
ref={(playerView) => this.playerViewRef = playerView}
{...this.props}
/>
)
}
}
GstPlayer.propTypes = {
// Props
uri: PropTypes.string,
autoPlay: PropTypes.bool,
audioLevelRefreshRate: PropTypes.number,
isDebugging: PropTypes.bool,
// Events callbacks
onPlayerInit: PropTypes.func,
onStateChanged: PropTypes.func,
onVolumeChanged: PropTypes.func,
onUriChanged: PropTypes.func,
onEOS: PropTypes.func,
onElementError: PropTypes.func,
// Methods
setGstState: PropTypes.func,
play: PropTypes.func,
pause: PropTypes.func,
stop: PropTypes.func,
// Helper methods
createDrawableSurface: PropTypes.func,
destroyDrawableSurface: PropTypes.func,
recreateView: PropTypes.func,
...View.propTypes
}
const RCTGstPlayer = requireNativeComponent('RCTGstPlayer', GstPlayer, {
nativeOnly: { onChange: true }
})