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 "RealtimeOutgoingVideoSource.h" |
31 | |
32 | #if USE(LIBWEBRTC) |
33 | |
34 | #include "Logging.h" |
35 | |
36 | ALLOW_UNUSED_PARAMETERS_BEGIN |
37 | |
38 | #include <webrtc/api/video/i420_buffer.h> |
39 | #include <webrtc/common_video/libyuv/include/webrtc_libyuv.h> |
40 | |
41 | ALLOW_UNUSED_PARAMETERS_END |
42 | |
43 | #include <wtf/MainThread.h> |
44 | |
45 | namespace WebCore { |
46 | |
47 | RealtimeOutgoingVideoSource::RealtimeOutgoingVideoSource(Ref<MediaStreamTrackPrivate>&& videoSource) |
48 | : m_videoSource(WTFMove(videoSource)) |
49 | , m_blackFrameTimer(*this, &RealtimeOutgoingVideoSource::sendOneBlackFrame) |
50 | #if !RELEASE_LOG_DISABLED |
51 | , m_logger(m_videoSource->logger()) |
52 | , m_logIdentifier(m_videoSource->logIdentifier()) |
53 | #endif |
54 | { |
55 | } |
56 | |
57 | RealtimeOutgoingVideoSource::~RealtimeOutgoingVideoSource() |
58 | { |
59 | ASSERT(m_sinks.isEmpty()); |
60 | stop(); |
61 | } |
62 | |
63 | void RealtimeOutgoingVideoSource::observeSource() |
64 | { |
65 | m_videoSource->addObserver(*this); |
66 | initializeFromSource(); |
67 | } |
68 | |
69 | void RealtimeOutgoingVideoSource::unobserveSource() |
70 | { |
71 | m_videoSource->removeObserver(*this); |
72 | } |
73 | |
74 | bool RealtimeOutgoingVideoSource::setSource(Ref<MediaStreamTrackPrivate>&& newSource) |
75 | { |
76 | if (!m_initialSettings) |
77 | m_initialSettings = m_videoSource->source().settings(); |
78 | |
79 | auto locker = holdLock(m_sinksLock); |
80 | bool hasSinks = !m_sinks.isEmpty(); |
81 | |
82 | if (hasSinks) |
83 | unobserveSource(); |
84 | m_videoSource = WTFMove(newSource); |
85 | if (hasSinks) |
86 | observeSource(); |
87 | |
88 | return true; |
89 | } |
90 | |
91 | void RealtimeOutgoingVideoSource::stop() |
92 | { |
93 | ASSERT(isMainThread()); |
94 | unobserveSource(); |
95 | m_blackFrameTimer.stop(); |
96 | } |
97 | |
98 | void RealtimeOutgoingVideoSource::updateBlackFramesSending() |
99 | { |
100 | if (!m_muted && m_enabled) { |
101 | if (m_blackFrameTimer.isActive()) |
102 | m_blackFrameTimer.stop(); |
103 | return; |
104 | } |
105 | |
106 | sendBlackFramesIfNeeded(); |
107 | } |
108 | |
109 | void RealtimeOutgoingVideoSource::sourceMutedChanged() |
110 | { |
111 | ASSERT(m_muted != m_videoSource->muted()); |
112 | |
113 | m_muted = m_videoSource->muted(); |
114 | |
115 | updateBlackFramesSending(); |
116 | } |
117 | |
118 | void RealtimeOutgoingVideoSource::sourceEnabledChanged() |
119 | { |
120 | ASSERT(m_enabled != m_videoSource->enabled()); |
121 | |
122 | m_enabled = m_videoSource->enabled(); |
123 | |
124 | updateBlackFramesSending(); |
125 | } |
126 | |
127 | void RealtimeOutgoingVideoSource::initializeFromSource() |
128 | { |
129 | const auto& settings = m_videoSource->source().settings(); |
130 | m_width = settings.width(); |
131 | m_height = settings.height(); |
132 | |
133 | m_muted = m_videoSource->muted(); |
134 | m_enabled = m_videoSource->enabled(); |
135 | |
136 | updateBlackFramesSending(); |
137 | } |
138 | |
139 | void RealtimeOutgoingVideoSource::AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, const rtc::VideoSinkWants& sinkWants) |
140 | { |
141 | ASSERT(!sinkWants.black_frames); |
142 | |
143 | if (sinkWants.rotation_applied) |
144 | m_shouldApplyRotation = true; |
145 | |
146 | { |
147 | auto locker = holdLock(m_sinksLock); |
148 | if (!m_sinks.add(sink) || m_sinks.size() != 1) |
149 | return; |
150 | } |
151 | |
152 | callOnMainThread([protectedThis = makeRef(*this)]() { |
153 | protectedThis->observeSource(); |
154 | }); |
155 | } |
156 | |
157 | void RealtimeOutgoingVideoSource::RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) |
158 | { |
159 | { |
160 | auto locker = holdLock(m_sinksLock); |
161 | |
162 | if (!m_sinks.remove(sink) || m_sinks.size()) |
163 | return; |
164 | } |
165 | |
166 | unobserveSource(); |
167 | |
168 | callOnMainThread([protectedThis = makeRef(*this)]() { |
169 | if (protectedThis->m_blackFrameTimer.isActive()) |
170 | protectedThis->m_blackFrameTimer.stop(); |
171 | }); |
172 | } |
173 | |
174 | void RealtimeOutgoingVideoSource::sendBlackFramesIfNeeded() |
175 | { |
176 | if (m_blackFrameTimer.isActive()) |
177 | return; |
178 | |
179 | if (!m_muted && m_enabled) |
180 | return; |
181 | |
182 | if (!m_width || !m_height) |
183 | return; |
184 | |
185 | if (!m_blackFrame) { |
186 | auto width = m_width; |
187 | auto height = m_height; |
188 | if (m_shouldApplyRotation && (m_currentRotation == webrtc::kVideoRotation_0 || m_currentRotation == webrtc::kVideoRotation_90)) |
189 | std::swap(width, height); |
190 | m_blackFrame = createBlackFrame(width, height); |
191 | ASSERT(m_blackFrame); |
192 | if (!m_blackFrame) { |
193 | ALWAYS_LOG(LOGIDENTIFIER, "Unable to send black frames" ); |
194 | return; |
195 | } |
196 | } |
197 | sendOneBlackFrame(); |
198 | m_blackFrameTimer.startRepeating(1_s); |
199 | } |
200 | |
201 | void RealtimeOutgoingVideoSource::sendOneBlackFrame() |
202 | { |
203 | ALWAYS_LOG(LOGIDENTIFIER); |
204 | sendFrame(rtc::scoped_refptr<webrtc::VideoFrameBuffer>(m_blackFrame)); |
205 | } |
206 | |
207 | void RealtimeOutgoingVideoSource::sendFrame(rtc::scoped_refptr<webrtc::VideoFrameBuffer>&& buffer) |
208 | { |
209 | MonotonicTime timestamp = MonotonicTime::now(); |
210 | webrtc::VideoFrame frame(buffer, m_shouldApplyRotation ? webrtc::kVideoRotation_0 : m_currentRotation, static_cast<int64_t>(timestamp.secondsSinceEpoch().microseconds())); |
211 | |
212 | #if !RELEASE_LOG_DISABLED |
213 | ++m_frameCount; |
214 | |
215 | auto delta = timestamp - m_lastFrameLogTime; |
216 | if (!m_lastFrameLogTime || delta >= 1_s) { |
217 | if (m_lastFrameLogTime) { |
218 | INFO_LOG(LOGIDENTIFIER, m_frameCount, " frames sent in " , delta.value(), " seconds" ); |
219 | m_frameCount = 0; |
220 | } |
221 | m_lastFrameLogTime = timestamp; |
222 | } |
223 | #endif |
224 | |
225 | auto locker = holdLock(m_sinksLock); |
226 | for (auto* sink : m_sinks) |
227 | sink->OnFrame(frame); |
228 | } |
229 | |
230 | #if !RELEASE_LOG_DISABLED |
231 | WTFLogChannel& RealtimeOutgoingVideoSource::logChannel() const |
232 | { |
233 | return LogWebRTC; |
234 | } |
235 | #endif |
236 | |
237 | } // namespace WebCore |
238 | |
239 | #endif // USE(LIBWEBRTC) |
240 | |