-
Notifications
You must be signed in to change notification settings - Fork 295
/
rtmpstream.cpp
545 lines (496 loc) · 11.4 KB
/
rtmpstream.cpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <sys/poll.h>
#include "log.h"
#include "rtmp/rtmpstream.h"
RTMPMediaStream::RTMPMediaStream()
{
this->id = 0;
this->data = 0;
}
RTMPMediaStream::RTMPMediaStream(DWORD id)
{
this->id = id;
this->data = 0;
}
RTMPMediaStream::~RTMPMediaStream()
{
//Remove all listeners
RemoveAllMediaListeners();
}
DWORD RTMPMediaStream::AddMediaListener(Listener *listener)
{
//Lock mutexk
lock.WaitUnusedAndLock();
//Apend
listeners.insert(listener);
//Get number of listeners
DWORD num = listeners.size();
//Attached
listener->onAttached(this);
//Unlock
lock.Unlock();
//return number of listeners
return num;
}
void RTMPMediaStream::RemoveAllMediaListeners()
{
//Lock mutexk
lock.WaitUnusedAndLock();
//For each listener
for(Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Detach it
(*it)->onDetached(this);
//Clean listeners
listeners.clear();
//Unlock
lock.Unlock();
}
DWORD RTMPMediaStream::RemoveMediaListener(Listener *listener)
{
//Lock mutexk
lock.WaitUnusedAndLock();
//Find it
Listeners::iterator it = listeners.find(listener);
//If present
if (it!=listeners.end())
{
//Detach
listener->onDetached(this);
//erase it
listeners.erase(it);
}
//Get number of listeners
DWORD num = listeners.size();
//Unlock
lock.Unlock();
//return number of listeners
return num;
}
void RTMPMediaStream::SendMediaFrame(RTMPMediaFrame* frame)
{
//Lock mutexk
lock.IncUse();
//Iterate
for (Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Send it
(*it)->onMediaFrame(id,frame);
//Unlock
lock.DecUse();
}
void RTMPMediaStream::SendCommand(const wchar_t *name,AMFData* obj)
{
//Lock mutexk
lock.IncUse();
//Iterate
for (Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Send it
(*it)->onCommand(id,name,obj);
//Unlock
lock.DecUse();
}
void RTMPMediaStream::SendMetaData(RTMPMetaData* meta)
{
//Lock mutexk
lock.IncUse();
//Iterate
for (Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Send it
(*it)->onMetaData(id,meta);
//Unlock
lock.DecUse();
}
void RTMPMediaStream::SendStreamEnd()
{
//Lock mutexk
lock.IncUse();
//Iterate
for (Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Send it
(*it)->onStreamEnd(id);
//Unlock
lock.DecUse();
}
void RTMPMediaStream::SendStreamBegin()
{
//Lock mutexk
lock.IncUse();
//Iterate
for (Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Send it
(*it)->onStreamBegin(id);
//Unlock
lock.DecUse();
}
void RTMPMediaStream::Reset()
{
//Lock mutexk
lock.IncUse();
//Iterate
for (Listeners::iterator it = listeners.begin(); it!=listeners.end(); ++it)
//Send it
(*it)->onStreamReset(id);
//Unlock
lock.DecUse();
}
/*****************************
* RTMPPipedMediaStream
****************************/
RTMPPipedMediaStream::RTMPPipedMediaStream() : RTMPMediaStream(0)
{
//Not attached
attached = NULL;
//No first frame
first = -1;
//NO meta
meta = NULL;
//No desc
desc = NULL;
aacSpecificConfig = NULL;
//Do not wait for intra
waitIntra = false;
//Rewrite ts on default
rewriteTimestamps = true;
}
RTMPPipedMediaStream::RTMPPipedMediaStream(DWORD id) : RTMPMediaStream(id)
{
//Not attached
attached = NULL;
//No first frame
first = -1;
//NO meta
meta = NULL;
//No desc
desc = NULL;
aacSpecificConfig = NULL;
//Do not wait for intra
waitIntra = false;
//Rewrite ts on default
rewriteTimestamps = true;
}
RTMPPipedMediaStream::~RTMPPipedMediaStream()
{
//Dettach just in case
Detach();
//Delete meta
if (meta)
//Delete it
delete(meta);
//Delete desc
if (desc)
//Delete it
delete(desc);
//Delete aac config
if (aacSpecificConfig)
//Delete it
delete(aacSpecificConfig);
}
DWORD RTMPPipedMediaStream::AddMediaListener(RTMPMediaStream::Listener *listener)
{
//Check meta
if (meta)
//Add media listener
listener->onMetaData(id,meta);
//Check desc
if (desc)
//Send it
listener->onMediaFrame(id,desc);
//return number of listeners
return RTMPMediaStream::AddMediaListener(listener);
}
void RTMPPipedMediaStream::Attach(RTMPMediaStream *stream)
{
//If attached to the same stream
if (stream==attached)
//Do nothing
return;
//Detach if joined
if (attached)
//Remove ourself as listeners
attached->RemoveMediaListener(this);
//If it is not null
if (stream)
//Join to the new one
stream->AddMediaListener(this);
//Set it anyway
attached = stream;
}
void RTMPPipedMediaStream::Detach()
{
//Detach if joined
if (attached)
//Remove ourself as listeners
attached->RemoveMediaListener(this);
//Detach it anyway
attached = NULL;
}
void RTMPPipedMediaStream::onAttached(RTMPMediaStream *stream)
{
//Check if attached to another stream
if (attached)
//Remove ourself as listeners
attached->RemoveMediaListener(this);
//Store new one
attached = stream;
}
void RTMPPipedMediaStream::onDetached(RTMPMediaStream *stream)
{
//Detach if joined
if (attached!=stream)
//Remove ourself as listeners
attached->RemoveMediaListener(this);
//Detach
attached = NULL;
}
void RTMPPipedMediaStream:: onMediaFrame(DWORD id,RTMPMediaFrame *frame)
{
//Get timestamp
QWORD ts = frame->GetTimestamp();
//Check if it is not set
if (ts==-1)
//Reuse it
SendMediaFrame(frame);
//Check if it is first
if (first==-1)
{
//If we have to wait to video
if (waitIntra)
{
//Get AAC config
if (frame->GetType()==RTMPMediaFrame::Audio)
{
//Check if it is aac config frame
RTMPAudioFrame* audio = (RTMPAudioFrame*)frame;
//check frame type
if (audio->GetAudioCodec()==RTMPAudioFrame::AAC && audio->GetAACPacketType()==RTMPAudioFrame::AACSequenceHeader)
//clone aac config
aacSpecificConfig = (RTMPAudioFrame*)audio->Clone();
//Skip frame
return;
}
//Check if it is intra video frame
RTMPVideoFrame* video = (RTMPVideoFrame*)frame;
//check frame type
if (video->GetFrameType()!=RTMPVideoFrame::INTRA && video->GetFrameType()!=RTMPVideoFrame::GENERATED_KEY_FRAME)
//Skip
return;
//Check if it is h264
if (video->GetVideoCodec()==RTMPVideoFrame::AVC)
{
//Check type
if(video->GetAVCType()==RTMPVideoFrame::AVCHEADER)
{
//Check if we already had one desc
if (desc)
//Delete it
delete(desc);
//Clone it
desc = (RTMPVideoFrame*)video->Clone();
//Set 0 timestamp
desc->SetTimestamp(0);
//Keep it for later
return;
}
//Check if we have desc
if (!desc)
//Skip
return;
}
}
//We got it
Log("-Got first frame [%llu,waitIntra:%d,type:%s]\n",ts,waitIntra,RTMPMediaFrame::GetTypeName(frame->GetType()));
//Send stream begin
SendStreamBegin();
//Store timestamp
first = ts;
//Check if we have meta
if (meta)
//Send it
SendMetaData(meta);
//Check
if (desc)
//Send previous desc before frame
SendMediaFrame(desc);
//Check aac config
if (aacSpecificConfig)
//Send it
SendMediaFrame(aacSpecificConfig);
}
//Check if we have to rewrite ts
if (rewriteTimestamps)
{
//Make sure it is not an out of order packet
if (ts<first)
{
//Exit
Error("ERROR: RTMP media frame ts before first one, dropping it!! [ts:%llu,first:%llu]\n",ts,first);
return;
}
//Modify timestamp
frame->SetTimestamp(ts-first);
}
//Send it
SendMediaFrame(frame);
//Check if we have rewriten ts
if (rewriteTimestamps)
//Set previous
frame->SetTimestamp(ts);
}
void RTMPPipedMediaStream:: onMetaData(DWORD id,RTMPMetaData *publishedMetaData)
{
//Check method
AMFString* name = (AMFString*)publishedMetaData->GetParams(0);
//Get timestamp
QWORD ts = publishedMetaData->GetTimestamp();
//Check it
if (name->GetWString().compare(L"@setDataFrame")==0)
{
//If we already had one
if (meta)
//Delete if already have one
delete(meta);
//Create new msg
meta = new RTMPMetaData(0);
//Copy the rest of params
for (DWORD i=1;i<publishedMetaData->GetParamsLength();i++)
//Clone and append
meta->AddParam(publishedMetaData->GetParams(i)->Clone());
//Check if we have started to send it
if (first!=-1)
{
//Check if we have to rewrite ts
if (rewriteTimestamps)
//Set new meta
meta->SetTimestamp(ts-first);
//Send it back
SendMetaData(meta);
}
} else {
RTMPMetaData *cloned = publishedMetaData->Clone();
//Check if we have to rewrite ts
if (rewriteTimestamps)
{
//Check if we have started to send it
if (first!=-1)
//Set new meta
cloned->SetTimestamp(ts-first);
else
//Now
cloned->SetTimestamp(0);
}
//Send it back
SendMetaData(cloned);
}
}
void RTMPPipedMediaStream:: onCommand(DWORD id,const wchar_t *name,AMFData* obj)
{
//Do nothing with commands
}
void RTMPPipedMediaStream:: onStreamBegin(DWORD id)
{
}
void RTMPPipedMediaStream:: onStreamEnd(DWORD id)
{
}
void RTMPPipedMediaStream:: onStreamReset(DWORD id)
{
//Reset
Reset();
}
RTMPCachedPipedMediaStream::~RTMPCachedPipedMediaStream()
{
//Free memory
Clear();
}
void RTMPCachedPipedMediaStream::Clear()
{
//Lock
use.WaitUnusedAndLock();
//Get frame
for (FrameChache::iterator it = cached.begin(); it!=cached.end(); ++it)
//Delete frame
delete(*it);
//Cleare list
cached.clear();
//Unlock
use.Unlock();
}
DWORD RTMPCachedPipedMediaStream::AddMediaListener(RTMPMediaStream::Listener* listener)
{
//return number of listeners
int num = RTMPMediaStream::AddMediaListener(listener);
//Send meta if available
if (meta)
//Add media listener
listener->onMetaData(id,meta);
//Check desc
if (desc)
//Send it
listener->onMediaFrame(id,desc);
//Lock cache
use.IncUse();
//Send all queued frames up to now
//Get frame
for (FrameChache::iterator it = cached.begin(); it!=cached.end(); ++it)
//Send it
listener->onMediaFrame(id,*it);
//Dec use
use.DecUse();
//Return it
return num;
}
void RTMPCachedPipedMediaStream::SendMediaFrame(RTMPMediaFrame *frame)
{
//Check if it is video intra
if (frame->GetType()==RTMPMediaFrame::Video && ((RTMPVideoFrame*)frame)->GetFrameType()==RTMPVideoFrame::INTRA)
//Clear cache
Clear();
//Append to queue
cached.push_back(frame->Clone());
//Call parent
RTMPPipedMediaStream::SendMediaFrame(frame);
}
/****************************
* RTMPNetStream
***************************/
RTMPNetStream::RTMPNetStream(DWORD id,Listener *listener) : RTMPPipedMediaStream(id)
{
//Store listener
this->listener = listener;
}
RTMPNetStream::~RTMPNetStream()
{
if (listener)
listener->onNetStreamDestroyed(id);
}
void RTMPNetStream::doPlay(std::wstring& url,RTMPMediaStream::Listener *listener)
{
fireOnNetStreamStatus(RTMP::Netstream::Failed,L"Play not supported in this stream");
}
void RTMPNetStream::doPublish(std::wstring& url)
{
fireOnNetStreamStatus(RTMP::Netstream::Failed,L"Publish not supported in this stream");
}
void RTMPNetStream::doPause()
{
fireOnNetStreamStatus(RTMP::Netstream::Failed,L"Pause not supported in this stream");
}
void RTMPNetStream::doResume()
{
fireOnNetStreamStatus(RTMP::Netstream::Failed,L"Resume not supported in this stream");
}
void RTMPNetStream::doSeek(DWORD time)
{
fireOnNetStreamStatus(RTMP::Netstream::Failed,L"Seek not supported in this stream");
}
void RTMPNetStream::doClose(RTMPMediaStream::Listener *listener)
{
fireOnNetStreamStatus(RTMP::Netstream::Failed,L"Close not supported in this stream");
}
void RTMPNetStream::doCommand(RTMPCommandMessage *cmd)
{
cmd->Dump();
}
void RTMPNetStream::fireOnNetStreamStatus(const RTMPNetStatusEventInfo &info,const wchar_t* message)
{
if (listener)
listener->onNetStreamStatus(id,info,message);
}