blob: 591839c30db92a4053fdac5e0079492c6d28a7bb [file] [log] [blame]
pbos@webrtc.org9115cde2014-12-09 10:36:40 +00001/*
2 * Copyright (c) 2014 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 */
11
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#ifndef MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_
13#define MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000014
kwiberg3f55dea2016-02-29 05:51:59 -080015#include <memory>
brandtr5e171752017-05-23 03:32:16 -070016#include <stack>
Peter Boströma5dec162016-01-20 15:53:55 +010017#include <string>
brandtr5e171752017-05-23 03:32:16 -070018#include <utility>
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000019#include <vector>
20
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +020021#include "absl/types/optional.h"
Elad Alon8f01c4e2019-06-28 15:19:43 +020022#include "api/fec_controller_override.h"
Jonathan Yu9a5da492018-10-19 00:51:18 -070023#include "api/video_codecs/sdp_video_format.h"
Elad Alon370f93a2019-06-11 14:57:57 +020024#include "api/video_codecs/video_encoder.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020025#include "modules/video_coding/include/video_codec_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/atomic_ops.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020027#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei66e76792019-04-02 11:33:59 +020028#include "rtc_base/system/rtc_export.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000029
30namespace webrtc {
31
Erik Språng78ce6192016-09-12 16:04:43 +020032class SimulcastRateAllocator;
Magnus Jedvertdf4883d2017-11-17 14:44:55 +010033class VideoEncoderFactory;
Erik Språng78ce6192016-09-12 16:04:43 +020034
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000035// SimulcastEncoderAdapter implements simulcast support by creating multiple
36// webrtc::VideoEncoder instances with the given VideoEncoderFactory.
brandtr5e171752017-05-23 03:32:16 -070037// The object is created and destroyed on the worker thread, but all public
38// interfaces should be called from the encoder task queue.
Mirko Bonadei66e76792019-04-02 11:33:59 +020039class RTC_EXPORT SimulcastEncoderAdapter : public VideoEncoder {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000040 public:
Erik Språngf4e0c292019-10-01 18:50:03 +020041 // TODO(bugs.webrtc.org/11000): Remove when downstream usage is gone.
42 SimulcastEncoderAdapter(VideoEncoderFactory* primarty_factory,
43 const SdpVideoFormat& format);
44 // |primary_factory| produces the first-choice encoders to use.
45 // |fallback_factory|, if non-null, is used to create fallback encoder that
46 // will be used if InitEncode() fails for the primary encoder.
47 SimulcastEncoderAdapter(VideoEncoderFactory* primary_factory,
48 VideoEncoderFactory* fallback_factory,
49 const SdpVideoFormat& format);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000050 virtual ~SimulcastEncoderAdapter();
51
brandtr5e171752017-05-23 03:32:16 -070052 // Implements VideoEncoder.
Elad Alon8f01c4e2019-06-28 15:19:43 +020053 void SetFecControllerOverride(
54 FecControllerOverride* fec_controller_override) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000055 int Release() override;
Elad Alon370f93a2019-06-11 14:57:57 +020056 int InitEncode(const VideoCodec* codec_settings,
57 const VideoEncoder::Settings& settings) override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070058 int Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +010059 const std::vector<VideoFrameType>* frame_types) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000060 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
Erik Språng16cb8f52019-04-12 13:59:09 +020061 void SetRates(const RateControlParameters& parameters) override;
Elad Alon65764e42019-06-28 18:43:44 +020062 void OnPacketLossRateUpdate(float packet_loss_rate) override;
63 void OnRttUpdate(int64_t rtt_ms) override;
64 void OnLossNotification(const LossNotification& loss_notification) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000065
Noah Richards41ee1ea2015-04-15 09:24:26 -070066 // Eventual handler for the contained encoders' EncodedImageCallbacks, but
67 // called from an internal helper that also knows the correct stream
68 // index.
Sergey Ulanov525df3f2016-08-02 17:46:41 -070069 EncodedImageCallback::Result OnEncodedImage(
70 size_t stream_idx,
71 const EncodedImage& encoded_image,
72 const CodecSpecificInfo* codec_specific_info,
73 const RTPFragmentationHeader* fragmentation);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000074
Erik Språng9b5b0702018-11-01 14:52:30 +010075 EncoderInfo GetEncoderInfo() const override;
jackychen6e2ce6e2015-07-13 16:26:33 -070076
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000077 private:
78 struct StreamInfo {
magjed3f897582017-08-28 08:05:42 -070079 StreamInfo(std::unique_ptr<VideoEncoder> encoder,
brandtr5e171752017-05-23 03:32:16 -070080 std::unique_ptr<EncodedImageCallback> callback,
philipelcce46fc2015-12-21 03:04:49 -080081 uint16_t width,
82 uint16_t height,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000083 bool send_stream)
magjed3f897582017-08-28 08:05:42 -070084 : encoder(std::move(encoder)),
brandtr5e171752017-05-23 03:32:16 -070085 callback(std::move(callback)),
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000086 width(width),
87 height(height),
88 key_frame_request(false),
89 send_stream(send_stream) {}
magjed3f897582017-08-28 08:05:42 -070090 std::unique_ptr<VideoEncoder> encoder;
brandtr5e171752017-05-23 03:32:16 -070091 std::unique_ptr<EncodedImageCallback> callback;
philipelcce46fc2015-12-21 03:04:49 -080092 uint16_t width;
93 uint16_t height;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000094 bool key_frame_request;
95 bool send_stream;
96 };
97
Florent Castelli1b761ca2019-01-21 14:33:02 +010098 enum class StreamResolution {
99 OTHER,
100 HIGHEST,
101 LOWEST,
102 };
103
brandtr5e171752017-05-23 03:32:16 -0700104 // Populate the codec settings for each simulcast stream.
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +0200105 void PopulateStreamCodec(const webrtc::VideoCodec& inst,
106 int stream_index,
107 uint32_t start_bitrate_kbps,
Florent Castelli1b761ca2019-01-21 14:33:02 +0100108 StreamResolution stream_resolution,
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +0200109 webrtc::VideoCodec* stream_codec);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000110
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000111 bool Initialized() const;
112
brandtr5e171752017-05-23 03:32:16 -0700113 void DestroyStoredEncoders();
114
115 volatile int inited_; // Accessed atomically.
Erik Språngf4e0c292019-10-01 18:50:03 +0200116 VideoEncoderFactory* const primary_encoder_factory_;
117 VideoEncoderFactory* const fallback_encoder_factory_;
Ilya Nikolaevskiy97b4ee52018-05-28 10:24:22 +0200118 const SdpVideoFormat video_format_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000119 VideoCodec codec_;
120 std::vector<StreamInfo> streaminfos_;
121 EncodedImageCallback* encoded_complete_callback_;
Erik Språng75de46a2018-11-07 14:53:32 +0100122 EncoderInfo encoder_info_;
brandtr5e171752017-05-23 03:32:16 -0700123
124 // Used for checking the single-threaded access of the encoder interface.
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200125 SequenceChecker encoder_queue_;
brandtr5e171752017-05-23 03:32:16 -0700126
127 // Store encoders in between calls to Release and InitEncode, so they don't
128 // have to be recreated. Remaining encoders are destroyed by the destructor.
magjed3f897582017-08-28 08:05:42 -0700129 std::stack<std::unique_ptr<VideoEncoder>> stored_encoders_;
Ilya Nikolaevskiyc30a1312018-08-27 11:07:48 +0200130
131 const absl::optional<unsigned int> experimental_boosted_screenshare_qp_;
Erik Språng7f24fb92019-02-13 10:49:37 +0100132 const bool boost_base_layer_quality_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000133};
134
135} // namespace webrtc
136
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200137#endif // MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_