blob: fe798b50c20f6699e02ecd0756a3fa75a62f3128 [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;
83
84class WebRtcVideoEncoderFactory2 {
85 public:
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +000086 virtual ~WebRtcVideoEncoderFactory2();
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000087 virtual std::vector<webrtc::VideoStream> CreateVideoStreams(
88 const VideoCodec& codec,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000089 const VideoOptions& options,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000090 size_t num_streams);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000091
92 virtual webrtc::VideoEncoder* CreateVideoEncoder(
93 const VideoCodec& codec,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000094 const VideoOptions& options);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000095
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000096 virtual bool SupportsCodec(const cricket::VideoCodec& codec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000097};
98
99// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
100class WebRtcVideoEngine2 : public sigslot::has_slots<> {
101 public:
102 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
103 WebRtcVideoEngine2();
104 // Custom WebRtcVideoChannelFactory for testing purposes.
105 explicit WebRtcVideoEngine2(WebRtcVideoChannelFactory* channel_factory);
106 ~WebRtcVideoEngine2();
107
108 // Basic video engine implementation.
109 bool Init(talk_base::Thread* worker_thread);
110 void Terminate();
111
112 int GetCapabilities();
113 bool SetOptions(const VideoOptions& options);
114 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
115 VideoEncoderConfig GetDefaultEncoderConfig() const;
116
117 WebRtcVideoChannel2* CreateChannel(VoiceMediaChannel* voice_channel);
118
119 const std::vector<VideoCodec>& codecs() const;
120 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
121 void SetLogging(int min_sev, const char* filter);
122
123 bool EnableTimedRender();
124 // No-op, never used.
125 bool SetLocalRenderer(VideoRenderer* renderer);
126 // This is currently ignored.
127 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
128
129 // Set the VoiceEngine for A/V sync. This can only be called before Init.
130 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
131
132 // Functions called by WebRtcVideoChannel2.
133 const VideoFormat& default_codec_format() const {
134 return default_codec_format_;
135 }
136
137 bool FindCodec(const VideoCodec& in);
138 bool CanSendCodec(const VideoCodec& in,
139 const VideoCodec& current,
140 VideoCodec* out);
141 // Check whether the supplied trace should be ignored.
142 bool ShouldIgnoreTrace(const std::string& trace);
143
144 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
145
146 talk_base::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
147
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000148 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000149
150 private:
151 void Construct(WebRtcVideoChannelFactory* channel_factory,
152 WebRtcVoiceEngine* voice_engine,
153 talk_base::CpuMonitor* cpu_monitor);
154
155 talk_base::Thread* worker_thread_;
156 WebRtcVoiceEngine* voice_engine_;
157 std::vector<VideoCodec> video_codecs_;
158 std::vector<RtpHeaderExtension> rtp_header_extensions_;
159 VideoFormat default_codec_format_;
160
161 bool initialized_;
162
163 bool capture_started_;
164
165 // Critical section to protect the media processor register/unregister
166 // while processing a frame
167 talk_base::CriticalSection signal_media_critical_;
168
169 talk_base::scoped_ptr<talk_base::CpuMonitor> cpu_monitor_;
170 WebRtcVideoChannelFactory* channel_factory_;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000171 WebRtcVideoEncoderFactory2 default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000172};
173
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000174class WebRtcVideoChannel2 : public talk_base::MessageHandler,
175 public VideoMediaChannel,
176 public webrtc::newapi::Transport {
177 public:
178 WebRtcVideoChannel2(WebRtcVideoEngine2* engine,
179 VoiceMediaChannel* voice_channel,
180 WebRtcVideoEncoderFactory2* encoder_factory);
181 // For testing purposes insert a pre-constructed call to verify that
182 // WebRtcVideoChannel2 calls the correct corresponding methods.
183 WebRtcVideoChannel2(webrtc::Call* call,
184 WebRtcVideoEngine2* engine,
185 WebRtcVideoEncoderFactory2* encoder_factory);
186 ~WebRtcVideoChannel2();
187 bool Init();
188
189 // VideoMediaChannel implementation
190 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
191 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
192 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
193 virtual bool SetSendStreamFormat(uint32 ssrc,
194 const VideoFormat& format) OVERRIDE;
195 virtual bool SetRender(bool render) OVERRIDE;
196 virtual bool SetSend(bool send) OVERRIDE;
197
198 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
199 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
200 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
201 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
202 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
203 virtual bool GetStats(const StatsOptions& options,
204 VideoMediaInfo* info) OVERRIDE;
205 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
206 virtual bool SendIntraFrame() OVERRIDE;
207 virtual bool RequestIntraFrame() OVERRIDE;
208
209 virtual void OnPacketReceived(talk_base::Buffer* packet,
210 const talk_base::PacketTime& packet_time)
211 OVERRIDE;
212 virtual void OnRtcpReceived(talk_base::Buffer* packet,
213 const talk_base::PacketTime& packet_time)
214 OVERRIDE;
215 virtual void OnReadyToSend(bool ready) OVERRIDE;
216 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000217
218 // Set send/receive RTP header extensions. This must be done before creating
219 // streams as it only has effect on future streams.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000220 virtual bool SetRecvRtpHeaderExtensions(
221 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
222 virtual bool SetSendRtpHeaderExtensions(
223 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
224 virtual bool SetStartSendBandwidth(int bps) OVERRIDE;
225 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
226 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
227 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
228 *options = options_;
229 return true;
230 }
231 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
232 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
233
234 virtual void OnMessage(talk_base::Message* msg) OVERRIDE;
235
236 // Implemented for VideoMediaChannelTest.
237 bool sending() const { return sending_; }
238 uint32 GetDefaultChannelSsrc() { return default_send_ssrc_; }
239 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
240
241 private:
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000242 void ConfigureReceiverRtp(webrtc::VideoReceiveStream::Config* config,
243 const StreamParams& sp) const;
244
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000245 struct VideoCodecSettings {
246 VideoCodecSettings();
247
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000248 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000249 webrtc::FecConfig fec;
250 int rtx_payload_type;
251 };
252
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000253 // Wrapper for the sender part, this is where the capturer is connected and
254 // frames are then converted from cricket frames to webrtc frames.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000255 class WebRtcVideoSendStream : public sigslot::has_slots<> {
256 public:
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000257 WebRtcVideoSendStream(
258 webrtc::Call* call,
259 WebRtcVideoEncoderFactory2* encoder_factory,
260 const VideoOptions& options,
261 const Settable<VideoCodecSettings>& codec_settings,
262 const StreamParams& sp,
263 const std::vector<webrtc::RtpExtension>& rtp_extensions);
264
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000265 ~WebRtcVideoSendStream();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000266 void SetOptions(const VideoOptions& options);
267 void SetCodec(const VideoCodecSettings& codec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000268 void SetRtpExtensions(
269 const std::vector<webrtc::RtpExtension>& rtp_extensions);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000270
271 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
272 bool SetCapturer(VideoCapturer* capturer);
273 bool SetVideoFormat(const VideoFormat& format);
274 bool MuteStream(bool mute);
275 bool DisconnectCapturer();
276
277 void Start();
278 void Stop();
279
280 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000281 // Parameters needed to reconstruct the underlying stream.
282 // webrtc::VideoSendStream doesn't support setting a lot of options on the
283 // fly, so when those need to be changed we tear down and reconstruct with
284 // similar parameters depending on which options changed etc.
285 struct VideoSendStreamParameters {
286 VideoSendStreamParameters(
287 const webrtc::VideoSendStream::Config& config,
288 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000289 const Settable<VideoCodecSettings>& codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000290 webrtc::VideoSendStream::Config config;
291 VideoOptions options;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000292 Settable<VideoCodecSettings> codec_settings;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000293 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
294 // typically changes when setting a new resolution or reconfiguring
295 // bitrates.
296 std::vector<webrtc::VideoStream> video_streams;
297 };
298
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000299 void SetCodecAndOptions(const VideoCodecSettings& codec,
300 const VideoOptions& options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000301 void RecreateWebRtcStream();
302 void SetDimensions(int width, int height);
303
304 webrtc::Call* const call_;
305 WebRtcVideoEncoderFactory2* const encoder_factory_;
306
307 talk_base::CriticalSection lock_;
308 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000309 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
310
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000311 VideoCapturer* capturer_ GUARDED_BY(lock_);
312 bool sending_ GUARDED_BY(lock_);
313 bool muted_ GUARDED_BY(lock_);
314 VideoFormat format_ GUARDED_BY(lock_);
315
316 talk_base::CriticalSection frame_lock_;
317 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
318 };
319
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000320 // Wrapper for the receiver part, contains configs etc. that are needed to
321 // reconstruct the underlying VideoReceiveStream. Also serves as a wrapper
322 // between webrtc::VideoRenderer and cricket::VideoRenderer.
323 class WebRtcVideoReceiveStream : public webrtc::VideoRenderer {
324 public:
325 WebRtcVideoReceiveStream(
326 webrtc::Call*,
327 const webrtc::VideoReceiveStream::Config& config,
328 const std::vector<VideoCodecSettings>& recv_codecs);
329 ~WebRtcVideoReceiveStream();
330
331 void SetRecvCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
332 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions);
333
334 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
335 int time_to_render_ms) OVERRIDE;
336
337 void SetRenderer(cricket::VideoRenderer* renderer);
338 cricket::VideoRenderer* GetRenderer();
339
340 private:
341 void SetSize(int width, int height);
342 void RecreateWebRtcStream();
343
344 webrtc::Call* const call_;
345
346 webrtc::VideoReceiveStream* stream_;
347 webrtc::VideoReceiveStream::Config config_;
348
349 talk_base::CriticalSection renderer_lock_;
350 cricket::VideoRenderer* renderer_ GUARDED_BY(renderer_lock_);
351 int last_width_;
352 int last_height_;
353 };
354
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000355 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
356
357 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
358 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
359
360 void StartAllSendStreams();
361 void StopAllSendStreams();
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000362
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000363 static std::vector<VideoCodecSettings> MapCodecs(
364 const std::vector<VideoCodec>& codecs);
365 std::vector<VideoCodecSettings> FilterSupportedCodecs(
366 const std::vector<VideoCodecSettings>& mapped_codecs);
367
368 uint32_t rtcp_receiver_report_ssrc_;
369 bool sending_;
370 talk_base::scoped_ptr<webrtc::Call> call_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000371 uint32_t default_send_ssrc_;
372 uint32_t default_recv_ssrc_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000373 VideoRenderer* default_renderer_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000374
375 // Using primary-ssrc (first ssrc) as key.
376 std::map<uint32, WebRtcVideoSendStream*> send_streams_;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000377 std::map<uint32, WebRtcVideoReceiveStream*> receive_streams_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000378
379 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000380 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
381
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000382 WebRtcVideoEncoderFactory2* const encoder_factory_;
383 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000384 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000385 VideoOptions options_;
386};
387
388} // namespace cricket
389
390#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_