pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 45 | namespace webrtc { |
| 46 | class Call; |
| 47 | class VideoCaptureModule; |
| 48 | class VideoDecoder; |
| 49 | class VideoEncoder; |
| 50 | class VideoRender; |
| 51 | class VideoSendStreamInput; |
| 52 | class VideoReceiveStream; |
| 53 | } |
| 54 | |
| 55 | namespace talk_base { |
| 56 | class CpuMonitor; |
| 57 | class Thread; |
| 58 | } // namespace talk_base |
| 59 | |
| 60 | namespace cricket { |
| 61 | |
| 62 | class VideoCapturer; |
| 63 | class VideoFrame; |
| 64 | class VideoProcessor; |
| 65 | class VideoRenderer; |
| 66 | class VoiceMediaChannel; |
| 67 | class WebRtcVideoChannel2; |
| 68 | class WebRtcDecoderObserver; |
| 69 | class WebRtcEncoderObserver; |
| 70 | class WebRtcLocalStreamInfo; |
| 71 | class WebRtcRenderAdapter; |
| 72 | class WebRtcVideoChannelRecvInfo; |
| 73 | class WebRtcVideoChannelSendInfo; |
| 74 | class WebRtcVideoDecoderFactory; |
| 75 | class WebRtcVoiceEngine; |
| 76 | |
| 77 | struct CapturedFrame; |
| 78 | struct Device; |
| 79 | |
| 80 | class WebRtcVideoEngine2; |
| 81 | class WebRtcVideoChannel2; |
| 82 | |
| 83 | class WebRtcVideoEncoderFactory2 { |
| 84 | public: |
pbos@webrtc.org | 0d523ee | 2014-06-05 09:10:55 +0000 | [diff] [blame] | 85 | virtual ~WebRtcVideoEncoderFactory2(); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 86 | virtual std::vector<webrtc::VideoStream> CreateVideoStreams( |
| 87 | const VideoCodec& codec, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 88 | const VideoOptions& options, |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 89 | size_t num_streams); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 90 | |
| 91 | virtual webrtc::VideoEncoder* CreateVideoEncoder( |
| 92 | const VideoCodec& codec, |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 93 | const VideoOptions& options); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 94 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 95 | virtual bool SupportsCodec(const cricket::VideoCodec& codec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | // WebRtcVideoEngine2 is used for the new native WebRTC Video API (webrtc:1667). |
| 99 | class 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.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 147 | virtual WebRtcVideoEncoderFactory2* GetVideoEncoderFactory(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 148 | |
| 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.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 170 | WebRtcVideoEncoderFactory2 default_video_encoder_factory_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 171 | }; |
| 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. |
| 178 | class 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 | |
| 196 | class 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.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 239 | |
| 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.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 242 | 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.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 267 | VideoCodec codec; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 268 | webrtc::FecConfig fec; |
| 269 | int rtx_payload_type; |
| 270 | }; |
| 271 | |
| 272 | class WebRtcVideoSendStream : public sigslot::has_slots<> { |
| 273 | public: |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame^] | 274 | WebRtcVideoSendStream( |
| 275 | webrtc::Call* call, |
| 276 | WebRtcVideoEncoderFactory2* encoder_factory, |
| 277 | const VideoOptions& options, |
| 278 | const Settable<VideoCodecSettings>& codec_settings, |
| 279 | const StreamParams& sp, |
| 280 | const std::vector<webrtc::RtpExtension>& rtp_extensions); |
| 281 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 282 | ~WebRtcVideoSendStream(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame^] | 283 | void SetOptions(const VideoOptions& options); |
| 284 | void SetCodec(const VideoCodecSettings& codec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 285 | |
| 286 | void InputFrame(VideoCapturer* capturer, const VideoFrame* frame); |
| 287 | bool SetCapturer(VideoCapturer* capturer); |
| 288 | bool SetVideoFormat(const VideoFormat& format); |
| 289 | bool MuteStream(bool mute); |
| 290 | bool DisconnectCapturer(); |
| 291 | |
| 292 | void Start(); |
| 293 | void Stop(); |
| 294 | |
| 295 | private: |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 296 | // Parameters needed to reconstruct the underlying stream. |
| 297 | // webrtc::VideoSendStream doesn't support setting a lot of options on the |
| 298 | // fly, so when those need to be changed we tear down and reconstruct with |
| 299 | // similar parameters depending on which options changed etc. |
| 300 | struct VideoSendStreamParameters { |
| 301 | VideoSendStreamParameters( |
| 302 | const webrtc::VideoSendStream::Config& config, |
| 303 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame^] | 304 | const Settable<VideoCodecSettings>& codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 305 | webrtc::VideoSendStream::Config config; |
| 306 | VideoOptions options; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame^] | 307 | Settable<VideoCodecSettings> codec_settings; |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 308 | // Sent resolutions + bitrates etc. by the underlying VideoSendStream, |
| 309 | // typically changes when setting a new resolution or reconfiguring |
| 310 | // bitrates. |
| 311 | std::vector<webrtc::VideoStream> video_streams; |
| 312 | }; |
| 313 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame^] | 314 | void SetCodecAndOptions(const VideoCodecSettings& codec, |
| 315 | const VideoOptions& options); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 316 | void RecreateWebRtcStream(); |
| 317 | void SetDimensions(int width, int height); |
| 318 | |
| 319 | webrtc::Call* const call_; |
| 320 | WebRtcVideoEncoderFactory2* const encoder_factory_; |
| 321 | |
| 322 | talk_base::CriticalSection lock_; |
| 323 | webrtc::VideoSendStream* stream_ GUARDED_BY(lock_); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 324 | VideoSendStreamParameters parameters_ GUARDED_BY(lock_); |
| 325 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 326 | VideoCapturer* capturer_ GUARDED_BY(lock_); |
| 327 | bool sending_ GUARDED_BY(lock_); |
| 328 | bool muted_ GUARDED_BY(lock_); |
| 329 | VideoFormat format_ GUARDED_BY(lock_); |
| 330 | |
| 331 | talk_base::CriticalSection frame_lock_; |
| 332 | webrtc::I420VideoFrame video_frame_ GUARDED_BY(frame_lock_); |
| 333 | }; |
| 334 | |
| 335 | void Construct(webrtc::Call* call, WebRtcVideoEngine2* engine); |
| 336 | |
| 337 | virtual bool SendRtp(const uint8_t* data, size_t len) OVERRIDE; |
| 338 | virtual bool SendRtcp(const uint8_t* data, size_t len) OVERRIDE; |
| 339 | |
| 340 | void StartAllSendStreams(); |
| 341 | void StopAllSendStreams(); |
| 342 | void SetCodecForAllSendStreams(const VideoCodecSettings& codec); |
| 343 | static std::vector<VideoCodecSettings> MapCodecs( |
| 344 | const std::vector<VideoCodec>& codecs); |
| 345 | std::vector<VideoCodecSettings> FilterSupportedCodecs( |
| 346 | const std::vector<VideoCodecSettings>& mapped_codecs); |
| 347 | |
| 348 | uint32_t rtcp_receiver_report_ssrc_; |
| 349 | bool sending_; |
| 350 | talk_base::scoped_ptr<webrtc::Call> call_; |
| 351 | std::map<uint32, WebRtcVideoRenderer*> renderers_; |
| 352 | VideoRenderer* default_renderer_; |
| 353 | uint32_t default_send_ssrc_; |
| 354 | uint32_t default_recv_ssrc_; |
| 355 | |
| 356 | // Using primary-ssrc (first ssrc) as key. |
| 357 | std::map<uint32, WebRtcVideoSendStream*> send_streams_; |
| 358 | std::map<uint32, webrtc::VideoReceiveStream*> receive_streams_; |
| 359 | |
| 360 | Settable<VideoCodecSettings> send_codec_; |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 361 | std::vector<webrtc::RtpExtension> send_rtp_extensions_; |
| 362 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 363 | WebRtcVideoEncoderFactory2* const encoder_factory_; |
| 364 | std::vector<VideoCodecSettings> recv_codecs_; |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 365 | std::vector<webrtc::RtpExtension> recv_rtp_extensions_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 366 | VideoOptions options_; |
| 367 | }; |
| 368 | |
| 369 | } // namespace cricket |
| 370 | |
| 371 | #endif // TALK_MEDIA_WEBRTC_WEBRTCVIDEOENGINE2_H_ |