summaryrefslogtreecommitdiffstats
path: root/src/plugins/winrt/qwinrtcameracontrol.cpp
blob: 98dd7c2f7c21a2eb43dd2a733c4e7e5c8437d4f7 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwinrtcameracontrol.h"
#include "qwinrtcameravideorenderercontrol.h"
#include "qwinrtvideodeviceselectorcontrol.h"
#include "qwinrtcameraimagecapturecontrol.h"
#include "qwinrtimageencodercontrol.h"
#include "qwinrtcameraflashcontrol.h"
#include "qwinrtcamerafocuscontrol.h"
#include "qwinrtcameralockscontrol.h"

#include <QtCore/qfunctions_winrt.h>
#include <QtCore/QMutex>
#include <QtCore/QPointer>
#include <QtGui/QGuiApplication>
#include <private/qeventdispatcher_winrt_p.h>

#include <functional>
#include <mfapi.h>
#include <mferror.h>
#include <mfidl.h>
#include <wrl.h>
#include <windows.devices.enumeration.h>
#include <windows.media.capture.h>
#include <windows.storage.streams.h>
#include <windows.media.devices.h>

#include <algorithm>

using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Devices::Enumeration;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::Media;
using namespace ABI::Windows::Media::Capture;
using namespace ABI::Windows::Media::Devices;
using namespace ABI::Windows::Media::MediaProperties;
using namespace ABI::Windows::Storage::Streams;

QT_BEGIN_NAMESPACE

#define RETURN_VOID_AND_EMIT_ERROR(msg) \
    if (FAILED(hr)) { \
        emit error(QCamera::CameraError, qt_error_string(hr)); \
        RETURN_VOID_IF_FAILED(msg); \
    }

#define FOCUS_RECT_SIZE         0.01f
#define FOCUS_RECT_HALF_SIZE    0.005f // FOCUS_RECT_SIZE / 2
#define FOCUS_RECT_BOUNDARY     1.0f
#define FOCUS_RECT_POSITION_MIN 0.0f
#define FOCUS_RECT_POSITION_MAX 0.995f // FOCUS_RECT_BOUNDARY - FOCUS_RECT_HALF_SIZE
#define ASPECTRATIO_EPSILON 0.01f

Q_LOGGING_CATEGORY(lcMMCamera, "qt.mm.camera")

HRESULT getMediaStreamResolutions(IMediaDeviceController *device,
                                  MediaStreamType type,
                                  IVectorView<IMediaEncodingProperties *> **propertiesList,
                                  QVector<QSize> *resolutions)
{
    HRESULT hr;
    hr = device->GetAvailableMediaStreamProperties(type, propertiesList);
    Q_ASSERT_SUCCEEDED(hr);
    quint32 listSize;
    hr = (*propertiesList)->get_Size(&listSize);
    Q_ASSERT_SUCCEEDED(hr);
    resolutions->reserve(int(listSize));
    for (quint32 index = 0; index < listSize; ++index) {
        ComPtr<IMediaEncodingProperties> properties;
        hr = (*propertiesList)->GetAt(index, &properties);
        Q_ASSERT_SUCCEEDED(hr);
        HString propertyType;
        hr = properties->get_Type(propertyType.GetAddressOf());
        Q_ASSERT_SUCCEEDED(hr);

        const HStringReference videoRef = HString::MakeReference(L"Video");
        const HStringReference imageRef = HString::MakeReference(L"Image");
        if (propertyType == videoRef) {
            ComPtr<IVideoEncodingProperties> videoProperties;
            hr = properties.As(&videoProperties);
            Q_ASSERT_SUCCEEDED(hr);
            UINT32 width, height;
            hr = videoProperties->get_Width(&width);
            Q_ASSERT_SUCCEEDED(hr);
            hr = videoProperties->get_Height(&height);
            Q_ASSERT_SUCCEEDED(hr);
            resolutions->append(QSize(int(width), int(height)));
        } else if (propertyType == imageRef) {
            ComPtr<IImageEncodingProperties> imageProperties;
            hr = properties.As(&imageProperties);
            Q_ASSERT_SUCCEEDED(hr);
            UINT32 width, height;
            hr = imageProperties->get_Width(&width);
            Q_ASSERT_SUCCEEDED(hr);
            hr = imageProperties->get_Height(&height);
            Q_ASSERT_SUCCEEDED(hr);
            resolutions->append(QSize(int(width), int(height)));
        }
    }
    return resolutions->isEmpty() ? MF_E_INVALID_FORMAT : hr;
}

template<typename T, size_t typeSize> struct CustomPropertyValue;

inline static ComPtr<IPropertyValueStatics> propertyValueStatics()
{
    ComPtr<IPropertyValueStatics> valueStatics;
    GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(), &valueStatics);
    return valueStatics;
}

template <typename T>
struct CustomPropertyValue<T, 4>
{
    static ComPtr<IReference<T>> create(T value)
    {
        ComPtr<IInspectable> propertyValueObject;
        HRESULT hr = propertyValueStatics()->CreateUInt32(value, &propertyValueObject);
        ComPtr<IReference<UINT32>> uint32Object;
        Q_ASSERT_SUCCEEDED(hr);
        hr = propertyValueObject.As(&uint32Object);
        Q_ASSERT_SUCCEEDED(hr);
        return reinterpret_cast<IReference<T> *>(uint32Object.Get());
    }
};

template <typename T>
struct CustomPropertyValue<T, 8>
{
    static ComPtr<IReference<T>> create(T value)
    {
        ComPtr<IInspectable> propertyValueObject;
        HRESULT hr = propertyValueStatics()->CreateUInt64(value, &propertyValueObject);
        ComPtr<IReference<UINT64>> uint64Object;
        Q_ASSERT_SUCCEEDED(hr);
        hr = propertyValueObject.As(&uint64Object);
        Q_ASSERT_SUCCEEDED(hr);
        return reinterpret_cast<IReference<T> *>(uint64Object.Get());
    }
};

// Required camera point focus
class WindowsRegionOfInterestIterableIterator : public RuntimeClass<IIterator<RegionOfInterest *>>
{
public:
    explicit WindowsRegionOfInterestIterableIterator(const ComPtr<IRegionOfInterest> &item)
    {
        regionOfInterest = item;
    }
    HRESULT __stdcall get_Current(IRegionOfInterest **current)
    {
        *current = regionOfInterest.Detach();
        return S_OK;
    }
    HRESULT __stdcall get_HasCurrent(boolean *hasCurrent)
    {
        *hasCurrent = true;
        return S_OK;
    }
    HRESULT __stdcall MoveNext(boolean *hasCurrent)
    {
        *hasCurrent = false;
        return S_OK;
    }
private:
    ComPtr<IRegionOfInterest> regionOfInterest;
};

class WindowsRegionOfInterestIterable : public RuntimeClass<IIterable<RegionOfInterest *>>
{
public:
    explicit WindowsRegionOfInterestIterable(const ComPtr<IRegionOfInterest> &item)
    {
        regionOfInterest = item;
    }
    HRESULT __stdcall First(IIterator<RegionOfInterest *> **first)
    {
        ComPtr<WindowsRegionOfInterestIterableIterator> iterator = Make<WindowsRegionOfInterestIterableIterator>(regionOfInterest);
        *first = iterator.Detach();
        return S_OK;
    }
private:
    ComPtr<IRegionOfInterest> regionOfInterest;
};

