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