1 | /* |
2 | * Copyright (C) 2016 Ericsson AB. All rights reserved. |
3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * 3. Neither the name of Ericsson nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this |
17 | * software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #include "config.h" |
33 | #include "RTCRtpTransceiver.h" |
34 | |
35 | #if ENABLE(WEB_RTC) |
36 | |
37 | #include <wtf/IsoMallocInlines.h> |
38 | #include <wtf/NeverDestroyed.h> |
39 | |
40 | namespace WebCore { |
41 | |
42 | WTF_MAKE_ISO_ALLOCATED_IMPL(RTCRtpTransceiver); |
43 | |
44 | RTCRtpTransceiver::RTCRtpTransceiver(Ref<RTCRtpSender>&& sender, Ref<RTCRtpReceiver>&& receiver, std::unique_ptr<RTCRtpTransceiverBackend>&& backend) |
45 | : m_direction(RTCRtpTransceiverDirection::Sendrecv) |
46 | , m_sender(WTFMove(sender)) |
47 | , m_receiver(WTFMove(receiver)) |
48 | , m_iceTransport(RTCIceTransport::create()) |
49 | , m_backend(WTFMove(backend)) |
50 | { |
51 | } |
52 | |
53 | String RTCRtpTransceiver::mid() const |
54 | { |
55 | return m_backend ? m_backend->mid() : String { }; |
56 | } |
57 | |
58 | bool RTCRtpTransceiver::hasSendingDirection() const |
59 | { |
60 | return m_direction == RTCRtpTransceiverDirection::Sendrecv || m_direction == RTCRtpTransceiverDirection::Sendonly; |
61 | } |
62 | |
63 | RTCRtpTransceiverDirection RTCRtpTransceiver::direction() const |
64 | { |
65 | if (!m_backend) |
66 | return m_direction; |
67 | return m_backend->direction(); |
68 | } |
69 | |
70 | Optional<RTCRtpTransceiverDirection> RTCRtpTransceiver::currentDirection() const |
71 | { |
72 | if (!m_backend) |
73 | return WTF::nullopt; |
74 | return m_backend->currentDirection(); |
75 | } |
76 | |
77 | void RTCRtpTransceiver::setDirection(RTCRtpTransceiverDirection direction) |
78 | { |
79 | m_direction = direction; |
80 | if (m_backend) |
81 | m_backend->setDirection(direction); |
82 | } |
83 | |
84 | |
85 | void RTCRtpTransceiver::enableSendingDirection() |
86 | { |
87 | if (m_direction == RTCRtpTransceiverDirection::Recvonly) |
88 | m_direction = RTCRtpTransceiverDirection::Sendrecv; |
89 | else if (m_direction == RTCRtpTransceiverDirection::Inactive) |
90 | m_direction = RTCRtpTransceiverDirection::Sendonly; |
91 | } |
92 | |
93 | void RTCRtpTransceiver::disableSendingDirection() |
94 | { |
95 | if (m_direction == RTCRtpTransceiverDirection::Sendrecv) |
96 | m_direction = RTCRtpTransceiverDirection::Recvonly; |
97 | else if (m_direction == RTCRtpTransceiverDirection::Sendonly) |
98 | m_direction = RTCRtpTransceiverDirection::Inactive; |
99 | } |
100 | |
101 | void RTCRtpTransceiver::stop() |
102 | { |
103 | if (m_stopped) |
104 | return; |
105 | m_stopped = true; |
106 | m_receiver->stop(); |
107 | m_sender->stop(); |
108 | if (m_backend) |
109 | m_backend->stop(); |
110 | } |
111 | |
112 | bool RTCRtpTransceiver::stopped() const |
113 | { |
114 | if (m_backend) |
115 | return m_backend->stopped(); |
116 | return m_stopped; |
117 | } |
118 | |
119 | void RtpTransceiverSet::append(Ref<RTCRtpTransceiver>&& transceiver) |
120 | { |
121 | m_transceivers.append(WTFMove(transceiver)); |
122 | } |
123 | |
124 | Vector<std::reference_wrapper<RTCRtpSender>> RtpTransceiverSet::senders() const |
125 | { |
126 | Vector<std::reference_wrapper<RTCRtpSender>> senders; |
127 | for (auto& transceiver : m_transceivers) { |
128 | if (transceiver->stopped()) |
129 | continue; |
130 | senders.append(transceiver->sender()); |
131 | } |
132 | return senders; |
133 | } |
134 | |
135 | Vector<std::reference_wrapper<RTCRtpReceiver>> RtpTransceiverSet::receivers() const |
136 | { |
137 | Vector<std::reference_wrapper<RTCRtpReceiver>> receivers; |
138 | for (auto& transceiver : m_transceivers) { |
139 | if (transceiver->stopped()) |
140 | continue; |
141 | receivers.append(transceiver->receiver()); |
142 | } |
143 | return receivers; |
144 | } |
145 | |
146 | } // namespace WebCore |
147 | |
148 | #endif // ENABLE(WEB_RTC) |
149 | |