class MediaStream : public RuntimeClass<RuntimeClassFlags<WinRtClassicComMix>, IMFStreamSink, IMFMediaEventGenerator, IMFMediaTypeHandler>
{
public:
    MediaStream(IMFMediaType *type, IMFMediaSink *mediaSink, QWinRTCameraVideoRendererControl *videoRenderer)
        : m_type(type), m_sink(mediaSink), m_videoRenderer(videoRenderer)
    {
        Q_ASSERT(m_videoRenderer);

        HRESULT hr;
        hr = MFCreateEventQueue(&m_eventQueue);
        Q_ASSERT_SUCCEEDED(hr);
        hr = MFAllocateSerialWorkQueue(MFASYNC_CALLBACK_QUEUE_STANDARD, &m_workQueueId);
        Q_ASSERT_SUCCEEDED(hr);
    }

    ~MediaStream() override
    {
        QMutexLocker locker(&m_mutex);
        m_eventQueue->Shutdown();
    }

    HRESULT RequestSample()
    {
        if (m_pendingSamples.load() < 3) {
            m_pendingSamples.ref();
            return QueueEvent(MEStreamSinkRequestSample, GUID_NULL, S_OK, nullptr);
        }
        return S_OK;
    }

    HRESULT __stdcall GetEvent(DWORD flags, IMFMediaEvent **event) override
    {
        QMutexLocker locker(&m_mutex);
        // Create an extra reference to avoid deadlock
        ComPtr<IMFMediaEventQueue> eventQueue = m_eventQueue;
        locker.unlock();

        return eventQueue->GetEvent(flags, event);
    }

    HRESULT __stdcall BeginGetEvent(IMFAsyncCallback *callback, IUnknown *state) override
    {
        QMutexLocker locker(&m_mutex);
        HRESULT hr = m_eventQueue->BeginGetEvent(callback, state);
        return hr;
    }

    HRESULT __stdcall EndGetEvent(IMFAsyncResult *result, IMFMediaEvent **event) override
    {
        QMutexLocker locker(&m_mutex);
        return m_eventQueue->EndGetEvent(result, event);
    }

    HRESULT __stdcall QueueEvent(MediaEventType eventType, const GUID &extendedType, HRESULT status, const PROPVARIANT *value) override
    {
        QMutexLocker locker(&m_mutex);
        return m_eventQueue->QueueEventParamVar(eventType, extendedType, status, value);
    }

    HRESULT __stdcall GetMediaSink(IMFMediaSink **mediaSink) override
    {
        m_sink->AddRef();
        *mediaSink = m_sink;
        return S_OK;
    }

    HRESULT __stdcall GetIdentifier(DWORD *identifier) override
    {
        *identifier = 0;
        return S_OK;
    }

    HRESULT __stdcall GetMediaTypeHandler(IMFMediaTypeHandler **handler) override
    {
        return QueryInterface(IID_PPV_ARGS(handler));
    }

    HRESULT __stdcall ProcessSample(IMFSample *sample) override
    {
        ComPtr<IMFMediaBuffer> buffer;
        HRESULT hr = sample->GetBufferByIndex(0, &buffer);
        RETURN_HR_IF_FAILED("Failed to get buffer from camera sample");
        ComPtr<IMF2DBuffer> buffer2d;
        hr = buffer.As(&buffer2d);
        RETURN_HR_IF_FAILED("Failed to cast camera sample buffer to 2D buffer");

        m_pendingSamples.deref();
        m_videoRenderer->queueBuffer(buffer2d.Get());

        return hr;
    }

    HRESULT __stdcall PlaceMarker(MFSTREAMSINK_MARKER_TYPE type, const PROPVARIANT *value, const PROPVARIANT *context) override
    {
        Q_UNUSED(type);
        Q_UNUSED(value);
        QueueEvent(MEStreamSinkMarker, GUID_NULL, S_OK, context);
        return S_OK;
    }

    HRESULT __stdcall Flush() override
    {
        m_videoRenderer->discardBuffers();
        m_pendingSamples.store(0);
        return S_OK;
    }

    HRESULT __stdcall IsMediaTypeSupported(IMFMediaType *type, IMFMediaType **) override
    {
        HRESULT hr;
        GUID majorType;
        hr = type->GetMajorType(&majorType);
        Q_ASSERT_SUCCEEDED(hr);
        if (!IsEqualGUID(majorType, MFMediaType_Video))
            return MF_E_INVALIDMEDIATYPE;
        return S_OK;
    }

    HRESULT __stdcall GetMediaTypeCount(DWORD *typeCount) override
    {
        *typeCount = 1;
        return S_OK;
    }

    HRESULT __stdcall GetMediaTypeByIndex(DWORD index, IMFMediaType **type) override
    {
        if (index == 0)
            return m_type.CopyTo(type);
        return E_BOUNDS;
    }

    HRESULT __stdcall SetCurrentMediaType(IMFMediaType *type) override
    {
        if (FAILED(IsMediaTypeSupported(type, nullptr)))
            return MF_E_INVALIDREQUEST;

        m_type = type;
        return S_OK;
    }

    HRESULT __stdcall GetCurrentMediaType(IMFMediaType **type) override
    {
        return m_type.CopyTo(type);
    }

    HRESULT __stdcall GetMajorType(GUID *majorType) override
    {
        return m_type->GetMajorType(majorType);
    }

private:
    QMutex m_mutex;
    ComPtr<IMFMediaType> m_type;
    IMFMediaSink *m_sink;
    ComPtr<IMFMediaEventQueue> m_eventQueue;
    DWORD m_workQueueId;

    QWinRTCameraVideoRendererControl *m_videoRenderer;
    QAtomicInt m_pendingSamples;
};

class MediaSink : public RuntimeClass<RuntimeClassFlags<WinRtClassicComMix>, IMediaExtension, IMFMediaSink, IMFClockStateSink>
{
public:
    MediaSink(IMediaEncodingProfile *encodingProfile, QWinRTCameraVideoRendererControl *videoRenderer)
        : m_videoRenderer(videoRenderer)
    {
        HRESULT hr;
        ComPtr<IVideoEncodingProperties> videoProperties;
        hr = encodingProfile->get_Video(&videoProperties);
        RETURN_VOID_IF_FAILED("Failed to get video properties");
        ComPtr<IMFMediaType> videoType;
        hr = MFCreateMediaTypeFromProperties(videoProperties.Get(), &videoType);
        RETURN_VOID_IF_FAILED("Failed to create video type");
        m_stream = Make<MediaStream>(videoType.Get(), this, videoRenderer);
    }

    ~MediaSink() override = default;

    HRESULT RequestSample()
    {
        return m_stream->RequestSample();
    }

