blob: 10a1608484788332d7cf8c4c9b376fa9d5d1c5a6 [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.orgb5a22b12014-05-13 11:07:01 +000086 virtual bool CreateEncoderSettings(
87 webrtc::VideoSendStream::Config::EncoderSettings* encoder_settings,
88 const VideoOptions& options,
89 const cricket::VideoCodec& codec,
90 size_t num_streams) = 0;
91 virtual bool SupportsCodec(const cricket::VideoCodec& codec) = 0;
92};
93
94// WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667).
95class WebRtcVideoEngine2 : public sigslot::has_slots<> {
96 public:
97 // Creates the WebRtcVideoEngine2 with internal VideoCaptureModule.
98 WebRtcVideoEngine2();
99 // Custom WebRtcVideoChannelFactory for testing purposes.
100 explicit WebRtcVideoEngine2(WebRtcVideoChannelFactory* channel_factory);
101 ~WebRtcVideoEngine2();
102
103 // Basic video engine implementation.
104 bool Init(talk_base::Thread* worker_thread);
105 void Terminate();
106
107 int GetCapabilities();
108 bool SetOptions(const VideoOptions& options);
109 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config);
110 VideoEncoderConfig GetDefaultEncoderConfig() const;
111
112 WebRtcVideoChannel2* CreateChannel(VoiceMediaChannel* voice_channel);
113
114 const std::vector<VideoCodec>& codecs() const;
115 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
116 void SetLogging(int min_sev, const char* filter);
117
118 bool EnableTimedRender();
119 // No-op, never used.
120 bool SetLocalRenderer(VideoRenderer* renderer);
121 // This is currently ignored.
122 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
123
124 // Set the VoiceEngine for A/V sync. This can only be called before Init.
125 bool SetVoiceEngine(WebRtcVoiceEngine* voice_engine);
126
127 // Functions called by WebRtcVideoChannel2.
128 const VideoFormat& default_codec_format() const {
129 return default_codec_format_;
130 }
131
132 bool FindCodec(const VideoCodec& in);
133 bool CanSendCodec(const VideoCodec& in,
134 const VideoCodec& current,
135 VideoCodec* out);
136 // Check whether the supplied trace should be ignored.
137 bool ShouldIgnoreTrace(const std::string& trace);
138
139 VideoFormat GetStartCaptureFormat() const { return default_codec_format_; }
140
141 talk_base::CpuMonitor* cpu_monitor() { return cpu_monitor_.get(); }
142
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000143 virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory() const;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000144
145 private:
146 void Construct(WebRtcVideoChannelFactory* channel_factory,
147 WebRtcVoiceEngine* voice_engine,
148 talk_base::CpuMonitor* cpu_monitor);
149
150 talk_base::Thread* worker_thread_;
151 WebRtcVoiceEngine* voice_engine_;
152 std::vector<VideoCodec> video_codecs_;
153 std::vector<RtpHeaderExtension> rtp_header_extensions_;
154 VideoFormat default_codec_format_;
155
156 bool initialized_;
157
158 bool capture_started_;
159
160 // Critical section to protect the media processor register/unregister
161 // while processing a frame
162 talk_base::CriticalSection signal_media_critical_;
163
164 talk_base::scoped_ptr<talk_base::CpuMonitor> cpu_monitor_;
165 WebRtcVideoChannelFactory* channel_factory_;
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000166 talk_base::scoped_ptr<WebRtcVideoEncoderFactory2>
167 default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000168};
169
170// Adapter between webrtc::VideoRenderer and cricket::VideoRenderer.
171// The webrtc::VideoRenderer is set once, whereas the cricket::VideoRenderer can
172// be set after initialization. This adapter will also convert the incoming
173// webrtc::I420VideoFrame to a frame type that cricket::VideoRenderer can
174// render.
175class WebRtcVideoRenderer : public webrtc::VideoRenderer {
176 public:
177 WebRtcVideoRenderer();
178
179 virtual void RenderFrame(const webrtc::I420VideoFrame& frame,
180 int time_to_render_ms) OVERRIDE;
181
182 void SetRenderer(cricket::VideoRenderer* renderer);
183 cricket::VideoRenderer* GetRenderer();
184
185 private:
186 void SetSize(int width, int height);
187 int last_width_;
188 int last_height_;
189 talk_base::CriticalSection lock_;
190 cricket::VideoRenderer* renderer_ GUARDED_BY(lock_);
191};
192
193class WebRtcVideoChannel2 : public talk_base::MessageHandler,
194 public VideoMediaChannel,
195 public webrtc::newapi::Transport {
196 public:
197 WebRtcVideoChannel2(WebRtcVideoEngine2* engine,
198 VoiceMediaChannel* voice_channel,
199 WebRtcVideoEncoderFactory2* encoder_factory);
200 // For testing purposes insert a pre-constructed call to verify that
201 // WebRtcVideoChannel2 calls the correct corresponding methods.
202 WebRtcVideoChannel2(webrtc::Call* call,
203 WebRtcVideoEngine2* engine,
204 WebRtcVideoEncoderFactory2* encoder_factory);
205 ~WebRtcVideoChannel2();
206 bool Init();
207
208 // VideoMediaChannel implementation
209 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
210 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) OVERRIDE;
211 virtual bool GetSendCodec(VideoCodec* send_codec) OVERRIDE;
212 virtual bool SetSendStreamFormat(uint32 ssrc,
213 const VideoFormat& format) OVERRIDE;
214 virtual bool SetRender(bool render) OVERRIDE;
215 virtual bool SetSend(bool send) OVERRIDE;
216
217 virtual bool AddSendStream(const StreamParams& sp) OVERRIDE;
218 virtual bool RemoveSendStream(uint32 ssrc) OVERRIDE;
219 virtual bool AddRecvStream(const StreamParams& sp) OVERRIDE;
220 virtual bool RemoveRecvStream(uint32 ssrc) OVERRIDE;
221 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) OVERRIDE;
222 virtual bool GetStats(const StatsOptions& options,
223 VideoMediaInfo* info) OVERRIDE;
224 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) OVERRIDE;
225 virtual bool SendIntraFrame() OVERRIDE;
226 virtual bool RequestIntraFrame() OVERRIDE;
227
228 virtual void OnPacketReceived(talk_base::Buffer* packet,
229 const talk_base::PacketTime& packet_time)
230 OVERRIDE;
231 virtual void OnRtcpReceived(talk_base::Buffer* packet,
232 const talk_base::PacketTime& packet_time)
233 OVERRIDE;
234 virtual void OnReadyToSend(bool ready) OVERRIDE;
235 virtual bool MuteStream(uint32 ssrc, bool mute) OVERRIDE;
236 virtual bool SetRecvRtpHeaderExtensions(
237 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
238 virtual bool SetSendRtpHeaderExtensions(
239 const std::vector<RtpHeaderExtension>& extensions) OVERRIDE;
240 virtual bool SetStartSendBandwidth(int bps) OVERRIDE;
241 virtual bool SetMaxSendBandwidth(int bps) OVERRIDE;
242 virtual bool SetOptions(const VideoOptions& options) OVERRIDE;
243 virtual bool GetOptions(VideoOptions* options) const OVERRIDE {
244 *options = options_;
245 return true;
246 }
247 virtual void SetInterface(NetworkInterface* iface) OVERRIDE;
248 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) OVERRIDE;
249
250 virtual void OnMessage(talk_base::Message* msg) OVERRIDE;
251
252 // Implemented for VideoMediaChannelTest.
253 bool sending() const { return sending_; }
254 uint32 GetDefaultChannelSsrc() { return default_send_ssrc_; }
255 bool GetRenderer(uint32 ssrc, VideoRenderer** renderer);
256
257 private:
258 struct VideoCodecSettings {
259 VideoCodecSettings();
260
261 cricket::VideoCodec codec;
262 webrtc::FecConfig fec;
263 int rtx_payload_type;
264 };
265
266 class WebRtcVideoSendStream : public sigslot::has_slots<> {
267 public:
268 WebRtcVideoSendStream(webrtc::Call* call,
269 const webrtc::VideoSendStream::Config& config,
270 WebRtcVideoEncoderFactory2* encoder_factory);
271 ~WebRtcVideoSendStream();
272 void SetCodec(const VideoOptions& options, const VideoCodecSettings& codec);
273
274 void InputFrame(VideoCapturer* capturer, const VideoFrame* frame);
275 bool SetCapturer(VideoCapturer* capturer);
276 bool SetVideoFormat(const VideoFormat& format);
277 bool MuteStream(bool mute);
278 bool DisconnectCapturer();
279
280 void Start();
281 void Stop();
282
283 private:
284 void RecreateWebRtcStream();
285 void SetDimensions(int width, int height);
286
287 webrtc::Call* const call_;
288 WebRtcVideoEncoderFactory2* const encoder_factory_;
289
290 talk_base::CriticalSection lock_;
291 webrtc::VideoSendStream* stream_ GUARDED_BY(lock_);
292 webrtc::VideoSendStream::Config config_ GUARDED_BY(lock_);
293 VideoCapturer* capturer_ GUARDED_BY(lock_);
294 bool sending_ GUARDED_BY(lock_);
295 bool muted_ GUARDED_BY(lock_);
296 VideoFormat format_ GUARDED_BY(lock_);
297
298 talk_base::CriticalSection frame_lock_;
299 webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_);
300 };
301
302 void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine);
303
304 virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE;
305 virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE;
306
307 void StartAllSendStreams();
308 void StopAllSendStreams();
309 void SetCodecForAllSendStreams(const VideoCodecSettings& codec);
310 static std::vector<VideoCodecSettings> MapCodecs(
311 const std::vector<VideoCodec>& codecs);
312 std::vector<VideoCodecSettings> FilterSupportedCodecs(
313 const std::vector<VideoCodecSettings>& mapped_codecs);
314
315 uint32_t rtcp_receiver_report_ssrc_;
316 bool sending_;
317 talk_base::scoped_ptr<webrtc::Call> call_;
318 std::map<uint32, WebRtcVideoRenderer*> renderers_;
319 VideoRenderer* default_renderer_;
320 uint32_t default_send_ssrc_;
321 uint32_t default_recv_ssrc_;
322
323 // Using primary-ssrc (first ssrc) as key.
324 std::map<uint32, WebRtcVideoSendStream*> send_streams_;
325 std::map<uint32, webrtc::VideoReceiveStream*> receive_streams_;
326
327 Settable<VideoCodecSettings> send_codec_;
328 WebRtcVideoEncoderFactory2* const encoder_factory_;
329 std::vector<VideoCodecSettings> recv_codecs_;
330 VideoOptions options_;
331};
332
333} // namespace cricket
334
335#endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_