blob: 52282a634f617428018f6517d47e871e8d2425d6 [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,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000158 public webrtc::newapi::Transport,
159 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;
172 bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000173 bool SetSend(bool send) override;
solenberg1dd98f32015-09-10 01:57:14 -0700174 bool SetVideoSend(uint32 ssrc, bool mute,
175 const VideoOptions* options) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000176 bool AddSendStream(const StreamParams& sp) override;
177 bool RemoveSendStream(uint32 ssrc) override;
178 bool AddRecvStream(const StreamParams& sp) override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000179 bool AddRecvStream(const StreamParams& sp, bool default_stream);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000180 bool RemoveRecvStream(uint32 ssrc) override;
181 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) override;
182 bool GetStats(VideoMediaInfo* info) override;
183 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) override;
184 bool SendIntraFrame() override;
185 bool RequestIntraFrame() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000186
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000187 void OnPacketReceived(rtc::Buffer* packet,
188 const rtc::PacketTime& packet_time) override;
189 void OnRtcpReceived(rtc::Buffer* packet,
190 const rtc::PacketTime& packet_time) override;
191 void OnReadyToSend(bool ready) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 void SetInterface(NetworkInterface* iface) override;
193 void UpdateAspectRatio(int ratio_w, int ratio_h) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000194
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000195 void OnMessage(rtc::Message* msg) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000196
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 void OnLoadUpdate(Load load) override;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000198
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000199 // Implemented for VideoMediaChannelTest.
200 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000201 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000202 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
203
204 private:
solenberg1dd98f32015-09-10 01:57:14 -0700205 bool MuteStream(uint32 ssrc, bool mute);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100206 class WebRtcVideoReceiveStream;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200207
208 bool SetSendCodecs(const std::vector<VideoCodec>& codecs);
209 bool SetSendRtpHeaderExtensions(
210 const std::vector<RtpHeaderExtension>& extensions);
211 bool SetMaxSendBandwidth(int bps);
212 bool SetOptions(const VideoOptions& options);
213 bool SetRecvCodecs(const std::vector<VideoCodec>& codecs);
214 bool SetRecvRtpHeaderExtensions(
215 const std::vector<RtpHeaderExtension>& extensions);
216
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000217 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
218 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000219 bool CodecIsExternallySupported(const std::string& name) const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100220 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
221 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
222 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
223 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
224 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
225 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000226
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000227 struct VideoCodecSettings {
228 VideoCodecSettings();
Peter Boströmee0b00e2015-04-22 18:41:14 +0200229
230 bool operator==(const VideoCodecSettings& other) const;
231 bool operator!=(const VideoCodecSettings& other) const;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000232
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000233 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000234 webrtc::FecConfig fec;
235 int rtx_payload_type;
236 };
237
deadbeef874ca3a2015-08-20 17:19:20 -0700238 static std::string CodecSettingsVectorToString(
239 const std::vector<VideoCodecSettings>& codecs);
240
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000241 // Wrapper for the sender part, this is where the capturer is connected and
242 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000243 class WebRtcVideoSendStream : public sigslot::has_slots<> {
244 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000245 WebRtcVideoSendStream(
246 webrtc::Call* call,
solenberg4fbae2b2015-08-28 04:07:10 -0700247 const StreamParams& sp,
248 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000249 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000250 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100251 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000252 const Settable<VideoCodecSettings>& codec_settings,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000253 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000254 ~WebRtcVideoSendStream();
Peter Boströmd6f4c252015-03-26 16:23:04 +0100255
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000256 void SetOptions(const VideoOptions& options);
257 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000258 void SetRtpExtensions(
259 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000260
261 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
262 bool SetCapturer(VideoCapturer* capturer);
263 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000264 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000265 bool DisconnectCapturer();
266
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700267 void SetApplyRotation(bool apply_rotation);
268
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000269 void Start();
270 void Stop();
271
Peter Boströmd6f4c252015-03-26 16:23:04 +0100272 const std::vector<uint32>& GetSsrcs() const;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000273 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000274 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000275
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100276 void SetMaxBitrateBps(int max_bitrate_bps);
277
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000278 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000279 // Parameters needed to reconstruct the underlying stream.
280 // webrtc::VideoSendStream doesn't support setting a lot of options on the
281 // fly, so when those need to be changed we tear down and reconstruct with
282 // similar parameters depending on which options changed etc.
283 struct VideoSendStreamParameters {
284 VideoSendStreamParameters(
285 const webrtc::VideoSendStream::Config& config,
286 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100287 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000288 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000289 webrtc::VideoSendStream::Config config;
290 VideoOptions options;
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100291 int max_bitrate_bps;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000292 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000293 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
294 // typically changes when setting a new resolution or reconfiguring
295 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000296 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000297 };
298
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000299 struct AllocatedEncoder {
300 AllocatedEncoder(webrtc::VideoEncoder* encoder,
301 webrtc::VideoCodecType type,
Peter Boström4d71ede2015-05-19 23:09:35 +0200302 bool external);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000303 webrtc::VideoEncoder* encoder;
Peter Boström4d71ede2015-05-19 23:09:35 +0200304 webrtc::VideoEncoder* external_encoder;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000305 webrtc::VideoCodecType type;
306 bool external;
307 };
308
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000309 struct Dimensions {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +0000310 // Initial encoder configuration (QCIF, 176x144) frame (to ensure that
311 // hardware encoders can be initialized). This gives us low memory usage
312 // but also makes it so configuration errors are discovered at the time we
313 // apply the settings rather than when we get the first frame (waiting for
314 // the first frame to know that you gave a bad codec parameter could make
315 // debugging hard).
316 // TODO(pbos): Consider setting up encoders lazily.
317 Dimensions() : width(176), height(144), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000318 int width;
319 int height;
320 bool is_screencast;
321 };
322
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000323 union VideoEncoderSettings {
324 webrtc::VideoCodecVP8 vp8;
325 webrtc::VideoCodecVP9 vp9;
326 };
327
328 static std::vector<webrtc::VideoStream> CreateVideoStreams(
329 const VideoCodec& codec,
330 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100331 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000332 size_t num_streams);
333 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
334 const VideoCodec& codec,
335 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100336 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000337 size_t num_streams);
338
339 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
Erik Språng143cec12015-04-28 10:01:41 +0200340 const VideoOptions& options,
341 bool is_screencast)
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000342 EXCLUSIVE_LOCKS_REQUIRED(lock_);
343
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000344 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
345 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000346 void DestroyVideoEncoder(AllocatedEncoder* encoder)
347 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000348 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000349 const VideoOptions& options)
350 EXCLUSIVE_LOCKS_REQUIRED(lock_);
351 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000352 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
353 const Dimensions& dimensions,
354 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000355 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000356 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000357
Peter Boströmd6f4c252015-03-26 16:23:04 +0100358 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200359 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000360 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000361 WebRtcVideoEncoderFactory* const external_encoder_factory_
362 GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000363
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000364 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000365 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000366 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000367 VideoEncoderSettings encoder_settings_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000368 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000369 Dimensions last_dimensions_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000370
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000371 VideoCapturer* capturer_ GUARDED_BY(lock_);
372 bool sending_ GUARDED_BY(lock_);
373 bool muted_ GUARDED_BY(lock_);
374 VideoFormat format_ GUARDED_BY(lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +0000375 int old_adapt_changes_ GUARDED_BY(lock_);
qiangchenc27d89f2015-07-16 10:27:16 -0700376
377 // The timestamp of the first frame received
378 // Used to generate the timestamps of subsequent frames
379 int64_t first_frame_timestamp_ms_ GUARDED_BY(lock_);
380
381 // The timestamp of the last frame received
382 // Used to generate timestamp for the black frame when capturer is removed
383 int64_t last_frame_timestamp_ms_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000384 };
385
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000386 // Wrapper for the receiver part, contains configs etc. that are needed to
387 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
388 // between webrtc::VideoRenderer and cricket::VideoRenderer.
389 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
390 public:
391 WebRtcVideoReceiveStream(
Peter Boström259bd202015-05-28 13:39:50 +0200392 webrtc::Call* call,
393 const StreamParams& sp,
solenberg4fbae2b2015-08-28 04:07:10 -0700394 const webrtc::VideoReceiveStream::Config& config,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000395 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000396 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000397 const std::vector<VideoCodecSettings>& recv_codecs);
398 ~WebRtcVideoReceiveStream();
399
Peter Boströmd6f4c252015-03-26 16:23:04 +0100400 const std::vector<uint32>& GetSsrcs() const;
401
Peter Boström3548dd22015-05-22 18:48:36 +0200402 void SetLocalSsrc(uint32_t local_ssrc);
Peter Boström67c9df72015-05-11 14:34:58 +0200403 void SetNackAndRemb(bool nack_enabled, bool remb_enabled);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000404 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
405 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
406
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700407 void RenderFrame(const webrtc::VideoFrame& frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000408 int time_to_render_ms) override;
409 bool IsTextureSupported() const override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000410 bool IsDefaultStream() const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000411
412 void SetRenderer(cricket::VideoRenderer* renderer);
413 cricket::VideoRenderer* GetRenderer();
414
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000415 VideoReceiverInfo GetVideoReceiverInfo();
416
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000417 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000418 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000419 AllocatedDecoder(webrtc::VideoDecoder* decoder,
420 webrtc::VideoCodecType type,
Peter Boström7252a2b2015-05-18 19:42:03 +0200421 bool external);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000422 webrtc::VideoDecoder* decoder;
Peter Boström7252a2b2015-05-18 19:42:03 +0200423 // Decoder wrapped into a fallback decoder to permit software fallback.
424 webrtc::VideoDecoder* external_decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000425 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000426 bool external;
427 };
428
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000429 void SetSize(int width, int height);
430 void RecreateWebRtcStream();
431
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000432 AllocatedDecoder CreateOrReuseVideoDecoder(
433 std::vector<AllocatedDecoder>* old_decoder,
434 const VideoCodec& codec);
435 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000436
pbosf42376c2015-08-28 07:35:32 -0700437 std::string GetCodecNameFromPayloadType(int payload_type);
438
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000439 webrtc::Call* const call_;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100440 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200441 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000442
443 webrtc::VideoReceiveStream* stream_;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000444 const bool default_stream_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000445 webrtc::VideoReceiveStream::Config config_;
446
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000447 WebRtcVideoDecoderFactory* const external_decoder_factory_;
448 std::vector<AllocatedDecoder> allocated_decoders_;
449
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000450 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000451 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000452 int last_width_ GUARDED_BY(renderer_lock_);
453 int last_height_ GUARDED_BY(renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +0000454 // Expands remote RTP timestamps to int64_t to be able to estimate how long
455 // the stream has been running.
456 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
457 GUARDED_BY(renderer_lock_);
458 int64_t first_frame_timestamp_ GUARDED_BY(renderer_lock_);
459 // Start NTP time is estimated as current remote NTP time (estimated from
460 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
461 int64_t estimated_remote_start_ntp_time_ms_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000462 };
463
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000464 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000465 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000466
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000467 bool SendRtp(const uint8_t* data, size_t len) override;
468 bool SendRtcp(const uint8_t* data, size_t len) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000469
470 void StartAllSendStreams();
471 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000472
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000473 static std::vector<VideoCodecSettings> MapCodecs(
474 const std::vector<VideoCodec>& codecs);
475 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000476 const std::vector<VideoCodecSettings>& mapped_codecs) const;
deadbeef874ca3a2015-08-20 17:19:20 -0700477 static bool ReceiveCodecsHaveChanged(std::vector<VideoCodecSettings> before,
478 std::vector<VideoCodecSettings> after);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000479
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000480 void FillSenderStats(VideoMediaInfo* info);
481 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000482 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
483 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000484
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200485 rtc::ThreadChecker thread_checker_;
486
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000487 uint32_t rtcp_receiver_report_ssrc_;
488 bool sending_;
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200489 webrtc::Call* const call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000490
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000491 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000492
493 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
494 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000495
Peter Boströme7b221f2015-04-13 15:34:32 +0200496 // Separate list of set capturers used to signal CPU adaptation. These should
497 // not be locked while calling methods that take other locks to prevent
498 // lock-order inversions.
499 rtc::CriticalSection capturer_crit_;
500 bool signal_cpu_adaptation_ GUARDED_BY(capturer_crit_);
501 std::map<uint32, VideoCapturer*> capturers_ GUARDED_BY(capturer_crit_);
502
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000503 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000504 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000505 std::map<uint32, WebRtcVideoSendStream*> send_streams_
506 GUARDED_BY(stream_crit_);
507 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
508 GUARDED_BY(stream_crit_);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100509 std::set<uint32> send_ssrcs_ GUARDED_BY(stream_crit_);
510 std::set<uint32> receive_ssrcs_ GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000511
512 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000513 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
514
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000515 WebRtcVideoEncoderFactory* const external_encoder_factory_;
516 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000517 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000518 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000519 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000520 VideoOptions options_;
521};
522
523} // namespace cricket
524
525#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_