    HRESULT __stdcall SetProperties(Collections::IPropertySet *configuration) override
    {
        Q_UNUSED(configuration);
        return E_NOTIMPL;
    }

    HRESULT __stdcall GetCharacteristics(DWORD *characteristics) override
    {
        *characteristics = MEDIASINK_FIXED_STREAMS | MEDIASINK_RATELESS;
        return S_OK;
    }

    HRESULT __stdcall AddStreamSink(DWORD streamSinkIdentifier, IMFMediaType *mediaType, IMFStreamSink **streamSink) override
    {
        Q_UNUSED(streamSinkIdentifier);
        Q_UNUSED(mediaType);
        Q_UNUSED(streamSink);
        return E_NOTIMPL;
    }

    HRESULT __stdcall RemoveStreamSink(DWORD streamSinkIdentifier) override
    {
        Q_UNUSED(streamSinkIdentifier);
        return E_NOTIMPL;
    }

    HRESULT __stdcall GetStreamSinkCount(DWORD *streamSinkCount) override
    {
        *streamSinkCount = 1;
        return S_OK;
    }

    HRESULT __stdcall GetStreamSinkByIndex(DWORD index, IMFStreamSink **streamSink) override
    {
        if (index == 0)
            return m_stream.CopyTo(streamSink);
        return MF_E_INVALIDINDEX;
    }

    HRESULT __stdcall GetStreamSinkById(DWORD streamSinkIdentifier, IMFStreamSink **streamSink) override
    {
        // ID and index are always 0
        HRESULT hr = GetStreamSinkByIndex(streamSinkIdentifier, streamSink);
        return hr == MF_E_INVALIDINDEX ? MF_E_INVALIDSTREAMNUMBER : hr;
    }

    HRESULT __stdcall SetPresentationClock(IMFPresentationClock *presentationClock) override
    {
        HRESULT hr = S_OK;
        m_presentationClock = presentationClock;
        if (m_presentationClock)
            hr = m_presentationClock->AddClockStateSink(this);
        return hr;
    }

    HRESULT __stdcall GetPresentationClock(IMFPresentationClock **presentationClock) override
    {
        return m_presentationClock.CopyTo(presentationClock);
    }

    HRESULT __stdcall Shutdown() override
    {
        m_stream->Flush();
        scheduleSetActive(false);
        return m_presentationClock ? m_presentationClock->Stop() : S_OK;
    }

    HRESULT __stdcall OnClockStart(MFTIME systemTime, LONGLONG clockStartOffset) override
    {
        Q_UNUSED(systemTime);
        Q_UNUSED(clockStartOffset);

        scheduleSetActive(true);

        return S_OK;
    }

    HRESULT __stdcall OnClockStop(MFTIME systemTime) override
    {
        Q_UNUSED(systemTime);

        scheduleSetActive(false);

        return m_stream->QueueEvent(MEStreamSinkStopped, GUID_NULL, S_OK, nullptr);
    }

    HRESULT __stdcall OnClockPause(MFTIME systemTime) override
    {
        Q_UNUSED(systemTime);

        scheduleSetActive(false);

        return m_stream->QueueEvent(MEStreamSinkPaused, GUID_NULL, S_OK, nullptr);
    }

    HRESULT __stdcall OnClockRestart(MFTIME systemTime) override
    {
        Q_UNUSED(systemTime);

        scheduleSetActive(true);

        return m_stream->QueueEvent(MEStreamSinkStarted, GUID_NULL, S_OK, nullptr);
    }

    HRESULT __stdcall OnClockSetRate(MFTIME systemTime, float rate) override
    {
        Q_UNUSED(systemTime);
        Q_UNUSED(rate);
        return E_NOTIMPL;
    }

private:

    inline void scheduleSetActive(bool active)
    {
        QMetaObject::invokeMethod(m_videoRenderer, "setActive", Qt::QueuedConnection, Q_ARG(bool, active));
    }

    ComPtr<MediaStream> m_stream;
    ComPtr<IMFPresentationClock> m_presentationClock;

    QWinRTCameraVideoRendererControl *m_videoRenderer;
};

class QWinRTCameraControlPrivate
{
public:
    QCamera::State state;
    QCamera::Status status;
    QCamera::CaptureModes captureMode;

    ComPtr<IMediaCapture> capture;
    ComPtr<IMediaCaptureVideoPreview> capturePreview;
    EventRegistrationToken captureFailedCookie;
    EventRegistrationToken recordLimitationCookie;

    ComPtr<IMediaEncodingProfileStatics> encodingProfileFactory;

    ComPtr<IMediaEncodingProfile> encodingProfile;
    ComPtr<MediaSink> mediaSink;
    ComPtr<IFocusControl> focusControl;
    ComPtr<IRegionsOfInterestControl> regionsOfInterestControl;
    ComPtr<IAsyncAction> focusOperation;

    QPointer<QWinRTCameraVideoRendererControl> videoRenderer;
    QPointer<QWinRTVideoDeviceSelectorControl> videoDeviceSelector;
    QPointer<QWinRTCameraImageCaptureControl> imageCaptureControl;
    QPointer<QWinRTImageEncoderControl> imageEncoderControl;
    QPointer<QWinRTCameraFlashControl> cameraFlashControl;
    QPointer<QWinRTCameraFocusControl> cameraFocusControl;
    QPointer<QWinRTCameraLocksControl> cameraLocksControl;
    QAtomicInt framesMapped;
    QEventLoop *delayClose;

    bool initializing = false;
    bool initialized = false;
    HANDLE initializationCompleteEvent;
};

QWinRTCameraControl::QWinRTCameraControl(QObject *parent)
    : QCameraControl(parent), d_ptr(new QWinRTCameraControlPrivate)
{
    qCDebug(lcMMCamera) << __FUNCTION__ << parent;
    Q_D(QWinRTCameraControl);

    d->delayClose = nullptr;
    d->state = QCamera::UnloadedState;
    d->status = QCamera::UnloadedStatus;
    d->captureMode = QCamera::CaptureStillImage;
    d->captureFailedCookie.value = 0;
    d->recordLimitationCookie.value = 0;
    d->videoRenderer = new QWinRTCameraVideoRendererControl(QSize(), this);
    connect(d->videoRenderer, &QWinRTCameraVideoRendererControl::bufferRequested,
            this, &QWinRTCameraControl::onBufferRequested);
    d->videoDeviceSelector = new QWinRTVideoDeviceSelectorControl(this);
    connect(d->videoDeviceSelector, QOverload<int>::of(&QWinRTVideoDeviceSelectorControl::selectedDeviceChanged),
            d->videoRenderer, &QWinRTCameraVideoRendererControl::resetSampleFormat);
    d->imageCaptureControl = new QWinRTCameraImageCaptureControl(this);
    d->imageEncoderControl = new QWinRTImageEncoderControl(this);
    d->cameraFlashControl = new QWinRTCameraFlashControl(this);
    d->cameraFocusControl = new QWinRTCameraFocusControl(this);
    d->cameraLocksControl = new QWinRTCameraLocksControl(this);

    d->initializationCompleteEvent = CreateEvent(nullptr, false, false, nullptr);

    if (qGuiApp) {
        connect(qGuiApp, &QGuiApplication::applicationStateChanged,
                this, &QWinRTCameraControl::onApplicationStateChanged);
    }
}

