| 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 | #include "config.h" |
| 26 | #include "LibWebRTCRtpSenderBackend.h" |
| 27 | |
| 28 | #if ENABLE(WEB_RTC) && USE(LIBWEBRTC) |
| 29 | |
| 30 | #include "LibWebRTCPeerConnectionBackend.h" |
| 31 | #include "LibWebRTCUtils.h" |
| 32 | #include "RTCPeerConnection.h" |
| 33 | #include "RTCRtpSender.h" |
| 34 | #include "RuntimeEnabledFeatures.h" |
| 35 | #include "ScriptExecutionContext.h" |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | template<typename Source> |
| 40 | static inline bool updateTrackSource(Source& source, MediaStreamTrack* track) |
| 41 | { |
| 42 | if (!track) { |
| 43 | source.stop(); |
| 44 | return true; |
| 45 | } |
| 46 | return source.setSource(track->privateTrack()); |
| 47 | } |
| 48 | |
| 49 | void LibWebRTCRtpSenderBackend::replaceTrack(ScriptExecutionContext& context, RTCRtpSender& sender, RefPtr<MediaStreamTrack>&& track, DOMPromiseDeferred<void>&& promise) |
| 50 | { |
| 51 | if (!m_peerConnectionBackend) { |
| 52 | promise.reject(Exception { InvalidStateError, "No WebRTC backend"_s }); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | auto* currentTrack = sender.track(); |
| 57 | |
| 58 | ASSERT(!track || !currentTrack || currentTrack->source().type() == track->source().type()); |
| 59 | if (currentTrack) { |
| 60 | switch (currentTrack->source().type()) { |
| 61 | case RealtimeMediaSource::Type::None: |
| 62 | ASSERT_NOT_REACHED(); |
| 63 | promise.reject(InvalidModificationError); |
| 64 | break; |
| 65 | case RealtimeMediaSource::Type::Audio: |
| 66 | if (!updateTrackSource(*audioSource(), track.get())) { |
| 67 | promise.reject(InvalidModificationError); |
| 68 | return; |
| 69 | } |
| 70 | break; |
| 71 | case RealtimeMediaSource::Type::Video: |
| 72 | if (!updateTrackSource(*videoSource(), track.get())) { |
| 73 | promise.reject(InvalidModificationError); |
| 74 | return; |
| 75 | } |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // FIXME: Remove this postTask once this whole function is executed as part of the RTCPeerConnection operation queue. |
| 81 | context.postTask([protectedSender = makeRef(sender), promise = WTFMove(promise), track = WTFMove(track), this](ScriptExecutionContext&) mutable { |
| 82 | if (protectedSender->isStopped()) |
| 83 | return; |
| 84 | |
| 85 | if (!track) { |
| 86 | protectedSender->setTrackToNull(); |
| 87 | promise.resolve(); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | bool hasTrack = protectedSender->track(); |
| 92 | protectedSender->setTrack(track.releaseNonNull()); |
| 93 | |
| 94 | if (hasTrack) { |
| 95 | promise.resolve(); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled()) { |
| 100 | m_source = nullptr; |
| 101 | m_peerConnectionBackend->setSenderSourceFromTrack(*this, *protectedSender->track()); |
| 102 | promise.resolve(); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | auto result = m_peerConnectionBackend->addTrack(*protectedSender->track(), { }); |
| 107 | if (result.hasException()) { |
| 108 | promise.reject(result.releaseException()); |
| 109 | return; |
| 110 | } |
| 111 | promise.resolve(); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | RTCRtpSendParameters LibWebRTCRtpSenderBackend::getParameters() const |
| 116 | { |
| 117 | if (!m_rtcSender) |
| 118 | return { }; |
| 119 | |
| 120 | m_currentParameters = m_rtcSender->GetParameters(); |
| 121 | return toRTCRtpSendParameters(*m_currentParameters); |
| 122 | } |
| 123 | |
| 124 | void LibWebRTCRtpSenderBackend::setParameters(const RTCRtpSendParameters& parameters, DOMPromiseDeferred<void>&& promise) |
| 125 | { |
| 126 | if (!m_rtcSender) { |
| 127 | promise.reject(NotSupportedError); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!m_currentParameters) { |
| 132 | promise.reject(Exception { InvalidStateError, "getParameters must be called before setParameters"_s }); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | auto rtcParameters = WTFMove(*m_currentParameters); |
| 137 | updateRTCRtpSendParameters(parameters, rtcParameters); |
| 138 | m_currentParameters = WTF::nullopt; |
| 139 | |
| 140 | auto error = m_rtcSender->SetParameters(rtcParameters); |
| 141 | if (!error.ok()) { |
| 142 | promise.reject(Exception { InvalidStateError, error.message() }); |
| 143 | return; |
| 144 | } |
| 145 | promise.resolve(); |
| 146 | } |
| 147 | |
| 148 | } // namespace WebCore |
| 149 | |
| 150 | #endif // ENABLE(WEB_RTC) && USE(LIBWEBRTC) |
| 151 | |