blob: bb236591e617353468f9843aceaa311bfdd9ab02 [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 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 CpuMonitor;
57class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058} // namespace rtc
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000059
60namespace cricket {
61
62class VideoCapturer;
63class VideoFrame;
64class VideoProcessor;
65class VideoRenderer;
66class VoiceMediaChannel;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000067class WebRtcDecoderObserver;
68class WebRtcEncoderObserver;
69class WebRtcLocalStreamInfo;
70class WebRtcRenderAdapter;
71class WebRtcVideoChannelRecvInfo;
72class WebRtcVideoChannelSendInfo;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000073class WebRtcVoiceEngine;
74
75struct CapturedFrame;
76struct Device;
77
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +000078class UnsignalledSsrcHandler {
79 public:
80 enum Action {
81 kDropPacket,
82 kDeliverPacket,
83 };
84 virtual Action OnUnsignalledSsrc(VideoMediaChannel* engine,
85 uint32_t ssrc) = 0;
86};
87
88// TODO(pbos): Remove, use external handlers only.
89class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
90 public:
91 DefaultUnsignalledSsrcHandler();
92 virtual Action OnUnsignalledSsrc(VideoMediaChannel* engine,
93 uint32_t ssrc) OVERRIDE;
94
95 VideoRenderer* GetDefaultRenderer() const;
96 void SetDefaultRenderer(VideoMediaChannel* channel, VideoRenderer* renderer);
97
98 private:
99 uint32_t default_recv_ssrc_;
100 VideoRenderer* default_renderer_;
101};
102
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000103// CallFactory, overridden for testing to verify that webrtc::Call is configured
104// properly.
105class WebRtcCallFactory {
106 public:
107 virtual ~WebRtcCallFactory();
108 virtual webrtc::Call* CreateCall(const webrtc::Call::Config& config);
109};
110
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000111// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
buildbot@webrtc.org3c16d8b2014-10-13 06:35:10 +0000112class WebRtcVideoEngine2 : public sigslot::has_slots<> {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000113 public:
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000114 WebRtcVideoEngine2();
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000115 virtual ~WebRtcVideoEngine2();
116
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000117 // Used for testing to be able to check and use the webrtc::Call config.
118 void SetCallFactory(WebRtcCallFactory* call_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000119
120 // Basic video engine implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000121 bool Init(rtc::Thread* worker_thread);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000122 void Terminate();
123
124 int GetCapabilities();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000125 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000126
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000127 WebRtcVideoChannel2* CreateChannel(const VideoOptions& options,
128 VoiceMediaChannel* voice_channel);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000129
130 const std::vector<VideoCodec>& codecs() const;
131 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
132 void SetLogging(int min_sev, const char* filter);
133
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000134 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
135 // not take the ownership of |decoder_factory|. The caller needs to make sure
136 // that |decoder_factory| outlives the video engine.
137 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
138 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
139 // not take the ownership of |encoder_factory|. The caller needs to make sure
140 // that |encoder_factory| outlives the video engine.
141 virtual void SetExternalEncoderFactory(
142 WebRtcVideoEncoderFactory* encoder_factory);
143
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000144 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000145 // This is currently ignored.
146 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
147
148 // Set the VoiceEngine for A/V sync. This can only be called before Init.
149 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
150
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
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000158 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
159
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000160 rtc::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000161
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000162 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000163 std::vector<VideoCodec> GetSupportedCodecs() const;
164
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 rtc::Thread* worker_thread_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000166 WebRtcVoiceEngine* voice_engine_;
167 std::vector<VideoCodec> video_codecs_;
168 std::vector<RtpHeaderExtension> rtp_header_extensions_;
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000169 VideoFormat default_codec_format_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000170
171 bool initialized_;
172
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000173 rtc::scoped_ptr<rtc::CpuMonitor> cpu_monitor_;
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000174
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000175 WebRtcCallFactory default_call_factory_;
176 WebRtcCallFactory* call_factory_;
177
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000178 WebRtcVideoDecoderFactory* external_decoder_factory_;
179 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000180 rtc::scoped_ptr<WebRtcVideoEncoderFactory> simulcast_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000181};
182
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000184 public VideoMediaChannel,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000185 public webrtc::newapi::Transport,
186 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000187 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000188 WebRtcVideoChannel2(WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000189 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000190 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000191 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000192 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000193 WebRtcVideoDecoderFactory* external_decoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000194 ~WebRtcVideoChannel2();
195 bool Init();
196
197 // VideoMediaChannel implementation
198 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
199 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
200 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
201 virtual bool SetSendStreamFormat(uint32 ssrc,
202 const VideoFormat& format) OVERRIDE;
203 virtual bool SetRender(bool render) OVERRIDE;
204 virtual bool SetSend(bool send) OVERRIDE;
205
206 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
207 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
208 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
209 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
210 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
211 virtual bool GetStats(const StatsOptions& options,
212 VideoMediaInfo* info) OVERRIDE;
213 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
214 virtual bool SendIntraFrame() OVERRIDE;
215 virtual bool RequestIntraFrame() OVERRIDE;
216
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000217 virtual void OnPacketReceived(rtc::Buffer* packet,
218 const rtc::PacketTime& packet_time)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000219 OVERRIDE;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 virtual void OnRtcpReceived(rtc::Buffer* packet,
221 const rtc::PacketTime& packet_time)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000222 OVERRIDE;
223 virtual void OnReadyToSend(bool ready) OVERRIDE;
224 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000225
226 // Set send/receive RTP header extensions. This must be done before creating
227 // streams as it only has effect on future streams.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000228 virtual bool SetRecvRtpHeaderExtensions(
229 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
230 virtual bool SetSendRtpHeaderExtensions(
231 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000232 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
233 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
234 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
235 *options = options_;
236 return true;
237 }
238 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
239 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
240
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241 virtual void OnMessage(rtc::Message* msg) OVERRIDE;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000242
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000243 virtual void OnLoadUpdate(Load load) OVERRIDE;
244
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000245 // Implemented for VideoMediaChannelTest.
246 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000247 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
249
250 private:
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000251 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
252 const StreamParams& sp) const;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000253 bool CodecIsExternallySupported(const std::string& name) const;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000254
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000255 struct VideoCodecSettings {
256 VideoCodecSettings();
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000257
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000258 bool operator ==(const VideoCodecSettings& other) const;
259
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000260 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000261 webrtc::FecConfig fec;
262 int rtx_payload_type;
263 };
264
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000265 // Wrapper for the sender part, this is where the capturer is connected and
266 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000267 class WebRtcVideoSendStream : public sigslot::has_slots<> {
268 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000269 WebRtcVideoSendStream(
270 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000271 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000272 const VideoOptions& options,
273 const Settable<VideoCodecSettings>& codec_settings,
274 const StreamParams& sp,
275 const std::vector<webrtc::RtpExtension>& rtp_extensions);
276
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000277 ~WebRtcVideoSendStream();
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
289 void Start();
290 void Stop();
291
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000292 VideoSenderInfo GetVideoSenderInfo();
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000293 void FillBandwidthEstimationInfo(BandwidthEstimationInfo* bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000294
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000295 void OnCpuResolutionRequest(
296 CoordinatedVideoAdapter::AdaptRequest adapt_request);
297
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000298 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000299 // Parameters needed to reconstruct the underlying stream.
300 // webrtc::VideoSendStream doesn't support setting a lot of options on the
301 // fly, so when those need to be changed we tear down and reconstruct with
302 // similar parameters depending on which options changed etc.
303 struct VideoSendStreamParameters {
304 VideoSendStreamParameters(
305 const webrtc::VideoSendStream::Config& config,
306 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000307 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000308 webrtc::VideoSendStream::Config config;
309 VideoOptions options;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000310 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000311 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
312 // typically changes when setting a new resolution or reconfiguring
313 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000314 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000315 };
316
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000317 struct AllocatedEncoder {
318 AllocatedEncoder(webrtc::VideoEncoder* encoder,
319 webrtc::VideoCodecType type,
320 bool external)
321 : encoder(encoder), type(type), external(external) {}
322 webrtc::VideoEncoder* encoder;
323 webrtc::VideoCodecType type;
324 bool external;
325 };
326
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000327 struct Dimensions {
328 Dimensions() : width(-1), height(-1), is_screencast(false) {}
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000329 int width;
330 int height;
331 bool is_screencast;
332 };
333
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000334 union VideoEncoderSettings {
335 webrtc::VideoCodecVP8 vp8;
336 webrtc::VideoCodecVP9 vp9;
337 };
338
339 static std::vector<webrtc::VideoStream> CreateVideoStreams(
340 const VideoCodec& codec,
341 const VideoOptions& options,
342 size_t num_streams);
343 static std::vector<webrtc::VideoStream> CreateSimulcastVideoStreams(
344 const VideoCodec& codec,
345 const VideoOptions& options,
346 size_t num_streams);
347
348 void* ConfigureVideoEncoderSettings(const VideoCodec& codec,
349 const VideoOptions& options)
350 EXCLUSIVE_LOCKS_REQUIRED(lock_);
351
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000352 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
353 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000354 void DestroyVideoEncoder(AllocatedEncoder* encoder)
355 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000356 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000357 const VideoOptions& options)
358 EXCLUSIVE_LOCKS_REQUIRED(lock_);
359 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000360 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
361 const Dimensions& dimensions,
362 const VideoCodec& codec) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000363 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000364 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000365
366 webrtc::Call* const call_;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000367 WebRtcVideoEncoderFactory* const external_encoder_factory_
368 GUARDED_BY(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000369
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000370 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000371 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000372 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000373 VideoEncoderSettings encoder_settings_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000374 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000375 Dimensions last_dimensions_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000376
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000377 VideoCapturer* capturer_ GUARDED_BY(lock_);
378 bool sending_ GUARDED_BY(lock_);
379 bool muted_ GUARDED_BY(lock_);
380 VideoFormat format_ GUARDED_BY(lock_);
381
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000382 rtc::CriticalSection frame_lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000383 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
384 };
385
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000386 // Wrapper for the receiver part, contains configs etc. that are needed to
387 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
388 // between webrtc::VideoRenderer and cricket::VideoRenderer.
389 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
390 public:
391 WebRtcVideoReceiveStream(
392 webrtc::Call*,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000393 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000394 const webrtc::VideoReceiveStream::Config& config,
395 const std::vector<VideoCodecSettings>& recv_codecs);
396 ~WebRtcVideoReceiveStream();
397
398 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
399 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
400
401 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
402 int time_to_render_ms) OVERRIDE;
403
404 void SetRenderer(cricket::VideoRenderer* renderer);
405 cricket::VideoRenderer* GetRenderer();
406
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000407 VideoReceiverInfo GetVideoReceiverInfo();
408
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000409 private:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000410 struct AllocatedDecoder {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000411 AllocatedDecoder(webrtc::VideoDecoder* decoder,
412 webrtc::VideoCodecType type,
413 bool external)
414 : decoder(decoder), type(type), external(external) {}
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000415 webrtc::VideoDecoder* decoder;
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000416 webrtc::VideoCodecType type;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000417 bool external;
418 };
419
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000420 void SetSize(int width, int height);
421 void RecreateWebRtcStream();
422
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000423 AllocatedDecoder CreateOrReuseVideoDecoder(
424 std::vector<AllocatedDecoder>* old_decoder,
425 const VideoCodec& codec);
426 void ClearDecoders(std::vector<AllocatedDecoder>* allocated_decoders);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000427
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000428 webrtc::Call* const call_;
429
430 webrtc::VideoReceiveStream* stream_;
431 webrtc::VideoReceiveStream::Config config_;
432
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000433 WebRtcVideoDecoderFactory* const external_decoder_factory_;
434 std::vector<AllocatedDecoder> allocated_decoders_;
435
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000436 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000437 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000438 int last_width_ GUARDED_BY(renderer_lock_);
439 int last_height_ GUARDED_BY(renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +0000440 // Expands remote RTP timestamps to int64_t to be able to estimate how long
441 // the stream has been running.
442 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
443 GUARDED_BY(renderer_lock_);
444 int64_t first_frame_timestamp_ GUARDED_BY(renderer_lock_);
445 // Start NTP time is estimated as current remote NTP time (estimated from
446 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
447 int64_t estimated_remote_start_ntp_time_ms_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000448 };
449
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000450 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000451 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000452
453 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
454 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
455
456 void StartAllSendStreams();
457 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000458
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000459 static std::vector<VideoCodecSettings> MapCodecs(
460 const std::vector<VideoCodec>& codecs);
461 std::vector<VideoCodecSettings> FilterSupportedCodecs(
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000462 const std::vector<VideoCodecSettings>& mapped_codecs) const;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000463
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000464 void FillSenderStats(VideoMediaInfo* info);
465 void FillReceiverStats(VideoMediaInfo* info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +0000466 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
467 VideoMediaInfo* info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000468
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000469 uint32_t rtcp_receiver_report_ssrc_;
470 bool sending_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000471 rtc::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000472 WebRtcCallFactory* call_factory_;
473
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000474 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000475
476 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
477 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000478
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000479 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000480 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000481 std::map<uint32, WebRtcVideoSendStream*> send_streams_
482 GUARDED_BY(stream_crit_);
483 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
484 GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000485
486 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000487 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
488
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000489 VoiceMediaChannel* const voice_channel_;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000490 WebRtcVideoEncoderFactory* const external_encoder_factory_;
491 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000492 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000493 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.org00873182014-11-25 14:03:34 +0000494 webrtc::Call::Config::BitrateConfig bitrate_config_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000495 VideoOptions options_;
496};
497
498} // namespace cricket
499
500#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_