blob: f5a97a26a17bc1cd5f3631d9ddae9f7150f8c0b0 [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"
pbos@webrtc.org42684be2014-10-03 11:25:45 +000043#include "webrtc/call.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000044#include "webrtc/common_video/interface/i420_video_frame.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000045#include "webrtc/transport.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 VideoCaptureModule;
52class VideoDecoder;
53class VideoEncoder;
54class VideoRender;
55class VideoSendStreamInput;
56class VideoReceiveStream;
57}
58
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059namespace rtc {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000060class CpuMonitor;
61class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000062} // namespace rtc
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000063
64namespace cricket {
65
66class VideoCapturer;
67class VideoFrame;
68class VideoProcessor;
69class VideoRenderer;
70class VoiceMediaChannel;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000071class WebRtcDecoderObserver;
72class WebRtcEncoderObserver;
73class WebRtcLocalStreamInfo;
74class WebRtcRenderAdapter;
75class WebRtcVideoChannelRecvInfo;
76class WebRtcVideoChannelSendInfo;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000077class WebRtcVoiceEngine;
78
79struct CapturedFrame;
80struct Device;
81
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +000082class WebRtcVideoRenderer;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000083
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000084class UnsignalledSsrcHandler {
85 public:
86 enum Action {
87 kDropPacket,
88 kDeliverPacket,
89 };
90 virtual Action OnUnsignalledSsrc(VideoMediaChannel* engine,
91 uint32_t ssrc) = 0;
92};
93
94// TODO(pbos): Remove, use external handlers only.
95class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
96 public:
97 DefaultUnsignalledSsrcHandler();
98 virtual Action OnUnsignalledSsrc(VideoMediaChannel* engine,
99 uint32_t ssrc) OVERRIDE;
100
101 VideoRenderer* GetDefaultRenderer() const;
102 void SetDefaultRenderer(VideoMediaChannel* channel, VideoRenderer* renderer);
103
104 private:
105 uint32_t default_recv_ssrc_;
106 VideoRenderer* default_renderer_;
107};
108
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000109// TODO(pbos): Remove this class and just inline configuring code.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000110class WebRtcVideoEncoderFactory2 {
111 public:
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000112 virtual ~WebRtcVideoEncoderFactory2();
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000113 virtual std::vector<webrtc::VideoStream> CreateVideoStreams(
114 const VideoCodec& codec,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000115 const VideoOptions& options,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000116 size_t num_streams);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000117
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000118 std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
119 const VideoCodec& codec,
120 const VideoOptions& options,
121 size_t num_streams);
122
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000123 virtual void* CreateVideoEncoderSettings(const VideoCodec& codec,
124 const VideoOptions& options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000125
126 virtual void DestroyVideoEncoderSettings(const VideoCodec& codec,
127 void* encoder_settings);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000128};
129
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000130// CallFactory, overridden for testing to verify that webrtc::Call is configured
131// properly.
132class WebRtcCallFactory {
133 public:
134 virtual ~WebRtcCallFactory();
135 virtual webrtc::Call* CreateCall(const webrtc::Call::Config& config);
136};
137
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000138// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
buildbot@webrtc.org3c16d8b2014-10-13 06:35:10 +0000139class WebRtcVideoEngine2 : public sigslot::has_slots<> {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000140 public:
141 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
142 WebRtcVideoEngine2();
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000143 virtual ~WebRtcVideoEngine2();
144
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000145 // Used for testing to be able to check and use the webrtc::Call config.
146 void SetCallFactory(WebRtcCallFactory* call_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000147
148 // Basic video engine implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000149 bool Init(rtc::Thread* worker_thread);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000150 void Terminate();
151
152 int GetCapabilities();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000153 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000154
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000155 WebRtcVideoChannel2* CreateChannel(const VideoOptions& options,
156 VoiceMediaChannel* voice_channel);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000157
158 const std::vector<VideoCodec>& codecs() const;
159 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
160 void SetLogging(int min_sev, const char* filter);
161
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000162 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
163 // not take the ownership of |decoder_factory|. The caller needs to make sure
164 // that |decoder_factory| outlives the video engine.
165 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
166 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
167 // not take the ownership of |encoder_factory|. The caller needs to make sure
168 // that |encoder_factory| outlives the video engine.
169 virtual void SetExternalEncoderFactory(
170 WebRtcVideoEncoderFactory* encoder_factory);
171
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000172 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000173 // This is currently ignored.
174 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
175
176 // Set the VoiceEngine for A/V sync. This can only be called before Init.
177 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
178
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000179 bool FindCodec(const VideoCodec& in);
180 bool CanSendCodec(const VideoCodec& in,
181 const VideoCodec& current,
182 VideoCodec* out);
183 // Check whether the supplied trace should be ignored.
184 bool ShouldIgnoreTrace(const std::string& trace);
185
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000186 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
187
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 rtc::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000189
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000190 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000191
192 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000193 std::vector<VideoCodec> GetSupportedCodecs() const;
194
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000195 rtc::Thread* worker_thread_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000196 WebRtcVoiceEngine* voice_engine_;
197 std::vector<VideoCodec> video_codecs_;
198 std::vector<RtpHeaderExtension> rtp_header_extensions_;
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000199 VideoFormat default_codec_format_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000200
201 bool initialized_;
202
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000203 rtc::scoped_ptr<rtc::CpuMonitor> cpu_monitor_;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000204 WebRtcVideoEncoderFactory2 default_video_encoder_factory_;
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000205
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000206 WebRtcCallFactory default_call_factory_;
207 WebRtcCallFactory* call_factory_;
208
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000209 WebRtcVideoDecoderFactory* external_decoder_factory_;
210 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000211 rtc::scoped_ptr<WebRtcVideoEncoderFactory> simulcast_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000212};
213
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000215 public VideoMediaChannel,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000216 public webrtc::newapi::Transport,
217 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000218 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000219 WebRtcVideoChannel2(WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000220 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000221 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000222 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000223 WebRtcVideoEncoderFactory* external_encoder_factory,
224 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000225 WebRtcVideoEncoderFactory2* encoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000226 ~WebRtcVideoChannel2();
227 bool Init();
228
229 // VideoMediaChannel implementation
230 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
231 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
232 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
233 virtual bool SetSendStreamFormat(uint32 ssrc,
234 const VideoFormat& format) OVERRIDE;
235 virtual bool SetRender(bool render) OVERRIDE;
236 virtual bool SetSend(bool send) OVERRIDE;
237
238 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
239 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
240 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
241 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
242 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
243 virtual bool GetStats(const StatsOptions& options,
244 VideoMediaInfo* info) OVERRIDE;
245 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
246 virtual bool SendIntraFrame() OVERRIDE;
247 virtual bool RequestIntraFrame() OVERRIDE;
248
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000249 virtual void OnPacketReceived(rtc::Buffer* packet,
250 const rtc::PacketTime& packet_time)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000251 OVERRIDE;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 virtual void OnRtcpReceived(rtc::Buffer* packet,
253 const rtc::PacketTime& packet_time)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000254 OVERRIDE;
255 virtual void OnReadyToSend(bool ready) OVERRIDE;
256 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000257
258 // Set send/receive RTP header extensions. This must be done before creating
259 // streams as it only has effect on future streams.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000260 virtual bool SetRecvRtpHeaderExtensions(
261 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
262 virtual bool SetSendRtpHeaderExtensions(
263 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000264 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
265 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
266 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
267 *options = options_;
268 return true;
269 }
270 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
271 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
272
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000273 virtual void OnMessage(rtc::Message* msg) OVERRIDE;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000274
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000275 virtual void OnLoadUpdate(Load load) OVERRIDE;
276
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000277 // Implemented for VideoMediaChannelTest.
278 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000279 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000280 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
281
282 private:
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000283 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
284 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000285 bool CodecIsExternallySupported(const std::string& name) const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000286
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000287 struct VideoCodecSettings {
288 VideoCodecSettings();
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000289
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000290 bool operator ==(const VideoCodecSettings& other) const;
291
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000292 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000293 webrtc::FecConfig fec;
294 int rtx_payload_type;
295 };
296
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000297 // Wrapper for the sender part, this is where the capturer is connected and
298 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000299 class WebRtcVideoSendStream : public sigslot::has_slots<> {
300 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000301 WebRtcVideoSendStream(
302 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000303 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000304 WebRtcVideoEncoderFactory2* encoder_factory,
305 const VideoOptions& options,
306 const Settable<VideoCodecSettings>& codec_settings,
307 const StreamParams& sp,
308 const std::vector<webrtc::RtpExtension>& rtp_extensions);
309
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000310 ~WebRtcVideoSendStream();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000311 void SetOptions(const VideoOptions& options);
312 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000313 void SetRtpExtensions(
314 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000315
316 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
317 bool SetCapturer(VideoCapturer* capturer);
318 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000319 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000320 bool DisconnectCapturer();
321
322 void Start();
323 void Stop();
324
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000325 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000326 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000327
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000328 void OnCpuResolutionRequest(
329 CoordinatedVideoAdapter::AdaptRequest adapt_request);
330
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000331 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000332 // Parameters needed to reconstruct the underlying stream.
333 // webrtc::VideoSendStream doesn't support setting a lot of options on the
334 // fly, so when those need to be changed we tear down and reconstruct with
335 // similar parameters depending on which options changed etc.
336 struct VideoSendStreamParameters {
337 VideoSendStreamParameters(
338 const webrtc::VideoSendStream::Config& config,
339 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000340 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000341 webrtc::VideoSendStream::Config config;
342 VideoOptions options;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000343 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000344 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
345 // typically changes when setting a new resolution or reconfiguring
346 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000347 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000348 };
349
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000350 struct AllocatedEncoder {
351 AllocatedEncoder(webrtc::VideoEncoder* encoder,
352 webrtc::VideoCodecType type,
353 bool external)
354 : encoder(encoder), type(type), external(external) {}
355 webrtc::VideoEncoder* encoder;
356 webrtc::VideoCodecType type;
357 bool external;
358 };
359
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000360 struct Dimensions {
361 Dimensions() : width(-1), height(-1), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000362 int width;
363 int height;
364 bool is_screencast;
365 };
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
381 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000382 WebRtcVideoEncoderFactory* const external_encoder_factory_
383 GUARDED_BY(lock_);
384 WebRtcVideoEncoderFactory2* const encoder_factory_ GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000385
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000386 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000387 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000388 VideoSendStreamParameters parameters_ 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_);
396
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000397 rtc::CriticalSection frame_lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000398 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
399 };
400
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000401 // Wrapper for the receiver part, contains configs etc. that are needed to
402 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
403 // between webrtc::VideoRenderer and cricket::VideoRenderer.
404 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
405 public:
406 WebRtcVideoReceiveStream(
407 webrtc::Call*,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000408 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000409 const webrtc::VideoReceiveStream::Config& config,
410 const std::vector<VideoCodecSettings>& recv_codecs);
411 ~WebRtcVideoReceiveStream();
412
413 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
414 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
415
416 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
417 int time_to_render_ms) OVERRIDE;
418
419 void SetRenderer(cricket::VideoRenderer* renderer);
420 cricket::VideoRenderer* GetRenderer();
421
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000422 VideoReceiverInfo GetVideoReceiverInfo();
423
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000424 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000425 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000426 AllocatedDecoder(webrtc::VideoDecoder* decoder,
427 webrtc::VideoCodecType type,
428 bool external)
429 : decoder(decoder), type(type), external(external) {}
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000430 webrtc::VideoDecoder* decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000431 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000432 bool external;
433 };
434
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000435 void SetSize(int width, int height);
436 void RecreateWebRtcStream();
437
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000438 AllocatedDecoder CreateOrReuseVideoDecoder(
439 std::vector<AllocatedDecoder>* old_decoder,
440 const VideoCodec& codec);
441 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000442
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000443 webrtc::Call* const call_;
444
445 webrtc::VideoReceiveStream* stream_;
446 webrtc::VideoReceiveStream::Config config_;
447
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000448 WebRtcVideoDecoderFactory* const external_decoder_factory_;
449 std::vector<AllocatedDecoder> allocated_decoders_;
450
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000451 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000452 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000453 int last_width_ GUARDED_BY(renderer_lock_);
454 int last_height_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000455 };
456
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000457 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000458 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000459
460 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
461 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
462
463 void StartAllSendStreams();
464 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000465
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000466 static std::vector<VideoCodecSettings> MapCodecs(
467 const std::vector<VideoCodec>& codecs);
468 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000469 const std::vector<VideoCodecSettings>& mapped_codecs) const;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000470
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000471 void FillSenderStats(VideoMediaInfo* info);
472 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000473 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
474 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000475
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000476 uint32_t rtcp_receiver_report_ssrc_;
477 bool sending_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000478 rtc::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000479 WebRtcCallFactory* call_factory_;
480
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000481 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000482
483 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
484 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000485
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000486 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000487 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000488 std::map<uint32, WebRtcVideoSendStream*> send_streams_
489 GUARDED_BY(stream_crit_);
490 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
491 GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000492
493 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000494 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
495
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000496 VoiceMediaChannel* const voice_channel_;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000497 WebRtcVideoEncoderFactory* const external_encoder_factory_;
498 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000499 WebRtcVideoEncoderFactory2* const encoder_factory_;
500 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000501 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000502 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000503 VideoOptions options_;
504};
505
506} // namespace cricket
507
508#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_