NeverDestroyed<String>(ASCIILiteral(...)) is not thread safe.
[WebKit-https.git] / Source / WebCore / Modules / mediastream / RTCRtpTransceiver.cpp
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/NeverDestroyed.h>
38
39 namespace WebCore {
40
41 #define STRING_FUNCTION(name) \
42     static const String& name##String() \
43     { \
44         static NeverDestroyed<const String> name(MAKE_STATIC_STRING_IMPL(#name)); \
45         return name; \
46     }
47
48 STRING_FUNCTION(sendrecv)
49 STRING_FUNCTION(sendonly)
50 STRING_FUNCTION(recvonly)
51 STRING_FUNCTION(inactive)
52
53 String RTCRtpTransceiver::getNextMid()
54 {
55     static unsigned mid = 0;
56     return String::number(++mid);
57 }
58
59 RTCRtpTransceiver::RTCRtpTransceiver(Ref<RTCRtpSender>&& sender, Ref<RTCRtpReceiver>&& receiver)
60     : m_direction(RTCRtpTransceiverDirection::Sendrecv)
61     , m_sender(WTFMove(sender))
62     , m_receiver(WTFMove(receiver))
63     , m_iceTransport(RTCIceTransport::create())
64 {
65 }
66
67 const String& RTCRtpTransceiver::directionString() const
68 {
69     switch (m_direction) {
70     case RTCRtpTransceiverDirection::Sendrecv:
71         return sendrecvString();
72     case RTCRtpTransceiverDirection::Sendonly:
73         return sendonlyString();
74     case RTCRtpTransceiverDirection::Recvonly:
75         return recvonlyString();
76     case RTCRtpTransceiverDirection::Inactive:
77         return inactiveString();
78     }
79
80     ASSERT_NOT_REACHED();
81     return inactiveString();
82 }
83
84 bool RTCRtpTransceiver::hasSendingDirection() const
85 {
86     return m_direction == RTCRtpTransceiverDirection::Sendrecv || m_direction == RTCRtpTransceiverDirection::Sendonly;
87 }
88
89 void RTCRtpTransceiver::enableSendingDirection()
90 {
91     if (m_direction == RTCRtpTransceiverDirection::Recvonly)
92         m_direction = RTCRtpTransceiverDirection::Sendrecv;
93     else if (m_direction == RTCRtpTransceiverDirection::Inactive)
94         m_direction = RTCRtpTransceiverDirection::Sendonly;
95 }
96
97 void RTCRtpTransceiver::disableSendingDirection()
98 {
99     if (m_direction == RTCRtpTransceiverDirection::Sendrecv)
100         m_direction = RTCRtpTransceiverDirection::Recvonly;
101     else if (m_direction == RTCRtpTransceiverDirection::Sendonly)
102         m_direction = RTCRtpTransceiverDirection::Inactive;
103 }
104
105 void RtpTransceiverSet::append(Ref<RTCRtpTransceiver>&& transceiver)
106 {
107     m_senders.append(transceiver->sender());
108     m_receivers.append(transceiver->receiver());
109
110     m_transceivers.append(WTFMove(transceiver));
111 }
112
113 } // namespace WebCore
114
115 #endif // ENABLE(WEB_RTC)