blob: 144d1c4cee05d287a222f9ceea2dea1cabe67b91 [file] [log] [blame]
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_
29#define TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_
30
31#include <map>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000032#include <string>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include <vector>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000034
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000035#include "talk/media/base/mediaengine.h"
36#include "talk/media/webrtc/webrtcvideochannelfactory.h"
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +000037#include "talk/media/webrtc/webrtcvideodecoderfactory.h"
38#include "talk/media/webrtc/webrtcvideoencoderfactory.h"
pbos@webrtc.org575d1262014-10-08 14:48:08 +000039#include "webrtc/base/criticalsection.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000040#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000041#include "webrtc/base/thread_annotations.h"
Fredrik Solenberg4b60c732015-05-07 14:07:48 +020042#include "webrtc/base/thread_checker.h"
pbos@webrtc.org42684be2014-10-03 11:25:45 +000043#include "webrtc/call.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000044#include "webrtc/transport.h"
Thiago Farina9bfe3da2015-04-10 12:52:13 +020045#include "webrtc/video_frame.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "webrtc/video_receive_stream.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000047#include "webrtc/video_renderer.h"
48#include "webrtc/video_send_stream.h"
49
50namespace webrtc {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000051class VideoDecoder;
52class VideoEncoder;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000053}
54
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055namespace rtc {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000056class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057} // namespace rtc
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000058
59namespace cricket {
60
61class VideoCapturer;
62class VideoFrame;
63class VideoProcessor;
64class VideoRenderer;
65class VoiceMediaChannel;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000066class WebRtcDecoderObserver;
67class WebRtcEncoderObserver;
68class WebRtcLocalStreamInfo;
69class WebRtcRenderAdapter;
70class WebRtcVideoChannelRecvInfo;
71class WebRtcVideoChannelSendInfo;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000072class WebRtcVoiceEngine;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +020073class WebRtcVoiceMediaChannel;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000074
75struct CapturedFrame;
76struct Device;
77
Peter Boström81ea54e2015-05-07 11:41:09 +020078// Exposed here for unittests.
79std::vector<VideoCodec> DefaultVideoCodecList();
80
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000081class UnsignalledSsrcHandler {
82 public:
83 enum Action {
84 kDropPacket,
85 kDeliverPacket,
86 };
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +000087 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel2* channel,
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000088 uint32_t ssrc) = 0;
89};
90
91// TODO(pbos): Remove, use external handlers only.
92class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
93 public:
94 DefaultUnsignalledSsrcHandler();
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +000095 Action OnUnsignalledSsrc(WebRtcVideoChannel2* channel,
96 uint32_t ssrc) override;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000097
98 VideoRenderer* GetDefaultRenderer() const;
99 void SetDefaultRenderer(VideoMediaChannel* channel, VideoRenderer* renderer);
100
101 private:
102 uint32_t default_recv_ssrc_;
103 VideoRenderer* default_renderer_;
104};
105
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000106// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200107class WebRtcVideoEngine2 {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000108 public:
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200109 WebRtcVideoEngine2();
110 ~WebRtcVideoEngine2();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000111
112 // Basic video engine implementation.
Fredrik Solenberg9a416bd2015-05-22 09:04:09 +0200113 void Init();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000114
115 int GetCapabilities();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000116 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000117
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200118 WebRtcVideoChannel2* CreateChannel(webrtc::Call* call,
119 const VideoOptions& options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000120
121 const std::vector<VideoCodec>& codecs() const;
122 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
123 void SetLogging(int min_sev, const char* filter);
124
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000125 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
126 // not take the ownership of |decoder_factory|. The caller needs to make sure
127 // that |decoder_factory| outlives the video engine.
128 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
129 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
130 // not take the ownership of |encoder_factory|. The caller needs to make sure
131 // that |encoder_factory| outlives the video engine.
132 virtual void SetExternalEncoderFactory(
133 WebRtcVideoEncoderFactory* encoder_factory);
134
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000135 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000136
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000137 bool FindCodec(const VideoCodec& in);
138 bool CanSendCodec(const VideoCodec& in,
139 const VideoCodec& current,
140 VideoCodec* out);
141 // Check whether the supplied trace should be ignored.
142 bool ShouldIgnoreTrace(const std::string& trace);
143
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000144 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000145 std::vector<VideoCodec> GetSupportedCodecs() const;
146
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000147 std::vector<VideoCodec> video_codecs_;
148 std::vector<RtpHeaderExtension> rtp_header_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000149
150 bool initialized_;
151
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000152 WebRtcVideoDecoderFactory* external_decoder_factory_;
153 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000154 rtc::scoped_ptr<WebRtcVideoEncoderFactory> simulcast_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000155};
156
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000157class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000158 public VideoMediaChannel,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000159 public webrtc::newapi::Transport,
160 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000161 public:
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200162 WebRtcVideoChannel2(webrtc::Call* call,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000163 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000164 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000165 WebRtcVideoDecoderFactory* external_decoder_factory);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200166 ~WebRtcVideoChannel2() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000167
168 // VideoMediaChannel implementation
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700169 bool SetSendParameters(const VideoSendParameters& params) override;
170 bool SetRecvParameters(const VideoRecvParameters& params) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000171 bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) override;
172 bool SetSendCodecs(const std::vector<VideoCodec>& codecs) override;
173 bool GetSendCodec(VideoCodec* send_codec) override;
174 bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) override;
175 bool SetRender(bool render) override;
176 bool SetSend(bool send) override;
solenberg1dd98f32015-09-10 01:57:14 -0700177 bool SetVideoSend(uint32 ssrc, bool mute,
178 const VideoOptions* options) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 bool AddSendStream(const StreamParams& sp) override;
180 bool RemoveSendStream(uint32 ssrc) override;
181 bool AddRecvStream(const StreamParams& sp) override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000182 bool AddRecvStream(const StreamParams& sp, bool default_stream);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000183 bool RemoveRecvStream(uint32 ssrc) override;
184 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) override;
185 bool GetStats(VideoMediaInfo* info) override;
186 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) override;
187 bool SendIntraFrame() override;
188 bool RequestIntraFrame() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000189
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000190 void OnPacketReceived(rtc::Buffer* packet,
191 const rtc::PacketTime& packet_time) override;
192 void OnRtcpReceived(rtc::Buffer* packet,
193 const rtc::PacketTime& packet_time) override;
194 void OnReadyToSend(bool ready) override;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000195
196 // Set send/receive RTP header extensions. This must be done before creating
197 // streams as it only has effect on future streams.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 bool SetRecvRtpHeaderExtensions(
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000199 const std::vector<RtpHeaderExtension>& extensions) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000200 bool SetSendRtpHeaderExtensions(
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000201 const std::vector<RtpHeaderExtension>& extensions) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000202 bool SetMaxSendBandwidth(int bps) override;
203 bool SetOptions(const VideoOptions& options) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000204 void SetInterface(NetworkInterface* iface) override;
205 void UpdateAspectRatio(int ratio_w, int ratio_h) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000206
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000207 void OnMessage(rtc::Message* msg) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000208
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000209 void OnLoadUpdate(Load load) override;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000210
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000211 // Implemented for VideoMediaChannelTest.
212 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000213 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000214 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
215
216 private:
solenberg1dd98f32015-09-10 01:57:14 -0700217 bool MuteStream(uint32 ssrc, bool mute);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100218 class WebRtcVideoReceiveStream;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000219 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
220 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000221 bool CodecIsExternallySupported(const std::string& name) const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100222 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
223 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
224 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
225 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
226 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
227 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000228
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000229 struct VideoCodecSettings {
230 VideoCodecSettings();
Peter Boströmee0b00e2015-04-22 18:41:14 +0200231
232 bool operator==(const VideoCodecSettings& other) const;
233 bool operator!=(const VideoCodecSettings& other) const;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000234
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000235 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000236 webrtc::FecConfig fec;
237 int rtx_payload_type;
238 };
239
deadbeef874ca3a2015-08-20 17:19:20 -0700240 static std::string CodecSettingsVectorToString(
241 const std::vector<VideoCodecSettings>& codecs);
242
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000243 // Wrapper for the sender part, this is where the capturer is connected and
244 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000245 class WebRtcVideoSendStream : public sigslot::has_slots<> {
246 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000247 WebRtcVideoSendStream(
248 webrtc::Call* call,
solenberg4fbae2b2015-08-28 04:07:10 -0700249 const StreamParams& sp,
250 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000251 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000252 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100253 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000254 const Settable<VideoCodecSettings>& codec_settings,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000255 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000256 ~WebRtcVideoSendStream();
Peter Boströmd6f4c252015-03-26 16:23:04 +0100257
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000258 void SetOptions(const VideoOptions& options);
259 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000260 void SetRtpExtensions(
261 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000262
263 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
264 bool SetCapturer(VideoCapturer* capturer);
265 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000266 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000267 bool DisconnectCapturer();
268
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700269 void SetApplyRotation(bool apply_rotation);
270
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000271 void Start();
272 void Stop();
273
Peter Boströmd6f4c252015-03-26 16:23:04 +0100274 const std::vector<uint32>& GetSsrcs() const;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000275 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000276 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000277
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100278 void SetMaxBitrateBps(int max_bitrate_bps);
279
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000280 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000281 // Parameters needed to reconstruct the underlying stream.
282 // webrtc::VideoSendStream doesn't support setting a lot of options on the
283 // fly, so when those need to be changed we tear down and reconstruct with
284 // similar parameters depending on which options changed etc.
285 struct VideoSendStreamParameters {
286 VideoSendStreamParameters(
287 const webrtc::VideoSendStream::Config& config,
288 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100289 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000290 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000291 webrtc::VideoSendStream::Config config;
292 VideoOptions options;
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100293 int max_bitrate_bps;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000294 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000295 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
296 // typically changes when setting a new resolution or reconfiguring
297 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000298 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000299 };
300
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000301 struct AllocatedEncoder {
302 AllocatedEncoder(webrtc::VideoEncoder* encoder,
303 webrtc::VideoCodecType type,
Peter Boström4d71ede2015-05-19 23:09:35 +0200304 bool external);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000305 webrtc::VideoEncoder* encoder;
Peter Boström4d71ede2015-05-19 23:09:35 +0200306 webrtc::VideoEncoder* external_encoder;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000307 webrtc::VideoCodecType type;
308 bool external;
309 };
310
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000311 struct Dimensions {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +0000312 // Initial encoder configuration (QCIF, 176x144) frame (to ensure that
313 // hardware encoders can be initialized). This gives us low memory usage
314 // but also makes it so configuration errors are discovered at the time we
315 // apply the settings rather than when we get the first frame (waiting for
316 // the first frame to know that you gave a bad codec parameter could make
317 // debugging hard).
318 // TODO(pbos): Consider setting up encoders lazily.
319 Dimensions() : width(176), height(144), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000320 int width;
321 int height;
322 bool is_screencast;
323 };
324
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000325 union VideoEncoderSettings {
326 webrtc::VideoCodecVP8 vp8;
327 webrtc::VideoCodecVP9 vp9;
328 };
329
330 static std::vector<webrtc::VideoStream> CreateVideoStreams(
331 const VideoCodec& codec,
332 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100333 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000334 size_t num_streams);
335 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
336 const VideoCodec& codec,
337 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100338 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000339 size_t num_streams);
340
341 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
Erik Språng143cec12015-04-28 10:01:41 +0200342 const VideoOptions& options,
343 bool is_screencast)
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000344 EXCLUSIVE_LOCKS_REQUIRED(lock_);
345
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000346 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
347 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000348 void DestroyVideoEncoder(AllocatedEncoder* encoder)
349 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000350 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000351 const VideoOptions& options)
352 EXCLUSIVE_LOCKS_REQUIRED(lock_);
353 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000354 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
355 const Dimensions& dimensions,
356 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000357 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000358 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000359
Peter Boströmd6f4c252015-03-26 16:23:04 +0100360 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200361 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000362 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000363 WebRtcVideoEncoderFactory* const external_encoder_factory_
364 GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000365
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000367 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000368 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000369 VideoEncoderSettings encoder_settings_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000370 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000371 Dimensions last_dimensions_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000372
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000373 VideoCapturer* capturer_ GUARDED_BY(lock_);
374 bool sending_ GUARDED_BY(lock_);
375 bool muted_ GUARDED_BY(lock_);
376 VideoFormat format_ GUARDED_BY(lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +0000377 int old_adapt_changes_ GUARDED_BY(lock_);
qiangchenc27d89f2015-07-16 10:27:16 -0700378
379 // The timestamp of the first frame received
380 // Used to generate the timestamps of subsequent frames
381 int64_t first_frame_timestamp_ms_ GUARDED_BY(lock_);
382
383 // The timestamp of the last frame received
384 // Used to generate timestamp for the black frame when capturer is removed
385 int64_t last_frame_timestamp_ms_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000386 };
387
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000388 // Wrapper for the receiver part, contains configs etc. that are needed to
389 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
390 // between webrtc::VideoRenderer and cricket::VideoRenderer.
391 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
392 public:
393 WebRtcVideoReceiveStream(
Peter Boström259bd202015-05-28 13:39:50 +0200394 webrtc::Call* call,
395 const StreamParams& sp,
solenberg4fbae2b2015-08-28 04:07:10 -0700396 const webrtc::VideoReceiveStream::Config& config,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000397 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000398 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000399 const std::vector<VideoCodecSettings>& recv_codecs);
400 ~WebRtcVideoReceiveStream();
401
Peter Boströmd6f4c252015-03-26 16:23:04 +0100402 const std::vector<uint32>& GetSsrcs() const;
403
Peter Boström3548dd22015-05-22 18:48:36 +0200404 void SetLocalSsrc(uint32_t local_ssrc);
Peter Boström67c9df72015-05-11 14:34:58 +0200405 void SetNackAndRemb(bool nack_enabled, bool remb_enabled);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000406 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
407 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
408
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700409 void RenderFrame(const webrtc::VideoFrame& frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000410 int time_to_render_ms) override;
411 bool IsTextureSupported() const override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000412 bool IsDefaultStream() const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000413
414 void SetRenderer(cricket::VideoRenderer* renderer);
415 cricket::VideoRenderer* GetRenderer();
416
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000417 VideoReceiverInfo GetVideoReceiverInfo();
418
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000419 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000420 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000421 AllocatedDecoder(webrtc::VideoDecoder* decoder,
422 webrtc::VideoCodecType type,
Peter Boström7252a2b2015-05-18 19:42:03 +0200423 bool external);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000424 webrtc::VideoDecoder* decoder;
Peter Boström7252a2b2015-05-18 19:42:03 +0200425 // Decoder wrapped into a fallback decoder to permit software fallback.
426 webrtc::VideoDecoder* external_decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000427 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000428 bool external;
429 };
430
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000431 void SetSize(int width, int height);
432 void RecreateWebRtcStream();
433
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000434 AllocatedDecoder CreateOrReuseVideoDecoder(
435 std::vector<AllocatedDecoder>* old_decoder,
436 const VideoCodec& codec);
437 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000438
pbosf42376c2015-08-28 07:35:32 -0700439 std::string GetCodecNameFromPayloadType(int payload_type);
440
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000441 webrtc::Call* const call_;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100442 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200443 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000444
445 webrtc::VideoReceiveStream* stream_;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000446 const bool default_stream_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000447 webrtc::VideoReceiveStream::Config config_;
448
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000449 WebRtcVideoDecoderFactory* const external_decoder_factory_;
450 std::vector<AllocatedDecoder> allocated_decoders_;
451
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000452 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000453 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000454 int last_width_ GUARDED_BY(renderer_lock_);
455 int last_height_ GUARDED_BY(renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +0000456 // Expands remote RTP timestamps to int64_t to be able to estimate how long
457 // the stream has been running.
458 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
459 GUARDED_BY(renderer_lock_);
460 int64_t first_frame_timestamp_ GUARDED_BY(renderer_lock_);
461 // Start NTP time is estimated as current remote NTP time (estimated from
462 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
463 int64_t estimated_remote_start_ntp_time_ms_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000464 };
465
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000466 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000467 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000468
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000469 bool SendRtp(const uint8_t* data, size_t len) override;
470 bool SendRtcp(const uint8_t* data, size_t len) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000471
472 void StartAllSendStreams();
473 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000474
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000475 static std::vector<VideoCodecSettings> MapCodecs(
476 const std::vector<VideoCodec>& codecs);
477 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000478 const std::vector<VideoCodecSettings>& mapped_codecs) const;
deadbeef874ca3a2015-08-20 17:19:20 -0700479 static bool ReceiveCodecsHaveChanged(std::vector<VideoCodecSettings> before,
480 std::vector<VideoCodecSettings> after);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000481
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000482 void FillSenderStats(VideoMediaInfo* info);
483 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000484 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
485 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000486
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200487 rtc::ThreadChecker thread_checker_;
488
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000489 uint32_t rtcp_receiver_report_ssrc_;
490 bool sending_;
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200491 webrtc::Call* const call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000492
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000493 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000494
495 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
496 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000497
Peter Boströme7b221f2015-04-13 15:34:32 +0200498 // Separate list of set capturers used to signal CPU adaptation. These should
499 // not be locked while calling methods that take other locks to prevent
500 // lock-order inversions.
501 rtc::CriticalSection capturer_crit_;
502 bool signal_cpu_adaptation_ GUARDED_BY(capturer_crit_);
503 std::map<uint32, VideoCapturer*> capturers_ GUARDED_BY(capturer_crit_);
504
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000505 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000506 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000507 std::map<uint32, WebRtcVideoSendStream*> send_streams_
508 GUARDED_BY(stream_crit_);
509 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
510 GUARDED_BY(stream_crit_);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100511 std::set<uint32> send_ssrcs_ GUARDED_BY(stream_crit_);
512 std::set<uint32> receive_ssrcs_ GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000513
514 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000515 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
516
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000517 WebRtcVideoEncoderFactory* const external_encoder_factory_;
518 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000519 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000520 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000521 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000522 VideoOptions options_;
523};
524
525} // namespace cricket
526
527#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_