blob: 994c223ed06f8aaa59d8d8b6696324aa5176c62b [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:231/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Peter Boströme8f07352015-12-09 11:13:3011#ifndef WEBRTC_VIDEO_VIE_ENCODER_H_
12#define WEBRTC_VIDEO_VIE_ENCODER_H_
andrew@webrtc.orgb015cbe2012-10-22 18:19:2313
kwibergda89edd2016-03-01 19:52:3314#include <memory>
mflodman@webrtc.orgc51222c2015-02-06 13:10:1915#include <vector>
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:2916
Tommif93f04c2016-01-21 22:24:5917#include "webrtc/base/criticalsection.h"
Peter Boström623f2f02015-06-04 13:18:1718#include "webrtc/base/scoped_ref_ptr.h"
pbos@webrtc.orgd54aa962014-09-24 06:05:0019#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org281cff82013-05-17 13:44:4820#include "webrtc/common_types.h"
nisse8d132b92016-04-18 12:15:2221#include "webrtc/media/base/videosinkinterface.h"
Henrik Kjellander36a14b52015-11-04 07:31:5222#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Henrik Kjellanderfe7633e2015-11-18 21:00:2123#include "webrtc/modules/video_coding/include/video_coding_defines.h"
sprang470b2742016-04-15 08:24:1424#include "webrtc/modules/video_coding/utility/ivf_file_writer.h"
Henrik Kjellander82064b02015-11-18 21:31:2425#include "webrtc/modules/video_processing/include/video_processing.h"
pbos@webrtc.org281cff82013-05-17 13:44:4826#include "webrtc/typedefs.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:2327
28namespace webrtc {
29
andresp@webrtc.orgac6d9192013-05-13 10:50:5030class Config;
sprang@webrtc.org2e98d452013-11-26 11:41:5931class EncodedImageCallback;
Peter Boström211f6642016-02-05 10:13:2832class OveruseFrameDetector;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:3933class PacedSender;
mflodman@webrtc.orgc51222c2015-02-06 13:10:1934class PayloadRouter;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2335class ProcessThread;
36class QMVideoSettingsCallback;
pbos@webrtc.orgee9497c2014-12-01 15:23:2137class SendStatisticsProxy;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2338class ViEBitrateObserver;
39class ViEEffectFilter;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:0040class VideoCodingModule;
Peter Boström24debf52016-03-03 15:29:0241class VideoEncoder;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2342
Peter Boström7b628162016-02-19 19:42:1943class ViEEncoder : public VideoEncoderRateObserver,
Peter Boströmd5d98ba2015-06-26 04:58:1644 public VCMPacketizationCallback,
Peter Boström24debf52016-03-03 15:29:0245 public VCMSendStatisticsCallback {
andrew@webrtc.orgb015cbe2012-10-22 18:19:2346 public:
47 friend class ViEBitrateObserver;
48
mflodmanfd3f2cd2015-10-19 15:12:1249 ViEEncoder(uint32_t number_of_cores,
Peter Boström7b628162016-02-19 19:42:1950 const std::vector<uint32_t>& ssrcs,
Peter Boström39ac69b2015-09-22 14:28:5151 ProcessThread* module_process_thread,
52 SendStatisticsProxy* stats_proxy,
nisse8d132b92016-04-18 12:15:2253 // TODO(nisse): Used only for tests, delete?
54 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback,
Peter Boström211f6642016-02-05 10:13:2855 OveruseFrameDetector* overuse_detector,
Stefan Holmerca55fa12015-03-26 10:11:0656 PacedSender* pacer,
mflodman3e089d32016-03-11 14:44:3257 PayloadRouter* payload_router);
andrew@webrtc.orgb015cbe2012-10-22 18:19:2358 ~ViEEncoder();
59
60 bool Init();
61
Peter Boströmd94758e2016-02-05 10:25:4662 VideoCodingModule* vcm() const;
mflodman@webrtc.orgc51222c2015-02-06 13:10:1963
stefan@webrtc.orgdca71b22013-03-27 16:36:0164 void SetNetworkTransmissionState(bool is_transmitting);
65
andrew@webrtc.orgb015cbe2012-10-22 18:19:2366 // Returns the id of the owning channel.
67 int Owner() const;
68
69 // Drops incoming packets before they get to the encoder.
70 void Pause();
71 void Restart();
72
andrew@webrtc.orgb015cbe2012-10-22 18:19:2373 // Codec settings.
pbos@webrtc.org67879bc2013-04-09 13:41:5174 int32_t RegisterExternalEncoder(VideoEncoder* encoder,
75 uint8_t pl_type,
76 bool internal_source);
77 int32_t DeRegisterExternalEncoder(uint8_t pl_type);
Peter Boströmd009c2d2016-03-03 10:36:1878 void SetEncoder(const VideoCodec& video_codec, int min_transmit_bitrate_bps);
andrew@webrtc.orgb015cbe2012-10-22 18:19:2379
Peter Boström24debf52016-03-03 15:29:0280 void EncodeVideoFrame(const VideoFrame& video_frame);
Peter Boströmc03c1182016-01-18 19:23:4081 void SendKeyFrame();
andrew@webrtc.orgb015cbe2012-10-22 18:19:2382
pbos@webrtc.org71310302015-03-18 14:40:0383 uint32_t LastObservedBitrateBps() const;
pbos@webrtc.org67879bc2013-04-09 13:41:5184 int CodecTargetBitrate(uint32_t* bitrate) const;
Peter Boström2b918e62015-11-10 15:27:1285 // Loss protection. Must be called before SetEncoder() to have max packet size
86 // updated according to protection.
87 // TODO(pbos): Set protection method on construction or extract vcm_ outside
88 // this class and set it on construction there.
89 void SetProtectionMethod(bool nack, bool fec);
andrew@webrtc.orgb015cbe2012-10-22 18:19:2390
pbos@webrtc.org34c89ae2015-02-26 13:15:2291 // Implements VideoEncoderRateObserver.
92 void OnSetRates(uint32_t bitrate_bps, int framerate) override;
93
andrew@webrtc.orgb015cbe2012-10-22 18:19:2394 // Implements VCMPacketizationCallback.
kjellander@webrtc.org860ac532015-03-04 12:58:3595 int32_t SendData(uint8_t payload_type,
96 const EncodedImage& encoded_image,
Peter Boström982cbdf2016-01-28 18:11:5897 const RTPFragmentationHeader* fragmentation_header,
kjellander@webrtc.org860ac532015-03-04 12:58:3598 const RTPVideoHeader* rtp_video_hdr) override;
Peter Boström8468abc2015-12-18 15:01:1199 void OnEncoderImplementationName(const char* implementation_name) override;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23100
andrew@webrtc.orgb015cbe2012-10-22 18:19:23101 // Implements VideoSendStatisticsCallback.
kjellander@webrtc.org860ac532015-03-04 12:58:35102 int32_t SendStatistics(const uint32_t bit_rate,
103 const uint32_t frame_rate) override;
henrike@webrtc.org9cd0bbc2014-09-11 22:45:54104
Peter Boström7b628162016-02-19 19:42:19105 // virtual to test EncoderStateFeedback with mocks.
106 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
107 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
108 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29109
sprang@webrtc.org2e98d452013-11-26 11:41:59110 // New-style callbacks, used by VideoSendStream.
sprang@webrtc.org2e98d452013-11-26 11:41:59111 void RegisterPostEncodeImageCallback(
112 EncodedImageCallback* post_encode_callback);
pbos@webrtc.org63301bd2013-10-21 10:34:43113
stefan8e71cce2015-07-15 11:39:22114 int GetPaddingNeededBps() const;
Stefan Holmerca55fa12015-03-26 10:11:06115
mflodman3e089d32016-03-11 14:44:32116 void OnBitrateUpdated(uint32_t bitrate_bps,
stefan@webrtc.orga5f17c82014-12-08 19:46:23117 uint8_t fraction_lost,
pkasting@chromium.orga3166c42015-01-12 21:51:21118 int64_t round_trip_time_ms);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23119
120 private:
sprang470b2742016-04-15 08:24:14121 static const bool kEnableFrameRecording = false;
122 static const int kMaxLayers = 3;
123
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31124 bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
sprang@webrtc.org9a8c28f2014-11-04 16:27:16125 void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
126 void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
stefan@webrtc.orgdca71b22013-03-27 16:36:01127
pbos@webrtc.org67879bc2013-04-09 13:41:51128 const uint32_t number_of_cores_;
Peter Boström7b628162016-02-19 19:42:19129 const std::vector<uint32_t> ssrcs_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23130
kwibergda89edd2016-03-01 19:52:33131 const std::unique_ptr<VideoProcessing> vp_;
132 const std::unique_ptr<QMVideoSettingsCallback> qm_callback_;
133 const std::unique_ptr<VideoCodingModule> vcm_;
mflodman@webrtc.org52be6c22015-02-25 13:50:10134
pbos7979ebb2016-02-01 17:00:51135 rtc::CriticalSection data_cs_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23136
Peter Boström39ac69b2015-09-22 14:28:51137 SendStatisticsProxy* const stats_proxy_;
nisse8d132b92016-04-18 12:15:22138 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_;
Peter Boström211f6642016-02-05 10:13:28139 OveruseFrameDetector* const overuse_detector_;
Stefan Holmerca55fa12015-03-26 10:11:06140 PacedSender* const pacer_;
Peter Boströmd94758e2016-02-05 10:25:46141 PayloadRouter* const send_payload_router_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23142
Noah Richards14cb9492015-04-15 16:14:12143 // The time we last received an input frame or encoded frame. This is used to
144 // track when video is stopped long enough that we also want to stop sending
145 // padding.
146 int64_t time_of_last_frame_activity_ms_ GUARDED_BY(data_cs_);
Peter Boström2b918e62015-11-10 15:27:12147 VideoCodec encoder_config_ GUARDED_BY(data_cs_);
Peter Boströmd009c2d2016-03-03 10:36:18148 int min_transmit_bitrate_bps_ GUARDED_BY(data_cs_);
pbos@webrtc.org71310302015-03-18 14:40:03149 uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_);
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31150 bool network_is_transmitting_ GUARDED_BY(data_cs_);
151 bool encoder_paused_ GUARDED_BY(data_cs_);
152 bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_);
Peter Boström7b628162016-02-19 19:42:19153 std::vector<int64_t> time_last_intra_request_ms_ GUARDED_BY(data_cs_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23154
Peter Boström39ac69b2015-09-22 14:28:51155 ProcessThread* module_process_thread_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23156
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31157 bool has_received_sli_ GUARDED_BY(data_cs_);
158 uint8_t picture_id_sli_ GUARDED_BY(data_cs_);
159 bool has_received_rpsi_ GUARDED_BY(data_cs_);
160 uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23161
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31162 bool video_suspended_ GUARDED_BY(data_cs_);
sprang470b2742016-04-15 08:24:14163
164 std::unique_ptr<IvfFileWriter> file_writers_[kMaxLayers] GUARDED_BY(data_cs_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23165};
166
167} // namespace webrtc
168
Peter Boströme8f07352015-12-09 11:13:30169#endif // WEBRTC_VIDEO_VIE_ENCODER_H_