forked from microsoft/HoloLensForCV
-
Notifications
You must be signed in to change notification settings - Fork 9
/
MediaFrameSourceGroup.cpp
787 lines (677 loc) · 30.9 KB
/
MediaFrameSourceGroup.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
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "pch.h"
namespace HoloLensForCV
{
static const Platform::Guid c_MF_MT_USER_DATA(
0xb6bc765f, 0x4c3b, 0x40a4, 0xbd, 0x51, 0x25, 0x35, 0xb6, 0x6f, 0xe0, 0x9d);
MediaFrameSourceGroup::MediaFrameSourceGroup(
_In_ MediaFrameSourceGroupType mediaFrameSourceGroupType,
_In_ SpatialPerception^ spatialPerception,
_In_ DeviceType deviceType,
_In_opt_ ISensorFrameSinkGroup^ optionalSensorFrameSinkGroup)
: _mediaFrameSourceGroupType(mediaFrameSourceGroupType)
, _spatialPerception(spatialPerception)
, _deviceType(deviceType)
, _optionalSensorFrameSinkGroup(optionalSensorFrameSinkGroup)
{
}
void MediaFrameSourceGroup::EnableAll()
{
switch (_mediaFrameSourceGroupType)
{
case MediaFrameSourceGroupType::PhotoVideoCamera:
Enable(SensorType::PhotoVideo);
break;
#if ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS
case MediaFrameSourceGroupType::HoloLensResearchModeSensors:
Enable(SensorType::ShortThrowToFDepth);
Enable(SensorType::ShortThrowToFReflectivity);
Enable(SensorType::LongThrowToFDepth);
Enable(SensorType::LongThrowToFReflectivity);
Enable(SensorType::VisibleLightLeftLeft);
Enable(SensorType::VisibleLightLeftFront);
Enable(SensorType::VisibleLightRightFront);
Enable(SensorType::VisibleLightRightRight);
break;
#endif /* ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS */
}
}
void MediaFrameSourceGroup::Enable(
_In_ SensorType sensorType)
{
const int32_t sensorTypeAsIndex =
(int32_t)sensorType;
REQUIRES(
0 <= sensorTypeAsIndex &&
sensorTypeAsIndex < (int32_t)_enabledFrameReaders.size());
_enabledFrameReaders[sensorTypeAsIndex] =
true;
}
bool MediaFrameSourceGroup::IsEnabled(
_In_ SensorType sensorType) const
{
const int32_t sensorTypeAsIndex =
(int32_t)sensorType;
REQUIRES(
0 <= sensorTypeAsIndex &&
sensorTypeAsIndex < (int32_t)_enabledFrameReaders.size());
return _enabledFrameReaders[sensorTypeAsIndex];
}
Windows::Foundation::IAsyncAction^ MediaFrameSourceGroup::StartAsync()
{
return concurrency::create_async(
[this]()
{
return InitializeMediaSourceWorkerAsync();
});
}
///
/// Control the ArUco marker tracker class. Instantiate class object with
/// initial parameters.
///
Windows::Foundation::IAsyncAction^ MediaFrameSourceGroup::StartArUcoMarkerTrackerAsync(
float markerSize,
int dictId,
Windows::Perception::Spatial::SpatialCoordinateSystem^ unitySpatialCoordinateSystem)
{
// Instantiate aruco marker tracker class with parameters.
return concurrency::create_async(
[this, markerSize, dictId, unitySpatialCoordinateSystem]()
{
_arUcoMarkerTracker = ref new ArUcoMarkerTracker(markerSize, dictId, unitySpatialCoordinateSystem);
});
}
///
/// Get the current sensor frame and process using aruco libary. Return a vector of
/// detected markers across the application boundary to the C# environment.
///
Windows::Foundation::Collections::IVector<DetectedArUcoMarker^>^
MediaFrameSourceGroup::DetectArUcoMarkers(SensorType type)
{
// Get the current sensor frame from media frame source group
SensorFrame^ frame = GetLatestSensorFrame(type);
// Process the sensor frame using aruco marker tracker class
auto detections = _arUcoMarkerTracker->DetectArUcoMarkersInFrame(frame);
return detections;
}
Windows::Foundation::IAsyncAction^ MediaFrameSourceGroup::StopAsync()
{
return concurrency::create_async(
[this]()
{
return CleanupMediaCaptureAsync();
});
}
SensorFrame^ MediaFrameSourceGroup::GetLatestSensorFrame(
SensorType sensorType)
{
const int32_t sensorTypeAsIndex =
(int32_t)sensorType;
REQUIRES(
0 <= sensorTypeAsIndex &&
sensorTypeAsIndex < (int32_t)_frameReaders.size());
if (_frameReaders[sensorTypeAsIndex] == nullptr)
{
return nullptr;
}
return _frameReaders[sensorTypeAsIndex]->GetLatestSensorFrame();
}
Concurrency::task<void> MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync()
{
return CleanupMediaCaptureAsync()
.then([this]()
{
return Concurrency::create_task(
Windows::Media::Capture::Frames::MediaFrameSourceGroup::FindAllAsync());
}, Concurrency::task_continuation_context::get_current_winrt_context())
.then([this](Windows::Foundation::Collections::IVectorView<Windows::Media::Capture::Frames::MediaFrameSourceGroup^>^ sourceGroups)
{
Windows::Media::Capture::Frames::MediaFrameSourceGroup^ selectedSourceGroup;
for (Windows::Media::Capture::Frames::MediaFrameSourceGroup^ sourceGroup : sourceGroups)
{
const wchar_t* sourceGroupDisplayName =
sourceGroup->DisplayName->Data();
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: source group display name: %s", sourceGroupDisplayName);
//
// Note: this is the display name of the media frame source group associated
// with the photo/video camera (or, RGB camera) on HoloLens Development Edition.
//
const wchar_t* c_HoloLensDevelopmentEditionPhotoVideoSourceGroupDisplayName =
L"MN34150";
const wchar_t* c_HoloLensResearchModeSensorStreamingGroupDisplayName =
L"Sensor Streaming";
const wchar_t* c_HoloLens2PhotoVideoSourceGroupDisplayName =
L"QC Back Camera";
//
// HoloLens 2: QC Back Camera is available.
// Source group: QC Back Camera
// Frame Source: Color VideoPreview
// Media Format: Video, NV12, 896 x 504, 30 fps
//
// Include the HoloLens 2 display name
if (MediaFrameSourceGroupType::PhotoVideoCamera == _mediaFrameSourceGroupType &&
(0 == wcscmp(c_HoloLensDevelopmentEditionPhotoVideoSourceGroupDisplayName, sourceGroupDisplayName) ||
0 == wcscmp(c_HoloLens2PhotoVideoSourceGroupDisplayName, sourceGroupDisplayName)))
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: found the photo-video media frame source group.");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
selectedSourceGroup =
sourceGroup;
break;
}
#if ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS
else if (MediaFrameSourceGroupType::HoloLensResearchModeSensors == _mediaFrameSourceGroupType &&
(0 == wcscmp(c_HoloLensResearchModeSensorStreamingGroupDisplayName, sourceGroupDisplayName)))
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: found the HoloLens Sensor Streaming media frame source group.");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
selectedSourceGroup =
sourceGroup;
break;
}
#endif /* ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS */
}
if (nullptr == selectedSourceGroup)
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: selected media frame source group not found.");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return Concurrency::task_from_result();
}
//
// Initialize MediaCapture with the selected group.
//
return TryInitializeMediaCaptureAsync(selectedSourceGroup)
.then([this, selectedSourceGroup](bool initialized)
{
if (!initialized)
{
return CleanupMediaCaptureAsync();
}
//
// Set up frame readers, register event handlers and start streaming.
//
auto startedSensors =
std::make_shared<std::unordered_set<SensorType, SensorTypeHash>>();
Concurrency::task<void> createReadersTask =
Concurrency::task_from_result();
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: selected group has %i media frame sources",
_mediaCapture->FrameSources->Size);
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
for (Windows::Foundation::Collections::IKeyValuePair<Platform::String^, Windows::Media::Capture::Frames::MediaFrameSource^>^ kvp : _mediaCapture->FrameSources)
{
Windows::Media::Capture::Frames::MediaFrameSource^ source =
kvp->Value;
createReadersTask = createReadersTask.then([this, startedSensors, source]()
{
SensorType sensorType =
GetSensorType(
source);
if (SensorType::Undefined == sensorType)
{
//
// We couldn't map the source to a Research Mode sensor type. Ignore this source.
//
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: could not map the media frame source to a Research Mode sensor type!");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return Concurrency::task_from_result();
}
else if (startedSensors->find(sensorType) != startedSensors->end())
{
//
// We couldn't map the source to a Research Mode sensor type. Ignore this source.
//
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: sensor type %s has already been initialized!",
sensorType.ToString());
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return Concurrency::task_from_result();
}
else if (!IsEnabled(sensorType))
{
//
// The sensor type was not explicitly enabled by user. Ignore this source.
//
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: sensor type %s has not been enabled!",
sensorType.ToString());
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return Concurrency::task_from_result();
}
//
// Look for a format which the FrameRenderer can render.
//
Platform::String^ requestedSubtype =
nullptr;
auto found =
std::find_if(
begin(source->SupportedFormats),
end(source->SupportedFormats),
[&](Windows::Media::Capture::Frames::MediaFrameFormat^ format)
{
requestedSubtype =
GetSubtypeForFrameReader(
source->Info->SourceKind,
format);
return requestedSubtype != nullptr;
});
if (requestedSubtype == nullptr)
{
//
// No acceptable format was found. Ignore this source.
//
return Concurrency::task_from_result();
}
//
// Tell the source to use the format we can render.
//
return Concurrency::create_task(
source->SetFormatAsync(*found))
.then([this, source, requestedSubtype]()
{
return Concurrency::create_task(
_mediaCapture->CreateFrameReaderAsync(
source,
requestedSubtype));
}, Concurrency::task_continuation_context::get_current_winrt_context())
.then([this, sensorType](Windows::Media::Capture::Frames::MediaFrameReader^ frameReader)
{
ISensorFrameSink^ optionalSensorFrameSink =
nullptr != _optionalSensorFrameSinkGroup
? _optionalSensorFrameSinkGroup->GetSensorFrameSink(
sensorType)
: nullptr;
MediaFrameReaderContext^ frameReaderContext =
ref new MediaFrameReaderContext(
sensorType,
_spatialPerception,
_deviceType,
optionalSensorFrameSink);
_frameReaders[(int32_t)sensorType] =
frameReaderContext;
Windows::Foundation::EventRegistrationToken token =
frameReader->FrameArrived +=
ref new Windows::Foundation::TypedEventHandler<
Windows::Media::Capture::Frames::MediaFrameReader^,
Windows::Media::Capture::Frames::MediaFrameArrivedEventArgs^>(
frameReaderContext,
&MediaFrameReaderContext::FrameArrived);
//
// Keep track of created reader and event handler so it can be stopped later.
//
_frameEventRegistrations.push_back(
std::make_pair(
frameReader,
token));
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: created the '%s' frame reader",
sensorType.ToString()->Data());
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return Concurrency::create_task(
frameReader->StartAsync());
}, Concurrency::task_continuation_context::get_current_winrt_context())
.then([this, sensorType, startedSensors](Windows::Media::Capture::Frames::MediaFrameReaderStartStatus status)
{
if (status == Windows::Media::Capture::Frames::MediaFrameReaderStartStatus::Success)
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: started the '%s' frame reader",
sensorType.ToString()->Data());
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
startedSensors->insert(
sensorType);
}
else
{
#if DBG_ENABLE_ERROR_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: unable to start the '%s' frame reader. Error: %s",
sensorType.ToString()->Data(),
status.ToString()->Data());
#endif /* DBG_ENABLE_ERROR_LOGGING */
}
}, Concurrency::task_continuation_context::get_current_winrt_context());
}, Concurrency::task_continuation_context::get_current_winrt_context());
}
//
// Run the loop and see if any sources were used.
//
return createReadersTask.then([this, startedSensors, selectedSourceGroup]()
{
if (startedSensors->size() == 0)
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::InitializeMediaSourceWorkerAsync: no eligible sources in '%s'",
selectedSourceGroup->DisplayName->Data());
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
}
}, Concurrency::task_continuation_context::get_current_winrt_context());
}, Concurrency::task_continuation_context::get_current_winrt_context());
}, Concurrency::task_continuation_context::get_current_winrt_context());
}
SensorType MediaFrameSourceGroup::GetSensorType(
Windows::Media::Capture::Frames::MediaFrameSource^ source)
{
//
// First check if the request is concerning the PV camera.
//
if (MediaFrameSourceGroupType::PhotoVideoCamera == _mediaFrameSourceGroupType)
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::GetSensorType:: assuming SensorType::PhotoVideo per _mediaFrameSourceGroupType check (source id is '%s')",
source->Info->Id->Data());
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return SensorType::PhotoVideo;
}
//
// The sensor streaming DMFT exposes the sensor names through the MF_MT_USER_DATA
// property (GUID for that property is {b6bc765f-4c3b-40a4-bd51-2535b66fe09d}).
//
if (!source->Info->Properties->HasKey(c_MF_MT_USER_DATA))
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::GetSensorType:: assuming SensorType::Undefined given missing MF_MT_USER_DATA");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return SensorType::Undefined;
}
Platform::Object^ mfMtUserData =
source->Info->Properties->Lookup(
c_MF_MT_USER_DATA);
Platform::Array<byte>^ sensorNameAsPlatformArray =
safe_cast<Platform::IBoxArray<byte>^>(
mfMtUserData)->Value;
const wchar_t* sensorName =
reinterpret_cast<wchar_t*>(
sensorNameAsPlatformArray->Data);
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::GetSensorType:: found sensor name '%s' in MF_MT_USER_DATA (blob has %i bytes)",
sensorName,
sensorNameAsPlatformArray->Length);
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
#if ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS
if (0 == wcscmp(sensorName, L"Short Throw ToF Depth"))
{
return SensorType::ShortThrowToFDepth;
}
else if (0 == wcscmp(sensorName, L"Short Throw ToF Reflectivity"))
{
return SensorType::ShortThrowToFReflectivity;
}
if (0 == wcscmp(sensorName, L"Long Throw ToF Depth"))
{
return SensorType::LongThrowToFDepth;
}
else if (0 == wcscmp(sensorName, L"Long Throw ToF Reflectivity"))
{
return SensorType::LongThrowToFReflectivity;
}
else if (0 == wcscmp(sensorName, L"Visible Light Left-Left"))
{
return SensorType::VisibleLightLeftLeft;
}
else if (0 == wcscmp(sensorName, L"Visible Light Left-Front"))
{
return SensorType::VisibleLightLeftFront;
}
else if (0 == wcscmp(sensorName, L"Visible Light Right-Front"))
{
return SensorType::VisibleLightRightFront;
}
else if (0 == wcscmp(sensorName, L"Visible Light Right-Right"))
{
return SensorType::VisibleLightRightRight;
}
else
#endif /* ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS */
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::GetSensorType:: could not match sensor name to SensorType enumeration");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return SensorType::Undefined;
}
}
Platform::String^ MediaFrameSourceGroup::GetSubtypeForFrameReader(
Windows::Media::Capture::Frames::MediaFrameSourceKind kind,
Windows::Media::Capture::Frames::MediaFrameFormat^ format)
{
//
// Note that media encoding subtypes may differ in case.
// https://docs.microsoft.com/en-us/uwp/api/Windows.Media.MediaProperties.MediaEncodingSubtypes
//
switch (kind)
{
case Windows::Media::Capture::Frames::MediaFrameSourceKind::Color:
// Force set the media stream to the desired format
// Ensure we select the desired video stream, check width and height
// MediaFrameSourceInfo->Source->Profile Width : 896, Height : 504, FrameRate : 29.970030
// Force the lowest resolution for streaming. TODO: make this something that can be selected.
if (format->VideoFormat->Width == 896 && format->VideoFormat->Height == 504)
//if (true)
{
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::GetSubtypeForFrameReader: evaluating MediaFrameSourceKind::Color with format %s-%s @%i/%iHz and resolution %i x %i",
format->MajorType->Data(),
format->Subtype->Data(),
format->FrameRate->Numerator,
format->FrameRate->Denominator,
format->VideoFormat->Width,
format->VideoFormat->Height);
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
//
// For color sources, we accept anything and request that it be converted to Bgra8.
//
return Windows::Media::MediaProperties::MediaEncodingSubtypes::Bgra8;
}
#if ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS
case Windows::Media::Capture::Frames::MediaFrameSourceKind::Depth:
{
if (MediaFrameSourceGroupType::HoloLensResearchModeSensors == _mediaFrameSourceGroupType)
{
//
// The only depth format we can render is D16.
//
dbg::trace(
L"MediaFrameSourceGroup::GetSubtypeForFrameReader: evaluating MediaFrameSourceKind::Depth with format %s-%s @%i/%iHz",
format->MajorType->Data(),
format->Subtype->Data(),
format->FrameRate->Numerator,
format->FrameRate->Denominator);
const bool isD16 =
CompareStringOrdinal(
format->Subtype->Data(),
-1 /* cchCount1 */,
Windows::Media::MediaProperties::MediaEncodingSubtypes::D16->Data(),
-1 /* cchCount2 */,
TRUE /* bIgnoreCase */) == CSTR_EQUAL;
return isD16 ? format->Subtype : nullptr;
}
else
{
return nullptr;
}
}
case Windows::Media::Capture::Frames::MediaFrameSourceKind::Infrared:
{
if (MediaFrameSourceGroupType::HoloLensResearchModeSensors == _mediaFrameSourceGroupType)
{
//
// The only infrared formats we can render are L8 and L16.
//
dbg::trace(
L"MediaFrameSourceGroup::GetSubtypeForFrameReader: evaluating MediaFrameSourceKind::Infrared with format %s-%s @%i/%iHz",
format->MajorType->Data(),
format->Subtype->Data(),
format->FrameRate->Numerator,
format->FrameRate->Denominator);
const bool isL8 =
CompareStringOrdinal(
format->Subtype->Data(),
-1 /* cchCount1 */,
Windows::Media::MediaProperties::MediaEncodingSubtypes::L8->Data(),
-1 /* cchCount2 */,
TRUE /* bIgnoreCase */) == CSTR_EQUAL;
const bool isL16 =
CompareStringOrdinal(
format->Subtype->Data(),
-1 /* cchCount1 */,
Windows::Media::MediaProperties::MediaEncodingSubtypes::L16->Data(),
-1 /* cchCount2 */,
TRUE /* bIgnoreCase */) == CSTR_EQUAL;
return (isL8 || isL16) ? format->Subtype : nullptr;
}
else
{
return nullptr;
}
}
#endif /* ENABLE_HOLOLENS_RESEARCH_MODE_SENSORS */
default:
//
// No other source kinds are supported by this class.
//
return nullptr;
}
}
Concurrency::task<bool> MediaFrameSourceGroup::TryInitializeMediaCaptureAsync(
Windows::Media::Capture::Frames::MediaFrameSourceGroup^ group)
{
if (_mediaCapture != nullptr)
{
//
// Already initialized.
//
return Concurrency::task_from_result(true);
}
//
// Initialize media capture with the source group.
//
_mediaCapture =
ref new Windows::Media::Capture::MediaCapture();
auto settings =
ref new Windows::Media::Capture::MediaCaptureInitializationSettings();
//
// Select the source we will be reading from.
//
settings->SourceGroup =
group;
//
// This media capture can share streaming with other apps.
//
// For HL1
if (_deviceType == DeviceType::HL1)
{
settings->SharingMode =
Windows::Media::Capture::MediaCaptureSharingMode::SharedReadOnly;
}
// For HL2
if (_deviceType == DeviceType::HL2)
{
settings->SharingMode =
Windows::Media::Capture::MediaCaptureSharingMode::ExclusiveControl;
}
//
// Only stream video and don't initialize audio capture devices.
//
settings->StreamingCaptureMode =
Windows::Media::Capture::StreamingCaptureMode::Video;
//
// Set to CPU to ensure frames always contain CPU SoftwareBitmap images,
// instead of preferring GPU D3DSurface images.
//
settings->MemoryPreference =
Windows::Media::Capture::MediaCaptureMemoryPreference::Cpu;
//
// Only stream video and don't initialize audio capture devices.
//
settings->StreamingCaptureMode =
Windows::Media::Capture::StreamingCaptureMode::Video;
//
// Initialize MediaCapture with the specified group.
// This must occur on the UI thread because some device families
// (such as Xbox) will prompt the user to grant consent for the
// app to access cameras.
// This can raise an exception if the source no longer exists,
// or if the source could not be initialized.
//
return Concurrency::create_task(
_mediaCapture->InitializeAsync(
settings))
.then([this](Concurrency::task<void> initializeMediaCaptureTask)
{
try
{
//
// Get the result of the initialization. This call will throw if initialization failed
// This pattern is documented at https://msdn.microsoft.com/en-us/library/dd997692.aspx
//
initializeMediaCaptureTask.get();
#if DBG_ENABLE_INFORMATIONAL_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::TryInitializeMediaCaptureAsync: MediaCapture is successfully initialized in shared mode.");
#endif /* DBG_ENABLE_INFORMATIONAL_LOGGING */
return true;
}
catch (Platform::Exception^ exception)
{
#if DBG_ENABLE_ERROR_LOGGING
dbg::trace(
L"MediaFrameSourceGroup::TryInitializeMediaCaptureAsync: failed to initialize media capture: %s",
exception->Message->Data());
#endif /* DBG_ENABLE_ERROR_LOGGING */
return false;
}
});
}
Concurrency::task<void> MediaFrameSourceGroup::CleanupMediaCaptureAsync()
{
Concurrency::task<void> cleanupTask =
Concurrency::task_from_result();
if (_mediaCapture != nullptr)
{
for (auto& readerAndToken : _frameEventRegistrations)
{
Windows::Media::Capture::Frames::MediaFrameReader^ reader =
readerAndToken.first;
Windows::Foundation::EventRegistrationToken token =
readerAndToken.second;
reader->FrameArrived -= token;
cleanupTask =
cleanupTask &&
Concurrency::create_task(
reader->StopAsync());
}
_frameEventRegistrations.clear();
_mediaCapture = nullptr;
}
return cleanupTask;
}
}