blob: e93f5714592372f9a7a9b4d39b234069a1ac75d7 [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
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000097 virtual bool SupportsCodec(const cricket::VideoCodec& codec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000098};
99
100// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
101class WebRtcVideoEngine2 : public sigslot::has_slots<> {
102 public:
103 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
104 WebRtcVideoEngine2();
105 // Custom WebRtcVideoChannelFactory for testing purposes.
106 explicit WebRtcVideoEngine2(WebRtcVideoChannelFactory* channel_factory);
107 ~WebRtcVideoEngine2();
108
109 // Basic video engine implementation.
110 bool Init(talk_base::Thread* worker_thread);
111 void Terminate();
112
113 int GetCapabilities();
114 bool SetOptions(const VideoOptions& options);
115 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
116 VideoEncoderConfig GetDefaultEncoderConfig() const;
117
118 WebRtcVideoChannel2* CreateChannel(VoiceMediaChannel* voice_channel);
119
120 const std::vector<VideoCodec>& codecs() const;
121 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
122 void SetLogging(int min_sev, const char* filter);
123
124 bool EnableTimedRender();
125 // No-op, never used.
126 bool SetLocalRenderer(VideoRenderer* renderer);
127 // This is currently ignored.
128 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
129
130 // Set the VoiceEngine for A/V sync. This can only be called before Init.
131 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
132
133 // Functions called by WebRtcVideoChannel2.
134 const VideoFormat& default_codec_format() const {
135 return default_codec_format_;
136 }
137
138 bool FindCodec(const VideoCodec& in);
139 bool CanSendCodec(const VideoCodec& in,
140 const VideoCodec& current,
141 VideoCodec* out);
142 // Check whether the supplied trace should be ignored.
143 bool ShouldIgnoreTrace(const std::string& trace);
144
145 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
146
147 talk_base::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
148
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000149 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000150
151 private:
152 void Construct(WebRtcVideoChannelFactory* channel_factory,
153 WebRtcVoiceEngine* voice_engine,
154 talk_base::CpuMonitor* cpu_monitor);
155
156 talk_base::Thread* worker_thread_;
157 WebRtcVoiceEngine* voice_engine_;
158 std::vector<VideoCodec> video_codecs_;
159 std::vector<RtpHeaderExtension> rtp_header_extensions_;
160 VideoFormat default_codec_format_;
161
162 bool initialized_;
163
164 bool capture_started_;
165
166 // Critical section to protect the media processor register/unregister
167 // while processing a frame
168 talk_base::CriticalSection signal_media_critical_;
169
170 talk_base::scoped_ptr<talk_base::CpuMonitor> cpu_monitor_;
171 WebRtcVideoChannelFactory* channel_factory_;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000172 WebRtcVideoEncoderFactory2 default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000173};
174
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000175class WebRtcVideoChannel2 : public talk_base::MessageHandler,
176 public VideoMediaChannel,
177 public webrtc::newapi::Transport {
178 public:
179 WebRtcVideoChannel2(WebRtcVideoEngine2* engine,
180 VoiceMediaChannel* voice_channel,
181 WebRtcVideoEncoderFactory2* encoder_factory);
182 // For testing purposes insert a pre-constructed call to verify that
183 // WebRtcVideoChannel2 calls the correct corresponding methods.
184 WebRtcVideoChannel2(webrtc::Call* call,
185 WebRtcVideoEngine2* engine,
186 WebRtcVideoEncoderFactory2* encoder_factory);
187 ~WebRtcVideoChannel2();
188 bool Init();
189
190 // VideoMediaChannel implementation
191 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
192 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
193 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
194 virtual bool SetSendStreamFormat(uint32 ssrc,
195 const VideoFormat& format) OVERRIDE;
196 virtual bool SetRender(bool render) OVERRIDE;
197 virtual bool SetSend(bool send) OVERRIDE;
198
199 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
200 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
201 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
202 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
203 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
204 virtual bool GetStats(const StatsOptions& options,
205 VideoMediaInfo* info) OVERRIDE;
206 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
207 virtual bool SendIntraFrame() OVERRIDE;
208 virtual bool RequestIntraFrame() OVERRIDE;
209
210 virtual void OnPacketReceived(talk_base::Buffer* packet,
211 const talk_base::PacketTime& packet_time)
212 OVERRIDE;
213 virtual void OnRtcpReceived(talk_base::Buffer* packet,
214 const talk_base::PacketTime& packet_time)
215 OVERRIDE;
216 virtual void OnReadyToSend(bool ready) OVERRIDE;
217 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000218
219 // Set send/receive RTP header extensions. This must be done before creating
220 // streams as it only has effect on future streams.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000221 virtual bool SetRecvRtpHeaderExtensions(
222 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
223 virtual bool SetSendRtpHeaderExtensions(
224 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
225 virtual bool SetStartSendBandwidth(int bps) OVERRIDE;
226 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
227 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
228 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
229 *options = options_;
230 return true;
231 }
232 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
233 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
234
235 virtual void OnMessage(talk_base::Message* msg) OVERRIDE;
236
237 // Implemented for VideoMediaChannelTest.
238 bool sending() const { return sending_; }
239 uint32 GetDefaultChannelSsrc() { return default_send_ssrc_; }
240 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
241
242 private:
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000243 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
244 const StreamParams& sp) const;
245
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000246 struct VideoCodecSettings {
247 VideoCodecSettings();
248
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000249 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000250 webrtc::FecConfig fec;
251 int rtx_payload_type;
252 };
253
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000254 // Wrapper for the sender part, this is where the capturer is connected and
255 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000256 class WebRtcVideoSendStream : public sigslot::has_slots<> {
257 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000258 WebRtcVideoSendStream(
259 webrtc::Call* call,
260 WebRtcVideoEncoderFactory2* encoder_factory,
261 const VideoOptions& options,
262 const Settable<VideoCodecSettings>& codec_settings,
263 const StreamParams& sp,
264 const std::vector<webrtc::RtpExtension>& rtp_extensions);
265
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000266 ~WebRtcVideoSendStream();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000267 void SetOptions(const VideoOptions& options);
268 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000269 void SetRtpExtensions(
270 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000271
272 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
273 bool SetCapturer(VideoCapturer* capturer);
274 bool SetVideoFormat(const VideoFormat& format);
275 bool MuteStream(bool mute);
276 bool DisconnectCapturer();
277
278 void Start();
279 void Stop();
280
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000281 VideoSenderInfo GetVideoSenderInfo();
282
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000283 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000284 // Parameters needed to reconstruct the underlying stream.
285 // webrtc::VideoSendStream doesn't support setting a lot of options on the
286 // fly, so when those need to be changed we tear down and reconstruct with
287 // similar parameters depending on which options changed etc.
288 struct VideoSendStreamParameters {
289 VideoSendStreamParameters(
290 const webrtc::VideoSendStream::Config& config,
291 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000292 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000293 webrtc::VideoSendStream::Config config;
294 VideoOptions options;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000295 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000296 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
297 // typically changes when setting a new resolution or reconfiguring
298 // bitrates.
299 std::vector<webrtc::VideoStream> video_streams;
300 };
301
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000302 void SetCodecAndOptions(const VideoCodecSettings& codec,
303 const VideoOptions& options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000304 void RecreateWebRtcStream();
305 void SetDimensions(int width, int height);
306
307 webrtc::Call* const call_;
308 WebRtcVideoEncoderFactory2* const encoder_factory_;
309
310 talk_base::CriticalSection lock_;
311 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000312 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
313
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000314 VideoCapturer* capturer_ GUARDED_BY(lock_);
315 bool sending_ GUARDED_BY(lock_);
316 bool muted_ GUARDED_BY(lock_);
317 VideoFormat format_ GUARDED_BY(lock_);
318
319 talk_base::CriticalSection frame_lock_;
320 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
321 };
322
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000323 // Wrapper for the receiver part, contains configs etc. that are needed to
324 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
325 // between webrtc::VideoRenderer and cricket::VideoRenderer.
326 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
327 public:
328 WebRtcVideoReceiveStream(
329 webrtc::Call*,
330 const webrtc::VideoReceiveStream::Config& config,
331 const std::vector<VideoCodecSettings>& recv_codecs);
332 ~WebRtcVideoReceiveStream();
333
334 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
335 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
336
337 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
338 int time_to_render_ms) OVERRIDE;
339
340 void SetRenderer(cricket::VideoRenderer* renderer);
341 cricket::VideoRenderer* GetRenderer();
342
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000343 VideoReceiverInfo GetVideoReceiverInfo();
344
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000345 private:
346 void SetSize(int width, int height);
347 void RecreateWebRtcStream();
348
349 webrtc::Call* const call_;
350
351 webrtc::VideoReceiveStream* stream_;
352 webrtc::VideoReceiveStream::Config config_;
353
354 talk_base::CriticalSection renderer_lock_;
355 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000356 int last_width_ GUARDED_BY(renderer_lock_);
357 int last_height_ GUARDED_BY(renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000358 };
359
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000360 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
361
362 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
363 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
364
365 void StartAllSendStreams();
366 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000367
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000368 static std::vector<VideoCodecSettings> MapCodecs(
369 const std::vector<VideoCodec>& codecs);
370 std::vector<VideoCodecSettings> FilterSupportedCodecs(
371 const std::vector<VideoCodecSettings>& mapped_codecs);
372
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +0000373 void FillSenderStats(VideoMediaInfo* info);
374 void FillReceiverStats(VideoMediaInfo* info);
375 void FillBandwidthEstimationStats(VideoMediaInfo* info);
376
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000377 uint32_t rtcp_receiver_report_ssrc_;
378 bool sending_;
379 talk_base::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000380 uint32_t default_send_ssrc_;
381 uint32_t default_recv_ssrc_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000382 VideoRenderer* default_renderer_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000383
384 // Using primary-ssrc (first ssrc) as key.
385 std::map<uint32, WebRtcVideoSendStream*> send_streams_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000386 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000387
388 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000389 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
390
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000391 WebRtcVideoEncoderFactory2* const encoder_factory_;
392 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000393 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000394 VideoOptions options_;
395};
396
397} // namespace cricket
398
399#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_