VideoStreamFactory.h (4235B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #ifndef VideoStreamFactory_h 8 #define VideoStreamFactory_h 9 10 #include "CodecConfig.h" 11 #include "mozilla/Atomics.h" 12 #include "mozilla/EventTargetCapability.h" 13 #include "mozilla/gfx/Point.h" 14 #include "video/config/video_encoder_config.h" 15 16 namespace webrtc { 17 class VideoFrame; 18 } 19 20 namespace mozilla { 21 22 // Factory class for VideoStreams... vie_encoder.cc will call this to 23 // reconfigure. 24 class VideoStreamFactory 25 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface { 26 public: 27 struct ResolutionAndBitrateLimits { 28 int resolution_in_mb; 29 int min_bitrate_bps; 30 int start_bitrate_bps; 31 int max_bitrate_bps; 32 }; 33 34 static ResolutionAndBitrateLimits GetLimitsFor(gfx::IntSize aSize, 35 int aCapBps = 0); 36 37 VideoStreamFactory(VideoCodecConfig aConfig, int aMinBitrate, 38 int aStartBitrate, int aPrefMaxBitrate, 39 int aNegotiatedMaxBitrate) 40 : mMaxFramerateForAllStreams(std::numeric_limits<unsigned int>::max()), 41 mCodecConfig(std::forward<VideoCodecConfig>(aConfig)), 42 mMinBitrate(aMinBitrate), 43 mStartBitrate(aStartBitrate), 44 mPrefMaxBitrate(aPrefMaxBitrate), 45 mNegotiatedMaxBitrate(aNegotiatedMaxBitrate) {} 46 47 // This gets called off-main thread and may hold internal webrtc.org 48 // locks. May *NOT* lock the conduit's mutex, to avoid deadlocks. 49 std::vector<webrtc::VideoStream> CreateEncoderStreams( 50 const webrtc::FieldTrialsView& field_trials, int aWidth, int aHeight, 51 const webrtc::VideoEncoderConfig& aConfig) override 52 MOZ_EXCLUDES(mEncodeQueue); 53 54 // Called right before CreateEncoderStreams with info about the encoder 55 // instance used. 56 void SetEncoderInfo(const webrtc::VideoEncoder::EncoderInfo& aInfo) override 57 MOZ_EXCLUDES(mEncodeQueue); 58 59 /** 60 * Called by CreateEncoderStreams and 61 * WebrtcVideoConduit::OnControlConfigChange to set VideoStream.max_framerate. 62 */ 63 void SelectResolutionAndMaxFramerate( 64 gfx::IntSize aSize, const VideoCodecConfig::Encoding& aEncoding, 65 webrtc::VideoStream& aVideoStream) MOZ_REQUIRES(mEncodeQueue); 66 67 /** 68 * Function to select and change the encoding resolution based on incoming 69 * frame size and current available bandwidth. 70 * @param aSize: dimensions of the frame 71 */ 72 void SelectMaxFramerateForAllStreams(gfx::IntSize aSize); 73 74 private: 75 /** 76 * Function to calculate a scaled down width and height based on 77 * scaleDownByResolution, maxFS, and max pixel count settings. 78 * @param aSize current frame size 79 * @param aScaleDownByResolution value to scale width and height down by. 80 * @return a gfx:IntSize containing width and height to use. These may match 81 * the aSize passed in if no scaling was needed. 82 */ 83 gfx::IntSize CalculateScaledResolution(gfx::IntSize aSize, 84 double aScaleDownByResolution) 85 MOZ_REQUIRES(mEncodeQueue); 86 87 /** 88 * Function to select and change the encoding frame rate based on incoming 89 * frame rate, current frame size and max-mbps setting. 90 * @param aOldFramerate current framerate 91 * @param aSendingSize size of frames being sent 92 * @return new framerate meeting max-mbps requriements based on frame size 93 */ 94 unsigned int SelectFrameRate(unsigned int aOldFramerate, gfx::IntSize aSize); 95 96 // The framerate we're currently sending at. 97 Atomic<unsigned int> mMaxFramerateForAllStreams; 98 99 Maybe<EventTargetCapability<nsISerialEventTarget>> mEncodeQueue; 100 Maybe<int> mRequestedResolutionAlignment MOZ_GUARDED_BY(mEncodeQueue); 101 102 // The current send codec config, containing simulcast layer configs. 103 const VideoCodecConfig mCodecConfig; 104 105 // Bitrate limits in bps. 106 const int mMinBitrate = 0; 107 const int mStartBitrate = 0; 108 const int mPrefMaxBitrate = 0; 109 const int mNegotiatedMaxBitrate = 0; 110 }; 111 112 } // namespace mozilla 113 114 #endif