1446b3fc1749b0668af3b1300c87dd6a9b6c7acd
[WebKit-https.git] / Source / WebCore / platform / mediastream / RealtimeIncomingVideoSource.cpp
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are 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
12  *    in the documentation and/or other materials provided with the
13  *    distribution.
14  * 3. Neither the name of Google Inc. nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "RealtimeIncomingVideoSource.h"
33
34 #if USE(LIBWEBRTC)
35
36 #include "Logging.h"
37 #include <wtf/CryptographicallyRandomNumber.h>
38
39 namespace WebCore {
40
41 RealtimeIncomingVideoSource::RealtimeIncomingVideoSource(rtc::scoped_refptr<webrtc::VideoTrackInterface>&& videoTrack, String&& videoTrackId)
42     : RealtimeMediaSource(Type::Video, "remote video"_s, WTFMove(videoTrackId))
43     , m_videoTrack(WTFMove(videoTrack))
44 #if !RELEASE_LOG_DISABLED
45     , m_logIdentifier(reinterpret_cast<const void*>(cryptographicallyRandomNumber()))
46 #endif
47 {
48     notifyMutedChange(!m_videoTrack);
49
50     RealtimeMediaSourceSupportedConstraints constraints;
51     constraints.setSupportsWidth(true);
52     constraints.setSupportsHeight(true);
53     m_currentSettings = RealtimeMediaSourceSettings { };
54     m_currentSettings->setSupportedConstraints(WTFMove(constraints));
55 }
56
57 void RealtimeIncomingVideoSource::startProducingData()
58 {
59     if (m_videoTrack)
60         m_videoTrack->AddOrUpdateSink(this, rtc::VideoSinkWants());
61 }
62
63 void RealtimeIncomingVideoSource::setSourceTrack(rtc::scoped_refptr<webrtc::VideoTrackInterface>&& track)
64 {
65     ASSERT(track);
66
67     if (m_videoTrack && isProducingData())
68         m_videoTrack->RemoveSink(this);
69     m_videoTrack = WTFMove(track);
70     notifyMutedChange(!m_videoTrack);
71     if (isProducingData())
72         m_videoTrack->AddOrUpdateSink(this, rtc::VideoSinkWants());
73 }
74
75 void RealtimeIncomingVideoSource::stopProducingData()
76 {
77     if (m_videoTrack)
78         m_videoTrack->RemoveSink(this);
79 }
80
81 const RealtimeMediaSourceCapabilities& RealtimeIncomingVideoSource::capabilities()
82 {
83     return RealtimeMediaSourceCapabilities::emptyCapabilities();
84 }
85
86 const RealtimeMediaSourceSettings& RealtimeIncomingVideoSource::settings()
87 {
88     if (m_currentSettings)
89         return m_currentSettings.value();
90
91     RealtimeMediaSourceSupportedConstraints constraints;
92     constraints.setSupportsWidth(true);
93     constraints.setSupportsHeight(true);
94
95     RealtimeMediaSourceSettings settings;
96     auto& size = this->size();
97     settings.setWidth(size.width());
98     settings.setHeight(size.height());
99     settings.setSupportedConstraints(constraints);
100
101     m_currentSettings = WTFMove(settings);
102     return m_currentSettings.value();
103 }
104
105 void RealtimeIncomingVideoSource::settingsDidChange(OptionSet<RealtimeMediaSourceSettings::Flag> settings)
106 {
107     if (settings.containsAny({ RealtimeMediaSourceSettings::Flag::Width, RealtimeMediaSourceSettings::Flag::Height }))
108         m_currentSettings = WTF::nullopt;
109 }
110
111 #if !RELEASE_LOG_DISABLED
112 WTFLogChannel& RealtimeIncomingVideoSource::logChannel() const
113 {
114     return LogWebRTC;
115 }
116
117 const Logger& RealtimeIncomingVideoSource::logger() const
118 {
119     if (!m_logger)
120         m_logger = Logger::create(this);
121     return *m_logger;
122 }
123 #endif
124
125 } // namespace WebCore
126
127 #endif // USE(LIBWEBRTC)