QWinRTCameraControl::~QWinRTCameraControl()
{
    setState(QCamera::UnloadedState);
}

QCamera::State QWinRTCameraControl::state() const
{
    Q_D(const QWinRTCameraControl);
    return d->state;
}

void QWinRTCameraControl::setState(QCamera::State state)
{
    qCDebug(lcMMCamera) << __FUNCTION__ << state;
    Q_D(QWinRTCameraControl);

    if (d->state == state)
        return;

    HRESULT hr;
    switch (state) {
    case QCamera::ActiveState: {
        // Capture has not been created or initialized
        if (d->state == QCamera::UnloadedState) {
            if (!d->initialized) {
                if (!d->initializing) {
                    hr = initialize();
                    RETURN_VOID_AND_EMIT_ERROR("Failed to initialize media capture");
                }
                DWORD waitResult = WaitForSingleObjectEx(d->initializationCompleteEvent, 30000, FALSE);
                if (waitResult != WAIT_OBJECT_0) {
                    RETURN_VOID_AND_EMIT_ERROR("Failed to initialize camera control.");
                    return;
                }
            }

            d->state = QCamera::LoadedState;
            emit stateChanged(d->state);

            d->status = QCamera::LoadedStatus;
            emit statusChanged(d->status);
        }
        Q_ASSERT(d->state == QCamera::LoadedState);

        ComPtr<IAsyncAction> op;
        hr = QEventDispatcherWinRT::runOnXamlThread([d, &op]() {
            d->mediaSink = Make<MediaSink>(d->encodingProfile.Get(), d->videoRenderer);
            HRESULT hr = d->capturePreview->StartPreviewToCustomSinkAsync(d->encodingProfile.Get(), d->mediaSink.Get(), &op);
            return hr;
        });
        RETURN_VOID_AND_EMIT_ERROR("Failed to initiate capture.");
        if (d->status != QCamera::StartingStatus) {
            d->status = QCamera::StartingStatus;
            emit statusChanged(d->status);
        }

        hr = QEventDispatcherWinRT::runOnXamlThread([&op]() {
            return QWinRTFunctions::await(op);
        });
        if (FAILED(hr)) {
            emit error(QCamera::CameraError, qt_error_string(hr));
            setState(QCamera::UnloadedState); // Unload everything, as initialize() will need be called again
            return;
        }

        QCameraFocus::FocusModes focusMode = d->cameraFocusControl->focusMode();
        if (focusMode != 0 && setFocus(focusMode) && focusMode == QCameraFocus::ContinuousFocus)
            focus();

        d->state = QCamera::ActiveState;
        emit stateChanged(d->state);
        d->status = QCamera::ActiveStatus;
        emit statusChanged(d->status);
        QEventDispatcherWinRT::runOnXamlThread([d]() { d->mediaSink->RequestSample(); return S_OK;});
        break;
    }
    case QCamera::LoadedState: {
        // If moving from unloaded, initialize the camera
        if (d->state == QCamera::UnloadedState) {
            if (!d->initialized) {
                if (!d->initializing) {
                    hr = initialize();
                    RETURN_VOID_AND_EMIT_ERROR("Failed to initialize media capture");
                }
                DWORD waitResult = WaitForSingleObjectEx(d->initializationCompleteEvent, 30000, FALSE);
                if (waitResult != WAIT_OBJECT_0) {
                    RETURN_VOID_AND_EMIT_ERROR("Failed to initialize camera control.");
                    return;
                }
            }

            d->state = QCamera::LoadedState;
            emit stateChanged(d->state);

            d->status = QCamera::LoadedStatus;
            emit statusChanged(d->status);
        }
        // fall through
    }
    case QCamera::UnloadedState: {
        // Stop the camera if it is running (transition to LoadedState)
        if (d->status == QCamera::ActiveStatus) {
            HRESULT hr;
            if (d->focusOperation) {
                hr = QWinRTFunctions::await(d->focusOperation);
                Q_ASSERT_SUCCEEDED(hr);
            }
            if (d->framesMapped > 0) {
                qWarning("%d QVideoFrame(s) mapped when closing down camera. Camera will wait for unmap before closing down.",
                         d->framesMapped);
                if (!d->delayClose)
                    d->delayClose = new QEventLoop(this);
                d->delayClose->exec();
            }

            ComPtr<IAsyncAction> op;
            hr = QEventDispatcherWinRT::runOnXamlThread([d, &op]() {
                HRESULT hr = d->capturePreview->StopPreviewAsync(&op);
                return hr;
            });
            RETURN_VOID_AND_EMIT_ERROR("Failed to stop camera preview");
            if (d->status != QCamera::StoppingStatus) {
                d->status = QCamera::StoppingStatus;
                emit statusChanged(d->status);
            }
            Q_ASSERT_SUCCEEDED(hr);
            hr = QEventDispatcherWinRT::runOnXamlThread([&op]() {
                return QWinRTFunctions::await(op); // Synchronize unloading
            });
            if (FAILED(hr))
                emit error(QCamera::InvalidRequestError, qt_error_string(hr));

            if (d->mediaSink) {
                hr = QEventDispatcherWinRT::runOnXamlThread([d]() {
                d->mediaSink->Shutdown();
                d->mediaSink.Reset();
                return S_OK;
                });
            }

            d->state = QCamera::LoadedState;
            emit stateChanged(d->state);

            d->status = QCamera::LoadedStatus;
            emit statusChanged(d->status);
        }
        // Completely unload if needed
        if (state == QCamera::UnloadedState) {
            if (!d->capture) // Already unloaded
                break;

            if (d->status != QCamera::UnloadingStatus) {
                d->status = QCamera::UnloadingStatus;
                emit statusChanged(d->status);
            }

            hr = QEventDispatcherWinRT::runOnXamlThread([d]() {
                HRESULT hr;
                if (d->capture && d->captureFailedCookie.value) {
                    hr = d->capture->remove_Failed(d->captureFailedCookie);
                    Q_ASSERT_SUCCEEDED(hr);
                    d->captureFailedCookie.value = 0;
                }
                if (d->capture && d->recordLimitationCookie.value) {
                    d->capture->remove_RecordLimitationExceeded(d->recordLimitationCookie);
                    Q_ASSERT_SUCCEEDED(hr);
                    d->recordLimitationCookie.value = 0;
                }
                ComPtr<IClosable> capture;
                hr = d->capture.As(&capture);
                Q_ASSERT_SUCCEEDED(hr);
                hr = capture->Close();
                RETURN_HR_IF_FAILED("");
                d->capture.Reset();
                return hr;
            });
            RETURN_VOID_AND_EMIT_ERROR("Failed to close the capture manger");
            if (d->state != QCamera::UnloadedState) {
                d->state = QCamera::UnloadedState;
                emit stateChanged(d->state);
            }
            if (d->status != QCamera::UnloadedStatus) {
                d->status = QCamera::UnloadedStatus;
                emit statusChanged(d->status);
            }
            d->initialized = false;
        }
        break;
    }
    default:
        break;
    }
}

