blob: 7096135cdd5bd33992a6cce5eece031165145802 [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
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000115 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000116
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200117 WebRtcVideoChannel2* CreateChannel(webrtc::Call* call,
118 const VideoOptions& options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000119
120 const std::vector<VideoCodec>& codecs() const;
121 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
122 void SetLogging(int min_sev, const char* filter);
123
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000124 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
125 // not take the ownership of |decoder_factory|. The caller needs to make sure
126 // that |decoder_factory| outlives the video engine.
127 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
128 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
129 // not take the ownership of |encoder_factory|. The caller needs to make sure
130 // that |encoder_factory| outlives the video engine.
131 virtual void SetExternalEncoderFactory(
132 WebRtcVideoEncoderFactory* encoder_factory);
133
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000135
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000136 bool FindCodec(const VideoCodec& in);
137 bool CanSendCodec(const VideoCodec& in,
138 const VideoCodec& current,
139 VideoCodec* out);
140 // Check whether the supplied trace should be ignored.
141 bool ShouldIgnoreTrace(const std::string& trace);
142
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000143 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000144 std::vector<VideoCodec> GetSupportedCodecs() const;
145
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000146 std::vector<VideoCodec> video_codecs_;
147 std::vector<RtpHeaderExtension> rtp_header_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000148
149 bool initialized_;
150
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000151 WebRtcVideoDecoderFactory* external_decoder_factory_;
152 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000153 rtc::scoped_ptr<WebRtcVideoEncoderFactory> simulcast_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000154};
155
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000156class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000157 public VideoMediaChannel,
pbos2d566682015-09-28 09:59:31 -0700158 public webrtc::Transport,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000159 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000160 public:
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200161 WebRtcVideoChannel2(webrtc::Call* call,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000162 const VideoOptions& options,
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200163 const std::vector<VideoCodec>& recv_codecs,
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 GetSendCodec(VideoCodec* send_codec) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200172 bool SetSendStreamFormat(uint32_t ssrc, const VideoFormat& format) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000173 bool SetSend(bool send) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174 bool SetVideoSend(uint32_t ssrc,
175 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -0700176 const VideoOptions* options) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000177 bool AddSendStream(const StreamParams& sp) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200178 bool RemoveSendStream(uint32_t ssrc) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 bool AddRecvStream(const StreamParams& sp) override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000180 bool AddRecvStream(const StreamParams& sp, bool default_stream);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200181 bool RemoveRecvStream(uint32_t ssrc) override;
182 bool SetRenderer(uint32_t ssrc, VideoRenderer* renderer) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000183 bool GetStats(VideoMediaInfo* info) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200184 bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000185 bool SendIntraFrame() override;
186 bool RequestIntraFrame() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000187
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000188 void OnPacketReceived(rtc::Buffer* packet,
189 const rtc::PacketTime& packet_time) override;
190 void OnRtcpReceived(rtc::Buffer* packet,
191 const rtc::PacketTime& packet_time) override;
192 void OnReadyToSend(bool ready) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 void SetInterface(NetworkInterface* iface) override;
194 void UpdateAspectRatio(int ratio_w, int ratio_h) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000195
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000196 void OnMessage(rtc::Message* msg) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000197
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 void OnLoadUpdate(Load load) override;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000199
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000200 // Implemented for VideoMediaChannelTest.
201 bool sending() const { return sending_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200202 uint32_t GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
203 bool GetRenderer(uint32_t ssrc, VideoRenderer** renderer);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000204
205 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200206 bool MuteStream(uint32_t ssrc, bool mute);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100207 class WebRtcVideoReceiveStream;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200208
209 bool SetSendCodecs(const std::vector<VideoCodec>& codecs);
210 bool SetSendRtpHeaderExtensions(
211 const std::vector<RtpHeaderExtension>& extensions);
212 bool SetMaxSendBandwidth(int bps);
213 bool SetOptions(const VideoOptions& options);
214 bool SetRecvCodecs(const std::vector<VideoCodec>& codecs);
215 bool SetRecvRtpHeaderExtensions(
216 const std::vector<RtpHeaderExtension>& extensions);
217
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000218 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
219 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000220 bool CodecIsExternallySupported(const std::string& name) const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100221 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
222 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
223 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
224 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
225 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
226 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000227
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000228 struct VideoCodecSettings {
229 VideoCodecSettings();
Peter Boströmee0b00e2015-04-22 18:41:14 +0200230
231 bool operator==(const VideoCodecSettings& other) const;
232 bool operator!=(const VideoCodecSettings& other) const;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000233
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000234 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000235 webrtc::FecConfig fec;
236 int rtx_payload_type;
237 };
238
deadbeef874ca3a2015-08-20 17:19:20 -0700239 static std::string CodecSettingsVectorToString(
240 const std::vector<VideoCodecSettings>& codecs);
241
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000242 // Wrapper for the sender part, this is where the capturer is connected and
243 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000244 class WebRtcVideoSendStream : public sigslot::has_slots<> {
245 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000246 WebRtcVideoSendStream(
247 webrtc::Call* call,
solenberg4fbae2b2015-08-28 04:07:10 -0700248 const StreamParams& sp,
249 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000250 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000251 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100252 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000253 const Settable<VideoCodecSettings>& codec_settings,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000254 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000255 ~WebRtcVideoSendStream();
Peter Boströmd6f4c252015-03-26 16:23:04 +0100256
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000257 void SetOptions(const VideoOptions& options);
258 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000259 void SetRtpExtensions(
260 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000261
262 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
263 bool SetCapturer(VideoCapturer* capturer);
264 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000265 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000266 bool DisconnectCapturer();
267
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700268 void SetApplyRotation(bool apply_rotation);
269
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000270 void Start();
271 void Stop();
272
Peter Boström0c4e06b2015-10-07 12:23:21 +0200273 const std::vector<uint32_t>& GetSsrcs() const;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000274 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000275 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000276
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100277 void SetMaxBitrateBps(int max_bitrate_bps);
278
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000279 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000280 // Parameters needed to reconstruct the underlying stream.
281 // webrtc::VideoSendStream doesn't support setting a lot of options on the
282 // fly, so when those need to be changed we tear down and reconstruct with
283 // similar parameters depending on which options changed etc.
284 struct VideoSendStreamParameters {
285 VideoSendStreamParameters(
286 const webrtc::VideoSendStream::Config& config,
287 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100288 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000289 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000290 webrtc::VideoSendStream::Config config;
291 VideoOptions options;
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100292 int max_bitrate_bps;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000293 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000294 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
295 // typically changes when setting a new resolution or reconfiguring
296 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000297 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000298 };
299
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000300 struct AllocatedEncoder {
301 AllocatedEncoder(webrtc::VideoEncoder* encoder,
302 webrtc::VideoCodecType type,
Peter Boström4d71ede2015-05-19 23:09:35 +0200303 bool external);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000304 webrtc::VideoEncoder* encoder;
Peter Boström4d71ede2015-05-19 23:09:35 +0200305 webrtc::VideoEncoder* external_encoder;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000306 webrtc::VideoCodecType type;
307 bool external;
308 };
309
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000310 struct Dimensions {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +0000311 // Initial encoder configuration (QCIF, 176x144) frame (to ensure that
312 // hardware encoders can be initialized). This gives us low memory usage
313 // but also makes it so configuration errors are discovered at the time we
314 // apply the settings rather than when we get the first frame (waiting for
315 // the first frame to know that you gave a bad codec parameter could make
316 // debugging hard).
317 // TODO(pbos): Consider setting up encoders lazily.
318 Dimensions() : width(176), height(144), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000319 int width;
320 int height;
321 bool is_screencast;
322 };
323
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000324 union VideoEncoderSettings {
325 webrtc::VideoCodecVP8 vp8;
326 webrtc::VideoCodecVP9 vp9;
327 };
328
329 static std::vector<webrtc::VideoStream> CreateVideoStreams(
330 const VideoCodec& codec,
331 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100332 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000333 size_t num_streams);
334 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
335 const VideoCodec& codec,
336 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100337 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000338 size_t num_streams);
339
340 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
Erik Språng143cec12015-04-28 10:01:41 +0200341 const VideoOptions& options,
342 bool is_screencast)
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000343 EXCLUSIVE_LOCKS_REQUIRED(lock_);
344
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000345 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
346 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000347 void DestroyVideoEncoder(AllocatedEncoder* encoder)
348 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000349 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000350 const VideoOptions& options)
351 EXCLUSIVE_LOCKS_REQUIRED(lock_);
352 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000353 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
354 const Dimensions& dimensions,
355 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000356 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000357 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000358
Peter Boström0c4e06b2015-10-07 12:23:21 +0200359 const std::vector<uint32_t> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200360 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000361 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000362 WebRtcVideoEncoderFactory* const external_encoder_factory_
363 GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000364
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000366 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000367 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000368 VideoEncoderSettings encoder_settings_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000369 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000370 Dimensions last_dimensions_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000371
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000372 VideoCapturer* capturer_ GUARDED_BY(lock_);
373 bool sending_ GUARDED_BY(lock_);
374 bool muted_ GUARDED_BY(lock_);
375 VideoFormat format_ GUARDED_BY(lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +0000376 int old_adapt_changes_ GUARDED_BY(lock_);
qiangchenc27d89f2015-07-16 10:27:16 -0700377
378 // The timestamp of the first frame received
379 // Used to generate the timestamps of subsequent frames
380 int64_t first_frame_timestamp_ms_ GUARDED_BY(lock_);
381
382 // The timestamp of the last frame received
383 // Used to generate timestamp for the black frame when capturer is removed
384 int64_t last_frame_timestamp_ms_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000385 };
386
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000387 // Wrapper for the receiver part, contains configs etc. that are needed to
388 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
389 // between webrtc::VideoRenderer and cricket::VideoRenderer.
390 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
391 public:
392 WebRtcVideoReceiveStream(
Peter Boström259bd202015-05-28 13:39:50 +0200393 webrtc::Call* call,
394 const StreamParams& sp,
solenberg4fbae2b2015-08-28 04:07:10 -0700395 const webrtc::VideoReceiveStream::Config& config,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000396 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000397 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000398 const std::vector<VideoCodecSettings>& recv_codecs);
399 ~WebRtcVideoReceiveStream();
400
Peter Boström0c4e06b2015-10-07 12:23:21 +0200401 const std::vector<uint32_t>& GetSsrcs() const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100402
Peter Boström3548dd22015-05-22 18:48:36 +0200403 void SetLocalSsrc(uint32_t local_ssrc);
Peter Boström67c9df72015-05-11 14:34:58 +0200404 void SetNackAndRemb(bool nack_enabled, bool remb_enabled);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000405 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
406 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
407
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700408 void RenderFrame(const webrtc::VideoFrame& frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000409 int time_to_render_ms) override;
410 bool IsTextureSupported() const override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000411 bool IsDefaultStream() const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000412
413 void SetRenderer(cricket::VideoRenderer* renderer);
414 cricket::VideoRenderer* GetRenderer();
415
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000416 VideoReceiverInfo GetVideoReceiverInfo();
417
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000418 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000419 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000420 AllocatedDecoder(webrtc::VideoDecoder* decoder,
421 webrtc::VideoCodecType type,
Peter Boström7252a2b2015-05-18 19:42:03 +0200422 bool external);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000423 webrtc::VideoDecoder* decoder;
Peter Boström7252a2b2015-05-18 19:42:03 +0200424 // Decoder wrapped into a fallback decoder to permit software fallback.
425 webrtc::VideoDecoder* external_decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000426 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000427 bool external;
428 };
429
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000430 void SetSize(int width, int height);
431 void RecreateWebRtcStream();
432
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000433 AllocatedDecoder CreateOrReuseVideoDecoder(
434 std::vector<AllocatedDecoder>* old_decoder,
435 const VideoCodec& codec);
436 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000437
pbosf42376c2015-08-28 07:35:32 -0700438 std::string GetCodecNameFromPayloadType(int payload_type);
439
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000440 webrtc::Call* const call_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200441 const std::vector<uint32_t> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200442 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000443
444 webrtc::VideoReceiveStream* stream_;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000445 const bool default_stream_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000446 webrtc::VideoReceiveStream::Config config_;
447
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000448 WebRtcVideoDecoderFactory* const external_decoder_factory_;
449 std::vector<AllocatedDecoder> allocated_decoders_;
450
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000451 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000452 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000453 int last_width_ GUARDED_BY(renderer_lock_);
454 int last_height_ GUARDED_BY(renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +0000455 // Expands remote RTP timestamps to int64_t to be able to estimate how long
456 // the stream has been running.
457 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
458 GUARDED_BY(renderer_lock_);
459 int64_t first_frame_timestamp_ GUARDED_BY(renderer_lock_);
460 // Start NTP time is estimated as current remote NTP time (estimated from
461 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
462 int64_t estimated_remote_start_ntp_time_ms_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000463 };
464
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000465 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000466 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000467
stefan1d8a5062015-10-02 03:39:33 -0700468 bool SendRtp(const uint8_t* data,
469 size_t len,
470 const webrtc::PacketOptions& options) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000471 bool SendRtcp(const uint8_t* data, size_t len) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000472
473 void StartAllSendStreams();
474 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000475
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000476 static std::vector<VideoCodecSettings> MapCodecs(
477 const std::vector<VideoCodec>& codecs);
478 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000479 const std::vector<VideoCodecSettings>& mapped_codecs) const;
deadbeef874ca3a2015-08-20 17:19:20 -0700480 static bool ReceiveCodecsHaveChanged(std::vector<VideoCodecSettings> before,
481 std::vector<VideoCodecSettings> after);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000482
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000483 void FillSenderStats(VideoMediaInfo* info);
484 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000485 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
486 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000487
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200488 rtc::ThreadChecker thread_checker_;
489
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000490 uint32_t rtcp_receiver_report_ssrc_;
491 bool sending_;
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200492 webrtc::Call* const call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000493
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000494 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000495
496 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
497 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000498
Peter Boströme7b221f2015-04-13 15:34:32 +0200499 // Separate list of set capturers used to signal CPU adaptation. These should
500 // not be locked while calling methods that take other locks to prevent
501 // lock-order inversions.
502 rtc::CriticalSection capturer_crit_;
503 bool signal_cpu_adaptation_ GUARDED_BY(capturer_crit_);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200504 std::map<uint32_t, VideoCapturer*> capturers_ GUARDED_BY(capturer_crit_);
Peter Boströme7b221f2015-04-13 15:34:32 +0200505
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000506 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000507 // Using primary-ssrc (first ssrc) as key.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200508 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000509 GUARDED_BY(stream_crit_);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200510 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000511 GUARDED_BY(stream_crit_);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200512 std::set<uint32_t> send_ssrcs_ GUARDED_BY(stream_crit_);
513 std::set<uint32_t> receive_ssrcs_ GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000514
515 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000516 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
517
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000518 WebRtcVideoEncoderFactory* const external_encoder_factory_;
519 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000520 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000521 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000522 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000523 VideoOptions options_;
524};
525
526} // namespace cricket
527
528#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_