| 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2011-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 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 | #include "AudioTrack.h" |
| 34 | |
| 35 | #if ENABLE(VIDEO_TRACK) |
| 36 | |
| 37 | #include "HTMLMediaElement.h" |
| 38 | #include <wtf/NeverDestroyed.h> |
| 39 | |
| 40 | namespace WebCore { |
| 41 | |
| 42 | const AtomicString& AudioTrack::alternativeKeyword() |
| 43 | { |
| 44 | static NeverDestroyed<AtomicString> alternative("alternative" , AtomicString::ConstructFromLiteral); |
| 45 | return alternative; |
| 46 | } |
| 47 | |
| 48 | const AtomicString& AudioTrack::descriptionKeyword() |
| 49 | { |
| 50 | static NeverDestroyed<AtomicString> description("description" , AtomicString::ConstructFromLiteral); |
| 51 | return description; |
| 52 | } |
| 53 | |
| 54 | const AtomicString& AudioTrack::mainKeyword() |
| 55 | { |
| 56 | static NeverDestroyed<AtomicString> main("main" , AtomicString::ConstructFromLiteral); |
| 57 | return main; |
| 58 | } |
| 59 | |
| 60 | const AtomicString& AudioTrack::mainDescKeyword() |
| 61 | { |
| 62 | static NeverDestroyed<AtomicString> mainDesc("main-desc" , AtomicString::ConstructFromLiteral); |
| 63 | return mainDesc; |
| 64 | } |
| 65 | |
| 66 | const AtomicString& AudioTrack::translationKeyword() |
| 67 | { |
| 68 | static NeverDestroyed<AtomicString> translation("translation" , AtomicString::ConstructFromLiteral); |
| 69 | return translation; |
| 70 | } |
| 71 | |
| 72 | const AtomicString& AudioTrack::() |
| 73 | { |
| 74 | static NeverDestroyed<AtomicString> ("commentary" , AtomicString::ConstructFromLiteral); |
| 75 | return commentary; |
| 76 | } |
| 77 | |
| 78 | AudioTrack::AudioTrack(AudioTrackClient& client, AudioTrackPrivate& trackPrivate) |
| 79 | : MediaTrackBase(MediaTrackBase::AudioTrack, trackPrivate.id(), trackPrivate.label(), trackPrivate.language()) |
| 80 | , m_client(&client) |
| 81 | , m_private(trackPrivate) |
| 82 | , m_enabled(trackPrivate.enabled()) |
| 83 | { |
| 84 | m_private->setClient(this); |
| 85 | #if !RELEASE_LOG_DISABLED |
| 86 | m_private->setLogger(logger(), logIdentifier()); |
| 87 | #endif |
| 88 | updateKindFromPrivate(); |
| 89 | } |
| 90 | |
| 91 | AudioTrack::~AudioTrack() |
| 92 | { |
| 93 | m_private->setClient(nullptr); |
| 94 | } |
| 95 | |
| 96 | void AudioTrack::setPrivate(AudioTrackPrivate& trackPrivate) |
| 97 | { |
| 98 | if (m_private.ptr() == &trackPrivate) |
| 99 | return; |
| 100 | |
| 101 | m_private->setClient(nullptr); |
| 102 | m_private = trackPrivate; |
| 103 | m_private->setEnabled(m_enabled); |
| 104 | m_private->setClient(this); |
| 105 | #if !RELEASE_LOG_DISABLED |
| 106 | m_private->setLogger(logger(), logIdentifier()); |
| 107 | #endif |
| 108 | |
| 109 | updateKindFromPrivate(); |
| 110 | } |
| 111 | |
| 112 | bool AudioTrack::isValidKind(const AtomicString& value) const |
| 113 | { |
| 114 | return value == alternativeKeyword() |
| 115 | || value == commentaryKeyword() |
| 116 | || value == descriptionKeyword() |
| 117 | || value == mainKeyword() |
| 118 | || value == mainDescKeyword() |
| 119 | || value == translationKeyword(); |
| 120 | } |
| 121 | |
| 122 | void AudioTrack::setEnabled(bool enabled) |
| 123 | { |
| 124 | if (m_enabled == enabled) |
| 125 | return; |
| 126 | |
| 127 | m_private->setEnabled(enabled); |
| 128 | } |
| 129 | |
| 130 | size_t AudioTrack::inbandTrackIndex() const |
| 131 | { |
| 132 | return m_private->trackIndex(); |
| 133 | } |
| 134 | |
| 135 | void AudioTrack::enabledChanged(bool enabled) |
| 136 | { |
| 137 | if (m_enabled == enabled) |
| 138 | return; |
| 139 | |
| 140 | m_enabled = enabled; |
| 141 | |
| 142 | if (m_client) |
| 143 | m_client->audioTrackEnabledChanged(*this); |
| 144 | } |
| 145 | |
| 146 | void AudioTrack::idChanged(const AtomicString& id) |
| 147 | { |
| 148 | setId(id); |
| 149 | } |
| 150 | |
| 151 | void AudioTrack::labelChanged(const AtomicString& label) |
| 152 | { |
| 153 | setLabel(label); |
| 154 | } |
| 155 | |
| 156 | void AudioTrack::languageChanged(const AtomicString& language) |
| 157 | { |
| 158 | setLanguage(language); |
| 159 | } |
| 160 | |
| 161 | void AudioTrack::willRemove() |
| 162 | { |
| 163 | auto element = makeRefPtr(mediaElement()); |
| 164 | if (!element) |
| 165 | return; |
| 166 | element->removeAudioTrack(*this); |
| 167 | } |
| 168 | |
| 169 | void AudioTrack::updateKindFromPrivate() |
| 170 | { |
| 171 | switch (m_private->kind()) { |
| 172 | case AudioTrackPrivate::Alternative: |
| 173 | setKind(AudioTrack::alternativeKeyword()); |
| 174 | break; |
| 175 | case AudioTrackPrivate::Description: |
| 176 | setKind(AudioTrack::descriptionKeyword()); |
| 177 | break; |
| 178 | case AudioTrackPrivate::Main: |
| 179 | setKind(AudioTrack::mainKeyword()); |
| 180 | break; |
| 181 | case AudioTrackPrivate::MainDesc: |
| 182 | setKind(AudioTrack::mainDescKeyword()); |
| 183 | break; |
| 184 | case AudioTrackPrivate::Translation: |
| 185 | setKind(AudioTrack::translationKeyword()); |
| 186 | break; |
| 187 | case AudioTrackPrivate::Commentary: |
| 188 | setKind(AudioTrack::commentaryKeyword()); |
| 189 | break; |
| 190 | case AudioTrackPrivate::None: |
| 191 | setKind(emptyString()); |
| 192 | break; |
| 193 | default: |
| 194 | ASSERT_NOT_REACHED(); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void AudioTrack::setMediaElement(HTMLMediaElement* element) |
| 200 | { |
| 201 | TrackBase::setMediaElement(element); |
| 202 | #if !RELEASE_LOG_DISABLED |
| 203 | m_private->setLogger(logger(), logIdentifier()); |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | } // namespace WebCore |
| 208 | |
| 209 | #endif |
| 210 | |