QCamera::Status QWinRTCameraControl::status() const
{
    Q_D(const QWinRTCameraControl);
    return d->status;
}

QCamera::CaptureModes QWinRTCameraControl::captureMode() const
{
    Q_D(const QWinRTCameraControl);
    return d->captureMode;
}

void QWinRTCameraControl::setCaptureMode(QCamera::CaptureModes mode)
{
    qCDebug(lcMMCamera) << __FUNCTION__ << mode;
    Q_D(QWinRTCameraControl);

    if (d->captureMode == mode)
        return;

    if (!isCaptureModeSupported(mode)) {
        qWarning("Unsupported capture mode: %d", mode);
        return;
    }

    d->captureMode = mode;
    emit captureModeChanged(d->captureMode);
}

bool QWinRTCameraControl::isCaptureModeSupported(QCamera::CaptureModes mode) const
{
    return mode >= QCamera::CaptureViewfinder && mode <= QCamera::CaptureStillImage;
}

bool QWinRTCameraControl::canChangeProperty(QCameraControl::PropertyChangeType changeType, QCamera::Status status) const
{
    Q_UNUSED(changeType);

    return status == QCamera::UnloadedStatus; // For now, assume shutdown is required for all property changes
}

QVideoRendererControl *QWinRTCameraControl::videoRenderer() const
{
    Q_D(const QWinRTCameraControl);
    return d->videoRenderer;
}

QVideoDeviceSelectorControl *QWinRTCameraControl::videoDeviceSelector() const
{
    Q_D(const QWinRTCameraControl);
    return d->videoDeviceSelector;
}

QCameraImageCaptureControl *QWinRTCameraControl::imageCaptureControl() const
{
    Q_D(const QWinRTCameraControl);
    return d->imageCaptureControl;
}

QImageEncoderControl *QWinRTCameraControl::imageEncoderControl() const
{
    Q_D(const QWinRTCameraControl);
    return d->imageEncoderControl;
}

QCameraFlashControl *QWinRTCameraControl::cameraFlashControl() const
{
    Q_D(const QWinRTCameraControl);
    return d->cameraFlashControl;
}

QCameraFocusControl *QWinRTCameraControl::cameraFocusControl() const
{
    Q_D(const QWinRTCameraControl);
    return d->cameraFocusControl;
}

QCameraLocksControl *QWinRTCameraControl::cameraLocksControl() const
{
    Q_D(const QWinRTCameraControl);
    return d->cameraLocksControl;
}

Microsoft::WRL::ComPtr<ABI::Windows::Media::Capture::IMediaCapture> QWinRTCameraControl::handle() const
{
    Q_D(const QWinRTCameraControl);
    return d->capture;
}

void QWinRTCameraControl::onBufferRequested()
{
    Q_D(QWinRTCameraControl);

    if (d->mediaSink)
        d->mediaSink->RequestSample();
}

void QWinRTCameraControl::onApplicationStateChanged(Qt::ApplicationState state)
{
    qCDebug(lcMMCamera) << __FUNCTION__ << state;
#ifdef _DEBUG
    return;
#else // !_DEBUG
    Q_D(QWinRTCameraControl);
    static QCamera::State savedState = d->state;
    switch (state) {
    case Qt::ApplicationInactive:
        if (d->state != QCamera::UnloadedState) {
            savedState = d->state;
            setState(QCamera::UnloadedState);
        }
        break;
    case Qt::ApplicationActive:
        setState(QCamera::State(savedState));
        break;
    default:
        break;
    }
#endif // _DEBUG
}

HRESULT QWinRTCameraControl::initialize()
{
    qCDebug(lcMMCamera) << __FUNCTION__;
    Q_D(QWinRTCameraControl);

    if (d->status != QCamera::LoadingStatus) {
        d->status = QCamera::LoadingStatus;
        emit statusChanged(d->status);
    }

    HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([this, d]() {
        HRESULT hr;
        ComPtr<IInspectable> capture;
        hr = RoActivateInstance(Wrappers::HString::MakeReference(RuntimeClass_Windows_Media_Capture_MediaCapture).Get(),
                                &capture);
        Q_ASSERT_SUCCEEDED(hr);
        hr = capture.As(&d->capture);
        Q_ASSERT_SUCCEEDED(hr);
        hr = d->capture.As(&d->capturePreview);
        Q_ASSERT_SUCCEEDED(hr);
        hr = d->capture->add_Failed(Callback<IMediaCaptureFailedEventHandler>(this, &QWinRTCameraControl::onCaptureFailed).Get(),
                                    &d->captureFailedCookie);
        Q_ASSERT_SUCCEEDED(hr);
        hr = d->capture->add_RecordLimitationExceeded(Callback<IRecordLimitationExceededEventHandler>(this, &QWinRTCameraControl::onRecordLimitationExceeded).Get(),
                                                      &d->recordLimitationCookie);
        Q_ASSERT_SUCCEEDED(hr);
        hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Media_MediaProperties_MediaEncodingProfile).Get(),
                                    IID_PPV_ARGS(&d->encodingProfileFactory));
        Q_ASSERT_SUCCEEDED(hr);

        int deviceIndex = d->videoDeviceSelector->selectedDevice();
        if (deviceIndex < 0)
            deviceIndex = d->videoDeviceSelector->defaultDevice();

        const QString deviceName = d->videoDeviceSelector->deviceName(deviceIndex);
        if (deviceName.isEmpty()) {
            qWarning("No video device available or selected.");
            return E_FAIL;
        }

        d->videoRenderer->setScanLineDirection(QVideoSurfaceFormat::TopToBottom);
        ComPtr<IMediaCaptureInitializationSettings> settings;
        hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Media_Capture_MediaCaptureInitializationSettings).Get(),
                                &settings);
        Q_ASSERT_SUCCEEDED(hr);
        HStringReference deviceId(reinterpret_cast<LPCWSTR>(deviceName.utf16()), uint(deviceName.length()));
        hr = settings->put_VideoDeviceId(deviceId.Get());
        Q_ASSERT_SUCCEEDED(hr);

        hr = settings->put_StreamingCaptureMode(StreamingCaptureMode_Video);
        Q_ASSERT_SUCCEEDED(hr);

        hr = settings->put_PhotoCaptureSource(PhotoCaptureSource_Auto);
        Q_ASSERT_SUCCEEDED(hr);

        ComPtr<IAsyncAction> op;
        hr = d->capture->InitializeWithSettingsAsync(settings.Get(), &op);
        RETURN_HR_IF_FAILED("Failed to begin initialization of media capture manager");
        hr = op.Get()->put_Completed(Callback<IAsyncActionCompletedHandler>(
                                         this, &QWinRTCameraControl::onInitializationCompleted).Get());
        RETURN_HR_IF_FAILED("Failed to register initialization callback");
        return S_OK;
    });
    return hr;
}

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)

