blob: 2b90953459fb849387925243ca4292ff94c579a0 [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.org42684be2014-10-03 11:25:45 +0000106// CallFactory, overridden for testing to verify that webrtc::Call is configured
107// properly.
108class WebRtcCallFactory {
109 public:
110 virtual ~WebRtcCallFactory();
111 virtual webrtc::Call* CreateCall(const webrtc::Call::Config& config);
112};
113
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000114// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
buildbot@webrtc.org3c16d8b2014-10-13 06:35:10 +0000115class WebRtcVideoEngine2 : public sigslot::has_slots<> {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000116 public:
pbos@webrtc.orgf1f0d9a2015-03-02 13:30:15 +0000117 explicit WebRtcVideoEngine2(WebRtcVoiceEngine* voice_engine);
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000118 virtual ~WebRtcVideoEngine2();
119
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000120 // Used for testing to be able to check and use the webrtc::Call config.
121 void SetCallFactory(WebRtcCallFactory* call_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000122
123 // Basic video engine implementation.
Fredrik Solenberg9a416bd2015-05-22 09:04:09 +0200124 void Init();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000125
126 int GetCapabilities();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000127 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000128
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000129 WebRtcVideoChannel2* CreateChannel(const VideoOptions& options,
130 VoiceMediaChannel* voice_channel);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000131
132 const std::vector<VideoCodec>& codecs() const;
133 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
134 void SetLogging(int min_sev, const char* filter);
135
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000136 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
137 // not take the ownership of |decoder_factory|. The caller needs to make sure
138 // that |decoder_factory| outlives the video engine.
139 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
140 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
141 // not take the ownership of |encoder_factory|. The caller needs to make sure
142 // that |encoder_factory| outlives the video engine.
143 virtual void SetExternalEncoderFactory(
144 WebRtcVideoEncoderFactory* encoder_factory);
145
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000146 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000147
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000148 bool FindCodec(const VideoCodec& in);
149 bool CanSendCodec(const VideoCodec& in,
150 const VideoCodec& current,
151 VideoCodec* out);
152 // Check whether the supplied trace should be ignored.
153 bool ShouldIgnoreTrace(const std::string& trace);
154
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000155 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000156 std::vector<VideoCodec> GetSupportedCodecs() const;
157
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000158 WebRtcVoiceEngine* voice_engine_;
159 std::vector<VideoCodec> video_codecs_;
160 std::vector<RtpHeaderExtension> rtp_header_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000161
162 bool initialized_;
163
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000164 WebRtcCallFactory default_call_factory_;
165 WebRtcCallFactory* call_factory_;
166
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000167 WebRtcVideoDecoderFactory* external_decoder_factory_;
168 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000169 rtc::scoped_ptr<WebRtcVideoEncoderFactory> simulcast_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000170};
171
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000173 public VideoMediaChannel,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000174 public webrtc::newapi::Transport,
175 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000176 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000177 WebRtcVideoChannel2(WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000178 WebRtcVoiceEngine* voice_engine,
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200179 WebRtcVoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000180 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000181 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000182 WebRtcVideoDecoderFactory* external_decoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000183 ~WebRtcVideoChannel2();
184 bool Init();
185
186 // VideoMediaChannel implementation
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200187 void DetachVoiceChannel() override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700188 bool SetSendParameters(const VideoSendParameters& params) override;
189 bool SetRecvParameters(const VideoRecvParameters& params) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000190 bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) override;
191 bool SetSendCodecs(const std::vector<VideoCodec>& codecs) override;
192 bool GetSendCodec(VideoCodec* send_codec) override;
193 bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) override;
194 bool SetRender(bool render) override;
195 bool SetSend(bool send) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000196
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 bool AddSendStream(const StreamParams& sp) override;
198 bool RemoveSendStream(uint32 ssrc) override;
199 bool AddRecvStream(const StreamParams& sp) override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000200 bool AddRecvStream(const StreamParams& sp, bool default_stream);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000201 bool RemoveRecvStream(uint32 ssrc) override;
202 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) override;
203 bool GetStats(VideoMediaInfo* info) override;
204 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) override;
205 bool SendIntraFrame() override;
206 bool RequestIntraFrame() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000207
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000208 void OnPacketReceived(rtc::Buffer* packet,
209 const rtc::PacketTime& packet_time) override;
210 void OnRtcpReceived(rtc::Buffer* packet,
211 const rtc::PacketTime& packet_time) override;
212 void OnReadyToSend(bool ready) override;
213 bool MuteStream(uint32 ssrc, bool mute) override;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000214
215 // Set send/receive RTP header extensions. This must be done before creating
216 // streams as it only has effect on future streams.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000217 bool SetRecvRtpHeaderExtensions(
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000218 const std::vector<RtpHeaderExtension>& extensions) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000219 bool SetSendRtpHeaderExtensions(
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000220 const std::vector<RtpHeaderExtension>& extensions) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000221 bool SetMaxSendBandwidth(int bps) override;
222 bool SetOptions(const VideoOptions& options) override;
223 bool GetOptions(VideoOptions* options) const override {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000224 *options = options_;
225 return true;
226 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000227 void SetInterface(NetworkInterface* iface) override;
228 void UpdateAspectRatio(int ratio_w, int ratio_h) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000229
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000230 void OnMessage(rtc::Message* msg) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000231
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000232 void OnLoadUpdate(Load load) override;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000233
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000234 // Implemented for VideoMediaChannelTest.
235 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000236 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000237 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
238
239 private:
Peter Boströmd6f4c252015-03-26 16:23:04 +0100240 class WebRtcVideoReceiveStream;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000241 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
242 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000243 bool CodecIsExternallySupported(const std::string& name) const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100244 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
245 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
246 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
247 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
248 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
249 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000250
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000251 struct VideoCodecSettings {
252 VideoCodecSettings();
Peter Boströmee0b00e2015-04-22 18:41:14 +0200253
254 bool operator==(const VideoCodecSettings& other) const;
255 bool operator!=(const VideoCodecSettings& other) const;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000256
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000257 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000258 webrtc::FecConfig fec;
259 int rtx_payload_type;
260 };
261
deadbeef874ca3a2015-08-20 17:19:20 -0700262 static std::string CodecSettingsVectorToString(
263 const std::vector<VideoCodecSettings>& codecs);
264
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000265 // Wrapper for the sender part, this is where the capturer is connected and
266 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000267 class WebRtcVideoSendStream : public sigslot::has_slots<> {
268 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000269 WebRtcVideoSendStream(
270 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000271 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000272 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100273 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000274 const Settable<VideoCodecSettings>& codec_settings,
275 const StreamParams& sp,
276 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000277 ~WebRtcVideoSendStream();
Peter Boströmd6f4c252015-03-26 16:23:04 +0100278
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000279 void SetOptions(const VideoOptions& options);
280 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000281 void SetRtpExtensions(
282 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000283
284 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
285 bool SetCapturer(VideoCapturer* capturer);
286 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000287 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000288 bool DisconnectCapturer();
289
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700290 void SetApplyRotation(bool apply_rotation);
291
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000292 void Start();
293 void Stop();
294
Peter Boströmd6f4c252015-03-26 16:23:04 +0100295 const std::vector<uint32>& GetSsrcs() const;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000296 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000297 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000298
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100299 void SetMaxBitrateBps(int max_bitrate_bps);
300
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000301 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000302 // Parameters needed to reconstruct the underlying stream.
303 // webrtc::VideoSendStream doesn't support setting a lot of options on the
304 // fly, so when those need to be changed we tear down and reconstruct with
305 // similar parameters depending on which options changed etc.
306 struct VideoSendStreamParameters {
307 VideoSendStreamParameters(
308 const webrtc::VideoSendStream::Config& config,
309 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100310 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000311 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000312 webrtc::VideoSendStream::Config config;
313 VideoOptions options;
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100314 int max_bitrate_bps;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000315 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000316 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
317 // typically changes when setting a new resolution or reconfiguring
318 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000319 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000320 };
321
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000322 struct AllocatedEncoder {
323 AllocatedEncoder(webrtc::VideoEncoder* encoder,
324 webrtc::VideoCodecType type,
Peter Boström4d71ede2015-05-19 23:09:35 +0200325 bool external);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000326 webrtc::VideoEncoder* encoder;
Peter Boström4d71ede2015-05-19 23:09:35 +0200327 webrtc::VideoEncoder* external_encoder;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000328 webrtc::VideoCodecType type;
329 bool external;
330 };
331
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000332 struct Dimensions {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +0000333 // Initial encoder configuration (QCIF, 176x144) frame (to ensure that
334 // hardware encoders can be initialized). This gives us low memory usage
335 // but also makes it so configuration errors are discovered at the time we
336 // apply the settings rather than when we get the first frame (waiting for
337 // the first frame to know that you gave a bad codec parameter could make
338 // debugging hard).
339 // TODO(pbos): Consider setting up encoders lazily.
340 Dimensions() : width(176), height(144), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000341 int width;
342 int height;
343 bool is_screencast;
344 };
345
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000346 union VideoEncoderSettings {
347 webrtc::VideoCodecVP8 vp8;
348 webrtc::VideoCodecVP9 vp9;
349 };
350
351 static std::vector<webrtc::VideoStream> CreateVideoStreams(
352 const VideoCodec& codec,
353 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100354 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000355 size_t num_streams);
356 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
357 const VideoCodec& codec,
358 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100359 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000360 size_t num_streams);
361
362 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
Erik Språng143cec12015-04-28 10:01:41 +0200363 const VideoOptions& options,
364 bool is_screencast)
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000365 EXCLUSIVE_LOCKS_REQUIRED(lock_);
366
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000367 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
368 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000369 void DestroyVideoEncoder(AllocatedEncoder* encoder)
370 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000371 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000372 const VideoOptions& options)
373 EXCLUSIVE_LOCKS_REQUIRED(lock_);
374 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000375 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
376 const Dimensions& dimensions,
377 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000378 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000379 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000380
Peter Boströmd6f4c252015-03-26 16:23:04 +0100381 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200382 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000383 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000384 WebRtcVideoEncoderFactory* const external_encoder_factory_
385 GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000386
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000387 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000388 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000389 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000390 VideoEncoderSettings encoder_settings_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000391 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000392 Dimensions last_dimensions_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000393
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000394 VideoCapturer* capturer_ GUARDED_BY(lock_);
395 bool sending_ GUARDED_BY(lock_);
396 bool muted_ GUARDED_BY(lock_);
397 VideoFormat format_ GUARDED_BY(lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +0000398 int old_adapt_changes_ GUARDED_BY(lock_);
qiangchenc27d89f2015-07-16 10:27:16 -0700399
400 // The timestamp of the first frame received
401 // Used to generate the timestamps of subsequent frames
402 int64_t first_frame_timestamp_ms_ GUARDED_BY(lock_);
403
404 // The timestamp of the last frame received
405 // Used to generate timestamp for the black frame when capturer is removed
406 int64_t last_frame_timestamp_ms_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000407 };
408
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000409 // Wrapper for the receiver part, contains configs etc. that are needed to
410 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
411 // between webrtc::VideoRenderer and cricket::VideoRenderer.
412 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
413 public:
414 WebRtcVideoReceiveStream(
Peter Boström259bd202015-05-28 13:39:50 +0200415 webrtc::Call* call,
416 const StreamParams& sp,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000417 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000418 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000419 const webrtc::VideoReceiveStream::Config& config,
420 const std::vector<VideoCodecSettings>& recv_codecs);
421 ~WebRtcVideoReceiveStream();
422
Peter Boströmd6f4c252015-03-26 16:23:04 +0100423 const std::vector<uint32>& GetSsrcs() const;
424
Peter Boström3548dd22015-05-22 18:48:36 +0200425 void SetLocalSsrc(uint32_t local_ssrc);
Peter Boström67c9df72015-05-11 14:34:58 +0200426 void SetNackAndRemb(bool nack_enabled, bool remb_enabled);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000427 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
428 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
429
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700430 void RenderFrame(const webrtc::VideoFrame& frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000431 int time_to_render_ms) override;
432 bool IsTextureSupported() const override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000433 bool IsDefaultStream() const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000434
435 void SetRenderer(cricket::VideoRenderer* renderer);
436 cricket::VideoRenderer* GetRenderer();
437
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000438 VideoReceiverInfo GetVideoReceiverInfo();
439
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000440 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000441 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000442 AllocatedDecoder(webrtc::VideoDecoder* decoder,
443 webrtc::VideoCodecType type,
Peter Boström7252a2b2015-05-18 19:42:03 +0200444 bool external);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000445 webrtc::VideoDecoder* decoder;
Peter Boström7252a2b2015-05-18 19:42:03 +0200446 // Decoder wrapped into a fallback decoder to permit software fallback.
447 webrtc::VideoDecoder* external_decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000448 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000449 bool external;
450 };
451
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000452 void SetSize(int width, int height);
453 void RecreateWebRtcStream();
454
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000455 AllocatedDecoder CreateOrReuseVideoDecoder(
456 std::vector<AllocatedDecoder>* old_decoder,
457 const VideoCodec& codec);
458 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000459
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000460 webrtc::Call* const call_;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100461 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200462 const std::vector<SsrcGroup> ssrc_groups_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000463
464 webrtc::VideoReceiveStream* stream_;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000465 const bool default_stream_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000466 webrtc::VideoReceiveStream::Config config_;
467
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000468 WebRtcVideoDecoderFactory* const external_decoder_factory_;
469 std::vector<AllocatedDecoder> allocated_decoders_;
470
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000471 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000472 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000473 int last_width_ GUARDED_BY(renderer_lock_);
474 int last_height_ GUARDED_BY(renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +0000475 // Expands remote RTP timestamps to int64_t to be able to estimate how long
476 // the stream has been running.
477 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
478 GUARDED_BY(renderer_lock_);
479 int64_t first_frame_timestamp_ GUARDED_BY(renderer_lock_);
480 // Start NTP time is estimated as current remote NTP time (estimated from
481 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
482 int64_t estimated_remote_start_ntp_time_ms_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000483 };
484
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000485 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000486 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000487
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000488 bool SendRtp(const uint8_t* data, size_t len) override;
489 bool SendRtcp(const uint8_t* data, size_t len) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000490
491 void StartAllSendStreams();
492 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000493
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000494 static std::vector<VideoCodecSettings> MapCodecs(
495 const std::vector<VideoCodec>& codecs);
496 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000497 const std::vector<VideoCodecSettings>& mapped_codecs) const;
deadbeef874ca3a2015-08-20 17:19:20 -0700498 static bool ReceiveCodecsHaveChanged(std::vector<VideoCodecSettings> before,
499 std::vector<VideoCodecSettings> after);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000500
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000501 void FillSenderStats(VideoMediaInfo* info);
502 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000503 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
504 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000505
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200506 rtc::ThreadChecker thread_checker_;
507
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000508 uint32_t rtcp_receiver_report_ssrc_;
509 bool sending_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 rtc::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000511 WebRtcCallFactory* call_factory_;
512
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000513 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000514
515 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
516 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000517
Peter Boströme7b221f2015-04-13 15:34:32 +0200518 // Separate list of set capturers used to signal CPU adaptation. These should
519 // not be locked while calling methods that take other locks to prevent
520 // lock-order inversions.
521 rtc::CriticalSection capturer_crit_;
522 bool signal_cpu_adaptation_ GUARDED_BY(capturer_crit_);
523 std::map<uint32, VideoCapturer*> capturers_ GUARDED_BY(capturer_crit_);
524
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000525 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000526 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000527 std::map<uint32, WebRtcVideoSendStream*> send_streams_
528 GUARDED_BY(stream_crit_);
529 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
530 GUARDED_BY(stream_crit_);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100531 std::set<uint32> send_ssrcs_ GUARDED_BY(stream_crit_);
532 std::set<uint32> receive_ssrcs_ GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000533
534 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000535 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
536
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200537 WebRtcVoiceMediaChannel* voice_channel_;
pbos@webrtc.org8296ec52015-03-20 14:27:49 +0000538 const int voice_channel_id_;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000539 WebRtcVideoEncoderFactory* const external_encoder_factory_;
540 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000541 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000542 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000543 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000544 VideoOptions options_;
545};
546
547} // namespace cricket
548
549#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_