blob: 79371ab4cc2aed2226ba2da3c2fd6be0cb5294b4 [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>
32#include <vector>
33#include <string>
34
35#include "talk/base/cpumonitor.h"
36#include "talk/base/scoped_ptr.h"
37#include "talk/media/base/mediaengine.h"
38#include "talk/media/webrtc/webrtcvideochannelfactory.h"
39#include "webrtc/common_video/interface/i420_video_frame.h"
40#include "webrtc/system_wrappers/interface/thread_annotations.h"
41#include "webrtc/transport.h"
42#include "webrtc/video_renderer.h"
43#include "webrtc/video_send_stream.h"
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +000044#include "webrtc/video_receive_stream.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000045
46namespace webrtc {
47class Call;
48class VideoCaptureModule;
49class VideoDecoder;
50class VideoEncoder;
51class VideoRender;
52class VideoSendStreamInput;
53class VideoReceiveStream;
54}
55
56namespace talk_base {
57class CpuMonitor;
58class Thread;
59} // namespace talk_base
60
61namespace cricket {
62
63class VideoCapturer;
64class VideoFrame;
65class VideoProcessor;
66class VideoRenderer;
67class VoiceMediaChannel;
68class WebRtcVideoChannel2;
69class WebRtcDecoderObserver;
70class WebRtcEncoderObserver;
71class WebRtcLocalStreamInfo;
72class WebRtcRenderAdapter;
73class WebRtcVideoChannelRecvInfo;
74class WebRtcVideoChannelSendInfo;
75class WebRtcVideoDecoderFactory;
76class WebRtcVoiceEngine;
77
78struct CapturedFrame;
79struct Device;
80
81class WebRtcVideoEngine2;
82class WebRtcVideoChannel2;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +000083class WebRtcVideoRenderer;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000084
85class WebRtcVideoEncoderFactory2 {
86 public:
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +000087 virtual ~WebRtcVideoEncoderFactory2();
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000088 virtual std::vector<webrtc::VideoStream> CreateVideoStreams(
89 const VideoCodec& codec,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000090 const VideoOptions& options,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000091 size_t num_streams);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000092
93 virtual webrtc::VideoEncoder* CreateVideoEncoder(
94 const VideoCodec& codec,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000095 const VideoOptions& options);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000096
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +000097 virtual void* CreateVideoEncoderSettings(
98 const VideoCodec& codec,
99 const VideoOptions& options);
100
101 virtual void DestroyVideoEncoderSettings(const VideoCodec& codec,
102 void* encoder_settings);
103
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000104 virtual bool SupportsCodec(const cricket::VideoCodec& codec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000105};
106
107// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
108class WebRtcVideoEngine2 : public sigslot::has_slots<> {
109 public:
110 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
111 WebRtcVideoEngine2();
112 // Custom WebRtcVideoChannelFactory for testing purposes.
113 explicit WebRtcVideoEngine2(WebRtcVideoChannelFactory* channel_factory);
114 ~WebRtcVideoEngine2();
115
116 // Basic video engine implementation.
117 bool Init(talk_base::Thread* worker_thread);
118 void Terminate();
119
120 int GetCapabilities();
121 bool SetOptions(const VideoOptions& options);
122 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
123 VideoEncoderConfig GetDefaultEncoderConfig() const;
124
125 WebRtcVideoChannel2* CreateChannel(VoiceMediaChannel* voice_channel);
126
127 const std::vector<VideoCodec>& codecs() const;
128 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
129 void SetLogging(int min_sev, const char* filter);
130
131 bool EnableTimedRender();
132 // No-op, never used.
133 bool SetLocalRenderer(VideoRenderer* renderer);
134 // This is currently ignored.
135 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
136
137 // Set the VoiceEngine for A/V sync. This can only be called before Init.
138 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
139
140 // Functions called by WebRtcVideoChannel2.
141 const VideoFormat& default_codec_format() const {
142 return default_codec_format_;
143 }
144
145 bool FindCodec(const VideoCodec& in);
146 bool CanSendCodec(const VideoCodec& in,
147 const VideoCodec& current,
148 VideoCodec* out);
149 // Check whether the supplied trace should be ignored.
150 bool ShouldIgnoreTrace(const std::string& trace);
151
152 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
153
154 talk_base::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
155
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000156 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000157
158 private:
159 void Construct(WebRtcVideoChannelFactory* channel_factory,
160 WebRtcVoiceEngine* voice_engine,
161 talk_base::CpuMonitor* cpu_monitor);
162
163 talk_base::Thread* worker_thread_;
164 WebRtcVoiceEngine* voice_engine_;
165 std::vector<VideoCodec> video_codecs_;
166 std::vector<RtpHeaderExtension> rtp_header_extensions_;
167 VideoFormat default_codec_format_;
168
169 bool initialized_;
170
171 bool capture_started_;
172
173 // Critical section to protect the media processor register/unregister
174 // while processing a frame
175 talk_base::CriticalSection signal_media_critical_;
176
177 talk_base::scoped_ptr<talk_base::CpuMonitor> cpu_monitor_;
178 WebRtcVideoChannelFactory* channel_factory_;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000179 WebRtcVideoEncoderFactory2 default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000180};
181
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000182class WebRtcVideoChannel2 : public talk_base::MessageHandler,
183 public VideoMediaChannel,
184 public webrtc::newapi::Transport {
185 public:
186 WebRtcVideoChannel2(WebRtcVideoEngine2* engine,
187 VoiceMediaChannel* voice_channel,
188 WebRtcVideoEncoderFactory2* encoder_factory);
189 // For testing purposes insert a pre-constructed call to verify that
190 // WebRtcVideoChannel2 calls the correct corresponding methods.
191 WebRtcVideoChannel2(webrtc::Call* call,
192 WebRtcVideoEngine2* engine,
193 WebRtcVideoEncoderFactory2* encoder_factory);
194 ~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
217 virtual void OnPacketReceived(talk_base::Buffer* packet,
218 const talk_base::PacketTime& packet_time)
219 OVERRIDE;
220 virtual void OnRtcpReceived(talk_base::Buffer* packet,
221 const talk_base::PacketTime& packet_time)
222 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;
232 virtual bool SetStartSendBandwidth(int bps) OVERRIDE;
233 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
234 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
235 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
236 *options = options_;
237 return true;
238 }
239 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
240 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
241
242 virtual void OnMessage(talk_base::Message* msg) OVERRIDE;
243
244 // Implemented for VideoMediaChannelTest.
245 bool sending() const { return sending_; }
246 uint32 GetDefaultChannelSsrc() { return default_send_ssrc_; }
247 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
248
249 private:
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000250 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
251 const StreamParams& sp) const;
252
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000253 struct VideoCodecSettings {
254 VideoCodecSettings();
255
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000256 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000257 webrtc::FecConfig fec;
258 int rtx_payload_type;
259 };
260
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000261 // Wrapper for the sender part, this is where the capturer is connected and
262 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000263 class WebRtcVideoSendStream : public sigslot::has_slots<> {
264 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000265 WebRtcVideoSendStream(
266 webrtc::Call* call,
267 WebRtcVideoEncoderFactory2* encoder_factory,
268 const VideoOptions& options,
269 const Settable<VideoCodecSettings>& codec_settings,
270 const StreamParams& sp,
271 const std::vector<webrtc::RtpExtension>& rtp_extensions);
272
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000273 ~WebRtcVideoSendStream();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000274 void SetOptions(const VideoOptions& options);
275 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000276 void SetRtpExtensions(
277 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000278
279 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
280 bool SetCapturer(VideoCapturer* capturer);
281 bool SetVideoFormat(const VideoFormat& format);
282 bool MuteStream(bool mute);
283 bool DisconnectCapturer();
284
285 void Start();
286 void Stop();
287
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000288 VideoSenderInfo GetVideoSenderInfo();
289
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000290 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000291 // Parameters needed to reconstruct the underlying stream.
292 // webrtc::VideoSendStream doesn't support setting a lot of options on the
293 // fly, so when those need to be changed we tear down and reconstruct with
294 // similar parameters depending on which options changed etc.
295 struct VideoSendStreamParameters {
296 VideoSendStreamParameters(
297 const webrtc::VideoSendStream::Config& config,
298 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000299 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000300 webrtc::VideoSendStream::Config config;
301 VideoOptions options;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000302 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000303 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
304 // typically changes when setting a new resolution or reconfiguring
305 // bitrates.
306 std::vector<webrtc::VideoStream> video_streams;
307 };
308
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000309 void SetCodecAndOptions(const VideoCodecSettings& codec,
310 const VideoOptions& options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000311 void RecreateWebRtcStream();
312 void SetDimensions(int width, int height);
313
314 webrtc::Call* const call_;
315 WebRtcVideoEncoderFactory2* const encoder_factory_;
316
317 talk_base::CriticalSection lock_;
318 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000319 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
320
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000321 VideoCapturer* capturer_ GUARDED_BY(lock_);
322 bool sending_ GUARDED_BY(lock_);
323 bool muted_ GUARDED_BY(lock_);
324 VideoFormat format_ GUARDED_BY(lock_);
325
326 talk_base::CriticalSection frame_lock_;
327 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
328 };
329
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000330 // Wrapper for the receiver part, contains configs etc. that are needed to
331 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
332 // between webrtc::VideoRenderer and cricket::VideoRenderer.
333 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
334 public:
335 WebRtcVideoReceiveStream(
336 webrtc::Call*,
337 const webrtc::VideoReceiveStream::Config& config,
338 const std::vector<VideoCodecSettings>& recv_codecs);
339 ~WebRtcVideoReceiveStream();
340
341 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
342 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
343
344 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
345 int time_to_render_ms) OVERRIDE;
346
347 void SetRenderer(cricket::VideoRenderer* renderer);
348 cricket::VideoRenderer* GetRenderer();
349
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000350 VideoReceiverInfo GetVideoReceiverInfo();
351
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000352 private:
353 void SetSize(int width, int height);
354 void RecreateWebRtcStream();
355
356 webrtc::Call* const call_;
357
358 webrtc::VideoReceiveStream* stream_;
359 webrtc::VideoReceiveStream::Config config_;
360
361 talk_base::CriticalSection renderer_lock_;
362 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000363 int last_width_ GUARDED_BY(renderer_lock_);
364 int last_height_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000365 };
366
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000367 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000368 void SetDefaultOptions();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000369
370 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
371 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
372
373 void StartAllSendStreams();
374 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000375
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000376 static std::vector<VideoCodecSettings> MapCodecs(
377 const std::vector<VideoCodec>& codecs);
378 std::vector<VideoCodecSettings> FilterSupportedCodecs(
379 const std::vector<VideoCodecSettings>& mapped_codecs);
380
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000381 void FillSenderStats(VideoMediaInfo* info);
382 void FillReceiverStats(VideoMediaInfo* info);
383 void FillBandwidthEstimationStats(VideoMediaInfo* info);
384
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000385 uint32_t rtcp_receiver_report_ssrc_;
386 bool sending_;
387 talk_base::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000388 uint32_t default_send_ssrc_;
389 uint32_t default_recv_ssrc_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000390 VideoRenderer* default_renderer_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000391
392 // Using primary-ssrc (first ssrc) as key.
393 std::map<uint32, WebRtcVideoSendStream*> send_streams_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000394 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000395
396 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000397 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
398
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000399 WebRtcVideoEncoderFactory2* const encoder_factory_;
400 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000401 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000402 VideoOptions options_;
403};
404
405} // namespace cricket
406
407#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_