HRESULT QWinRTCameraControl::initializeFocus()
{
    Q_D(QWinRTCameraControl);
    ComPtr<IFocusControl2> focusControl2;
    HRESULT hr = d->focusControl.As(&focusControl2);
    Q_ASSERT_SUCCEEDED(hr);
    ComPtr<IVectorView<enum FocusMode>> focusModes;
    hr = focusControl2->get_SupportedFocusModes(&focusModes);
    if (FAILED(hr)) {
        d->cameraFocusControl->setSupportedFocusMode(nullptr);
        d->cameraFocusControl->setSupportedFocusPointMode(QSet<QCameraFocus::FocusPointMode>());
        qErrnoWarning(hr, "Failed to get camera supported focus mode list");
        return hr;
    }
    quint32 size;
    hr = focusModes->get_Size(&size);
    Q_ASSERT_SUCCEEDED(hr);
    QCameraFocus::FocusModes supportedModeFlag = nullptr;
    for (quint32 i = 0; i < size; ++i) {
        FocusMode mode;
        hr = focusModes->GetAt(i, &mode);
        Q_ASSERT_SUCCEEDED(hr);
        switch (mode) {
        case FocusMode_Continuous:
            supportedModeFlag |= QCameraFocus::ContinuousFocus;
            break;
        case FocusMode_Single:
            supportedModeFlag |= QCameraFocus::AutoFocus;
            break;
        default:
            break;
        }
    }

    ComPtr<IVectorView<enum AutoFocusRange>> focusRange;
    hr = focusControl2->get_SupportedFocusRanges(&focusRange);
    if (FAILED(hr)) {
        qErrnoWarning(hr, "Failed to get camera supported focus range list");
    } else {
        hr = focusRange->get_Size(&size);
        Q_ASSERT_SUCCEEDED(hr);
        for (quint32 i = 0; i < size; ++i) {
            AutoFocusRange range;
            hr = focusRange->GetAt(i, &range);
            Q_ASSERT_SUCCEEDED(hr);
            switch (range) {
            case AutoFocusRange_Macro:
                supportedModeFlag |= QCameraFocus::MacroFocus;
                break;
            case AutoFocusRange_FullRange:
                supportedModeFlag |= QCameraFocus::InfinityFocus;
                break;
            default:
                break;
            }
        }
    }
    d->cameraFocusControl->setSupportedFocusMode(supportedModeFlag);
    if (!d->regionsOfInterestControl) {
        d->cameraFocusControl->setSupportedFocusPointMode(QSet<QCameraFocus::FocusPointMode>());
        return S_OK;
    }
    boolean isRegionsfocusSupported = false;
    hr = d->regionsOfInterestControl->get_AutoFocusSupported(&isRegionsfocusSupported);
    Q_ASSERT_SUCCEEDED(hr);
    UINT32 maxRegions;
    hr = d->regionsOfInterestControl->get_MaxRegions(&maxRegions);
    Q_ASSERT_SUCCEEDED(hr);
    if (!isRegionsfocusSupported || maxRegions == 0) {
        d->cameraFocusControl->setSupportedFocusPointMode(QSet<QCameraFocus::FocusPointMode>());
        return S_OK;
    }
    QSet<QCameraFocus::FocusPointMode> supportedFocusPointModes;
    supportedFocusPointModes << QCameraFocus::FocusPointCustom
                             << QCameraFocus::FocusPointCenter
                             << QCameraFocus::FocusPointAuto;
    d->cameraFocusControl->setSupportedFocusPointMode(supportedFocusPointModes);
    return S_OK;
}

bool QWinRTCameraControl::setFocus(QCameraFocus::FocusModes modes)
{
    Q_D(QWinRTCameraControl);
    if (d->status == QCamera::UnloadedStatus)
        return false;

    bool result = false;
    HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([modes, &result, d, this]() {
        ComPtr<IFocusSettings> focusSettings;
        ComPtr<IInspectable> focusSettingsObject;
        HRESULT hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Media_Devices_FocusSettings).Get(), &focusSettingsObject);
        Q_ASSERT_SUCCEEDED(hr);
        hr = focusSettingsObject.As(&focusSettings);
        Q_ASSERT_SUCCEEDED(hr);
        FocusMode mode;
        if (modes.testFlag(QCameraFocus::ContinuousFocus)) {
            mode = FocusMode_Continuous;
        } else if (modes.testFlag(QCameraFocus::AutoFocus)
                   || modes.testFlag(QCameraFocus::MacroFocus)
                   || modes.testFlag(QCameraFocus::InfinityFocus)) {
            // The Macro and infinity focus modes are only supported in auto focus mode on WinRT.
            // QML camera focus doesn't support combined focus flags settings. In the case of macro
            // and infinity Focus modes, the auto focus setting is applied.
            mode = FocusMode_Single;
        } else {
            emit error(QCamera::NotSupportedFeatureError, QStringLiteral("Unsupported camera focus modes."));
            result = false;
            return S_OK;
        }
        hr = focusSettings->put_Mode(mode);
        Q_ASSERT_SUCCEEDED(hr);
        AutoFocusRange range = AutoFocusRange_Normal;
        if (modes.testFlag(QCameraFocus::MacroFocus))
            range = AutoFocusRange_Macro;
        else if (modes.testFlag(QCameraFocus::InfinityFocus))
            range = AutoFocusRange_FullRange;
        hr = focusSettings->put_AutoFocusRange(range);
        Q_ASSERT_SUCCEEDED(hr);
        hr = focusSettings->put_WaitForFocus(true);
        Q_ASSERT_SUCCEEDED(hr);
        hr = focusSettings->put_DisableDriverFallback(false);
        Q_ASSERT_SUCCEEDED(hr);

        ComPtr<IFocusControl2> focusControl2;
        hr = d->focusControl.As(&focusControl2);
        Q_ASSERT_SUCCEEDED(hr);
        hr = focusControl2->Configure(focusSettings.Get());
        result = SUCCEEDED(hr);
        RETURN_OK_IF_FAILED("Failed to configure camera focus control");
        return S_OK;
    });
    Q_ASSERT_SUCCEEDED(hr);
    Q_UNUSED(hr); // Silence release build
    return result;
}

