blob: 81466eb65791c166d5b0ce30a2c8671a63097177 [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"
44
45namespace webrtc {
46class Call;
47class VideoCaptureModule;
48class VideoDecoder;
49class VideoEncoder;
50class VideoRender;
51class VideoSendStreamInput;
52class VideoReceiveStream;
53}
54
55namespace talk_base {
56class CpuMonitor;
57class Thread;
58} // namespace talk_base
59
60namespace cricket {
61
62class VideoCapturer;
63class VideoFrame;
64class VideoProcessor;
65class VideoRenderer;
66class VoiceMediaChannel;
67class WebRtcVideoChannel2;
68class WebRtcDecoderObserver;
69class WebRtcEncoderObserver;
70class WebRtcLocalStreamInfo;
71class WebRtcRenderAdapter;
72class WebRtcVideoChannelRecvInfo;
73class WebRtcVideoChannelSendInfo;
74class WebRtcVideoDecoderFactory;
75class WebRtcVoiceEngine;
76
77struct CapturedFrame;
78struct Device;
79
80class WebRtcVideoEngine2;
81class WebRtcVideoChannel2;
82
83class WebRtcVideoEncoderFactory2 {
84 public:
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +000085 virtual ~WebRtcVideoEncoderFactory2();
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000086 virtual std::vector<webrtc::VideoStream> CreateVideoStreams(
87 const VideoCodec& codec,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000088 const VideoOptions& options,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000089 size_t num_streams);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000090
91 virtual webrtc::VideoEncoder* CreateVideoEncoder(
92 const VideoCodec& codec,
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000093 const VideoOptions& options);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000094
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +000095 virtual bool SupportsCodec(const cricket::VideoCodec& codec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000096};
97
98// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
99class WebRtcVideoEngine2 : public sigslot::has_slots<> {
100 public:
101 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
102 WebRtcVideoEngine2();
103 // Custom WebRtcVideoChannelFactory for testing purposes.
104 explicit WebRtcVideoEngine2(WebRtcVideoChannelFactory* channel_factory);
105 ~WebRtcVideoEngine2();
106
107 // Basic video engine implementation.
108 bool Init(talk_base::Thread* worker_thread);
109 void Terminate();
110
111 int GetCapabilities();
112 bool SetOptions(const VideoOptions& options);
113 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
114 VideoEncoderConfig GetDefaultEncoderConfig() const;
115
116 WebRtcVideoChannel2* CreateChannel(VoiceMediaChannel* voice_channel);
117
118 const std::vector<VideoCodec>& codecs() const;
119 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
120 void SetLogging(int min_sev, const char* filter);
121
122 bool EnableTimedRender();
123 // No-op, never used.
124 bool SetLocalRenderer(VideoRenderer* renderer);
125 // This is currently ignored.
126 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
127
128 // Set the VoiceEngine for A/V sync. This can only be called before Init.
129 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
130
131 // Functions called by WebRtcVideoChannel2.
132 const VideoFormat& default_codec_format() const {
133 return default_codec_format_;
134 }
135
136 bool FindCodec(const VideoCodec& in);
137 bool CanSendCodec(const VideoCodec& in,
138 const VideoCodec& current,
139 VideoCodec* out);
140 // Check whether the supplied trace should be ignored.
141 bool ShouldIgnoreTrace(const std::string& trace);
142
143 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
144
145 talk_base::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
146
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000147 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000148
149 private:
150 void Construct(WebRtcVideoChannelFactory* channel_factory,
151 WebRtcVoiceEngine* voice_engine,
152 talk_base::CpuMonitor* cpu_monitor);
153
154 talk_base::Thread* worker_thread_;
155 WebRtcVoiceEngine* voice_engine_;
156 std::vector<VideoCodec> video_codecs_;
157 std::vector<RtpHeaderExtension> rtp_header_extensions_;
158 VideoFormat default_codec_format_;
159
160 bool initialized_;
161
162 bool capture_started_;
163
164 // Critical section to protect the media processor register/unregister
165 // while processing a frame
166 talk_base::CriticalSection signal_media_critical_;
167
168 talk_base::scoped_ptr<talk_base::CpuMonitor> cpu_monitor_;
169 WebRtcVideoChannelFactory* channel_factory_;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000170 WebRtcVideoEncoderFactory2 default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000171};
172
173// Adapter between webrtc::VideoRenderer and cricket::VideoRenderer.
174// The webrtc::VideoRenderer is set once, whereas the cricket::VideoRenderer can
175// be set after initialization. This adapter will also convert the incoming
176// webrtc::I420VideoFrame to a frame type that cricket::VideoRenderer can
177// render.
178class WebRtcVideoRenderer : public webrtc::VideoRenderer {
179 public:
180 WebRtcVideoRenderer();
181
182 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
183 int time_to_render_ms) OVERRIDE;
184
185 void SetRenderer(cricket::VideoRenderer* renderer);
186 cricket::VideoRenderer* GetRenderer();
187
188 private:
189 void SetSize(int width, int height);
190 int last_width_;
191 int last_height_;
192 talk_base::CriticalSection lock_;
193 cricket::VideoRenderer* renderer_ GUARDED_BY(lock_);
194};
195
196class WebRtcVideoChannel2 : public talk_base::MessageHandler,
197 public VideoMediaChannel,
198 public webrtc::newapi::Transport {
199 public:
200 WebRtcVideoChannel2(WebRtcVideoEngine2* engine,
201 VoiceMediaChannel* voice_channel,
202 WebRtcVideoEncoderFactory2* encoder_factory);
203 // For testing purposes insert a pre-constructed call to verify that
204 // WebRtcVideoChannel2 calls the correct corresponding methods.
205 WebRtcVideoChannel2(webrtc::Call* call,
206 WebRtcVideoEngine2* engine,
207 WebRtcVideoEncoderFactory2* encoder_factory);
208 ~WebRtcVideoChannel2();
209 bool Init();
210
211 // VideoMediaChannel implementation
212 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
213 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
214 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
215 virtual bool SetSendStreamFormat(uint32 ssrc,
216 const VideoFormat& format) OVERRIDE;
217 virtual bool SetRender(bool render) OVERRIDE;
218 virtual bool SetSend(bool send) OVERRIDE;
219
220 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
221 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
222 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
223 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
224 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
225 virtual bool GetStats(const StatsOptions& options,
226 VideoMediaInfo* info) OVERRIDE;
227 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
228 virtual bool SendIntraFrame() OVERRIDE;
229 virtual bool RequestIntraFrame() OVERRIDE;
230
231 virtual void OnPacketReceived(talk_base::Buffer* packet,
232 const talk_base::PacketTime& packet_time)
233 OVERRIDE;
234 virtual void OnRtcpReceived(talk_base::Buffer* packet,
235 const talk_base::PacketTime& packet_time)
236 OVERRIDE;
237 virtual void OnReadyToSend(bool ready) OVERRIDE;
238 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000239
240 // Set send/receive RTP header extensions. This must be done before creating
241 // streams as it only has effect on future streams.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000242 virtual bool SetRecvRtpHeaderExtensions(
243 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
244 virtual bool SetSendRtpHeaderExtensions(
245 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
246 virtual bool SetStartSendBandwidth(int bps) OVERRIDE;
247 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
248 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
249 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
250 *options = options_;
251 return true;
252 }
253 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
254 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
255
256 virtual void OnMessage(talk_base::Message* msg) OVERRIDE;
257
258 // Implemented for VideoMediaChannelTest.
259 bool sending() const { return sending_; }
260 uint32 GetDefaultChannelSsrc() { return default_send_ssrc_; }
261 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
262
263 private:
264 struct VideoCodecSettings {
265 VideoCodecSettings();
266
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000267 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000268 webrtc::FecConfig fec;
269 int rtx_payload_type;
270 };
271
272 class WebRtcVideoSendStream : public sigslot::has_slots<> {
273 public:
274 WebRtcVideoSendStream(webrtc::Call* call,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000275 const webrtc::VideoSendStream::Config& config,
276 const VideoOptions& options,
277 const VideoCodec& codec,
278 const std::vector<webrtc::VideoStream>& video_streams,
279 WebRtcVideoEncoderFactory2* encoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000280 ~WebRtcVideoSendStream();
281 void SetCodec(const VideoOptions& options, const VideoCodecSettings& codec);
282
283 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
284 bool SetCapturer(VideoCapturer* capturer);
285 bool SetVideoFormat(const VideoFormat& format);
286 bool MuteStream(bool mute);
287 bool DisconnectCapturer();
288
289 void Start();
290 void Stop();
291
292 private:
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000293 // Parameters needed to reconstruct the underlying stream.
294 // webrtc::VideoSendStream doesn't support setting a lot of options on the
295 // fly, so when those need to be changed we tear down and reconstruct with
296 // similar parameters depending on which options changed etc.
297 struct VideoSendStreamParameters {
298 VideoSendStreamParameters(
299 const webrtc::VideoSendStream::Config& config,
300 const VideoOptions& options,
301 const VideoCodec& codec,
302 const std::vector<webrtc::VideoStream>& video_streams);
303 webrtc::VideoSendStream::Config config;
304 VideoOptions options;
305 VideoCodec codec;
306 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
307 // typically changes when setting a new resolution or reconfiguring
308 // bitrates.
309 std::vector<webrtc::VideoStream> video_streams;
310 };
311
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000312 void RecreateWebRtcStream();
313 void SetDimensions(int width, int height);
314
315 webrtc::Call* const call_;
316 WebRtcVideoEncoderFactory2* const encoder_factory_;
317
318 talk_base::CriticalSection lock_;
319 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000320 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
321
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000322 VideoCapturer* capturer_ GUARDED_BY(lock_);
323 bool sending_ GUARDED_BY(lock_);
324 bool muted_ GUARDED_BY(lock_);
325 VideoFormat format_ GUARDED_BY(lock_);
326
327 talk_base::CriticalSection frame_lock_;
328 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
329 };
330
331 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
332
333 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
334 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
335
336 void StartAllSendStreams();
337 void StopAllSendStreams();
338 void SetCodecForAllSendStreams(const VideoCodecSettings& codec);
339 static std::vector<VideoCodecSettings> MapCodecs(
340 const std::vector<VideoCodec>& codecs);
341 std::vector<VideoCodecSettings> FilterSupportedCodecs(
342 const std::vector<VideoCodecSettings>& mapped_codecs);
343
344 uint32_t rtcp_receiver_report_ssrc_;
345 bool sending_;
346 talk_base::scoped_ptr<webrtc::Call> call_;
347 std::map<uint32, WebRtcVideoRenderer*> renderers_;
348 VideoRenderer* default_renderer_;
349 uint32_t default_send_ssrc_;
350 uint32_t default_recv_ssrc_;
351
352 // Using primary-ssrc (first ssrc) as key.
353 std::map<uint32, WebRtcVideoSendStream*> send_streams_;
354 std::map<uint32, webrtc::VideoReceiveStream*> receive_streams_;
355
356 Settable<VideoCodecSettings> send_codec_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000357 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
358
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000359 WebRtcVideoEncoderFactory2* const encoder_factory_;
360 std::vector<VideoCodecSettings> recv_codecs_;
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000361 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000362 VideoOptions options_;
363};
364
365} // namespace cricket
366
367#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_