Use NeverDestroyed instead of DEPRECATED_DEFINE_STATIC_LOCAL
[WebKit-https.git] / Source / WebCore / html / track / AudioTrack.cpp
1 /*
2  * Copyright (C) 2011 Google Inc.  All rights reserved.
3  * Copyright (C) 2011, 2012, 2013 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 are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this 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
34 #if ENABLE(VIDEO_TRACK)
35
36 #include "AudioTrack.h"
37
38 #include "AudioTrackList.h"
39 #include "Event.h"
40 #include "HTMLMediaElement.h"
41 #include <wtf/NeverDestroyed.h>
42
43 namespace WebCore {
44
45 const AtomicString& AudioTrack::alternativeKeyword()
46 {
47     static NeverDestroyed<AtomicString> alternative("alternative", AtomicString::ConstructFromLiteral);
48     return alternative;
49 }
50
51 const AtomicString& AudioTrack::descriptionKeyword()
52 {
53     static NeverDestroyed<AtomicString> description("description", AtomicString::ConstructFromLiteral);
54     return description;
55 }
56
57 const AtomicString& AudioTrack::mainKeyword()
58 {
59     static NeverDestroyed<AtomicString> main("main", AtomicString::ConstructFromLiteral);
60     return main;
61 }
62
63 const AtomicString& AudioTrack::mainDescKeyword()
64 {
65     static NeverDestroyed<AtomicString> mainDesc("main-desc", AtomicString::ConstructFromLiteral);
66     return mainDesc;
67 }
68
69 const AtomicString& AudioTrack::translationKeyword()
70 {
71     static NeverDestroyed<AtomicString> translation("translation", AtomicString::ConstructFromLiteral);
72     return translation;
73 }
74
75 const AtomicString& AudioTrack::commentaryKeyword()
76 {
77     static NeverDestroyed<AtomicString> commentary("commentary", AtomicString::ConstructFromLiteral);
78     return commentary;
79 }
80
81 AudioTrack::AudioTrack(AudioTrackClient* client, PassRefPtr<AudioTrackPrivate> trackPrivate)
82     : TrackBase(TrackBase::AudioTrack, trackPrivate->id(), trackPrivate->label(), trackPrivate->language())
83     , m_enabled(trackPrivate->enabled())
84     , m_client(client)
85     , m_private(trackPrivate)
86 {
87     m_private->setClient(this);
88     updateKindFromPrivate();
89 }
90
91 AudioTrack::~AudioTrack()
92 {
93     m_private->setClient(0);
94 }
95
96 void AudioTrack::setPrivate(PassRefPtr<AudioTrackPrivate> trackPrivate)
97 {
98     ASSERT(m_private);
99     ASSERT(trackPrivate);
100
101     if (m_private == trackPrivate)
102         return;
103
104     m_private->setClient(0);
105     m_private = trackPrivate;
106     m_private->setClient(this);
107
108     m_private->setEnabled(m_enabled);
109     updateKindFromPrivate();
110 }
111
112 bool AudioTrack::isValidKind(const AtomicString& value) const
113 {
114     if (value == alternativeKeyword())
115         return true;
116     if (value == descriptionKeyword())
117         return true;
118     if (value == mainKeyword())
119         return true;
120     if (value == mainDescKeyword())
121         return true;
122     if (value == translationKeyword())
123         return true;
124     if (value == commentaryKeyword())
125         return true;
126
127     return false;
128 }
129
130 void AudioTrack::setEnabled(const bool enabled)
131 {
132     if (m_enabled == enabled)
133         return;
134
135     m_enabled = enabled;
136     m_private->setEnabled(enabled);
137
138     if (m_client)
139         m_client->audioTrackEnabledChanged(this);
140 }
141
142 size_t AudioTrack::inbandTrackIndex()
143 {
144     ASSERT(m_private);
145     return m_private->trackIndex();
146 }
147
148 void AudioTrack::enabledChanged(AudioTrackPrivate* trackPrivate, bool enabled)
149 {
150     ASSERT_UNUSED(trackPrivate, trackPrivate == m_private);
151     m_enabled = enabled;
152
153     if (m_client)
154         m_client->audioTrackEnabledChanged(this);
155 }
156
157 void AudioTrack::idChanged(TrackPrivateBase* trackPrivate, const AtomicString& id)
158 {
159     ASSERT_UNUSED(trackPrivate, trackPrivate == m_private);
160     setId(id);
161 }
162
163 void AudioTrack::labelChanged(TrackPrivateBase* trackPrivate, const AtomicString& label)
164 {
165     ASSERT_UNUSED(trackPrivate, trackPrivate == m_private);
166     setLabel(label);
167 }
168
169 void AudioTrack::languageChanged(TrackPrivateBase* trackPrivate, const AtomicString& language)
170 {
171     ASSERT_UNUSED(trackPrivate, trackPrivate == m_private);
172     setLanguage(language);
173 }
174
175 void AudioTrack::willRemove(TrackPrivateBase* trackPrivate)
176 {
177     ASSERT_UNUSED(trackPrivate, trackPrivate == m_private);
178     mediaElement()->removeAudioTrack(this);
179 }
180
181 void AudioTrack::updateKindFromPrivate()
182 {
183     switch (m_private->kind()) {
184     case AudioTrackPrivate::Alternative:
185         setKind(AudioTrack::alternativeKeyword());
186         break;
187     case AudioTrackPrivate::Description:
188         setKind(AudioTrack::descriptionKeyword());
189         break;
190     case AudioTrackPrivate::Main:
191         setKind(AudioTrack::mainKeyword());
192         break;
193     case AudioTrackPrivate::MainDesc:
194         setKind(AudioTrack::mainDescKeyword());
195         break;
196     case AudioTrackPrivate::Translation:
197         setKind(AudioTrack::translationKeyword());
198         break;
199     case AudioTrackPrivate::Commentary:
200         setKind(AudioTrack::commentaryKeyword());
201         break;
202     case AudioTrackPrivate::None:
203         setKind(emptyString());
204         break;
205     default:
206         ASSERT_NOT_REACHED();
207         break;
208     }
209 }
210
211 } // namespace WebCore
212
213 #endif