blob: f27099bf973f50b52ba2134f6b105aeb24fdafc1 [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;
solenberg1dd98f32015-09-10 01:57:14 -0700196 bool SetVideoSend(uint32 ssrc, bool mute,
197 const VideoOptions* options) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 bool AddSendStream(const StreamParams& sp) override;
199 bool RemoveSendStream(uint32 ssrc) override;
200 bool AddRecvStream(const StreamParams& sp) override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000201 bool AddRecvStream(const StreamParams& sp, bool default_stream);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000202 bool RemoveRecvStream(uint32 ssrc) override;
203 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) override;
204 bool GetStats(VideoMediaInfo* info) override;
205 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) override;
206 bool SendIntraFrame() override;
207 bool RequestIntraFrame() override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000208
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000209 void OnPacketReceived(rtc::Buffer* packet,
210 const rtc::PacketTime& packet_time) override;
211 void OnRtcpReceived(rtc::Buffer* packet,
212 const rtc::PacketTime& packet_time) override;
213 void OnReadyToSend(bool ready) 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;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000223 void SetInterface(NetworkInterface* iface) override;
224 void UpdateAspectRatio(int ratio_w, int ratio_h) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000225
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000226 void OnMessage(rtc::Message* msg) override;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000227
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000228 void OnLoadUpdate(Load load) override;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000229
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000230 // Implemented for VideoMediaChannelTest.
231 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000232 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000233 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
234
235 private:
solenberg1dd98f32015-09-10 01:57:14 -0700236 bool MuteStream(uint32 ssrc, bool mute);
Peter Boströmd6f4c252015-03-26 16:23:04 +0100237 class WebRtcVideoReceiveStream;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000238 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
239 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000240 bool CodecIsExternallySupported(const std::string& name) const;
Peter Boströmd6f4c252015-03-26 16:23:04 +0100241 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
242 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
243 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
244 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
245 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
246 EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000247
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248 struct VideoCodecSettings {
249 VideoCodecSettings();
Peter Boströmee0b00e2015-04-22 18:41:14 +0200250
251 bool operator==(const VideoCodecSettings& other) const;
252 bool operator!=(const VideoCodecSettings& other) const;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000253
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000254 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000255 webrtc::FecConfig fec;
256 int rtx_payload_type;
257 };
258
deadbeef874ca3a2015-08-20 17:19:20 -0700259 static std::string CodecSettingsVectorToString(
260 const std::vector<VideoCodecSettings>& codecs);
261
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000262 // Wrapper for the sender part, this is where the capturer is connected and
263 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000264 class WebRtcVideoSendStream : public sigslot::has_slots<> {
265 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000266 WebRtcVideoSendStream(
267 webrtc::Call* call,
solenberg4fbae2b2015-08-28 04:07:10 -0700268 const StreamParams& sp,
269 const webrtc::VideoSendStream::Config& config,
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,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000274 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000275 ~WebRtcVideoSendStream();
Peter Boströmd6f4c252015-03-26 16:23:04 +0100276
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000277 void SetOptions(const VideoOptions& options);
278 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000279 void SetRtpExtensions(
280 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000281
282 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
283 bool SetCapturer(VideoCapturer* capturer);
284 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000285 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000286 bool DisconnectCapturer();
287
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700288 void SetApplyRotation(bool apply_rotation);
289
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000290 void Start();
291 void Stop();
292
Peter Boströmd6f4c252015-03-26 16:23:04 +0100293 const std::vector<uint32>& GetSsrcs() const;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000294 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000295 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000296
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100297 void SetMaxBitrateBps(int max_bitrate_bps);
298
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000299 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000300 // Parameters needed to reconstruct the underlying stream.
301 // webrtc::VideoSendStream doesn't support setting a lot of options on the
302 // fly, so when those need to be changed we tear down and reconstruct with
303 // similar parameters depending on which options changed etc.
304 struct VideoSendStreamParameters {
305 VideoSendStreamParameters(
306 const webrtc::VideoSendStream::Config& config,
307 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100308 int max_bitrate_bps,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000309 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000310 webrtc::VideoSendStream::Config config;
311 VideoOptions options;
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100312 int max_bitrate_bps;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000313 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000314 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
315 // typically changes when setting a new resolution or reconfiguring
316 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000317 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000318 };
319
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000320 struct AllocatedEncoder {
321 AllocatedEncoder(webrtc::VideoEncoder* encoder,
322 webrtc::VideoCodecType type,
Peter Boström4d71ede2015-05-19 23:09:35 +0200323 bool external);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000324 webrtc::VideoEncoder* encoder;
Peter Boström4d71ede2015-05-19 23:09:35 +0200325 webrtc::VideoEncoder* external_encoder;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000326 webrtc::VideoCodecType type;
327 bool external;
328 };
329
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000330 struct Dimensions {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +0000331 // Initial encoder configuration (QCIF, 176x144) frame (to ensure that
332 // hardware encoders can be initialized). This gives us low memory usage
333 // but also makes it so configuration errors are discovered at the time we
334 // apply the settings rather than when we get the first frame (waiting for
335 // the first frame to know that you gave a bad codec parameter could make
336 // debugging hard).
337 // TODO(pbos): Consider setting up encoders lazily.
338 Dimensions() : width(176), height(144), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000339 int width;
340 int height;
341 bool is_screencast;
342 };
343
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000344 union VideoEncoderSettings {
345 webrtc::VideoCodecVP8 vp8;
346 webrtc::VideoCodecVP9 vp9;
347 };
348
349 static std::vector<webrtc::VideoStream> CreateVideoStreams(
350 const VideoCodec& codec,
351 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100352 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000353 size_t num_streams);
354 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
355 const VideoCodec& codec,
356 const VideoOptions& options,
Peter Boströmdfd53fe2015-03-27 15:58:11 +0100357 int max_bitrate_bps,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000358 size_t num_streams);
359
360 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
Erik Språng143cec12015-04-28 10:01:41 +0200361 const VideoOptions& options,
362 bool is_screencast)
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000363 EXCLUSIVE_LOCKS_REQUIRED(lock_);
364
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000365 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
366 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000367 void DestroyVideoEncoder(AllocatedEncoder* encoder)
368 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000369 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000370 const VideoOptions& options)
371 EXCLUSIVE_LOCKS_REQUIRED(lock_);
372 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000373 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
374 const Dimensions& dimensions,
375 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000376 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000377 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000378
Peter Boströmd6f4c252015-03-26 16:23:04 +0100379 const std::vector<uint32> ssrcs_;
Peter Boström259bd202015-05-28 13:39:50 +0200380 const std::vector<SsrcGroup> ssrc_groups_;
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_);
qiangchenc27d89f2015-07-16 10:27:16 -0700397
398 // The timestamp of the first frame received
399 // Used to generate the timestamps of subsequent frames
400 int64_t first_frame_timestamp_ms_ GUARDED_BY(lock_);
401
402 // The timestamp of the last frame received
403 // Used to generate timestamp for the black frame when capturer is removed
404 int64_t last_frame_timestamp_ms_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000405 };
406
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000407 // Wrapper for the receiver part, contains configs etc. that are needed to
408 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
409 // between webrtc::VideoRenderer and cricket::VideoRenderer.
410 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
411 public:
412 WebRtcVideoReceiveStream(
Peter Boström259bd202015-05-28 13:39:50 +0200413 webrtc::Call* call,
414 const StreamParams& sp,
solenberg4fbae2b2015-08-28 04:07:10 -0700415 const webrtc::VideoReceiveStream::Config& config,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000416 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000417 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000418 const std::vector<VideoCodecSettings>& recv_codecs);
419 ~WebRtcVideoReceiveStream();
420
Peter Boströmd6f4c252015-03-26 16:23:04 +0100421 const std::vector<uint32>& GetSsrcs() const;
422
Peter Boström3548dd22015-05-22 18:48:36 +0200423 void SetLocalSsrc(uint32_t local_ssrc);
Peter Boström67c9df72015-05-11 14:34:58 +0200424 void SetNackAndRemb(bool nack_enabled, bool remb_enabled);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000425 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
426 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
427
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700428 void RenderFrame(const webrtc::VideoFrame& frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000429 int time_to_render_ms) override;
430 bool IsTextureSupported() const override;
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000431 bool IsDefaultStream() const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000432
433 void SetRenderer(cricket::VideoRenderer* renderer);
434 cricket::VideoRenderer* GetRenderer();
435
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000436 VideoReceiverInfo GetVideoReceiverInfo();
437
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000438 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000439 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000440 AllocatedDecoder(webrtc::VideoDecoder* decoder,
441 webrtc::VideoCodecType type,
Peter Boström7252a2b2015-05-18 19:42:03 +0200442 bool external);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000443 webrtc::VideoDecoder* decoder;
Peter Boström7252a2b2015-05-18 19:42:03 +0200444 // Decoder wrapped into a fallback decoder to permit software fallback.
445 webrtc::VideoDecoder* external_decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000446 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000447 bool external;
448 };
449
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000450 void SetSize(int width, int height);
451 void RecreateWebRtcStream();
452
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000453 AllocatedDecoder CreateOrReuseVideoDecoder(
454 std::vector<AllocatedDecoder>* old_decoder,
455 const VideoCodec& codec);
456 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000457
pbosf42376c2015-08-28 07:35:32 -0700458 std::string GetCodecNameFromPayloadType(int payload_type);
459
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_