blob: b9a8682ae5ff4abce274eb940024d1d4dbd86ed8 [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
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000109class WebRtcVideoEncoderFactory2 {
110 public:
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000111 virtual ~WebRtcVideoEncoderFactory2();
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000112 virtual std::vector<webrtc::VideoStream> CreateVideoStreams(
113 const VideoCodec& codec,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000114 const VideoOptions& options,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000115 size_t num_streams);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000116
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000117 virtual void* CreateVideoEncoderSettings(const VideoCodec& codec,
118 const VideoOptions& options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000119
120 virtual void DestroyVideoEncoderSettings(const VideoCodec& codec,
121 void* encoder_settings);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000122};
123
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000124// CallFactory, overridden for testing to verify that webrtc::Call is configured
125// properly.
126class WebRtcCallFactory {
127 public:
128 virtual ~WebRtcCallFactory();
129 virtual webrtc::Call* CreateCall(const webrtc::Call::Config& config);
130};
131
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000132// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
buildbot@webrtc.org3c16d8b2014-10-13 06:35:10 +0000133class WebRtcVideoEngine2 : public sigslot::has_slots<> {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134 public:
135 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
136 WebRtcVideoEngine2();
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000137 virtual ~WebRtcVideoEngine2();
138
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000139 // Used for testing to be able to check and use the webrtc::Call config.
140 void SetCallFactory(WebRtcCallFactory* call_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000141
142 // Basic video engine implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000143 bool Init(rtc::Thread* worker_thread);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000144 void Terminate();
145
146 int GetCapabilities();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000147 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
148 VideoEncoderConfig GetDefaultEncoderConfig() const;
149
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000150 WebRtcVideoChannel2* CreateChannel(const VideoOptions& options,
151 VoiceMediaChannel* voice_channel);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000152
153 const std::vector<VideoCodec>& codecs() const;
154 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
155 void SetLogging(int min_sev, const char* filter);
156
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000157 // Set a WebRtcVideoDecoderFactory for external decoding. Video engine does
158 // not take the ownership of |decoder_factory|. The caller needs to make sure
159 // that |decoder_factory| outlives the video engine.
160 void SetExternalDecoderFactory(WebRtcVideoDecoderFactory* decoder_factory);
161 // Set a WebRtcVideoEncoderFactory for external encoding. Video engine does
162 // not take the ownership of |encoder_factory|. The caller needs to make sure
163 // that |encoder_factory| outlives the video engine.
164 virtual void SetExternalEncoderFactory(
165 WebRtcVideoEncoderFactory* encoder_factory);
166
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000167 bool EnableTimedRender();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000168 // This is currently ignored.
169 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
170
171 // Set the VoiceEngine for A/V sync. This can only be called before Init.
172 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
173
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000174 bool FindCodec(const VideoCodec& in);
175 bool CanSendCodec(const VideoCodec& in,
176 const VideoCodec& current,
177 VideoCodec* out);
178 // Check whether the supplied trace should be ignored.
179 bool ShouldIgnoreTrace(const std::string& trace);
180
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000181 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
182
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 rtc::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000184
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000185 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000186
187 private:
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000188 std::vector<VideoCodec> GetSupportedCodecs() const;
189
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000190 rtc::Thread* worker_thread_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000191 WebRtcVoiceEngine* voice_engine_;
192 std::vector<VideoCodec> video_codecs_;
193 std::vector<RtpHeaderExtension> rtp_header_extensions_;
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000194 VideoFormat default_codec_format_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000195
196 bool initialized_;
197
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 rtc::scoped_ptr<rtc::CpuMonitor> cpu_monitor_;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000199 WebRtcVideoEncoderFactory2 default_video_encoder_factory_;
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000200
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000201 WebRtcCallFactory default_call_factory_;
202 WebRtcCallFactory* call_factory_;
203
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000204 WebRtcVideoDecoderFactory* external_decoder_factory_;
205 WebRtcVideoEncoderFactory* external_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000206};
207
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000208class WebRtcVideoChannel2 : public rtc::MessageHandler,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000209 public VideoMediaChannel,
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000210 public webrtc::newapi::Transport,
211 public webrtc::LoadObserver {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000212 public:
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000213 WebRtcVideoChannel2(WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000214 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000215 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000216 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000217 WebRtcVideoEncoderFactory* external_encoder_factory,
218 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000219 WebRtcVideoEncoderFactory2* encoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000220 ~WebRtcVideoChannel2();
221 bool Init();
222
223 // VideoMediaChannel implementation
224 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
225 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
226 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
227 virtual bool SetSendStreamFormat(uint32 ssrc,
228 const VideoFormat& format) OVERRIDE;
229 virtual bool SetRender(bool render) OVERRIDE;
230 virtual bool SetSend(bool send) OVERRIDE;
231
232 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
233 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
234 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
235 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
236 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
237 virtual bool GetStats(const StatsOptions& options,
238 VideoMediaInfo* info) OVERRIDE;
239 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
240 virtual bool SendIntraFrame() OVERRIDE;
241 virtual bool RequestIntraFrame() OVERRIDE;
242
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000243 virtual void OnPacketReceived(rtc::Buffer* packet,
244 const rtc::PacketTime& packet_time)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000245 OVERRIDE;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000246 virtual void OnRtcpReceived(rtc::Buffer* packet,
247 const rtc::PacketTime& packet_time)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248 OVERRIDE;
249 virtual void OnReadyToSend(bool ready) OVERRIDE;
250 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000251
252 // Set send/receive RTP header extensions. This must be done before creating
253 // streams as it only has effect on future streams.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000254 virtual bool SetRecvRtpHeaderExtensions(
255 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
256 virtual bool SetSendRtpHeaderExtensions(
257 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000258 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
259 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
260 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
261 *options = options_;
262 return true;
263 }
264 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
265 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
266
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 virtual void OnMessage(rtc::Message* msg) OVERRIDE;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000268
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000269 virtual void OnLoadUpdate(Load load) OVERRIDE;
270
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000271 // Implemented for VideoMediaChannelTest.
272 bool sending() const { return sending_; }
buildbot@webrtc.org2c0fb052014-08-13 16:47:12 +0000273 uint32 GetDefaultSendChannelSsrc() { return default_send_ssrc_; }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000274 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
275
276 private:
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000277 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
278 const StreamParams& sp) const;
279
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000280 struct VideoCodecSettings {
281 VideoCodecSettings();
282
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000283 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000284 webrtc::FecConfig fec;
285 int rtx_payload_type;
286 };
287
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000288 // Wrapper for the sender part, this is where the capturer is connected and
289 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000290 class WebRtcVideoSendStream : public sigslot::has_slots<> {
291 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000292 WebRtcVideoSendStream(
293 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000294 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000295 WebRtcVideoEncoderFactory2* encoder_factory,
296 const VideoOptions& options,
297 const Settable<VideoCodecSettings>& codec_settings,
298 const StreamParams& sp,
299 const std::vector<webrtc::RtpExtension>& rtp_extensions);
300
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000301 ~WebRtcVideoSendStream();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000302 void SetOptions(const VideoOptions& options);
303 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000304 void SetRtpExtensions(
305 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000306
307 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
308 bool SetCapturer(VideoCapturer* capturer);
309 bool SetVideoFormat(const VideoFormat& format);
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +0000310 void MuteStream(bool mute);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000311 bool DisconnectCapturer();
312
313 void Start();
314 void Stop();
315
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000316 VideoSenderInfo GetVideoSenderInfo();
317
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000318 void OnCpuResolutionRequest(
319 CoordinatedVideoAdapter::AdaptRequest adapt_request);
320
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000321 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000322 // Parameters needed to reconstruct the underlying stream.
323 // webrtc::VideoSendStream doesn't support setting a lot of options on the
324 // fly, so when those need to be changed we tear down and reconstruct with
325 // similar parameters depending on which options changed etc.
326 struct VideoSendStreamParameters {
327 VideoSendStreamParameters(
328 const webrtc::VideoSendStream::Config& config,
329 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000330 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000331 webrtc::VideoSendStream::Config config;
332 VideoOptions options;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000333 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000334 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
335 // typically changes when setting a new resolution or reconfiguring
336 // bitrates.
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000337 webrtc::VideoEncoderConfig encoder_config;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000338 };
339
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000340 struct AllocatedEncoder {
341 AllocatedEncoder(webrtc::VideoEncoder* encoder,
342 webrtc::VideoCodecType type,
343 bool external)
344 : encoder(encoder), type(type), external(external) {}
345 webrtc::VideoEncoder* encoder;
346 webrtc::VideoCodecType type;
347 bool external;
348 };
349
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000350 struct LastDimensions {
351 LastDimensions() : width(-1), height(-1), is_screencast(false) {}
352 int width;
353 int height;
354 bool is_screencast;
355 };
356
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000357 AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec)
358 EXCLUSIVE_LOCKS_REQUIRED(lock_);
359 void DestroyVideoEncoder(AllocatedEncoder* encoder);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000360 void SetCodecAndOptions(const VideoCodecSettings& codec,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000361 const VideoOptions& options)
362 EXCLUSIVE_LOCKS_REQUIRED(lock_);
363 void RecreateWebRtcStream() EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000364 void SetDimensions(int width, int height, bool is_screencast)
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +0000365 EXCLUSIVE_LOCKS_REQUIRED(lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000366
367 webrtc::Call* const call_;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000368 WebRtcVideoEncoderFactory* const external_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000369 WebRtcVideoEncoderFactory2* const encoder_factory_;
370
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000371 rtc::CriticalSection lock_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000372 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000373 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000374 AllocatedEncoder allocated_encoder_ GUARDED_BY(lock_);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000375 LastDimensions 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 {
411 AllocatedDecoder(webrtc::VideoDecoder* decoder, bool external)
412 : decoder(decoder), external(external) {}
413 webrtc::VideoDecoder* decoder;
414 bool external;
415 };
416
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000417 void SetSize(int width, int height);
418 void RecreateWebRtcStream();
419
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000420 void ClearDecoders();
421
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000422 webrtc::Call* const call_;
423
424 webrtc::VideoReceiveStream* stream_;
425 webrtc::VideoReceiveStream::Config config_;
426
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000427 WebRtcVideoDecoderFactory* const external_decoder_factory_;
428 std::vector<AllocatedDecoder> allocated_decoders_;
429
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 rtc::CriticalSection renderer_lock_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000431 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000432 int last_width_ GUARDED_BY(renderer_lock_);
433 int last_height_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000434 };
435
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000436 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000437 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000438
439 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
440 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
441
442 void StartAllSendStreams();
443 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000444
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000445 static std::vector<VideoCodecSettings> MapCodecs(
446 const std::vector<VideoCodec>& codecs);
447 std::vector<VideoCodecSettings> FilterSupportedCodecs(
448 const std::vector<VideoCodecSettings>& mapped_codecs);
449
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000450 void FillSenderStats(VideoMediaInfo* info);
451 void FillReceiverStats(VideoMediaInfo* info);
452 void FillBandwidthEstimationStats(VideoMediaInfo* info);
453
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000454 uint32_t rtcp_receiver_report_ssrc_;
455 bool sending_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456 rtc::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000457 WebRtcCallFactory* call_factory_;
458
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000459 uint32_t default_send_ssrc_;
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000460
461 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
462 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000463
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000464 rtc::CriticalSection stream_crit_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000465 // Using primary-ssrc (first ssrc) as key.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000466 std::map<uint32, WebRtcVideoSendStream*> send_streams_
467 GUARDED_BY(stream_crit_);
468 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_
469 GUARDED_BY(stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000470
471 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000472 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
473
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000474 VoiceMediaChannel* const voice_channel_;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000475 WebRtcVideoEncoderFactory* const external_encoder_factory_;
476 WebRtcVideoDecoderFactory* const external_decoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000477 WebRtcVideoEncoderFactory2* const encoder_factory_;
478 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000479 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000480 VideoOptions options_;
481};
482
483} // namespace cricket
484
485#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_