04b8d8ca832989120084995891e419b8460456dc
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / ScreenDisplayCaptureSourceMac.h
1 /*
2  * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if ENABLE(MEDIA_STREAM) && PLATFORM(MAC)
29
30 #include "DisplayCaptureSourceCocoa.h"
31 #include "IOSurface.h"
32 #include <CoreGraphics/CGDisplayConfiguration.h>
33 #include <CoreGraphics/CGDisplayStream.h>
34 #include <wtf/Lock.h>
35 #include <wtf/OSObjectPtr.h>
36
37 typedef struct __CVBuffer *CVPixelBufferRef;
38 typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
39
40 namespace WebCore {
41
42 class ScreenDisplayCaptureSourceMac : public DisplayCaptureSourceCocoa {
43 public:
44     static CaptureSourceOrError create(String&&, const MediaConstraints*);
45
46     static Optional<CaptureDevice> screenCaptureDeviceWithPersistentID(const String&);
47     static void screenCaptureDevices(Vector<CaptureDevice>&);
48
49 private:
50     ScreenDisplayCaptureSourceMac(uint32_t, String&&);
51     virtual ~ScreenDisplayCaptureSourceMac();
52
53     static void displayReconfigurationCallBack(CGDirectDisplayID, CGDisplayChangeSummaryFlags, void*);
54
55     void displayWasReconfigured(CGDirectDisplayID, CGDisplayChangeSummaryFlags);
56
57     void frameAvailable(CGDisplayStreamFrameStatus, uint64_t, IOSurfaceRef, CGDisplayStreamUpdateRef);
58
59     DisplayCaptureSourceCocoa::DisplayFrameType generateFrame() final;
60     RealtimeMediaSourceSettings::DisplaySurfaceType surfaceType() const final { return RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor; }
61
62     void startProducingData() final;
63     void stopProducingData() final;
64     void commitConfiguration() final;
65     CaptureDevice::DeviceType deviceType() const final { return CaptureDevice::DeviceType::Screen; }
66
67     bool createDisplayStream();
68     void startDisplayStream();
69     
70     class DisplaySurface {
71     public:
72         DisplaySurface() = default;
73         ~DisplaySurface()
74         {
75             if (m_surface)
76                 IOSurfaceDecrementUseCount(m_surface.get());
77         }
78
79         DisplaySurface& operator=(IOSurfaceRef surface)
80         {
81             if (m_surface)
82                 IOSurfaceDecrementUseCount(m_surface.get());
83             if (surface)
84                 IOSurfaceIncrementUseCount(surface);
85             m_surface = surface;
86             return *this;
87         }
88
89         IOSurfaceRef ioSurface() const { return m_surface.get(); }
90
91     private:
92         RetainPtr<IOSurfaceRef> m_surface;
93     };
94
95     mutable Lock m_currentFrameMutex;
96     DisplaySurface m_currentFrame;
97     RetainPtr<CGDisplayStreamRef> m_displayStream;
98     OSObjectPtr<dispatch_queue_t> m_captureQueue;
99
100     uint32_t m_displayID { 0 };
101     bool m_isRunning { false };
102     bool m_observingDisplayChanges { false };
103 };
104
105 } // namespace WebCore
106
107 #endif // ENABLE(MEDIA_STREAM) && PLATFORM(MAC)