| 1 | /* |
| 2 | * Copyright (C) 2017-2019 Apple Inc. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted, provided that the following conditions |
| 6 | * are required to be met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Inc. nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. AND ITS CONTRIBUTORS BE LIABLE FOR |
| 21 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "RealtimeOutgoingAudioSource.h" |
| 31 | |
| 32 | #if USE(LIBWEBRTC) |
| 33 | |
| 34 | #include "LibWebRTCAudioFormat.h" |
| 35 | #include "LibWebRTCProvider.h" |
| 36 | #include "Logging.h" |
| 37 | #include <wtf/CryptographicallyRandomNumber.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | RealtimeOutgoingAudioSource::RealtimeOutgoingAudioSource(Ref<MediaStreamTrackPrivate>&& source) |
| 42 | : m_audioSource(WTFMove(source)) |
| 43 | #if !RELEASE_LOG_DISABLED |
| 44 | , m_logger(m_audioSource->logger()) |
| 45 | , m_logIdentifier(m_audioSource->logIdentifier()) |
| 46 | #endif |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | RealtimeOutgoingAudioSource::~RealtimeOutgoingAudioSource() |
| 51 | { |
| 52 | ASSERT(m_sinks.isEmpty()); |
| 53 | stop(); |
| 54 | } |
| 55 | |
| 56 | void RealtimeOutgoingAudioSource::observeSource() |
| 57 | { |
| 58 | m_audioSource->addObserver(*this); |
| 59 | initializeConverter(); |
| 60 | } |
| 61 | |
| 62 | void RealtimeOutgoingAudioSource::unobserveSource() |
| 63 | { |
| 64 | m_audioSource->removeObserver(*this); |
| 65 | } |
| 66 | |
| 67 | bool RealtimeOutgoingAudioSource::setSource(Ref<MediaStreamTrackPrivate>&& newSource) |
| 68 | { |
| 69 | auto locker = holdLock(m_sinksLock); |
| 70 | bool hasSinks = !m_sinks.isEmpty(); |
| 71 | |
| 72 | if (hasSinks) |
| 73 | unobserveSource(); |
| 74 | m_audioSource = WTFMove(newSource); |
| 75 | if (hasSinks) |
| 76 | observeSource(); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | void RealtimeOutgoingAudioSource::initializeConverter() |
| 82 | { |
| 83 | m_muted = m_audioSource->muted(); |
| 84 | m_enabled = m_audioSource->enabled(); |
| 85 | } |
| 86 | |
| 87 | void RealtimeOutgoingAudioSource::sourceMutedChanged() |
| 88 | { |
| 89 | m_muted = m_audioSource->muted(); |
| 90 | } |
| 91 | |
| 92 | void RealtimeOutgoingAudioSource::sourceEnabledChanged() |
| 93 | { |
| 94 | m_enabled = m_audioSource->enabled(); |
| 95 | } |
| 96 | |
| 97 | void RealtimeOutgoingAudioSource::AddSink(webrtc::AudioTrackSinkInterface* sink) |
| 98 | { |
| 99 | { |
| 100 | auto locker = holdLock(m_sinksLock); |
| 101 | if (!m_sinks.add(sink) || m_sinks.size() != 1) |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | callOnMainThread([protectedThis = makeRef(*this)]() { |
| 106 | protectedThis->observeSource(); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | void RealtimeOutgoingAudioSource::RemoveSink(webrtc::AudioTrackSinkInterface* sink) |
| 111 | { |
| 112 | { |
| 113 | auto locker = holdLock(m_sinksLock); |
| 114 | if (!m_sinks.remove(sink) || !m_sinks.isEmpty()) |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | unobserveSource(); |
| 119 | } |
| 120 | |
| 121 | void RealtimeOutgoingAudioSource::sendAudioFrames(const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames) |
| 122 | { |
| 123 | #if !RELEASE_LOG_DISABLED |
| 124 | if (!(++m_chunksSent % 200)) |
| 125 | ALWAYS_LOG(LOGIDENTIFIER, "chunk " , m_chunksSent); |
| 126 | #endif |
| 127 | |
| 128 | auto locker = holdLock(m_sinksLock); |
| 129 | for (auto sink : m_sinks) |
| 130 | sink->OnData(audioData, bitsPerSample, sampleRate, numberOfChannels, numberOfFrames); |
| 131 | } |
| 132 | |
| 133 | #if !RELEASE_LOG_DISABLED |
| 134 | WTFLogChannel& RealtimeOutgoingAudioSource::logChannel() const |
| 135 | { |
| 136 | return LogWebRTC; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | } // namespace WebCore |
| 141 | |
| 142 | #endif // USE(LIBWEBRTC) |
| 143 | |