blob: 4287d2826e4cb4acd42c0e270e76939ccedbc90a [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,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000089 size_t num_streams) = 0;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +000090
91 virtual webrtc::VideoEncoder* CreateVideoEncoder(
92 const VideoCodec& codec,
93 const VideoOptions& options) = 0;
94
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000095 virtual bool SupportsCodec(const cricket::VideoCodec& codec) = 0;
96};
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
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000147 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory() const;
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_;
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000170 talk_base::scoped_ptr<WebRtcVideoEncoderFactory2>
171 default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000172};
173
174// Adapter between webrtc::VideoRenderer and cricket::VideoRenderer.
175// The webrtc::VideoRenderer is set once, whereas the cricket::VideoRenderer can
176// be set after initialization. This adapter will also convert the incoming
177// webrtc::I420VideoFrame to a frame type that cricket::VideoRenderer can
178// render.
179class WebRtcVideoRenderer : public webrtc::VideoRenderer {
180 public:
181 WebRtcVideoRenderer();
182
183 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
184 int time_to_render_ms) OVERRIDE;
185
186 void SetRenderer(cricket::VideoRenderer* renderer);
187 cricket::VideoRenderer* GetRenderer();
188
189 private:
190 void SetSize(int width, int height);
191 int last_width_;
192 int last_height_;
193 talk_base::CriticalSection lock_;
194 cricket::VideoRenderer* renderer_ GUARDED_BY(lock_);
195};
196
197class WebRtcVideoChannel2 : public talk_base::MessageHandler,
198 public VideoMediaChannel,
199 public webrtc::newapi::Transport {
200 public:
201 WebRtcVideoChannel2(WebRtcVideoEngine2* engine,
202 VoiceMediaChannel* voice_channel,
203 WebRtcVideoEncoderFactory2* encoder_factory);
204 // For testing purposes insert a pre-constructed call to verify that
205 // WebRtcVideoChannel2 calls the correct corresponding methods.
206 WebRtcVideoChannel2(webrtc::Call* call,
207 WebRtcVideoEngine2* engine,
208 WebRtcVideoEncoderFactory2* encoder_factory);
209 ~WebRtcVideoChannel2();
210 bool Init();
211
212 // VideoMediaChannel implementation
213 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
214 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
215 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
216 virtual bool SetSendStreamFormat(uint32 ssrc,
217 const VideoFormat& format) OVERRIDE;
218 virtual bool SetRender(bool render) OVERRIDE;
219 virtual bool SetSend(bool send) OVERRIDE;
220
221 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
222 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
223 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
224 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
225 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
226 virtual bool GetStats(const StatsOptions& options,
227 VideoMediaInfo* info) OVERRIDE;
228 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
229 virtual bool SendIntraFrame() OVERRIDE;
230 virtual bool RequestIntraFrame() OVERRIDE;
231
232 virtual void OnPacketReceived(talk_base::Buffer* packet,
233 const talk_base::PacketTime& packet_time)
234 OVERRIDE;
235 virtual void OnRtcpReceived(talk_base::Buffer* packet,
236 const talk_base::PacketTime& packet_time)
237 OVERRIDE;
238 virtual void OnReadyToSend(bool ready) OVERRIDE;
239 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
240 virtual bool SetRecvRtpHeaderExtensions(
241 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
242 virtual bool SetSendRtpHeaderExtensions(
243 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
244 virtual bool SetStartSendBandwidth(int bps) OVERRIDE;
245 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
246 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
247 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
248 *options = options_;
249 return true;
250 }
251 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
252 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
253
254 virtual void OnMessage(talk_base::Message* msg) OVERRIDE;
255
256 // Implemented for VideoMediaChannelTest.
257 bool sending() const { return sending_; }
258 uint32 GetDefaultChannelSsrc() { return default_send_ssrc_; }
259 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
260
261 private:
262 struct VideoCodecSettings {
263 VideoCodecSettings();
264
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000265 VideoCodec codec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000266 webrtc::FecConfig fec;
267 int rtx_payload_type;
268 };
269
270 class WebRtcVideoSendStream : public sigslot::has_slots<> {
271 public:
272 WebRtcVideoSendStream(webrtc::Call* call,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000273 const webrtc::VideoSendStream::Config& config,
274 const VideoOptions& options,
275 const VideoCodec& codec,
276 const std::vector<webrtc::VideoStream>& video_streams,
277 WebRtcVideoEncoderFactory2* encoder_factory);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000278 ~WebRtcVideoSendStream();
279 void SetCodec(const VideoOptions& options, const VideoCodecSettings& codec);
280
281 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
282 bool SetCapturer(VideoCapturer* capturer);
283 bool SetVideoFormat(const VideoFormat& format);
284 bool MuteStream(bool mute);
285 bool DisconnectCapturer();
286
287 void Start();
288 void Stop();
289
290 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,
299 const VideoCodec& codec,
300 const std::vector<webrtc::VideoStream>& video_streams);
301 webrtc::VideoSendStream::Config config;
302 VideoOptions options;
303 VideoCodec codec;
304 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
305 // typically changes when setting a new resolution or reconfiguring
306 // bitrates.
307 std::vector<webrtc::VideoStream> video_streams;
308 };
309
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000310 void RecreateWebRtcStream();
311 void SetDimensions(int width, int height);
312
313 webrtc::Call* const call_;
314 WebRtcVideoEncoderFactory2* const encoder_factory_;
315
316 talk_base::CriticalSection lock_;
317 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000318 VideoSendStreamParameters parameters_ GUARDED_BY(lock_);
319
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000320 VideoCapturer* capturer_ GUARDED_BY(lock_);
321 bool sending_ GUARDED_BY(lock_);
322 bool muted_ GUARDED_BY(lock_);
323 VideoFormat format_ GUARDED_BY(lock_);
324
325 talk_base::CriticalSection frame_lock_;
326 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
327 };
328
329 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
330
331 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
332 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
333
334 void StartAllSendStreams();
335 void StopAllSendStreams();
336 void SetCodecForAllSendStreams(const VideoCodecSettings& codec);
337 static std::vector<VideoCodecSettings> MapCodecs(
338 const std::vector<VideoCodec>& codecs);
339 std::vector<VideoCodecSettings> FilterSupportedCodecs(
340 const std::vector<VideoCodecSettings>& mapped_codecs);
341
342 uint32_t rtcp_receiver_report_ssrc_;
343 bool sending_;
344 talk_base::scoped_ptr<webrtc::Call> call_;
345 std::map<uint32, WebRtcVideoRenderer*> renderers_;
346 VideoRenderer* default_renderer_;
347 uint32_t default_send_ssrc_;
348 uint32_t default_recv_ssrc_;
349
350 // Using primary-ssrc (first ssrc) as key.
351 std::map<uint32, WebRtcVideoSendStream*> send_streams_;
352 std::map<uint32, webrtc::VideoReceiveStream*> receive_streams_;
353
354 Settable<VideoCodecSettings> send_codec_;
355 WebRtcVideoEncoderFactory2* const encoder_factory_;
356 std::vector<VideoCodecSettings> recv_codecs_;
357 VideoOptions options_;
358};
359
360} // namespace cricket
361
362#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_