blob: 240a621134dea48e9dab05ee76a6efb2ec07feba [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
Zhi Huangaea84f52017-11-16 18:46:27 +000021#include "media/engine/webrtcvideoencoderfactory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/codecs/vp8/include/vp8.h"
23#include "rtc_base/atomicops.h"
24#include "rtc_base/sequenced_task_checker.h"
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000025
26namespace webrtc {
27
Erik Språng78ce6192016-09-12 16:04:43 +020028class SimulcastRateAllocator;
Magnus Jedvertdf4883d2017-11-17 14:44:55 +010029class VideoEncoderFactory;
Erik Språng78ce6192016-09-12 16:04:43 +020030
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000031// SimulcastEncoderAdapter implements simulcast support by creating multiple
32// webrtc::VideoEncoder instances with the given VideoEncoderFactory.
brandtr5e171752017-05-23 03:32:16 -070033// The object is created and destroyed on the worker thread, but all public
34// interfaces should be called from the encoder task queue.
Noah Richards41ee1ea2015-04-15 09:24:26 -070035class SimulcastEncoderAdapter : public VP8Encoder {
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000036 public:
Magnus Jedvertdf4883d2017-11-17 14:44:55 +010037 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000038 virtual ~SimulcastEncoderAdapter();
39
brandtr5e171752017-05-23 03:32:16 -070040 // Implements VideoEncoder.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000041 int Release() override;
42 int InitEncode(const VideoCodec* inst,
43 int number_of_cores,
44 size_t max_payload_size) override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070045 int Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000046 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070047 const std::vector<FrameType>* frame_types) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000048 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
49 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
Erik Språng08127a92016-11-16 16:41:30 +010050 int SetRateAllocation(const BitrateAllocation& bitrate,
51 uint32_t new_framerate) override;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000052
Noah Richards41ee1ea2015-04-15 09:24:26 -070053 // Eventual handler for the contained encoders' EncodedImageCallbacks, but
54 // called from an internal helper that also knows the correct stream
55 // index.
Sergey Ulanov525df3f2016-08-02 17:46:41 -070056 EncodedImageCallback::Result OnEncodedImage(
57 size_t stream_idx,
58 const EncodedImage& encoded_image,
59 const CodecSpecificInfo* codec_specific_info,
60 const RTPFragmentationHeader* fragmentation);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000061
kthelgason876222f2016-11-29 01:44:11 -080062 VideoEncoder::ScalingSettings GetScalingSettings() const override;
jackychen61b4d512015-04-21 15:30:11 -070063
pbos65e15ba2015-10-15 10:52:15 -070064 bool SupportsNativeHandle() const override;
pbosecd21b42016-01-07 08:03:05 -080065 const char* ImplementationName() const override;
jackychen6e2ce6e2015-07-13 16:26:33 -070066
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000067 private:
68 struct StreamInfo {
magjed3f897582017-08-28 08:05:42 -070069 StreamInfo(std::unique_ptr<VideoEncoder> encoder,
brandtr5e171752017-05-23 03:32:16 -070070 std::unique_ptr<EncodedImageCallback> callback,
philipelcce46fc2015-12-21 03:04:49 -080071 uint16_t width,
72 uint16_t height,
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000073 bool send_stream)
magjed3f897582017-08-28 08:05:42 -070074 : encoder(std::move(encoder)),
brandtr5e171752017-05-23 03:32:16 -070075 callback(std::move(callback)),
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000076 width(width),
77 height(height),
78 key_frame_request(false),
79 send_stream(send_stream) {}
magjed3f897582017-08-28 08:05:42 -070080 std::unique_ptr<VideoEncoder> encoder;
brandtr5e171752017-05-23 03:32:16 -070081 std::unique_ptr<EncodedImageCallback> callback;
philipelcce46fc2015-12-21 03:04:49 -080082 uint16_t width;
83 uint16_t height;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000084 bool key_frame_request;
85 bool send_stream;
86 };
87
brandtr5e171752017-05-23 03:32:16 -070088 // Populate the codec settings for each simulcast stream.
89 static void PopulateStreamCodec(const webrtc::VideoCodec& inst,
90 int stream_index,
91 uint32_t start_bitrate_kbps,
92 bool highest_resolution_stream,
93 webrtc::VideoCodec* stream_codec);
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000094
pbos@webrtc.org9115cde2014-12-09 10:36:40 +000095 bool Initialized() const;
96
brandtr5e171752017-05-23 03:32:16 -070097 void DestroyStoredEncoders();
98
99 volatile int inited_; // Accessed atomically.
Magnus Jedvertdf4883d2017-11-17 14:44:55 +0100100 VideoEncoderFactory* const factory_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000101 VideoCodec codec_;
102 std::vector<StreamInfo> streaminfos_;
103 EncodedImageCallback* encoded_complete_callback_;
Peter Boströma5dec162016-01-20 15:53:55 +0100104 std::string implementation_name_;
brandtr5e171752017-05-23 03:32:16 -0700105
106 // Used for checking the single-threaded access of the encoder interface.
107 rtc::SequencedTaskChecker encoder_queue_;
108
109 // Store encoders in between calls to Release and InitEncode, so they don't
110 // have to be recreated. Remaining encoders are destroyed by the destructor.
magjed3f897582017-08-28 08:05:42 -0700111 std::stack<std::unique_ptr<VideoEncoder>> stored_encoders_;
pbos@webrtc.org9115cde2014-12-09 10:36:40 +0000112};
113
114} // namespace webrtc
115
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200116#endif // MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_