forked from langhuihui/jessibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJessibuca.cpp
More file actions
364 lines (360 loc) · 10.7 KB
/
Jessibuca.cpp
File metadata and controls
364 lines (360 loc) · 10.7 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#ifdef EDITTIME
#undef __cplusplus
#define __cplusplus 201703L
#endif
#include "base.h"
#include <regex>
#define PROP(name, type) \
type name; \
val get##name() const \
{ \
return val(name); \
} \
void set##name(val value) \
{ \
name = value.as<type>(); \
emscripten_log(0, #name" = %d", name);\
}
extern "C"
{
extern void init(void);
}
int main()
{
init();
return 0;
}
struct Jessica
{
val wrapped;
bool flvHeadRead;
string buffer;
AUDIO_DECODER audioDecoder;
VIDEO_DECODER videoDecoder;
queue<VideoPacket> videoBuffers;
bool bufferIsPlaying = false;
int videoTimeoutId = 0;
bool waitFirstVideo = true;
bool waitFirstAudio = true;
clock_t lastDataTime = 0;
clock_t lastVideoTimeStamp = 0;
int bytesCount = 0;
PROP(isPlaying, bool)
PROP(flvMode, bool)
PROP(audioBuffer, int)
PROP(videoBuffer, int)
PROP(bps, double)
Jessica(val &&v) : wrapped(forward<val>(v)), isPlaying(false), flvMode(false), flvHeadRead(false), audioBuffer(12)
{
videoDecoder.jsObject = &wrapped;
}
template <typename... Args>
Jessica(Args &&... args) : wrapped(val::undefined())
{
}
virtual ~Jessica()
{
val::global("clearTimeout")(videoTimeoutId);
emscripten_log(0, "FlvDecoder release!\n");
}
void $play(const string &url)
{
if (isPlaying)
{
call<void>("close");
}
// smatch urlm;
// regex_match(url, urlm, regex("(ws|http)s?://([^/:]+)(:\\d+)?(/.*)?"));
// string protocol(urlm.str(1));
// if (!regex_match(urlm.str(2), regex("(.+\\.monibuca\\.com|localhost|[0-9\\.]+)"))){
// emscripten_log(1, "%s Unauthorized",urlm.str(2).c_str());
// return;
// }
isPlaying = true;
bool webgl = wrapped["isWebGL"].as<bool>();
emscripten_log(0, "webgl:%s", webgl ? "true" : "false");
videoDecoder.webgl = webgl;
flvMode = url.find(".flv") != string::npos;
lastDataTime = clock();
if(url.find("http") == 0) {
call<void>("fetch",url);
}else{
#ifdef WS_PREFIX
val ws = val::global("WebSocket").new_(WS_PREFIX + url);
#else
val ws = val::global("WebSocket").new_(url);
#endif
ws.set("binaryType", "arraybuffer");
ws.set("onmessage", bind("onData"));
// ws.set("onerror", bind("onError"));
wrapped.set("ws", ws);
}
}
void onFetchData(val evt){
if(evt["done"].as<bool>()){
call<void>("close");
}else{
onData(evt);
wrapped.call<void>("fetchNext");
}
}
void onData(val evt)
{
string data = evt["data"].as<string>();
bytesCount += data.length();
auto now = clock();
if (now > lastDataTime)
{
bps = bytesCount * 1000.0 / (now - lastDataTime);
lastDataTime = now;
bytesCount = 0;
}
if (flvMode)
{
buffer.append(data);
if (!flvHeadRead)
{
if (buffer.length() >= 13)
{
flvHeadRead = true;
buffer = buffer.substr(13);
}
}
else
{
while (buffer.length() > 3)
{
size_t length = 0;
auto attr = (u8*)(&length);
attr[2] = buffer[1];
attr[1] = buffer[2];
attr[0] = buffer[3];
if (buffer.length() < length + 15)
{
break;
}
unsigned int timestamp = 0;
attr = (u8*)(×tamp);
attr[2] = buffer[4];
attr[1] = buffer[5];
attr[0] = buffer[6];
IOBuffer payload(buffer.substr(11,length));
switch (buffer[0])
{
case 8:
decodeAudio(timestamp, payload);
break;
case 9:
decodeVideo(timestamp, payload);
break;
default:
emscripten_log(0, "unknow type: %d", buffer[0]);
break;
}
buffer = buffer.substr(length+15);
}
}
}
else
{
switch (data.at(0))
{
case 1:
{
IOBuffer b(move(data));
b >>= 1;
decodeAudio(b.readUInt32B(), b);
}
break;
case 2:
{
IOBuffer b(move(data));
b >>= 1;
decodeVideo(b.readUInt32B(), b);
}
break;
case 10:
{
wrapped["ws"].call<void>("send", val("[\"__bandwidth\"]"));
}
break;
default:
emscripten_log(1, "error type :%d", data.at(0));
break;
}
}
}
#ifdef USE_FFMPEG
void decodeAudio(clock_t timestamp, IOBuffer ms)
{
unsigned char flag = 0;
ms.readB<1>(flag);
int bytesCount = audioDecoder.decode(ms);
if(!bytesCount)return;
if (!waitFirstAudio)
{
call<void>("playAudioPlanar", int(audioDecoder.frame->data), bytesCount,timestamp);
}
else
{
call<void>("initAudioPlanar", audioDecoder.dec_ctx->channels, audioDecoder.dec_ctx->sample_rate);
waitFirstAudio = false;
}
}
#else
void decodeAudio(clock_t timestamp, IOBuffer ms)
{
if (ms[0] == 0xFF && (ms[1] & 0xF0) == 0xF0)
{
//ADTS 头
call<void>("playAudio", int(ms.point()), ms.length);
return;
}
unsigned char flag = 0;
ms.readB<1>(flag);
auto audioType = flag >> 4;
if (waitFirstAudio)
{
int channels = (flag & 1) + 1;
int rate = (flag >> 2) & 3;
switch (rate)
{
case 1:
rate = 11025;
break;
case 2:
rate = 22050;
break;
case 3:
rate = 44100;
break;
}
switch (audioType)
{
case 10: //AAC
// initAudio(audioBuffer * 1024, rate, channels);
audioDecoder.decode(ms);
initAudio(audioBuffer * 1024, audioDecoder.samplerate, audioDecoder.channels);
return;
case 11: //Speex
initAudio(50 * 320, 16000, channels);
break;
case 2: //MP3
initAudio(audioBuffer * 576, rate, channels);
break;
}
}
if (!waitFirstAudio && audioDecoder.decode(ms))
call<void>("playAudio",timestamp);
}
void initAudio(int frameCount, int samplerate, int channels)
{
waitFirstAudio = false;
audioDecoder.init(frameCount * channels * 2);
call<void>("initAudio", frameCount, samplerate, channels, (int)audioDecoder.outputBuffer >> 1);
}
#endif
void decodeVideo(clock_t _timestamp, IOBuffer data)
{
if (waitFirstVideo)
{
if (videoDecoder.isAVCSequence(data))
{
videoDecoder.decode(data);
waitFirstVideo = false;
emscripten_log(0, "video info set! video buffer: %dms",videoBuffer);
}
}
else if (data[1] == 1 || data[1] == 0)
{
if (_timestamp == 0 && lastVideoTimeStamp != 0)
return;
if(videoBuffer == 0){
videoDecoder.timestamp = _timestamp;
videoDecoder.decode(data);
}else{
videoBuffers.emplace(_timestamp, data);
auto &v = videoBuffers.front();
if(lastVideoTimeStamp - v.timestamp > videoBuffer){
if(!bufferIsPlaying){
bufferIsPlaying = true;
decodeVideoBuffer();
}
}
}
}
lastVideoTimeStamp = _timestamp;
}
void decodeVideoBuffer()
{
if (videoBuffers.size()>1)
{
auto &v = videoBuffers.front();
videoDecoder.timestamp = v.timestamp;
videoDecoder.decode(v.data);
videoBuffers.pop();
auto timeout = videoBuffers.front().timestamp - v.timestamp;
if(lastVideoTimeStamp - videoBuffers.front().timestamp > videoBuffer){
timeout=timeout>>1;
}
val::global("setTimeout")(bind("decodeVideoBuffer"),timeout);
return;
}
bufferIsPlaying = false;
}
val getBufferInfo() const
{
val &&result = val::object();
result.set("front", videoBuffers.front().timestamp);
result.set("back", videoBuffers.back().timestamp);
result.set("size", videoBuffers.size());
return result;
}
void $close()
{
val::global("clearTimeout")(videoTimeoutId);
videoBuffers = queue<VideoPacket>();
videoDecoder.clear();
audioDecoder.clear();
videoTimeoutId = 0;
waitFirstVideo = true;
waitFirstAudio = true;
bufferIsPlaying = false;
lastVideoTimeStamp = 0;
buffer.clear();
flvHeadRead = false;
}
val bind(const char *name)
{
return wrapped[name].call<val>("bind", wrapped);
}
template <typename ReturnType, typename... Args>
ReturnType call(const char *name, Args &&... args) const
{
return wrapped.call<ReturnType>(name, forward<Args>(args)...);
}
};
struct Jessibuca : public wrapper<Jessica>
{
EMSCRIPTEN_WRAPPER(Jessibuca)
};
#define FUNC(name) function(#name, &Jessica::name)
#undef PROP
#define PROP(name) property(#name, &Jessica::get##name, &Jessica::set##name)
EMSCRIPTEN_BINDINGS(Jessica)
{
class_<Jessica>("Jessica")
.FUNC($play)
.FUNC(onFetchData)
.FUNC(onData)
.FUNC($close)
.FUNC(decodeVideoBuffer)
.PROP(isPlaying)
.PROP(flvMode)
.PROP(audioBuffer)
.PROP(videoBuffer)
.PROP(bps)
.property("bufferInfo", &Jessica::getBufferInfo)
// .function("invoke", &H5LCBase::invoke, pure_virtual())
.allow_subclass<Jessibuca, val>("Jessibuca", constructor<val>());
}