forked from iizukanao/node-rtsp-rtmp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avstreams.coffee
224 lines (196 loc) · 6.81 KB
/
avstreams.coffee
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
h264 = require './h264'
EventEmitterModule = require './event_emitter'
logger = require './logger'
class AVStream
constructor: (id) ->
@id = id # string
@initAVParams()
initAVParams: ->
@audioClockRate = null # int
@audioSampleRate = null # int
@audioChannels = null # int
@audioPeriodSize = 1024 # TODO: detect this from stream?
@audioObjectType = null # int
@videoWidth = null # int
@videoHeight = null # int
@videoProfileLevelId = null # string (e.g. '42C01F')
@videoFrameRate = 30.0 # float # TODO: default value
@videoAVCLevel = null # int
@videoAVCProfile = null # int
@isVideoStarted = false # boolean
@isAudioStarted = false # boolean
@timeAtVideoStart = null # milliseconds since the epoch
@timeAtAudioStart = null # milliseconds since the epoch
@spsString = '' # string
@ppsString = '' # string
@spsNALUnit = null # buffer
@ppsNALUnit = null # buffer
@spropParameterSets = '' # string
reset: ->
logger.debug "[stream:#{@id}] reset"
@initAVParams()
@emit 'reset'
updateSpropParam: (buf) ->
nalUnitType = buf[0] & 0x1f
if nalUnitType is 7 # SPS packet
@spsString = buf.toString 'base64'
@videoProfileLevelId = buf[1..3].toString('hex').toUpperCase()
else if nalUnitType is 8 # PPS packet
@ppsString = buf.toString 'base64'
@spropParameterSets = @spsString + ',' + @ppsString
resetFrameRate: ->
@frameRateCalcBasePTS = null
@frameRateCalcNumFrames = null
@videoFrameRate = 30.0 # TODO: What value should we use?
calcFrameRate: (pts) ->
if @frameRateCalcBasePTS?
diffMs = (pts - @frameRateCalcBasePTS) / 90
@frameRateCalcNumFrames++
if (@frameRateCalcNumFrames >= 150) or (diffMs >= 5000)
frameRate = @frameRateCalcNumFrames * 1000 / diffMs
if frameRate isnt @videoFrameRate
logger.debug "[stream:#{@id}] frame rate: #{@videoFrameRate}"
@videoFrameRate = frameRate
@emit 'update_frame_rate', frameRate
@frameRateCalcBasePTS = pts
@frameRateCalcNumFrames = 0
else
@frameRateCalcBasePTS = pts
@frameRateCalcNumFrames = 0
updateConfig: (obj) ->
isConfigUpdated = false
for name, value of obj
if @[name] isnt value
@[name] = value
if value instanceof Buffer
logger.debug "[stream:#{@id}] update #{name}: Buffer=<0x#{value.toString 'hex'}>"
else if typeof(value) is 'object'
logger.debug "[stream:#{@id}] update #{name}:"
logger.debug value
else
logger.debug "[stream:#{@id}] update #{name}: #{value}"
if name is 'audioASCInfo'
if value.sbrPresentFlag is 1
if value.psPresentFlag is 1
logger.debug "[stream:#{@id}] audio: HE-AAC v2"
else
logger.debug "[stream:#{@id}] audio: HE-AAC v1"
isConfigUpdated = true
if isConfigUpdated
@emit 'updateConfig'
# nal_unit_type 7
updateSPS: (nalUnit) ->
if (not @spsNALUnit?) or (nalUnit.compare(@spsNALUnit) isnt 0)
@spsNALUnit = nalUnit
@updateSpropParam nalUnit
try
sps = h264.readSPS nalUnit
catch e
logger.error "[stream:#{@id}] video data error: failed to read SPS"
logger.error e.stack
return
frameSize = h264.getFrameSize sps
isConfigUpdated = false
if @videoWidth isnt frameSize.width
@videoWidth = frameSize.width
logger.debug "[stream:#{@id}] video width: #{@videoWidth}"
isConfigUpdated = true
if @videoHeight isnt frameSize.height
@videoHeight = frameSize.height
logger.debug "[stream:#{@id}] video height: #{@videoHeight}"
isConfigUpdated = true
if @videoAVCLevel isnt sps.level_idc
@videoAVCLevel = sps.level_idc
logger.debug "[stream:#{@id}] video avclevel: #{@videoAVCLevel}"
isConfigUpdated = true
if @videoAVCProfile isnt sps.profile_idc
@videoAVCProfile = sps.profile_idc
logger.debug "[stream:#{@id}] video avcprofile: #{@videoAVCProfile}"
isConfigUpdated = true
if isConfigUpdated
logger.debug "[stream:#{@id}] updated SPS"
@emit 'updateConfig'
# nal_unit_type 8
updatePPS: (nalUnit) ->
if (not @ppsNALUnit?) or (nalUnit.compare(@ppsNALUnit) isnt 0)
logger.debug "[stream:#{@id}] updated PPS"
@ppsNALUnit = nalUnit
@updateSpropParam nalUnit
@emit 'updateConfig'
toString: ->
str = "#{@id}: "
if @videoWidth?
str += "video: #{@videoWidth}x#{@videoHeight} profile=#{@videoAVCProfile} level=#{@videoAVCLevel}"
else
str += "video: (waiting for data)"
if @audioSampleRate?
str += "; audio: samplerate=#{@audioSampleRate} channels=#{@audioChannels} objecttype=#{@audioObjectType}"
else
str += "; audio: (waiting for data)"
return str
EventEmitterModule.mixin AVStream
eventListeners = {}
streams = {}
api =
AVStream: AVStream
emit: (name, data...) ->
if eventListeners[name]?
for listener in eventListeners[name]
listener data...
return
on: (name, listener) ->
if eventListeners[name]?
eventListeners[name].push listener
else
eventListeners[name] = [ listener ]
removeListener: (name, listener) ->
if eventListeners[name]?
for _listener, i in eventListeners[name]
if _listener is listener
eventListeners[i..i] = [] # remove element at index i
return
getAll: ->
return streams
exists: (streamId) ->
return streams[streamId]?
get: (streamId) ->
return streams[streamId]
create: (streamId) ->
stream = new AVStream streamId
api.emit 'new', stream
api.add stream
return stream
getOrCreate: (streamId) ->
stream = streams[streamId]
if not stream?
stream = api.create streamId
return stream
add: (stream) ->
if streams[stream.id]?
logger.warn "warning: overwriting stream: #{stream.id}"
streams[stream.id] = stream
api.emit 'add_stream', stream
stream._onAnyListener = ((stream) ->
(eventName, data...) ->
api.emit eventName, stream, data...
)(stream)
stream.onAny stream._onAnyListener
remove: (streamId) ->
if typeof(streamId) is 'object'
# streamId argument might be stream object
stream = streamId
streamId = stream?.id
else
stream = streams[streamId]
if stream?
stream.offAny stream._onAnyListener
api.emit 'remove_stream', stream
delete streams[streamId]
clear: ->
streams = {}
api.emit 'clear_streams'
dump: ->
logger.raw "[streams: #{Object.keys(streams).length}]"
for streamId, stream of streams
logger.raw " " + stream.toString()
module.exports = api