| 1 | /* |
| 2 | * Copyright (C) 2018-2019 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) |
| 29 | |
| 30 | #include "ImageBuffer.h" |
| 31 | #include "MediaSample.h" |
| 32 | #include "RealtimeMediaSource.h" |
| 33 | #include "VideoPreset.h" |
| 34 | #include <wtf/Lock.h> |
| 35 | #include <wtf/RunLoop.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | class ImageTransferSessionVT; |
| 40 | |
| 41 | class RealtimeVideoSource : public RealtimeMediaSource { |
| 42 | public: |
| 43 | virtual ~RealtimeVideoSource(); |
| 44 | |
| 45 | protected: |
| 46 | RealtimeVideoSource(String&& name, String&& id, String&& hashSalt); |
| 47 | |
| 48 | void prepareToProduceData(); |
| 49 | bool supportsSizeAndFrameRate(Optional<int> width, Optional<int> height, Optional<double>) override; |
| 50 | void setSizeAndFrameRate(Optional<int> width, Optional<int> height, Optional<double>) override; |
| 51 | |
| 52 | virtual void generatePresets() = 0; |
| 53 | virtual bool prefersPreset(VideoPreset&) { return true; } |
| 54 | virtual void setFrameRateWithPreset(double, RefPtr<VideoPreset>) { }; |
| 55 | virtual bool canResizeVideoFrames() const { return false; } |
| 56 | bool shouldUsePreset(VideoPreset& current, VideoPreset& candidate); |
| 57 | |
| 58 | void setSupportedPresets(const Vector<Ref<VideoPreset>>&); |
| 59 | void setSupportedPresets(Vector<VideoPresetData>&&); |
| 60 | const Vector<Ref<VideoPreset>>& presets(); |
| 61 | |
| 62 | bool frameRateRangeIncludesRate(const FrameRateRange&, double); |
| 63 | |
| 64 | void updateCapabilities(RealtimeMediaSourceCapabilities&); |
| 65 | |
| 66 | void setDefaultSize(const IntSize& size) { m_defaultSize = size; } |
| 67 | |
| 68 | double observedFrameRate() const { return m_observedFrameRate; } |
| 69 | |
| 70 | void dispatchMediaSampleToObservers(MediaSample&); |
| 71 | const Vector<IntSize>& standardVideoSizes(); |
| 72 | |
| 73 | private: |
| 74 | struct CaptureSizeAndFrameRate { |
| 75 | RefPtr<VideoPreset> encodingPreset; |
| 76 | IntSize requestedSize; |
| 77 | double requestedFrameRate { 0 }; |
| 78 | }; |
| 79 | bool supportsCaptureSize(Optional<int>, Optional<int>, const Function<bool(const IntSize&)>&&); |
| 80 | Optional<CaptureSizeAndFrameRate> bestSupportedSizeAndFrameRate(Optional<int> width, Optional<int> height, Optional<double>); |
| 81 | bool presetSupportsFrameRate(RefPtr<VideoPreset>, double); |
| 82 | |
| 83 | #if !RELEASE_LOG_DISABLED |
| 84 | const char* logClassName() const override { return "RealtimeVideoSource" ; } |
| 85 | #endif |
| 86 | |
| 87 | Vector<Ref<VideoPreset>> m_presets; |
| 88 | Deque<double> m_observedFrameTimeStamps; |
| 89 | double m_observedFrameRate { 0 }; |
| 90 | IntSize m_defaultSize; |
| 91 | #if PLATFORM(COCOA) |
| 92 | std::unique_ptr<ImageTransferSessionVT> m_imageTransferSession; |
| 93 | #endif |
| 94 | }; |
| 95 | |
| 96 | struct SizeAndFrameRate { |
| 97 | Optional<int> width; |
| 98 | Optional<int> height; |
| 99 | Optional<double> frameRate; |
| 100 | |
| 101 | String toJSONString() const; |
| 102 | Ref<JSON::Object> toJSONObject() const; |
| 103 | }; |
| 104 | |
| 105 | } // namespace WebCore |
| 106 | |
| 107 | namespace WTF { |
| 108 | template<typename Type> struct LogArgument; |
| 109 | template <> |
| 110 | struct LogArgument<WebCore::SizeAndFrameRate> { |
| 111 | static String toString(const WebCore::SizeAndFrameRate& size) |
| 112 | { |
| 113 | return size.toJSONString(); |
| 114 | } |
| 115 | }; |
| 116 | }; // namespace WTF |
| 117 | |
| 118 | #endif // ENABLE(MEDIA_STREAM) |
| 119 | |
| 120 | |