| 1 | /* |
| 2 | * Copyright (C) 2018 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. 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 USE(LIBWEBRTC) |
| 28 | |
| 29 | #include "ExceptionCode.h" |
| 30 | #include "LibWebRTCMacros.h" |
| 31 | |
| 32 | ALLOW_UNUSED_PARAMETERS_BEGIN |
| 33 | |
| 34 | #include <webrtc/api/peerconnectioninterface.h> |
| 35 | |
| 36 | ALLOW_UNUSED_PARAMETERS_END |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | static inline ExceptionCode toExceptionCode(webrtc::RTCErrorType type) |
| 41 | { |
| 42 | switch (type) { |
| 43 | case webrtc::RTCErrorType::INVALID_PARAMETER: |
| 44 | return InvalidAccessError; |
| 45 | case webrtc::RTCErrorType::INVALID_RANGE: |
| 46 | return RangeError; |
| 47 | case webrtc::RTCErrorType::SYNTAX_ERROR: |
| 48 | return SyntaxError; |
| 49 | case webrtc::RTCErrorType::INVALID_STATE: |
| 50 | return InvalidStateError; |
| 51 | case webrtc::RTCErrorType::INVALID_MODIFICATION: |
| 52 | return InvalidModificationError; |
| 53 | case webrtc::RTCErrorType::NETWORK_ERROR: |
| 54 | return NetworkError; |
| 55 | default: |
| 56 | return OperationError; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | template<typename Endpoint> |
| 61 | class CreateSessionDescriptionObserver final : public webrtc::CreateSessionDescriptionObserver { |
| 62 | public: |
| 63 | explicit CreateSessionDescriptionObserver(Endpoint &endpoint) |
| 64 | : m_endpoint(endpoint) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void OnSuccess(webrtc::SessionDescriptionInterface* sessionDescription) final { m_endpoint.createSessionDescriptionSucceeded(std::unique_ptr<webrtc::SessionDescriptionInterface>(sessionDescription)); } |
| 69 | void OnFailure(webrtc::RTCError error) final { m_endpoint.createSessionDescriptionFailed(toExceptionCode(error.type()), error.message()); } |
| 70 | |
| 71 | void AddRef() const { m_endpoint.AddRef(); } |
| 72 | rtc::RefCountReleaseStatus Release() const { return m_endpoint.Release(); } |
| 73 | |
| 74 | private: |
| 75 | Endpoint& m_endpoint; |
| 76 | }; |
| 77 | |
| 78 | template<typename Endpoint> |
| 79 | class SetLocalSessionDescriptionObserver final : public webrtc::SetSessionDescriptionObserver { |
| 80 | public: |
| 81 | explicit SetLocalSessionDescriptionObserver(Endpoint &endpoint) |
| 82 | : m_endpoint(endpoint) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | void OnSuccess() final { m_endpoint.setLocalSessionDescriptionSucceeded(); } |
| 87 | void OnFailure(webrtc::RTCError error) final { m_endpoint.setLocalSessionDescriptionFailed(toExceptionCode(error.type()), error.message()); } |
| 88 | |
| 89 | void AddRef() const { m_endpoint.AddRef(); } |
| 90 | rtc::RefCountReleaseStatus Release() const { return m_endpoint.Release(); } |
| 91 | |
| 92 | private: |
| 93 | Endpoint& m_endpoint; |
| 94 | }; |
| 95 | |
| 96 | template<typename Endpoint> |
| 97 | class SetRemoteSessionDescriptionObserver final : public webrtc::SetSessionDescriptionObserver { |
| 98 | public: |
| 99 | explicit SetRemoteSessionDescriptionObserver(Endpoint &endpoint) |
| 100 | : m_endpoint(endpoint) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | void OnSuccess() final { m_endpoint.setRemoteSessionDescriptionSucceeded(); } |
| 105 | void OnFailure(webrtc::RTCError error) final { m_endpoint.setRemoteSessionDescriptionFailed(toExceptionCode(error.type()), error.message()); } |
| 106 | |
| 107 | void AddRef() const { m_endpoint.AddRef(); } |
| 108 | rtc::RefCountReleaseStatus Release() const { return m_endpoint.Release(); } |
| 109 | |
| 110 | private: |
| 111 | Endpoint& m_endpoint; |
| 112 | }; |
| 113 | |
| 114 | } // namespace WebCore |
| 115 | |
| 116 | #endif // USE(LIBWEBRTC) |
| 117 | |