[MediaSource] Push capabilities across process boundary during UIProcess capture.
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / AVMediaCaptureSource.h
1 /*
2  * Copyright (C) 2013-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 #ifndef AVMediaCaptureSource_h
27 #define AVMediaCaptureSource_h
28
29 #if ENABLE(MEDIA_STREAM) && USE(AVFOUNDATION)
30
31 #include "GenericTaskQueue.h"
32 #include "RealtimeMediaSource.h"
33 #include "Timer.h"
34 #include <wtf/Function.h>
35
36 OBJC_CLASS AVCaptureAudioDataOutput;
37 OBJC_CLASS AVCaptureConnection;
38 OBJC_CLASS AVCaptureDevice;
39 OBJC_CLASS AVCaptureOutput;
40 OBJC_CLASS AVCaptureSession;
41 OBJC_CLASS AVCaptureVideoDataOutput;
42 OBJC_CLASS NSError;
43 OBJC_CLASS NSNotification;
44 OBJC_CLASS WebCoreAVMediaCaptureSourceObserver;
45
46 typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
47
48 namespace WebCore {
49
50 class AVMediaCaptureSource;
51
52 class AVMediaCaptureSource : public RealtimeMediaSource {
53 public:
54     virtual ~AVMediaCaptureSource();
55
56     virtual void captureOutputDidOutputSampleBufferFromConnection(AVCaptureOutput*, CMSampleBufferRef, AVCaptureConnection*) = 0;
57
58     void captureSessionIsRunningDidChange(bool);
59     void captureSessionRuntimeError(RetainPtr<NSError>);
60
61     enum class InterruptionReason { None, VideoNotAllowedInBackground, AudioInUse, VideoInUse, VideoNotAllowedInSideBySide };
62     void captureSessionBeginInterruption(RetainPtr<NSNotification>);
63     void captureSessionEndInterruption(RetainPtr<NSNotification>);
64
65     AVCaptureSession *session() const { return m_session.get(); }
66
67     const RealtimeMediaSourceSettings& settings() const final;
68
69     void startProducingData() final;
70     void stopProducingData() final;
71     bool isProducingData() const final;
72
73 protected:
74     AVMediaCaptureSource(AVCaptureDevice*, const AtomicString&, RealtimeMediaSource::Type);
75
76     AudioSourceProvider* audioSourceProvider() override;
77
78     virtual void setupCaptureSession() = 0;
79     virtual void shutdownCaptureSession() = 0;
80     virtual void updateSettings(RealtimeMediaSourceSettings&) = 0;
81     virtual void initializeCapabilities(RealtimeMediaSourceCapabilities&) = 0;
82     virtual void initializeSupportedConstraints(RealtimeMediaSourceSupportedConstraints&) = 0;
83
84     AVCaptureDevice *device() const { return m_device.get(); }
85
86     RealtimeMediaSourceSupportedConstraints& supportedConstraints();
87     const RealtimeMediaSourceCapabilities& capabilities() const final;
88
89     void setVideoSampleBufferDelegate(AVCaptureVideoDataOutput*);
90     void setAudioSampleBufferDelegate(AVCaptureAudioDataOutput*);
91
92 private:
93     void setupSession();
94     void reset() final;
95
96     void beginConfiguration() final;
97     void commitConfiguration() final;
98
99     bool isCaptureSource() const final { return true; }
100
101     void initializeSettings();
102     void initializeCapabilities();
103
104     RealtimeMediaSourceSettings m_currentSettings;
105     RealtimeMediaSourceSupportedConstraints m_supportedConstraints;
106     RetainPtr<WebCoreAVMediaCaptureSourceObserver> m_objcObserver;
107     std::unique_ptr<RealtimeMediaSourceCapabilities> m_capabilities;
108     RetainPtr<AVCaptureSession> m_session;
109     RetainPtr<AVCaptureDevice> m_device;
110     InterruptionReason m_interruption { InterruptionReason::None };
111     bool m_isRunning { false };
112 };
113
114 } // namespace WebCore
115
116 #endif // ENABLE(MEDIA_STREAM)
117
118 #endif // AVMediaCaptureSource_h