bool QWinRTCameraControl::setFocusPoint(const QPointF &focusPoint)
{
    Q_D(QWinRTCameraControl);
    if (focusPoint.x() < double(FOCUS_RECT_POSITION_MIN)
            || focusPoint.x() > double(FOCUS_RECT_BOUNDARY)) {
        emit error(QCamera::CameraError, QStringLiteral("Focus horizontal location should be between 0.0 and 1.0."));
        return false;
    }

    if (focusPoint.y() < double(FOCUS_RECT_POSITION_MIN)
            || focusPoint.y() > double(FOCUS_RECT_BOUNDARY)) {
        emit error(QCamera::CameraError, QStringLiteral("Focus vertical location should be between 0.0 and 1.0."));
        return false;
    }

    ABI::Windows::Foundation::Rect rect;
    rect.X = qBound<float>(FOCUS_RECT_POSITION_MIN, float(focusPoint.x() - double(FOCUS_RECT_HALF_SIZE)), FOCUS_RECT_POSITION_MAX);
    rect.Y = qBound<float>(FOCUS_RECT_POSITION_MIN, float(focusPoint.y() - double(FOCUS_RECT_HALF_SIZE)), FOCUS_RECT_POSITION_MAX);
    rect.Width  = (rect.X + FOCUS_RECT_SIZE) < FOCUS_RECT_BOUNDARY ? FOCUS_RECT_SIZE : FOCUS_RECT_BOUNDARY - rect.X;
    rect.Height = (rect.Y + FOCUS_RECT_SIZE) < FOCUS_RECT_BOUNDARY ? FOCUS_RECT_SIZE : FOCUS_RECT_BOUNDARY - rect.Y;

    ComPtr<IRegionOfInterest> regionOfInterest;
    ComPtr<IInspectable> regionOfInterestObject;
    HRESULT hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Media_Devices_RegionOfInterest).Get(), &regionOfInterestObject);
    Q_ASSERT_SUCCEEDED(hr);
    hr = regionOfInterestObject.As(&regionOfInterest);
    Q_ASSERT_SUCCEEDED(hr);
    ComPtr<IRegionOfInterest2> regionOfInterest2;
    hr = regionOfInterestObject.As(&regionOfInterest2);
    Q_ASSERT_SUCCEEDED(hr);
    hr = regionOfInterest2->put_BoundsNormalized(true);
    Q_ASSERT_SUCCEEDED(hr);
    hr = regionOfInterest2->put_Weight(1);
    Q_ASSERT_SUCCEEDED(hr);
    hr = regionOfInterest2->put_Type(RegionOfInterestType_Unknown);
    Q_ASSERT_SUCCEEDED(hr);
    hr = regionOfInterest->put_AutoFocusEnabled(true);
    Q_ASSERT_SUCCEEDED(hr);
    hr = regionOfInterest->put_Bounds(rect);
    Q_ASSERT_SUCCEEDED(hr);

    ComPtr<WindowsRegionOfInterestIterable> regionOfInterestIterable = Make<WindowsRegionOfInterestIterable>(regionOfInterest);
    ComPtr<IAsyncAction> op;
    hr = d->regionsOfInterestControl->SetRegionsAsync(regionOfInterestIterable.Get(), &op);
    Q_ASSERT_SUCCEEDED(hr);
    return QWinRTFunctions::await(op) == S_OK;
}

bool QWinRTCameraControl::focus()
{
    Q_D(QWinRTCameraControl);
    HRESULT hr;
    if (!d->focusControl)
        return false;

    QEventDispatcherWinRT::runOnXamlThread([&d, &hr]() {
        if (d->focusOperation) {
            ComPtr<IAsyncInfo> info;
            hr = d->focusOperation.As(&info);
            Q_ASSERT_SUCCEEDED(hr);

            AsyncStatus status = AsyncStatus::Completed;
            hr = info->get_Status(&status);
            Q_ASSERT_SUCCEEDED(hr);
            if (status == AsyncStatus::Started)
                return E_ASYNC_OPERATION_NOT_STARTED;
        }

        hr = d->focusControl->FocusAsync(&d->focusOperation);
        Q_ASSERT_SUCCEEDED(hr);

        const long errorCode = HRESULT_CODE(hr);
        if (errorCode == ERROR_OPERATION_IN_PROGRESS
                || errorCode == ERROR_WRITE_PROTECT) {
            return E_ASYNC_OPERATION_NOT_STARTED;
        }
        Q_ASSERT_SUCCEEDED(hr);
        return S_OK;
    });

    return hr == S_OK;
}

void QWinRTCameraControl::clearFocusPoint()
{
    Q_D(QWinRTCameraControl);
    if (!d->focusControl)
        return;
    ComPtr<IAsyncAction> op;
    HRESULT hr = d->regionsOfInterestControl->ClearRegionsAsync(&op);
    Q_ASSERT_SUCCEEDED(hr);
    hr = QWinRTFunctions::await(op);
    Q_ASSERT_SUCCEEDED(hr);
}

bool QWinRTCameraControl::lockFocus()
{
    Q_D(QWinRTCameraControl);
    if (!d->focusControl)
        return false;

    bool result = false;
    ComPtr<IAsyncAction> op;
    HRESULT hr;
    hr = QEventDispatcherWinRT::runOnXamlThread([d, &result, &op]() {
        ComPtr<IFocusControl2> focusControl2;
        HRESULT hr = d->focusControl.As(&focusControl2);
        Q_ASSERT_SUCCEEDED(hr);
        hr = focusControl2->LockAsync(&op);
        if (HRESULT_CODE(hr) == ERROR_WRITE_PROTECT)
            return S_OK;
        Q_ASSERT_SUCCEEDED(hr);
        result = true;
        return hr;
    });
    return result ? (QWinRTFunctions::await(op) == S_OK) : false;
}

bool QWinRTCameraControl::unlockFocus()
{
    Q_D(QWinRTCameraControl);
    if (!d->focusControl)
        return false;

    bool result = false;
    ComPtr<IAsyncAction> op;
    HRESULT hr;
    hr = QEventDispatcherWinRT::runOnXamlThread([d, &result, &op]() {
        ComPtr<IFocusControl2> focusControl2;
        HRESULT hr = d->focusControl.As(&focusControl2);
        Q_ASSERT_SUCCEEDED(hr);
        hr = focusControl2->UnlockAsync(&op);
        if (HRESULT_CODE(hr) == ERROR_WRITE_PROTECT)
            return S_OK;
        Q_ASSERT_SUCCEEDED(hr);
        result = true;
        return hr;
    });
    return result ? (QWinRTFunctions::await(op) == S_OK) : false;
}

#else // !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)

HRESULT QWinRTCameraControl::initializeFocus()
{
    Q_D(QWinRTCameraControl);
    d->cameraFocusControl->setSupportedFocusMode(0);
    d->cameraFocusControl->setSupportedFocusPointMode(QSet<QCameraFocus::FocusPointMode>());
    return S_OK;
}

bool QWinRTCameraControl::setFocus(QCameraFocus::FocusModes modes)
{
    Q_UNUSED(modes)
    return false;
}

bool QWinRTCameraControl::setFocusPoint(const QPointF &focusPoint)
{
    Q_UNUSED(focusPoint)
    return false;
}

bool QWinRTCameraControl::focus()
{
    return false;
}

void QWinRTCameraControl::clearFocusPoint()
{
}

bool QWinRTCameraControl::lockFocus()
{
    return false;
}

bool QWinRTCameraControl::unlockFocus()
{
    return false;
}

#endif // !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)

void QWinRTCameraControl::frameMapped()
{
    Q_D(QWinRTCameraControl);
    ++d->framesMapped;
}

void QWinRTCameraControl::frameUnmapped()
{
    Q_D(QWinRTCameraControl);
    --d->framesMapped;
    Q_ASSERT(d->framesMapped >= 0);
    if (!d->framesMapped && d->delayClose && d->delayClose->isRunning())
        d->delayClose->exit();
}

