blob: 8c33069fe8ce3dbc6dcc4786eaf704dd2e6dd53e [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"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000039#include "webrtc/base/cpumonitor.h"
pbos@webrtc.org575d1262014-10-08 14:48:08 +000040#include "webrtc/base/criticalsection.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000041#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000042#include "webrtc/base/thread_annotations.h"
Fredrik Solenberg4b60c732015-05-07 14:07:48 +020043#include "webrtc/base/thread_checker.h"
pbos@webrtc.org42684be2014-10-03 11:25:45 +000044#include "webrtc/call.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000045#include "webrtc/transport.h"
Thiago Farina9bfe3da2015-04-10 12:52:13 +020046#include "webrtc/video_frame.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "webrtc/video_receive_stream.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000048#include "webrtc/video_renderer.h"
49#include "webrtc/video_send_stream.h"
50
51namespace webrtc {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000052class VideoDecoder;
53class VideoEncoder;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000054}
55
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000056namespace rtc {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000057class CpuMonitor;
58class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059} // namespace rtc
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000060
61namespace cricket {
62
63class VideoCapturer;
64class VideoFrame;
65class VideoProcessor;
66class VideoRenderer;
67class VoiceMediaChannel;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000068class WebRtcDecoderObserver;
69class WebRtcEncoderObserver;
70class WebRtcLocalStreamInfo;
71class WebRtcRenderAdapter;
72class WebRtcVideoChannelRecvInfo;
73class WebRtcVideoChannelSendInfo;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000074class WebRtcVoiceEngine;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +020075class WebRtcVoiceMediaChannel;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000076
77struct CapturedFrame;
78struct Device;
79
Peter Boström81ea54e2015-05-07 11:41:09 +020080// Exposed here for unittests.
81std::vector<VideoCodec> DefaultVideoCodecList();
82
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000083class UnsignalledSsrcHandler {
84 public:
85 enum Action {
86 kDropPacket,
87 kDeliverPacket,
88 };
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +000089 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel2* channel,
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000090 uint32_t ssrc) = 0;
91};
92
93// TODO(pbos): Remove, use external handlers only.
94class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
95 public:
96 DefaultUnsignalledSsrcHandler();
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +000097 Action OnUnsignalledSsrc(WebRtcVideoChannel2* channel,
98 uint32_t ssrc) override;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000099
100 VideoRenderer* GetDefaultRenderer() const;
101 void SetDefaultRenderer(VideoMediaChannel* channel, VideoRenderer* renderer);
102
103 private:
104 uint32_t default_recv_ssrc_;
105 VideoRenderer* default_renderer_;
106};
107
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000108// CallFactory, overridden for testing to verify that webrtc::Call is configured
109// properly.
110class WebRtcCallFactory {
111 public:
112 virtual ~WebRtcCallFactory();
113 virtual webrtc::Call* CreateCall(const webrtc::Call::Config& config);
114};
115
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000116// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
buildbot@webrtc.org3c16d8b2014-10-13 06:35:10 +0000117class WebRtcVideoEngine2 : public sigslot::has_slots<> {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000118 public:
pbos@webrtc.orgf1f0d9a2015-03-02 13:30:15 +0000119 explicit WebRtcVideoEngine2(WebRtcVoiceEngine* voice_engine);
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000120 virtual ~WebRtcVideoEngine2();
121
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000122 // Used for testing to be able to check and use the webrtc::Call config.
123 void SetCallFactory(WebRtcCallFactory* call_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000124
125 // Basic video engine implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000126 bool Init(rtc::Thread* worker_thread);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000127 void Terminate();
128
129 int GetCapabilities();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000130 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000131
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000132 WebRtcVideoChannel2* CreateChannel(const VideoOptions& options,
133 VoiceMediaChannel* voice_channel);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134
135 const std::vector<VideoCodec>& codecs() const;
136 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
137 void SetLogging(int min_sev, const char* filter);
138
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000139 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
140 // not take the ownership of |decoder_factory|. The caller needs to make sure
141 // that |decoder_factory| outlives the video engine.
142 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
143 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
144 // not take the ownership of |encoder_factory|. The caller needs to make sure
145 // that |encoder_factory| outlives the video engine.
146 virtual void SetExternalEncoderFactory(
147 WebRtcVideoEncoderFactory* encoder_factory);
148
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000149 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000150
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000151 bool FindCodec(const VideoCodec& in);
152 bool CanSendCodec(const VideoCodec& in,
153 const VideoCodec& current,
154 VideoCodec* out);
155 // Check whether the supplied trace should be ignored.
156 bool ShouldIgnoreTrace(const std::string& trace);
157
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000158 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000159 std::vector<VideoCodec> GetSupportedCodecs() const;
160
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161 rtc::Thread* worker_thread_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000162 WebRtcVoiceEngine* voice_engine_;
163 std::vector<VideoCodec> video_codecs_;
164 std::vector<RtpHeaderExtension> rtp_header_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000165
166 bool initialized_;
167
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000168 WebRtcCallFactory default_call_factory_;
169 WebRtcCallFactory* call_factory_;
170
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000171 WebRtcVideoDecoderFactory* external_decoder_factory_;
172 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000173 rtc::scoped_ptr<WebRtcVideoEncoderFactory> simulcast_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000174};
175
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000177 public VideoMediaChannel,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000178 public webrtc::newapi::Transport,
179 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000180 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000181 WebRtcVideoChannel2(WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000182 WebRtcVoiceEngine* voice_engine,
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200183 WebRtcVoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000184 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000185 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000186 WebRtcVideoDecoderFactory* external_decoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000187 ~WebRtcVideoChannel2();
188 bool Init();
189
190 // VideoMediaChannel implementation
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200191 void DetachVoiceChannel() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) override;
193 bool SetSendCodecs(const std::vector<VideoCodec>& codecs) override;
194 bool GetSendCodec(VideoCodec* send_codec) override;
195 bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) override;
196 bool SetRender(bool render) override;
197 bool SetSend(bool send) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000198
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000199 bool AddSendStream(const StreamParams& sp) override;
200 bool RemoveSendStream(uint32 ssrc) override;
201 bool AddRecvStream(const StreamParams& sp) override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000202 bool AddRecvStream(const StreamParams& sp, bool default_stream);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000203 bool RemoveRecvStream(uint32 ssrc) override;
204 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) override;
205 bool GetStats(VideoMediaInfo* info) override;
206 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) override;
207 bool SendIntraFrame() override;
208 bool RequestIntraFrame() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000209
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000210 void OnPacketReceived(rtc::Buffer* packet,
211 const rtc::PacketTime& packet_time) override;
212 void OnRtcpReceived(rtc::Buffer* packet,
213 const rtc::PacketTime& packet_time) override;
214 void OnReadyToSend(bool ready) override;
215 bool MuteStream(uint32 ssrc, bool mute) override;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000216
217 // Set send/receive RTP header extensions. This must be done before creating
218 // streams as it only has effect on future streams.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000219 bool SetRecvRtpHeaderExtensions(
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 SetSendRtpHeaderExtensions(
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000222 const std::vector<RtpHeaderExtension>& extensions) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000223 bool SetMaxSendBandwidth(int bps) override;
224 bool SetOptions(const VideoOptions& options) override;
225 bool GetOptions(VideoOptions* options) const override {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000226 *options = options_;
227 return true;
228 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000229 void SetInterface(NetworkInterface* iface) override;
230 void UpdateAspectRatio(int ratio_w, int ratio_h) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000231
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000232 void OnMessage(rtc::Message* msg) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000233
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000234 void OnLoadUpdate(Load load) override;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000235
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000236 // Implemented for VideoMediaChannelTest.
237 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000238 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000239 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
240
241 private:
Peter Boströmd6f4c252015-03-26 16:23:04 +0100242 class WebRtcVideoReceiveStream;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000243 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
244 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000245 bool CodecIsExternallySupported(const std::string& name) const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100246 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
247 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
248 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
249 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
250 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
251 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000252
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000253 struct VideoCodecSettings {
254 VideoCodecSettings();
Peter Boströmee0b00e2015-04-22 18:41:14 +0200255
256 bool operator==(const VideoCodecSettings& other) const;
257 bool operator!=(const VideoCodecSettings& other) const;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000258
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000259 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000260 webrtc::FecConfig fec;
261 int rtx_payload_type;
262 };
263
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000264 // Wrapper for the sender part, this is where the capturer is connected and
265 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000266 class WebRtcVideoSendStream : public sigslot::has_slots<> {
267 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000268 WebRtcVideoSendStream(
269 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000270 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000271 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100272 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000273 const Settable<VideoCodecSettings>& codec_settings,
274 const StreamParams& sp,
275 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000276 ~WebRtcVideoSendStream();
Peter Boströmd6f4c252015-03-26 16:23:04 +0100277
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000278 void SetOptions(const VideoOptions& options);
279 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000280 void SetRtpExtensions(
281 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000282
283 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
284 bool SetCapturer(VideoCapturer* capturer);
285 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000286 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000287 bool DisconnectCapturer();
288
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700289 void SetApplyRotation(bool apply_rotation);
290
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000291 void Start();
292 void Stop();
293
Peter Boströmd6f4c252015-03-26 16:23:04 +0100294 const std::vector<uint32>& GetSsrcs() const;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000295 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000296 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000297
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100298 void SetMaxBitrateBps(int max_bitrate_bps);
299
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000300 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000301 // Parameters needed to reconstruct the underlying stream.
302 // webrtc::VideoSendStream doesn't support setting a lot of options on the
303 // fly, so when those need to be changed we tear down and reconstruct with
304 // similar parameters depending on which options changed etc.
305 struct VideoSendStreamParameters {
306 VideoSendStreamParameters(
307 const webrtc::VideoSendStream::Config& config,
308 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100309 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000310 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000311 webrtc::VideoSendStream::Config config;
312 VideoOptions options;
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100313 int max_bitrate_bps;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000314 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000315 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
316 // typically changes when setting a new resolution or reconfiguring
317 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000318 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000319 };
320
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000321 struct AllocatedEncoder {
322 AllocatedEncoder(webrtc::VideoEncoder* encoder,
323 webrtc::VideoCodecType type,
324 bool external)
325 : encoder(encoder), type(type), external(external) {}
326 webrtc::VideoEncoder* encoder;
327 webrtc::VideoCodecType type;
328 bool external;
329 };
330
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000331 struct Dimensions {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +0000332 // Initial encoder configuration (QCIF, 176x144) frame (to ensure that
333 // hardware encoders can be initialized). This gives us low memory usage
334 // but also makes it so configuration errors are discovered at the time we
335 // apply the settings rather than when we get the first frame (waiting for
336 // the first frame to know that you gave a bad codec parameter could make
337 // debugging hard).
338 // TODO(pbos): Consider setting up encoders lazily.
339 Dimensions() : width(176), height(144), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000340 int width;
341 int height;
342 bool is_screencast;
343 };
344
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000345 union VideoEncoderSettings {
346 webrtc::VideoCodecVP8 vp8;
347 webrtc::VideoCodecVP9 vp9;
348 };
349
350 static std::vector<webrtc::VideoStream> CreateVideoStreams(
351 const VideoCodec& codec,
352 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100353 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000354 size_t num_streams);
355 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
356 const VideoCodec& codec,
357 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100358 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000359 size_t num_streams);
360
361 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
Erik Språng143cec12015-04-28 10:01:41 +0200362 const VideoOptions& options,
363 bool is_screencast)
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000364 EXCLUSIVE_LOCKS_REQUIRED(lock_);
365
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000366 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
367 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000368 void DestroyVideoEncoder(AllocatedEncoder* encoder)
369 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000370 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000371 const VideoOptions& options)
372 EXCLUSIVE_LOCKS_REQUIRED(lock_);
373 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000374 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
375 const Dimensions& dimensions,
376 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000377 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000378 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000379
Peter Boströmd6f4c252015-03-26 16:23:04 +0100380 const std::vector<uint32> ssrcs_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000381 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000382 WebRtcVideoEncoderFactory* const external_encoder_factory_
383 GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000384
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000385 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000386 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000387 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000388 VideoEncoderSettings encoder_settings_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000389 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000390 Dimensions last_dimensions_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000391
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000392 VideoCapturer* capturer_ GUARDED_BY(lock_);
393 bool sending_ GUARDED_BY(lock_);
394 bool muted_ GUARDED_BY(lock_);
395 VideoFormat format_ GUARDED_BY(lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +0000396 int old_adapt_changes_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000397 };
398
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000399 // Wrapper for the receiver part, contains configs etc. that are needed to
400 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
401 // between webrtc::VideoRenderer and cricket::VideoRenderer.
402 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
403 public:
404 WebRtcVideoReceiveStream(
405 webrtc::Call*,
Peter Boströmd6f4c252015-03-26 16:23:04 +0100406 const std::vector<uint32>& ssrcs,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000407 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000408 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000409 const webrtc::VideoReceiveStream::Config& config,
410 const std::vector<VideoCodecSettings>& recv_codecs);
411 ~WebRtcVideoReceiveStream();
412
Peter Boströmd6f4c252015-03-26 16:23:04 +0100413 const std::vector<uint32>& GetSsrcs() const;
414
Peter Boström67c9df72015-05-11 14:34:58 +0200415 void SetNackAndRemb(bool nack_enabled, bool remb_enabled);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000416 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
417 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
418
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000419 void RenderFrame(const webrtc::I420VideoFrame& frame,
420 int time_to_render_ms) override;
421 bool IsTextureSupported() const override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000422 bool IsDefaultStream() const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000423
424 void SetRenderer(cricket::VideoRenderer* renderer);
425 cricket::VideoRenderer* GetRenderer();
426
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000427 VideoReceiverInfo GetVideoReceiverInfo();
428
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000429 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000430 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000431 AllocatedDecoder(webrtc::VideoDecoder* decoder,
432 webrtc::VideoCodecType type,
433 bool external)
434 : decoder(decoder), type(type), external(external) {}
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000435 webrtc::VideoDecoder* decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000436 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000437 bool external;
438 };
439
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000440 void SetSize(int width, int height);
441 void RecreateWebRtcStream();
442
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000443 AllocatedDecoder CreateOrReuseVideoDecoder(
444 std::vector<AllocatedDecoder>* old_decoder,
445 const VideoCodec& codec);
446 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000447
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000448 webrtc::Call* const call_;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100449 const std::vector<uint32> ssrcs_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000450
451 webrtc::VideoReceiveStream* stream_;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000452 const bool default_stream_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000453 webrtc::VideoReceiveStream::Config config_;
454
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000455 WebRtcVideoDecoderFactory* const external_decoder_factory_;
456 std::vector<AllocatedDecoder> allocated_decoders_;
457
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000459 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000460 int last_width_ GUARDED_BY(renderer_lock_);
461 int last_height_ GUARDED_BY(renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +0000462 // Expands remote RTP timestamps to int64_t to be able to estimate how long
463 // the stream has been running.
464 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
465 GUARDED_BY(renderer_lock_);
466 int64_t first_frame_timestamp_ GUARDED_BY(renderer_lock_);
467 // Start NTP time is estimated as current remote NTP time (estimated from
468 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
469 int64_t estimated_remote_start_ntp_time_ms_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000470 };
471
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000472 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000473 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000474
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000475 bool SendRtp(const uint8_t* data, size_t len) override;
476 bool SendRtcp(const uint8_t* data, size_t len) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000477
478 void StartAllSendStreams();
479 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000480
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000481 static std::vector<VideoCodecSettings> MapCodecs(
482 const std::vector<VideoCodec>& codecs);
483 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000484 const std::vector<VideoCodecSettings>& mapped_codecs) const;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000485
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000486 void FillSenderStats(VideoMediaInfo* info);
487 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000488 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
489 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000490
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200491 rtc::ThreadChecker thread_checker_;
492
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000493 uint32_t rtcp_receiver_report_ssrc_;
494 bool sending_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000495 rtc::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000496 WebRtcCallFactory* call_factory_;
497
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000498 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000499
500 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
501 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000502
Peter Boströme7b221f2015-04-13 15:34:32 +0200503 // Separate list of set capturers used to signal CPU adaptation. These should
504 // not be locked while calling methods that take other locks to prevent
505 // lock-order inversions.
506 rtc::CriticalSection capturer_crit_;
507 bool signal_cpu_adaptation_ GUARDED_BY(capturer_crit_);
508 std::map<uint32, VideoCapturer*> capturers_ GUARDED_BY(capturer_crit_);
509
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000510 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000511 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000512 std::map<uint32, WebRtcVideoSendStream*> send_streams_
513 GUARDED_BY(stream_crit_);
514 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
515 GUARDED_BY(stream_crit_);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100516 std::set<uint32> send_ssrcs_ GUARDED_BY(stream_crit_);
517 std::set<uint32> receive_ssrcs_ GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000518
519 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000520 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
521
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200522 WebRtcVoiceMediaChannel* voice_channel_;
pbos@webrtc.org8296ec52015-03-20 14:27:49 +0000523 const int voice_channel_id_;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000524 WebRtcVideoEncoderFactory* const external_encoder_factory_;
525 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000526 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000527 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000528 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000529 VideoOptions options_;
530};
531
532} // namespace cricket
533
534#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_