| 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 | #pragma once |
| 30 | |
| 31 | |
| 32 | #if USE(LIBWEBRTC) |
| 33 | |
| 34 | #include "LibWebRTCMacros.h" |
| 35 | #include "MediaStreamTrackPrivate.h" |
| 36 | #include "Timer.h" |
| 37 | |
| 38 | ALLOW_UNUSED_PARAMETERS_BEGIN |
| 39 | |
| 40 | #include <webrtc/api/mediastreaminterface.h> |
| 41 | |
| 42 | ALLOW_UNUSED_PARAMETERS_END |
| 43 | |
| 44 | #include <wtf/LoggerHelper.h> |
| 45 | #include <wtf/ThreadSafeRefCounted.h> |
| 46 | |
| 47 | namespace webrtc { |
| 48 | class AudioTrackInterface; |
| 49 | class AudioTrackSinkInterface; |
| 50 | } |
| 51 | |
| 52 | namespace WebCore { |
| 53 | |
| 54 | class RealtimeOutgoingAudioSource |
| 55 | : public ThreadSafeRefCounted<RealtimeOutgoingAudioSource, WTF::DestructionThread::Main> |
| 56 | , public webrtc::AudioSourceInterface |
| 57 | , private MediaStreamTrackPrivate::Observer |
| 58 | #if !RELEASE_LOG_DISABLED |
| 59 | , private LoggerHelper |
| 60 | #endif |
| 61 | { |
| 62 | public: |
| 63 | static Ref<RealtimeOutgoingAudioSource> create(Ref<MediaStreamTrackPrivate>&& audioSource); |
| 64 | |
| 65 | ~RealtimeOutgoingAudioSource(); |
| 66 | |
| 67 | void stop() { unobserveSource(); } |
| 68 | |
| 69 | bool setSource(Ref<MediaStreamTrackPrivate>&&); |
| 70 | MediaStreamTrackPrivate& source() const { return m_audioSource.get(); } |
| 71 | |
| 72 | protected: |
| 73 | explicit RealtimeOutgoingAudioSource(Ref<MediaStreamTrackPrivate>&&); |
| 74 | |
| 75 | void unobserveSource(); |
| 76 | |
| 77 | virtual void pullAudioData() { } |
| 78 | |
| 79 | bool isSilenced() const { return m_muted || !m_enabled; } |
| 80 | |
| 81 | void sendAudioFrames(const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames); |
| 82 | |
| 83 | #if !RELEASE_LOG_DISABLED |
| 84 | // LoggerHelper API |
| 85 | const Logger& logger() const final { return m_logger.get(); } |
| 86 | const void* logIdentifier() const final { return m_logIdentifier; } |
| 87 | const char* logClassName() const final { return "RealtimeOutgoingAudioSource" ; } |
| 88 | WTFLogChannel& logChannel() const final; |
| 89 | #endif |
| 90 | |
| 91 | private: |
| 92 | // webrtc::AudioSourceInterface API |
| 93 | void AddSink(webrtc::AudioTrackSinkInterface*) final; |
| 94 | void RemoveSink(webrtc::AudioTrackSinkInterface*) final; |
| 95 | |
| 96 | void AddRef() const final { ref(); } |
| 97 | rtc::RefCountReleaseStatus Release() const final |
| 98 | { |
| 99 | auto result = refCount() - 1; |
| 100 | deref(); |
| 101 | return result ? rtc::RefCountReleaseStatus::kOtherRefsRemained : rtc::RefCountReleaseStatus::kDroppedLastRef; |
| 102 | } |
| 103 | |
| 104 | SourceState state() const final { return kLive; } |
| 105 | bool remote() const final { return false; } |
| 106 | void RegisterObserver(webrtc::ObserverInterface*) final { } |
| 107 | void UnregisterObserver(webrtc::ObserverInterface*) final { } |
| 108 | |
| 109 | void observeSource(); |
| 110 | |
| 111 | void sourceMutedChanged(); |
| 112 | void sourceEnabledChanged(); |
| 113 | virtual void audioSamplesAvailable(const MediaTime&, const PlatformAudioData&, const AudioStreamDescription&, size_t) { }; |
| 114 | |
| 115 | virtual bool isReachingBufferedAudioDataHighLimit() { return false; }; |
| 116 | virtual bool isReachingBufferedAudioDataLowLimit() { return false; }; |
| 117 | virtual bool hasBufferedEnoughData() { return false; }; |
| 118 | |
| 119 | // MediaStreamTrackPrivate::Observer API |
| 120 | void trackMutedChanged(MediaStreamTrackPrivate&) final { sourceMutedChanged(); } |
| 121 | void trackEnabledChanged(MediaStreamTrackPrivate&) final { sourceEnabledChanged(); } |
| 122 | void audioSamplesAvailable(MediaStreamTrackPrivate&, const MediaTime& mediaTime, const PlatformAudioData& data, const AudioStreamDescription& description, size_t sampleCount) { audioSamplesAvailable(mediaTime, data, description, sampleCount); } |
| 123 | void trackEnded(MediaStreamTrackPrivate&) final { } |
| 124 | void trackSettingsChanged(MediaStreamTrackPrivate&) final { } |
| 125 | |
| 126 | void initializeConverter(); |
| 127 | |
| 128 | Ref<MediaStreamTrackPrivate> m_audioSource; |
| 129 | bool m_muted { false }; |
| 130 | bool m_enabled { true }; |
| 131 | |
| 132 | mutable RecursiveLock m_sinksLock; |
| 133 | HashSet<webrtc::AudioTrackSinkInterface*> m_sinks; |
| 134 | |
| 135 | #if !RELEASE_LOG_DISABLED |
| 136 | mutable Ref<const Logger> m_logger; |
| 137 | const void* m_logIdentifier; |
| 138 | size_t m_chunksSent { 0 }; |
| 139 | #endif |
| 140 | }; |
| 141 | |
| 142 | } // namespace WebCore |
| 143 | |
| 144 | #endif // USE(LIBWEBRTC) |
| 145 | |