This repository was archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathNetworkMediaSinkStream.cpp
More file actions
1304 lines (1027 loc) · 37.7 KB
/
Copy pathNetworkMediaSinkStream.cpp
File metadata and controls
1304 lines (1027 loc) · 37.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "NetworkMediaSinkStream.h"
#include "Media.h"
#define SET_SAMPLE_FLAG(dest, destMask, pSample, flagName) \
{ \
UINT32 unValue; \
if (SUCCEEDED(pSample->GetUINT32(MFSampleExtension_##flagName, &unValue))) \
{ \
dest |= (unValue != FALSE) ? static_cast<DWORD>(SampleFlags_SampleFlag_##flagName) : 0; \
destMask |= static_cast<DWORD>(SampleFlags_SampleFlag_##flagName); \
} \
}
// If an entry is TRUE, the operation is valid from that state.
BOOL NetworkMediaSinkStreamImpl::ValidStateMatrix[SinkStreamState_Count][SinkStreamOperation_Count] =
{
// Operations:
// States: SetType Start Restart Pause Stop Sample Marker
/* NotSet */ TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
/* Ready */ TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE,
/* Start */ TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE,
/* Pause */ TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
/* Stop */ TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE,
};
_Use_decl_annotations_
NetworkMediaSinkStreamImpl::AsyncOperation::AsyncOperation(SinkStreamOperation op)
: _cRef(1)
, _op(op)
{
}
_Use_decl_annotations_
NetworkMediaSinkStreamImpl::AsyncOperation::~AsyncOperation()
{
assert(_cRef == 0);
}
_Use_decl_annotations_
ULONG NetworkMediaSinkStreamImpl::AsyncOperation::AddRef()
{
return InterlockedIncrement(&_cRef);
}
_Use_decl_annotations_
ULONG NetworkMediaSinkStreamImpl::AsyncOperation::Release()
{
ULONG cRef = InterlockedDecrement(&_cRef);
if (cRef == 0)
{
delete this;
}
return cRef;
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::AsyncOperation::QueryInterface(REFIID iid, void** ppv)
{
if (!ppv)
{
return E_POINTER;
}
if (iid == IID_IUnknown)
{
* ppv = static_cast<IUnknown*>(this);
}
else
{
* ppv = nullptr;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
_Use_decl_annotations_
NetworkMediaSinkStreamImpl::NetworkMediaSinkStreamImpl()
: _dwStreamId(-1)
, _state(SinkStreamState_NotSet)
, _isShutdown(false)
, _isPlayerConnected(false)
, _fIsVideo(false)
, _fGetFirstSampleTime(false)
, _adjustedStartTime(0)
, _spConnection(nullptr)
, _spParentMediaSink(nullptr)
, _workQueueId(0)
, _workQueueCB(this, &NetworkMediaSinkStreamImpl::OnDispatchWorkItem)
{
ZeroMemory(&_currentSubtype, sizeof(_currentSubtype));
}
_Use_decl_annotations_
NetworkMediaSinkStreamImpl::~NetworkMediaSinkStreamImpl()
{
assert(_isShutdown);
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::RuntimeClassInitialize(
DWORD id,
IConnection* pConnection,
INetworkMediaSink* pParentMediaSink)
{
NULL_CHK(pConnection);
NULL_CHK(pParentMediaSink);
// Create the event queue helper.
IFR(MFCreateEventQueue(&_eventQueue));
IFR(MFAllocateSerialWorkQueue(MFASYNC_CALLBACK_QUEUE_STANDARD, &_workQueueId));
_dwStreamId = id;
_spConnection = pConnection;
_spParentMediaSink = pParentMediaSink;
return S_OK;
}
// IMFMediaEventGenerator methods.
// Note: These methods call through to the event queue helper object.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::BeginGetEvent(
IMFAsyncCallback* pCallback,
IUnknown* punkState)
{
auto lock = _lock.Lock();
IFR(CheckShutdown());
return _eventQueue->BeginGetEvent(pCallback, punkState);
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::EndGetEvent(
IMFAsyncResult* pResult,
IMFMediaEvent** ppEvent)
{
NULL_CHK(ppEvent);
auto lock = _lock.Lock();
IFR(CheckShutdown());
return _eventQueue->EndGetEvent(pResult, ppEvent);
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetEvent(
DWORD dwFlags,
IMFMediaEvent** ppEvent)
{
NULL_CHK(ppEvent);
// NOTE:
// GetEvent can block indefinitely, so we don't hold the lock.
// This requires some juggling with the event queue pointer.
HRESULT hr = S_OK;
ComPtr<IMFMediaEventQueue> spQueue;
{
auto lock = _lock.Lock();
// Check shutdown
IFR(CheckShutdown());
// Get the pointer to the event queue.
spQueue = _eventQueue;
}
// Now get the event.
return spQueue->GetEvent(dwFlags, ppEvent);
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::QueueEvent(
MediaEventType met,
REFGUID guidExtendedType,
HRESULT hrStatus,
PROPVARIANT const* pvValue)
{
auto lock = _lock.Lock();
IFR(CheckShutdown());
return _eventQueue->QueueEventParamVar(met, guidExtendedType, hrStatus, pvValue);
}
// IMFStreamSink methods
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetMediaSink(
IMFMediaSink** ppMediaSink)
{
NULL_CHK(ppMediaSink);
auto lock = _lock.Lock();
IFR(CheckShutdown());
NULL_CHK_HR(_spParentMediaSink, E_NOT_SET);
return _spParentMediaSink.CopyTo(ppMediaSink);
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetIdentifier(
DWORD* pdwIdentifier)
{
NULL_CHK(pdwIdentifier);
auto lock = _lock.Lock();
IFR(CheckShutdown());
* pdwIdentifier = _dwStreamId;
return S_OK;
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetMediaTypeHandler(
IMFMediaTypeHandler** ppHandler)
{
NULL_CHK(ppHandler);
auto lock = _lock.Lock();
IFR(CheckShutdown());
// This stream object acts as its own type handler, so we QI ourselves.
return QueryInterface(IID_IMFMediaTypeHandler, reinterpret_cast<void**>(ppHandler));
}
// We received a sample from an upstream component
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::ProcessSample(
IMFSample* pSample)
{
NULL_CHK(pSample);
Log(Log_Level_All, L"NetworkMediaSinkStreamImpl::ProcessSample() begin...\n");
auto lock = _lock.Lock();
HRESULT hr = S_OK;
IFC(CheckShutdown());
// Validate the operation.
IFC(ValidateOperation(SinkStreamOperation_ProcessSample));
if(!_isPlayerConnected)
{
// we do not have a connected player yet
Log(Log_Level_Info, L"Waiting for a connected player, ignoring sample...\n");
//if (nullptr != _spConnection)
//{
// IFR(_spConnection->SendPayloadType(PayloadType_State_CaptureReady));
//}
IFC(QueueEvent(MEStreamSinkRequestSample, GUID_NULL, hr, nullptr));
}
else
{
// is this our first sample since connecting?
if (_isPlayerConnected && _fGetFirstSampleTime)
{
// pulls the sample time to rebase timestamps
IFC(pSample->GetSampleTime(&_adjustedStartTime));
Log(Log_Level_Info, L"first sample timeStamp: %I64d\n", _adjustedStartTime);
_fGetFirstSampleTime = false;
}
// Add the sample to the sample queue.
IFC(_sampleQueue.InsertBack(pSample));
// Unless we are paused, start an async operation to dispatch the next sample.
if (SinkStreamState_Paused != _state)
{
// Queue the operation.
IFC(QueueAsyncOperation(SinkStreamOperation_ProcessSample));
}
}
//if (nullptr != _spParentMediaSink) // for local playback
//{
// //_spParentMediaSink->OnSampleUpdated(pSample);
//}
done:
return hr;
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::PlaceMarker(
MFSTREAMSINK_MARKER_TYPE eMarkerType,
PROPVARIANT const* pvarMarkerValue,
PROPVARIANT const* pvarContextValue)
{
auto lock = _lock.Lock();
IFR(ValidateOperation(SinkStreamOperation_PlaceMarker));
ComPtr<IMarker> spMarker;
IFR(MarkerImpl::Create(eMarkerType, pvarMarkerValue, pvarContextValue, &spMarker));
_sampleQueue.InsertBack(spMarker.Get());
if (SinkStreamState_Paused == _state)
{
return S_OK;
}
// Queue the operation.
return QueueAsyncOperation(SinkStreamOperation_PlaceMarker); // Increments ref count on pOp.
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::Flush()
{
auto lock = _lock.Lock();
IFR(CheckShutdown());
// Note: Even though we are flushing data, we still need to send
// any marker events that were queued.
return HandleError(ProcessSamplesFromQueue(true, nullptr));
}
/// IMFMediaTypeHandler methods
// Check if a media type is supported.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::IsMediaTypeSupported(
IMFMediaType* pMediaType,
IMFMediaType** ppMediaType)
{
NULL_CHK(pMediaType);
auto lock = _lock.Lock();
IFR(CheckShutdown());
GUID majorType = GUID_NULL;
IFR(pMediaType->GetGUID(MF_MT_MAJOR_TYPE, &majorType));
IFR((majorType == MFMediaType_Video || majorType == MFMediaType_Audio) ? S_OK : MF_E_INVALIDTYPE);
if (nullptr != _currentType)
{
GUID guiNewSubtype = GUID_NULL;
IFR(pMediaType->GetGUID(MF_MT_SUBTYPE, &guiNewSubtype));
if(guiNewSubtype != _currentSubtype)
{
return MF_E_INVALIDTYPE;
}
}
if (nullptr != ppMediaType)
{
* ppMediaType = nullptr;
}
return S_OK;
}
// Return the number of preferred media types.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetMediaTypeCount(
DWORD* pdwTypeCount)
{
NULL_CHK(pdwTypeCount);
auto lock = _lock.Lock();
IFR(CheckShutdown());
// We've have only one media type
* pdwTypeCount = 1;
return S_OK;
}
// Return a preferred media type by index.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetMediaTypeByIndex(
DWORD dwIndex,
IMFMediaType** ppType)
{
NULL_CHK(ppType);
auto lock = _lock.Lock();
IFR(CheckShutdown());
if (dwIndex > 0)
{
IFR(MF_E_NO_MORE_TYPES);
}
NULL_CHK_HR(_currentType, E_NOT_SET);
return _currentType.CopyTo(ppType);
}
// Set the current media type.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::SetCurrentMediaType(
IMFMediaType* pMediaType)
{
NULL_CHK(pMediaType);
auto lock = _lock.Lock();
IFR(CheckShutdown());
// don't allow format changes after streaming starts.
IFR(ValidateOperation(SinkStreamOperation_SetMediaType));
// set media type already
if (SinkStreamState_Ready <= _state)
{
IFR(IsMediaTypeSupported(pMediaType, nullptr));
}
GUID guiMajorType = GUID_NULL;
IFR(pMediaType->GetMajorType(&guiMajorType));
_fIsVideo = (guiMajorType == MFMediaType_Video);
IFR(MFCreateMediaType(&_currentType));
IFR(pMediaType->CopyAllItems(_currentType.Get()));
IFR(_currentType->GetGUID(MF_MT_SUBTYPE, &_currentSubtype));
if (_state < SinkStreamState_Ready)
{
_state = SinkStreamState_Ready;
}
else if (_state > SinkStreamState_Ready)
{
ComPtr<IMFMediaType> spType;
IFR(MFCreateMediaType(&spType));
IFR(pMediaType->CopyAllItems(spType.Get()));
IFR(ProcessFormatChange(spType.Get()));
}
return S_OK;
}
// Return the current media type, if any.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetCurrentMediaType(
IMFMediaType** ppMediaType)
{
NULL_CHK(ppMediaType);
auto lock = _lock.Lock();
IFR(CheckShutdown());
if (nullptr == _currentType)
{
IFR(MF_E_NOT_INITIALIZED);
}
NULL_CHK_HR(_currentType, E_NOT_SET);
return _currentType.CopyTo(ppMediaType);
}
// Return the major type GUID.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::GetMajorType(
GUID* pguidMajorType)
{
NULL_CHK(pguidMajorType);
auto lock = _lock.Lock();
IFR(CheckShutdown());
if (nullptr == _currentType)
{
IFR(MF_E_NOT_INITIALIZED);
}
* pguidMajorType = (_fIsVideo) ? MFMediaType_Video : MFMediaType_Audio;
return S_OK;
}
// Called when the presentation clock starts.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::Start(MFTIME start)
{
auto lock = _lock.Lock();
IFR(ValidateOperation(SinkStreamOperation_Start));
if (start != PRESENTATION_CURRENT_POSITION)
{
_adjustedStartTime = start; // Cache the start time
}
_state = SinkStreamState_Started;
return QueueAsyncOperation(SinkStreamOperation_Start);
}
// Called when the presentation clock stops.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::Stop()
{
IFR(ValidateOperation(SinkStreamOperation_Stop));
_state = SinkStreamState_Stopped;
return QueueAsyncOperation(SinkStreamOperation_Stop);
}
// Called when the presentation clock pauses.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::Pause()
{
IFR(ValidateOperation(SinkStreamOperation_Pause));
_state = SinkStreamState_Paused;
return QueueAsyncOperation(SinkStreamOperation_Pause);
}
// Called when the presentation clock restarts.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::Restart()
{
IFR(ValidateOperation(SinkStreamOperation_Restart));
_state = SinkStreamState_Started;
return QueueAsyncOperation(SinkStreamOperation_Restart);
}
// Shuts down the stream sink.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::Shutdown()
{
_isPlayerConnected = false;
if (!_isShutdown)
{
if (_eventQueue)
{
_eventQueue->Shutdown();
}
MFUnlockWorkQueue(_workQueueId);
_sampleQueue.Clear();
_eventQueue.Reset();
_currentType.Reset();
_isShutdown = true;
_spConnection.Reset();
_spConnection = nullptr;
}
return S_OK;
}
// Set the information if we are connected to a client
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::ConnectedFunc(
bool fConnected,
LONGLONG llCurrentTime)
{
auto lock = _lock.Lock();
HRESULT hr = S_OK;
if (_currentType == nullptr)
{
IFC(MF_E_NOT_INITIALIZED);
}
_isPlayerConnected = fConnected;
_adjustedStartTime = llCurrentTime;
if (_isPlayerConnected)
{
_fGetFirstSampleTime = true;
}
if (fConnected)
{
Log(Log_Level_Info, L"SetConnected start=%I64d\n", _adjustedStartTime);
}
done:
return HandleError(hr);
}
// Puts an async operation on the work queue.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::QueueAsyncOperation(
SinkStreamOperation op)
{
ComPtr<IUnknown> spOp;
spOp.Attach(new (std::nothrow) AsyncOperation(op)); // Created with ref count = 1
NULL_CHK_HR(spOp.Get(), E_OUTOFMEMORY);
return MFPutWorkItem2(_workQueueId, 0, &_workQueueCB, spOp.Get());
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::OnDispatchWorkItem(
IMFAsyncResult* pAsyncResult)
{
// Called by work queue thread. Need to hold the critical section.
auto lock = _lock.Lock();
Log(Log_Level_All, L"NetworkMediaSinkStreamImpl::OnDispatchWorkItem() begin...\n");
HRESULT hr = S_OK;
// The state object is a AsncOperation object.
ComPtr<IUnknown> spState;
IFR(pAsyncResult->GetState(&spState));
AsyncOperation* pOp = static_cast<AsyncOperation *>(spState.Get());
switch (pOp->_op)
{
case SinkStreamOperation_Start:
case SinkStreamOperation_Restart:
// Send MEStreamSinkStarted.
IFC(QueueEvent(MEStreamSinkStarted, GUID_NULL, S_OK, nullptr));
// There might be samples queue from earlier (ie, while paused).
boolean fRequestMoreSamples;
if (!_isPlayerConnected)
{
// Just drop samples if we are not connected
IFC(DropSamplesFromQueue(&fRequestMoreSamples));
}
else
{
IFC(SendSampleFromQueue(&fRequestMoreSamples));
}
// If false there is no samples in the queue now so request one
if (fRequestMoreSamples)
{
IFC(QueueEvent(MEStreamSinkRequestSample, GUID_NULL, S_OK, nullptr));
}
break;
case SinkStreamOperation_Stop:
// Drop samples from queue.
IFC(DropSamplesFromQueue(nullptr));
// Send the event even if the previous call failed.
IFC(QueueEvent(MEStreamSinkStopped, GUID_NULL, S_OK, nullptr));
break;
case SinkStreamOperation_Pause:
IFC(QueueEvent(MEStreamSinkPaused, GUID_NULL, S_OK, nullptr));
break;
case SinkStreamOperation_ProcessSample:
case SinkStreamOperation_PlaceMarker:
case SinkStreamOperation_SetMediaType:
IFC(DispatchProcessedSample(pOp));
break;
}
done:
return HandleError(hr);
}
// Complete a ProcessSample or PlaceMarker request.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::DispatchProcessedSample(
AsyncOperation* pOp)
{
NULL_CHK(pOp);
boolean fRequestMoreSamples = false;
if (!_isPlayerConnected)
{
IFR(DropSamplesFromQueue(&fRequestMoreSamples));
}
else
{
IFR(SendSampleFromQueue(&fRequestMoreSamples));
}
// Ask for another sample
if (fRequestMoreSamples)
{
if (pOp->_op == SinkStreamOperation_ProcessSample)
{
IFR(QueueEvent(MEStreamSinkRequestSample, GUID_NULL, S_OK, nullptr));
}
}
return S_OK;
}
// Drop samples in the queue
HRESULT NetworkMediaSinkStreamImpl::DropSamplesFromQueue(
boolean* fRequestMoreSamples)
{
return ProcessSamplesFromQueue(true, fRequestMoreSamples);
}
// Send sample from the queue
HRESULT NetworkMediaSinkStreamImpl::SendSampleFromQueue(
boolean* fRequestMoreSamples)
{
return ProcessSamplesFromQueue(false, fRequestMoreSamples);
}
// Checks if an operation is valid in the current state.
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::ValidateOperation(SinkStreamOperation op)
{
assert(!_isShutdown);
HRESULT hr = S_OK;
if (NetworkMediaSinkStreamImpl::ValidStateMatrix[static_cast<int>(_state)][static_cast<int>(op)])
{
return S_OK;
}
else if (_state == SinkStreamState_NotSet)
{
return MF_E_NOT_INITIALIZED;
}
else
{
return MF_E_INVALIDREQUEST;
}
}
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::ProcessSamplesFromQueue(
boolean fFlush,
boolean *fNeedMoreSamples)
{
Log(Log_Level_Info, L"NetworkMediaSinkStreamImpl::ProcessSamplesFromQueue()\n");
if (FAILED(CheckShutdown()))
{
fFlush = true;
}
boolean fRequestSamples = false;
boolean fSendSamples = true;
boolean fSendEOS = false;
ComPtr<IUnknown> spUnknown;
if (FAILED(_sampleQueue.RemoveFront(&spUnknown)))
{
fRequestSamples = true;
fSendSamples = false;
}
while (fSendSamples)
{
ComPtr<IDataBundle> spDataBundle;
assert(spUnknown);
bool fProcessingSample = false;
// Determine if this is a marker or a sample.
ComPtr<IMFSample> spMediaSample;
if (SUCCEEDED(spUnknown.As(&spMediaSample)))
{
if (!fFlush)
{
IFR(PrepareSample(spMediaSample.Get(), false, &spDataBundle));
fProcessingSample = true;
}
}
else
{
// Check for marker
ComPtr<IMarker> spMarker;
if (SUCCEEDED(spUnknown.As(&spMarker)))
{
PROPVARIANT var;
PropVariantInit(&var);
MFSTREAMSINK_MARKER_TYPE markerType;
HRESULT hr = spMarker->GetMarkerType(&markerType);
if (SUCCEEDED(hr))
{
// Get the context data.
hr = spMarker->GetContext(&var);
if (SUCCEEDED(hr))
{
hr = QueueEvent(MEStreamSinkMarker, GUID_NULL, S_OK, &var);
}
}
PropVariantClear(&var);
IFR(hr);
switch (markerType)
{
case MFSTREAMSINK_MARKER_ENDOFSEGMENT:
fSendEOS = true;
break;
case MFSTREAMSINK_MARKER_TICK:
if (!fFlush || (_isPlayerConnected && !_fGetFirstSampleTime))
{
PROPVARIANT var;
PropVariantInit(&var);
LARGE_INTEGER timeStamp;;
hr = spMarker->GetMarkerValue(&var);
if (SUCCEEDED(hr))
{
ZeroMemory(&timeStamp, sizeof(timeStamp));
if (var.vt == VT_I8)
{
timeStamp = var.hVal;
}
}
PropVariantClear(&var);
IFR(hr)
ComPtr<IMFSample> spSample;
IFR(MFCreateSample(&spSample));
IFR(spSample->SetSampleTime(timeStamp.QuadPart));
IFR(PrepareStreamTick(spSample.Get(), &spDataBundle));
}
break;
}
}
else
{
ComPtr<IMFMediaType> spMediaType;
IFR(spUnknown.As(&spMediaType));
if (!fFlush && !_fGetFirstSampleTime)
{
IFR(PrepareFormatChange(spMediaType.Get(), &spDataBundle));
}
}
}
if (nullptr != spDataBundle.Get())
{
ComPtr<IAsyncAction> spSendAction;
if (FAILED(_spConnection->SendBundleAsync(spDataBundle.Get(), &spSendAction)))
{
fProcessingSample = false;
}
else
{
ComPtr<NetworkMediaSinkStreamImpl> spThis(this);
IFR(StartAsyncThen(
spSendAction.Get(),
[this, spThis, fProcessingSample](_In_ HRESULT hr, _In_ IAsyncAction* pResult, _In_ AsyncStatus asyncStatus) -> HRESULT
{
LOG_RESULT(hr);
if (_state == SinkStreamState_Started && fProcessingSample)
{
// If we are still in started state request another sample
IFR(QueueEvent(MEStreamSinkRequestSample, GUID_NULL, S_OK, nullptr));
}
return S_OK;
}));
}
// We stop if we processed a sample otherwise keep looking
fSendSamples = !fProcessingSample;
}
if (fSendSamples)
{
if (FAILED(_sampleQueue.RemoveFront(&spUnknown)))
{
fRequestSamples = true;
fSendSamples = false;
}
}
}
if (fSendEOS)
{
ComPtr<NetworkMediaSinkStreamImpl> spThis(this);
ComPtr<INetworkMediaSink> spParent(_spParentMediaSink.Get());
concurrency::create_task([this, spThis, spParent]()
{
spParent->OnEndOfStream(_dwStreamId);
});
fSendSamples = false;
}
if (nullptr != fNeedMoreSamples)
{
if (FAILED(CheckShutdown()))
{
*fNeedMoreSamples = false;
}
else
{
*fNeedMoreSamples = !fSendSamples;
}
}
return S_OK;
}
HRESULT NetworkMediaSinkStreamImpl::ProcessFormatChange(
IMFMediaType* pMediaType)
{
Log(Log_Level_Info, L"NetworkMediaSinkStreamImpl::ProcessFormatChange()\n");
NULL_CHK(pMediaType)
// Add the media type to the sample queue.
IFR(_sampleQueue.InsertBack(pMediaType));
// Unless we are paused, start an async operation to dispatch the next sample.
// Queue the operation.
return QueueAsyncOperation(SinkStreamOperation_SetMediaType);
}
// Prepare bundle to send with frame sample data
_Use_decl_annotations_
HRESULT NetworkMediaSinkStreamImpl::PrepareSample(
IMFSample* pSample,
bool fForce,
IDataBundle** ppDataBundle)
{
Log(Log_Level_Info, L"NetworkMediaSinkStreamImpl::PrepareSample()\n");
NULL_CHK(pSample);
const size_t c_cPayloadHeader = sizeof(PayloadHeader);
const size_t c_cMediaSampleHeader = sizeof(MediaSampleHeader);
const size_t c_cbSampleHeaderSize = c_cPayloadHeader + c_cMediaSampleHeader;
LONGLONG llSampleTime;
IFR(pSample->GetSampleTime(&llSampleTime));
LONGLONG llDuration;
IFR(pSample->GetSampleDuration(&llDuration));
llSampleTime -= _adjustedStartTime;
if (llSampleTime < 0 && !fForce)
{
IFR(MF_E_LATE_SAMPLE);
}
if (llSampleTime < 0)
{
llSampleTime = 0;
}
DWORD cbTotalSampleLength = 0;
IFR(pSample->GetTotalLength(&cbTotalSampleLength));
// Create a bundle and initialize it with the sample
ComPtr<IDataBundle> spBundle;
IFR(MakeAndInitialize<DataBundleImpl>(&spBundle, pSample));
// create a buffer for the media sample header
ComPtr<IDataBuffer> spDataBuffer;
IFR(MakeAndInitialize<DataBufferImpl>(&spDataBuffer, c_cbSampleHeaderSize));
// Prepare the buffer
ComPtr<IBuffer> spHeaderBuffer;
IFR(spDataBuffer.As(&spHeaderBuffer));
BYTE* pBuf = GetDataType<BYTE*>(spHeaderBuffer.Get());
// populate the PayloadType header
PayloadHeader* pOpHeader = reinterpret_cast<PayloadHeader*>(pBuf);
ZeroMemory(pOpHeader, c_cPayloadHeader);
// fill in the PayloadType header info