HRESULT QWinRTCameraControl::onCaptureFailed(IMediaCapture *, IMediaCaptureFailedEventArgs *args)
{
    qCDebug(lcMMCamera) << __FUNCTION__ << args;
    HRESULT hr;
    UINT32 code;
    hr = args->get_Code(&code);
    RETURN_HR_IF_FAILED("Failed to get error code");
    HString message;
    args->get_Message(message.GetAddressOf());
    RETURN_HR_IF_FAILED("Failed to get error message");
    quint32 messageLength;
    const wchar_t *messageBuffer = message.GetRawBuffer(&messageLength);
    emit error(QCamera::CameraError, QString::fromWCharArray(messageBuffer, int(messageLength)));
    setState(QCamera::LoadedState);
    return S_OK;
}

HRESULT QWinRTCameraControl::onRecordLimitationExceeded(IMediaCapture *)
{
    qCDebug(lcMMCamera) << __FUNCTION__;
    emit error(QCamera::CameraError, QStringLiteral("Recording limit exceeded."));
    setState(QCamera::LoadedState);
    return S_OK;
}

HRESULT QWinRTCameraControl::onInitializationCompleted(IAsyncAction *, AsyncStatus status)
{
    qCDebug(lcMMCamera) << __FUNCTION__;
    Q_D(QWinRTCameraControl);

    if (status != Completed) {
        d->initializing = false;
        d->initialized = false;
        return S_OK;
    }

    ComPtr<IVideoDeviceController> videoDeviceController;
    HRESULT hr = d->capture->get_VideoDeviceController(&videoDeviceController);
    ComPtr<IAdvancedVideoCaptureDeviceController2> advancedVideoDeviceController;
    hr = videoDeviceController.As(&advancedVideoDeviceController);
    Q_ASSERT_SUCCEEDED(hr);
    hr = advancedVideoDeviceController->get_FocusControl(&d->focusControl);
    Q_ASSERT_SUCCEEDED(hr);

    d->cameraFlashControl->initialize(advancedVideoDeviceController);

    boolean isFocusSupported;
    hr = d->focusControl->get_Supported(&isFocusSupported);
    Q_ASSERT_SUCCEEDED(hr);
    if (isFocusSupported) {
        hr = advancedVideoDeviceController->get_RegionsOfInterestControl(&d->regionsOfInterestControl);
        if (FAILED(hr))
            qCDebug(lcMMCamera) << "Focus supported, but no control for regions of interest available";
        hr = initializeFocus();
        Q_ASSERT_SUCCEEDED(hr);
    }

    Q_ASSERT_SUCCEEDED(hr);
    ComPtr<IMediaDeviceController> deviceController;
    hr = videoDeviceController.As(&deviceController);
    Q_ASSERT_SUCCEEDED(hr);

    // Get preview stream properties.
    ComPtr<IVectorView<IMediaEncodingProperties *>> previewPropertiesList;
    QVector<QSize> previewResolutions;
    hr = getMediaStreamResolutions(deviceController.Get(),
                                   MediaStreamType_VideoPreview,
                                   &previewPropertiesList,
                                   &previewResolutions);
    RETURN_HR_IF_FAILED("Failed to find a suitable video format");

    MediaStreamType mediaStreamType =
            d->captureMode == QCamera::CaptureVideo ? MediaStreamType_VideoRecord : MediaStreamType_Photo;

    // Get capture stream properties.
    ComPtr<IVectorView<IMediaEncodingProperties *>> capturePropertiesList;
    QVector<QSize> captureResolutions;
    hr = getMediaStreamResolutions(deviceController.Get(),
                                   mediaStreamType,
                                   &capturePropertiesList,
                                   &captureResolutions);
    RETURN_HR_IF_FAILED("Failed to find a suitable video format");

    std::sort(captureResolutions.begin(), captureResolutions.end(), [](QSize size1, QSize size2) {
        return size1.width() * size1.height() < size2.width() * size2.height();
    });

    // Set capture resolutions.
    d->imageEncoderControl->setSupportedResolutionsList(captureResolutions.toList());
    const QSize captureResolution = d->imageEncoderControl->imageSettings().resolution();
    const quint32 captureResolutionIndex = quint32(captureResolutions.indexOf(captureResolution));
    ComPtr<IMediaEncodingProperties> captureProperties;
    hr = capturePropertiesList->GetAt(captureResolutionIndex, &captureProperties);
    Q_ASSERT_SUCCEEDED(hr);
    ComPtr<IAsyncAction> op;
    hr = deviceController->SetMediaStreamPropertiesAsync(mediaStreamType, captureProperties.Get(), &op);
    Q_ASSERT_SUCCEEDED(hr);
    hr = QWinRTFunctions::await(op);
    Q_ASSERT_SUCCEEDED(hr);

    // Set preview resolution.
    QSize maxSize;
    const float captureAspectRatio = float(captureResolution.width()) / captureResolution.height();
    for (const QSize &resolution : qAsConst(previewResolutions)) {
        const float aspectRatio = float(resolution.width()) / resolution.height();
        if ((qAbs(aspectRatio - captureAspectRatio) <= ASPECTRATIO_EPSILON)
                && (maxSize.width() * maxSize.height() < resolution.width() * resolution.height())) {
            maxSize = resolution;
        }
    }

    const QSize &viewfinderResolution = maxSize;
    const quint32 viewfinderResolutionIndex = quint32(previewResolutions.indexOf(viewfinderResolution));
    hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Media_MediaProperties_MediaEncodingProfile).Get(),
                            &d->encodingProfile);
    Q_ASSERT_SUCCEEDED(hr);
    ComPtr<IMediaEncodingProperties> previewProperties;
    hr = previewPropertiesList->GetAt(viewfinderResolutionIndex, &previewProperties);
    Q_ASSERT_SUCCEEDED(hr);
    hr = deviceController->SetMediaStreamPropertiesAsync(MediaStreamType_VideoPreview, previewProperties.Get(), &op);
    Q_ASSERT_SUCCEEDED(hr);
    hr = QWinRTFunctions::await(op);
    Q_ASSERT_SUCCEEDED(hr);
    ComPtr<IVideoEncodingProperties> videoPreviewProperties;
    hr = previewProperties.As(&videoPreviewProperties);
    Q_ASSERT_SUCCEEDED(hr);
    hr = d->encodingProfile->put_Video(videoPreviewProperties.Get());
    Q_ASSERT_SUCCEEDED(hr);

    if (d->videoRenderer)
        d->videoRenderer->setSize(viewfinderResolution);

    if (!isFocusSupported) {
        d->cameraFocusControl->setSupportedFocusMode(0);
        d->cameraFocusControl->setSupportedFocusPointMode(QSet<QCameraFocus::FocusPointMode>());
    }
    d->cameraLocksControl->initialize();

    d->initializing = false;
    d->initialized = true;
    SetEvent(d->initializationCompleteEvent);
    return S_OK;
}

void QWinRTCameraControl::emitError(int errorCode, const QString &errorString)
{
    qCDebug(lcMMCamera) << __FUNCTION__ << errorString << errorCode;
    emit error(errorCode, errorString);
}

QT_END_NAMESPACE