| 1 | /* |
| 2 | * Copyright (C) 2018 Apple Inc. |
| 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. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #pragma once |
| 26 | |
| 27 | #if ENABLE(WEB_RTC) |
| 28 | |
| 29 | #include "LibWebRTCMacros.h" |
| 30 | #include "LibWebRTCPeerConnectionBackend.h" |
| 31 | #include "RTCRtpSenderBackend.h" |
| 32 | #include "RealtimeOutgoingAudioSource.h" |
| 33 | #include "RealtimeOutgoingVideoSource.h" |
| 34 | #include <wtf/WeakPtr.h> |
| 35 | |
| 36 | ALLOW_UNUSED_PARAMETERS_BEGIN |
| 37 | |
| 38 | #include <webrtc/api/rtpsenderinterface.h> |
| 39 | #include <webrtc/rtc_base/scoped_ref_ptr.h> |
| 40 | |
| 41 | ALLOW_UNUSED_PARAMETERS_END |
| 42 | |
| 43 | namespace WebCore { |
| 44 | |
| 45 | class LibWebRTCPeerConnectionBackend; |
| 46 | |
| 47 | class LibWebRTCRtpSenderBackend final : public RTCRtpSenderBackend { |
| 48 | WTF_MAKE_FAST_ALLOCATED; |
| 49 | public: |
| 50 | LibWebRTCRtpSenderBackend(LibWebRTCPeerConnectionBackend& backend, rtc::scoped_refptr<webrtc::RtpSenderInterface>&& rtcSender) |
| 51 | : m_peerConnectionBackend(makeWeakPtr(&backend)) |
| 52 | , m_rtcSender(WTFMove(rtcSender)) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | using Source = Variant<std::nullptr_t, Ref<RealtimeOutgoingAudioSource>, Ref<RealtimeOutgoingVideoSource>>; |
| 57 | LibWebRTCRtpSenderBackend(LibWebRTCPeerConnectionBackend& backend, rtc::scoped_refptr<webrtc::RtpSenderInterface>&& rtcSender, Source&& source) |
| 58 | : m_peerConnectionBackend(makeWeakPtr(&backend)) |
| 59 | , m_rtcSender(WTFMove(rtcSender)) |
| 60 | , m_source(WTFMove(source)) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void setRTCSender(rtc::scoped_refptr<webrtc::RtpSenderInterface>&& rtcSender) { m_rtcSender = WTFMove(rtcSender); } |
| 65 | webrtc::RtpSenderInterface* rtcSender() { return m_rtcSender.get(); } |
| 66 | |
| 67 | RealtimeOutgoingAudioSource* audioSource() |
| 68 | { |
| 69 | return WTF::switchOn(m_source, |
| 70 | [] (Ref<RealtimeOutgoingAudioSource>& source) { return source.ptr(); }, |
| 71 | [] (const auto&) -> RealtimeOutgoingAudioSource* { return nullptr; } |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | RealtimeOutgoingVideoSource* videoSource() |
| 76 | { |
| 77 | return WTF::switchOn(m_source, |
| 78 | [] (Ref<RealtimeOutgoingVideoSource>& source) { return source.ptr(); }, |
| 79 | [] (const auto&) -> RealtimeOutgoingVideoSource* { return nullptr; } |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | bool hasSource() const |
| 84 | { |
| 85 | return WTF::switchOn(m_source, |
| 86 | [] (const std::nullptr_t&) { return false; }, |
| 87 | [] (const auto&) { return true; } |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | void clearSource() |
| 92 | { |
| 93 | ASSERT(hasSource()); |
| 94 | m_source = nullptr; |
| 95 | } |
| 96 | |
| 97 | void setSource(Source&& source) |
| 98 | { |
| 99 | ASSERT(!hasSource()); |
| 100 | m_source = WTFMove(source); |
| 101 | ASSERT(hasSource()); |
| 102 | } |
| 103 | |
| 104 | void takeSource(LibWebRTCRtpSenderBackend& backend) |
| 105 | { |
| 106 | ASSERT(backend.hasSource()); |
| 107 | setSource(WTFMove(backend.m_source)); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | void replaceTrack(ScriptExecutionContext&, RTCRtpSender&, RefPtr<MediaStreamTrack>&&, DOMPromiseDeferred<void>&&) final; |
| 112 | RTCRtpSendParameters getParameters() const final; |
| 113 | void setParameters(const RTCRtpSendParameters&, DOMPromiseDeferred<void>&&) final; |
| 114 | |
| 115 | WeakPtr<LibWebRTCPeerConnectionBackend> m_peerConnectionBackend; |
| 116 | rtc::scoped_refptr<webrtc::RtpSenderInterface> m_rtcSender; |
| 117 | Source m_source; |
| 118 | mutable Optional<webrtc::RtpParameters> m_currentParameters; |
| 119 | }; |
| 120 | |
| 121 | } // namespace WebCore |
| 122 | |
| 123 | #endif // ENABLE(WEB_RTC) |
| 124 | |