[MediaStream] Add Mac screen capture source
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / ScreenDisplayCaptureSourceMac.h
1 /*
2  * Copyright (C) 2017 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 <CoreGraphics/CGDisplayConfiguration.h>
32 #include <CoreGraphics/CGDisplayStream.h>
33 #include <wtf/Lock.h>
34 #include <wtf/OSObjectPtr.h>
35 #include <wtf/WeakPtr.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(const String&, const MediaConstraints*);
45
46     WEBCORE_EXPORT static VideoCaptureFactory& factory();
47
48     WEBCORE_EXPORT static std::optional<CGDirectDisplayID> updateDisplayID(CGDirectDisplayID);
49
50 private:
51     ScreenDisplayCaptureSourceMac(uint32_t);
52     virtual ~ScreenDisplayCaptureSourceMac();
53
54     static void displayReconfigurationCallBack(CGDirectDisplayID, CGDisplayChangeSummaryFlags, void*);
55
56     void displayWasReconfigured(CGDirectDisplayID, CGDisplayChangeSummaryFlags);
57
58     void frameAvailable(CGDisplayStreamFrameStatus, uint64_t, IOSurfaceRef, CGDisplayStreamUpdateRef);
59
60     void generateFrame() final;
61     void startProducingData() final;
62     void stopProducingData() final;
63     bool applySize(const IntSize&) final;
64     bool applyFrameRate(double) final;
65     void commitConfiguration() final;
66
67     RetainPtr<CMSampleBufferRef> sampleBufferFromPixelBuffer(CVPixelBufferRef);
68     RetainPtr<CVPixelBufferRef> pixelBufferFromIOSurface(IOSurfaceRef);
69     bool createDisplayStream();
70     void startDisplayStream();
71     
72     class DisplaySurface {
73     public:
74         DisplaySurface() = default;
75         ~DisplaySurface()
76         {
77             if (m_surface)
78                 IOSurfaceDecrementUseCount(m_surface.get());
79         }
80
81         DisplaySurface& operator=(IOSurfaceRef surface)
82         {
83             if (m_surface)
84                 IOSurfaceDecrementUseCount(m_surface.get());
85             if (surface)
86                 IOSurfaceIncrementUseCount(surface);
87             m_surface = surface;
88             return *this;
89         }
90
91         IOSurfaceRef ioSurface() const { return m_surface.get(); }
92
93     private:
94         RetainPtr<IOSurfaceRef> m_surface;
95     };
96
97     mutable Lock m_currentFrameMutex;
98     DisplaySurface m_currentFrame;
99     RetainPtr<CGDisplayStreamRef> m_displayStream;
100     RetainPtr<CFMutableDictionaryRef> m_bufferAttributes;
101     CGDisplayStreamFrameAvailableHandler m_frameAvailableBlock;
102     WeakPtrFactory<ScreenDisplayCaptureSourceMac> m_weakFactory;
103     MediaTime m_presentationTimeStamp;
104     MediaTime m_frameDuration;
105
106     OSObjectPtr<dispatch_queue_t> m_captureQueue;
107
108     double m_lastFrameTime { NAN };
109     uint32_t m_displayID { 0 };
110     bool m_isRunning { false };
111     bool m_observingDisplayChanges { false };
112 };
113
114 } // namespace WebCore
115
116 #endif // ENABLE(MEDIA_STREAM) && PLATFORM(MAC)