| 1 | /* |
| 2 | * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies). |
| 3 | * Copyright (C) 2015 Ericsson AB. All rights reserved. |
| 4 | * Copyright (C) 2013-2019 Apple Inc. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 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 in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 19 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 21 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #pragma once |
| 29 | |
| 30 | #if ENABLE(MEDIA_STREAM) |
| 31 | |
| 32 | #include "RealtimeMediaSource.h" |
| 33 | #include <wtf/LoggerHelper.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | class AudioSourceProvider; |
| 38 | class GraphicsContext; |
| 39 | class MediaSample; |
| 40 | class RealtimeMediaSourceCapabilities; |
| 41 | class WebAudioSourceProvider; |
| 42 | |
| 43 | class MediaStreamTrackPrivate final |
| 44 | : public RefCounted<MediaStreamTrackPrivate> |
| 45 | , public RealtimeMediaSource::Observer |
| 46 | #if !RELEASE_LOG_DISABLED |
| 47 | , private LoggerHelper |
| 48 | #endif |
| 49 | { |
| 50 | public: |
| 51 | class Observer { |
| 52 | public: |
| 53 | virtual ~Observer() = default; |
| 54 | |
| 55 | virtual void trackStarted(MediaStreamTrackPrivate&) { }; |
| 56 | virtual void trackEnded(MediaStreamTrackPrivate&) = 0; |
| 57 | virtual void trackMutedChanged(MediaStreamTrackPrivate&) = 0; |
| 58 | virtual void trackSettingsChanged(MediaStreamTrackPrivate&) = 0; |
| 59 | virtual void trackEnabledChanged(MediaStreamTrackPrivate&) = 0; |
| 60 | virtual void sampleBufferUpdated(MediaStreamTrackPrivate&, MediaSample&) { }; |
| 61 | virtual void readyStateChanged(MediaStreamTrackPrivate&) { }; |
| 62 | |
| 63 | // May get called on a background thread. |
| 64 | virtual void audioSamplesAvailable(MediaStreamTrackPrivate&, const MediaTime&, const PlatformAudioData&, const AudioStreamDescription&, size_t) { }; |
| 65 | }; |
| 66 | |
| 67 | static Ref<MediaStreamTrackPrivate> create(Ref<RealtimeMediaSource>&&); |
| 68 | static Ref<MediaStreamTrackPrivate> create(Ref<RealtimeMediaSource>&&, String&& id); |
| 69 | |
| 70 | virtual ~MediaStreamTrackPrivate(); |
| 71 | |
| 72 | const String& id() const { return m_id; } |
| 73 | const String& label() const; |
| 74 | |
| 75 | bool ended() const { return m_isEnded; } |
| 76 | |
| 77 | enum class HintValue { Empty, Speech, Music, Motion, Detail, Text }; |
| 78 | HintValue contentHint() const { return m_contentHint; } |
| 79 | void setContentHint(HintValue); |
| 80 | |
| 81 | void startProducingData() { m_source->start(); } |
| 82 | void stopProducingData() { m_source->stop(); } |
| 83 | bool isProducingData() { return m_source->isProducingData(); } |
| 84 | |
| 85 | bool isIsolated() const { return m_source->isIsolated(); } |
| 86 | |
| 87 | bool muted() const; |
| 88 | void setMuted(bool muted) { m_source->setMuted(muted); } |
| 89 | |
| 90 | bool isCaptureTrack() const; |
| 91 | |
| 92 | bool enabled() const { return m_isEnabled; } |
| 93 | void setEnabled(bool); |
| 94 | |
| 95 | Ref<MediaStreamTrackPrivate> clone(); |
| 96 | |
| 97 | RealtimeMediaSource& source() { return m_source.get(); } |
| 98 | RealtimeMediaSource::Type type() const; |
| 99 | |
| 100 | void endTrack(); |
| 101 | |
| 102 | void addObserver(Observer&); |
| 103 | void removeObserver(Observer&); |
| 104 | |
| 105 | const RealtimeMediaSourceSettings& settings() const; |
| 106 | const RealtimeMediaSourceCapabilities& capabilities() const; |
| 107 | |
| 108 | void applyConstraints(const MediaConstraints&, RealtimeMediaSource::ApplyConstraintsHandler&&); |
| 109 | |
| 110 | AudioSourceProvider* audioSourceProvider(); |
| 111 | |
| 112 | void paintCurrentFrameInContext(GraphicsContext&, const FloatRect&); |
| 113 | |
| 114 | enum class ReadyState { None, Live, Ended }; |
| 115 | ReadyState readyState() const { return m_readyState; } |
| 116 | |
| 117 | void setIdForTesting(String&& id) { m_id = WTFMove(id); } |
| 118 | |
| 119 | #if !RELEASE_LOG_DISABLED |
| 120 | void setLogger(const Logger&, const void*); |
| 121 | const Logger& logger() const final { ASSERT(m_logger); return *m_logger.get(); } |
| 122 | const void* logIdentifier() const final { return m_logIdentifier; } |
| 123 | #endif |
| 124 | |
| 125 | private: |
| 126 | MediaStreamTrackPrivate(Ref<RealtimeMediaSource>&&, String&& id); |
| 127 | |
| 128 | // RealtimeMediaSourceObserver |
| 129 | void sourceStarted() final; |
| 130 | void sourceStopped() final; |
| 131 | void sourceMutedChanged() final; |
| 132 | void sourceSettingsChanged() final; |
| 133 | bool preventSourceFromStopping() final; |
| 134 | void videoSampleAvailable(MediaSample&) final; |
| 135 | void audioSamplesAvailable(const MediaTime&, const PlatformAudioData&, const AudioStreamDescription&, size_t) final; |
| 136 | |
| 137 | void updateReadyState(); |
| 138 | |
| 139 | void forEachObserver(const WTF::Function<void(Observer&)>&) const; |
| 140 | |
| 141 | #if !RELEASE_LOG_DISABLED |
| 142 | const char* logClassName() const final { return "MediaStreamTrackPrivate" ; } |
| 143 | WTFLogChannel& logChannel() const final; |
| 144 | |
| 145 | RefPtr<const Logger> m_logger; |
| 146 | const void* m_logIdentifier; |
| 147 | #endif |
| 148 | |
| 149 | mutable RecursiveLock m_observersLock; |
| 150 | HashSet<Observer*> m_observers; |
| 151 | Ref<RealtimeMediaSource> m_source; |
| 152 | |
| 153 | String m_id; |
| 154 | ReadyState m_readyState { ReadyState::None }; |
| 155 | bool m_isEnabled { true }; |
| 156 | bool m_isEnded { false }; |
| 157 | bool m_haveProducedData { false }; |
| 158 | HintValue m_contentHint { HintValue::Empty }; |
| 159 | RefPtr<WebAudioSourceProvider> m_audioSourceProvider; |
| 160 | }; |
| 161 | |
| 162 | typedef Vector<RefPtr<MediaStreamTrackPrivate>> MediaStreamTrackPrivateVector; |
| 163 | |
| 164 | } // namespace WebCore |
| 165 | |
| 166 | #endif // ENABLE(MEDIA_STREAM) |
| 167 | |