forked from langhuihui/jessibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetStream.h
379 lines (344 loc) · 9.74 KB
/
NetStream.h
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#pragma once
#include "Broadway\basetype.h"
#include "Broadway\H264SwDecApi.h"
#include <speex\speex.h>
#include <time.h>
class NetConnection;
int yuv2rgbcalc(int y,int u,int v) {
int a0 = 1192 * (y - 16);
int a1 = 1634 * (v - 128);
int a2 = 832 * (v - 128);
int a3 = 400 * (u - 128);
int a4 = 2066 * (u - 128);
int r = (a0 + a1) >> 10 ;
int g = (a0 - a2 - a3) >> 10 ;
int b = (a0 + a4) >> 10 ;
if ((r & 255) != r ) {
r = min(255, max(0, r ) ) ;
}
if ((g & 255) != g ) {
g = min(255, max(0, g ) ) ;
}
if ((b & 255) != b ) {
b = min(255, max(0, b ) ) ;
}
return (((((255 << 8) + b) << 8) + g) << 8) + r;
};
class NetStream
{
private:
H264SwDecInst decInst;
H264SwDecInput decInput;
H264SwDecOutput decOutput;
H264SwDecPicture decPicture;
H264SwDecInfo decInfo;
u32 picDecodeNumber;
u32 picDisplayNumber;
u32 picSize;
u8* heap;
i16* audioOutput;
int audioBufferLength;
u8* audioBuffer;
int audioBufferCounts;
void* speexState;
SpeexBits speexBits;
bool canvasAttached;
void broadwayOnPictureDecoded(u8* buffer,u32 width,u32 height) {
if (!jsThis)return;
auto outSize = width * height << 2;
if (webgl) {
if (!heap) {
heap = buffer;
jsThis->call<void>("setVideoSize", width, height, (int)heap);
}
jsThis->call<void>("draw",(int)buffer);
}
else {
if (!heap) {
emscripten_log(0, "canvas:%d,%d", width,height);
//auto lumaSize = width * height;
//auto chromaSize = lumaSize >> 2;
//auto inpSize = lumaSize + chromaSize + chromaSize;
auto cacheSize = 0x2000000;
auto size = outSize + cacheSize;//inpSize + outSize + cacheSize;
auto chunkSize = 0x1000000;
auto heapSize = chunkSize;
while (heapSize < size) {
heapSize += chunkSize;
};
heap = (u8*)malloc(heapSize);
jsThis->call<void>("setVideoSize", width, height, (int)heap);
};
int ystart = 0;
int ustart = 0;
int vstart = 0;
int y = 0;
int yn = 0;
int u = 0;
int v = 0;
u32* o = 0;
int line = 0;
int col = 0;
int usave = 0;
int vsave = 0;
int ostart = 0;
int cacheAdr = 0;
ustart = ystart + width * height;
vstart = ustart +( width * height>>2);
u8* cacheStart = heap+outSize;
for (line = 0; line < height; line+=2) {
usave = ustart;
vsave = vstart;
for (col = 0; col < width; col +=2) {
y = buffer[ystart];
yn = buffer[ystart + width];
u = buffer[ustart];
v = buffer[vstart];
cacheAdr = (y << 16) + (u << 8)+ v ;
o =(u32*)(cacheStart + cacheAdr);
if (!*o) *o = yuv2rgbcalc(y, u, v);
*(u32*)(heap+ostart) = *o;
cacheAdr = ((yn << 16) + (u << 8)) + v;
o = (u32*)(cacheStart + cacheAdr);
if (!*o) *o = yuv2rgbcalc(yn, u, v);
*(u32*)(heap + (ostart + (width << 2))) = *o;
//yuv2rgb5(y, u, v, ostart);
//yuv2rgb5(yn, u, v, (ostart + widthFour)|0);
ostart+=4;
// next step only for y. u and v stay the same
ystart++;
y = buffer[ystart];
yn = buffer[(ystart + width)];
//yuv2rgb5(y, u, v, ostart);
cacheAdr = (y << 16) + (u << 8)+ v ;
o = (u32*)(cacheStart + cacheAdr);
if (!*o) *o = yuv2rgbcalc(y, u, v);
*(u32*)(heap + ostart) = *o;
//yuv2rgb5(yn, u, v, (ostart + widthFour)|0);
cacheAdr = ((yn << 16) + (u << 8)) + v;
o = (u32*)(cacheStart + cacheAdr);
if (!*o) *o = yuv2rgbcalc(yn, u, v);
*(u32*)(heap + (ostart + (width << 2))) = *o;
ostart+=4;
//all positions inc 1
ystart ++;
ustart ++;
vstart ++;
};
ostart += (width << 2);
ystart += width;
};
jsThis->call<void>("draw");
}
}
u32 broadwayDecode() {
decInput.picId = picDecodeNumber;
H264SwDecRet ret = H264SwDecDecode(decInst, &decInput, &decOutput);
switch (ret) {
case H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY:
/* Stream headers were successfully decoded, thus stream information is available for query now. */
ret = H264SwDecGetInfo(decInst, &decInfo);
if (ret != H264SWDEC_OK) {
return -1;
}
picSize = decInfo.picWidth * decInfo.picHeight;
picSize = (3 * picSize) / 2;
//broadwayOnHeadersDecoded();
decInput.dataLen -= decOutput.pStrmCurrPos - decInput.pStream;
decInput.pStream = decOutput.pStrmCurrPos;
break;
case H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY:
/* Picture is ready and more data remains in the input buffer,
* update input structure.
*/
decInput.dataLen -= decOutput.pStrmCurrPos - decInput.pStream;
decInput.pStream = decOutput.pStrmCurrPos;
/* fall through */
case H264SWDEC_PIC_RDY:
if (ret == H264SWDEC_PIC_RDY) {
decInput.dataLen = 0;
}
/* Increment decoding number for every decoded picture */
picDecodeNumber++;
while (H264SwDecNextPicture(decInst, &decPicture, 0) == H264SWDEC_PIC_RDY) {
// printf(" Decoded Picture Decode: %d, Display: %d, Type: %s\n", picDecodeNumber, picDisplayNumber, decPicture.isIdrPicture ? "IDR" : "NON-IDR");
/* Increment display number for every displayed picture */
picDisplayNumber++;
broadwayOnPictureDecoded((u8*)decPicture.pOutputPicture, decInfo.picWidth, decInfo.picHeight);
}
break;
case H264SWDEC_STRM_PROCESSED:
case H264SWDEC_STRM_ERR:
/* Input stream was decoded but no picture is ready, thus get more data. */
decInput.dataLen = 0;
break;
default:
emscripten_log(0, "%d",-ret);
decInput.dataLen = 0;
break;
}
return ret;
}
public:
NetConnection *nc;
bool isFirstVideoReceived = true;
byte NAL_unit_length;
int spsLen = 0;
int ppsLen = 0;
//string sps;
//string pps;
bool webgl = false;
val* jsThis;
int id;
clock_t timestamp;
int timeoutId;
vector<RtmpPacket> videoBuffers;
NetStream(NetConnection* _nc);
~NetStream()
{
if(heap!=NULL)free(heap);
if (audioBuffer != NULL)free(audioBuffer);
if (audioOutput != NULL)free(audioOutput);
speex_decoder_destroy(speexState);
speex_bits_destroy(&speexBits);
if (jsThis != nullptr)delete jsThis;
}
void onConnect(val&& result);
void play(string name);
void attachCanvas(val _this,bool webgl) {
this->webgl = webgl;
canvasAttached = true;
emscripten_log(0, "webgl:%s", webgl ? "true" : "false");
//consoleLog("webgl:%s",webgl?"true":"false");
if (!jsThis)jsThis = new val(_this);
}
void _decodeVideo(const char* data,int len) {
//emscripten_log(0, "%d %d",len,(int)data);
decInput.pStream = (u8*)data;
decInput.dataLen = len;
u32 i = 0;
do {
u8 *start = decInput.pStream;
u32 ret = broadwayDecode();
//emscripten_log(0,"Decoded Unit #%d, Size: %x, Result: %i\n", i++, (decInput.pStream - start), ret);
} while (decInput.dataLen > 0);
}
inline void decodeVideo(MemoryStream& data) {
int NALUnitLength = 0;
data >>= 5;
while (data.length() > 4) {
switch (NAL_unit_length) {
case 3:
data.read4B(NALUnitLength);
break;
case 2:
data.read3B(NALUnitLength);
break;
case 1:
data.read2B(NALUnitLength);
break;
default:
data.read1(NALUnitLength);
}
byte naluType = data[0] & 0x1f;
switch (naluType) {
case 5:
case 1:
_decodeVideo((const char *)data, NALUnitLength);
break;
}
data >>= NALUnitLength;
}
}
void decodeVideoBuffer() {
timeoutId = 0;
if (videoBuffers.size() > 0) {
decodeVideo(videoBuffers[videoBuffers.size()-1].data);
videoBuffers.pop_back();
if (videoBuffers.size() > 0) {
int time = videoBuffers[videoBuffers.size() - 1].rtmpHeader.timestamp;
auto targetTime = clock() / 1000 - timestamp;
if (time > targetTime) {
timeoutId = jsThis->call<int>("checkVideoBuffer", time - targetTime);
return;
}
else {
decodeVideoBuffer();
}
}
}
}
bool decodeVideo(RtmpHeader& rtmpHeader, MemoryStream& data) {
int index = 0;
byte frame_type = data[0];
int codec_id = frame_type & 0x0f;
frame_type = (frame_type >> 4) & 0x0f;
if (codec_id != 7) {
emscripten_log(0, "Only support video h.264/avc codec. actual=%d", codec_id);
return -1;
}
byte avc_packet_type = data[1];
if (data[0] == 0x17 && avc_packet_type == 0) {
if (isFirstVideoReceived) {
timestamp = clock() / 1000 - rtmpHeader.timestamp;
byte lengthSizeMinusOne = data[9];
lengthSizeMinusOne &= 0x03;
NAL_unit_length = lengthSizeMinusOne;
data.offset = 11;
data.consoleHex();
data.read2B(spsLen);
if (spsLen > 0) {
_decodeVideo((const char*)data,spsLen);
data >>= spsLen;
}
data >>= 1;
data.read2B(ppsLen);
if (ppsLen > 0) {
_decodeVideo((const char*)data,ppsLen);
}
isFirstVideoReceived = false;
}
}
else {
if (!canvasAttached)return false;
auto targetTime = clock()/1000 - timestamp;
//consoleLog("%d %d", rtmpHeader.timestamp,targetTime);
if (rtmpHeader.timestamp > targetTime) {
videoBuffers.insert(videoBuffers.begin(), RtmpPacket(rtmpHeader,data));
if(timeoutId == 0)
timeoutId = jsThis->call<int>("checkVideoBuffer", rtmpHeader.timestamp - targetTime);
return false;
}
decodeVideo(data);
}
return true;
}
bool decodeAudio(RtmpHeader& rtmpHeader,MemoryStream & data) {
data.offset = 1;
if (data.length() <= 11) {
memset(audioBuffer + audioBufferCounts * 640,0,640);
}
else {
speex_bits_read_from(&speexBits, (const char *)data, 52);
speex_decode_int(speexState, &speexBits, audioOutput);
memcpy(audioBuffer + audioBufferCounts * 640, audioOutput, 640);
}
audioBufferCounts++;
if (audioBufferCounts == audioBufferLength) {
audioBufferCounts = 0;
jsThis->call<void>("playAudio");
}
return true;
}
int initAudio(val _this,int bufferFrames) {
audioBufferLength = bufferFrames;
if(!jsThis)jsThis =new val(_this);
auto mode = speex_lib_get_mode(SPEEX_MODEID_WB);
speexState = speex_decoder_init(mode);
speex_bits_init(&speexBits);
audioOutput = (i16*)malloc(640);
audioBufferCounts = 0;
audioBuffer = (u8*)malloc(640* bufferFrames);
return (int)audioBuffer;
}
};