bitrate.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_BITRATE_H_
  5. #define MEDIA_BASE_BITRATE_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "media/base/media_export.h"
  9. namespace media {
  10. class MEDIA_EXPORT Bitrate {
  11. public:
  12. // Indicates whether constant bitrate (CBR) or variable bitrate (VBR) should
  13. // be used for encoding.
  14. enum class Mode { kConstant, kVariable };
  15. // Required by Mojo for serialization and de-serialization. Creates an
  16. // invalid constant bitrate with |target_| and |peak_| set to 0u. Prefer
  17. // to use the Bitrate::ConstantBitrate() method.
  18. constexpr Bitrate() = default;
  19. constexpr Bitrate(const Bitrate& other) = default;
  20. constexpr Bitrate& operator=(const Bitrate& other) = default;
  21. // Do not use int or uint64_t variations of these. If you have a signed
  22. // or 64-bit value you want to use as input, you must explicitly convert to
  23. // uint32_t before calling. This is intended to prevent implicit and unsafe
  24. // type conversion.
  25. static constexpr Bitrate ConstantBitrate(uint32_t target_bps) {
  26. return Bitrate(Mode::kConstant, target_bps, 0);
  27. }
  28. static Bitrate VariableBitrate(uint32_t target_bps, uint32_t peak_bps);
  29. // Deleted variants: you must SAFELY convert to uint32_t before calling.
  30. // See base/numerics/safe_conversions.h for functions to safely convert
  31. // between types.
  32. static Bitrate ConstantBitrate(int target_bps) = delete;
  33. static Bitrate VariableBitrate(int target_bps, int peak_bps) = delete;
  34. static Bitrate VariableBitrate(int target_bps, uint32_t peak_bps) = delete;
  35. static Bitrate VariableBitrate(uint32_t target_bps, int peak_bps) = delete;
  36. static Bitrate ConstantBitrate(uint64_t target_bps) = delete;
  37. static Bitrate VariableBitrate(uint64_t target_bps,
  38. uint64_t peak_bps) = delete;
  39. static Bitrate VariableBitrate(uint64_t target_bps,
  40. uint32_t peak_bps) = delete;
  41. static Bitrate VariableBitrate(uint32_t target_bps,
  42. uint64_t peak_bps) = delete;
  43. bool operator==(const Bitrate& right) const;
  44. bool operator!=(const Bitrate& right) const;
  45. // Accessor for |mode_|.
  46. constexpr Mode mode() const { return mode_; }
  47. // Accessor for |target_|.
  48. constexpr uint32_t target_bps() const { return target_bps_; }
  49. // Accessor for |peak_|. Returns 0 if |mode_| is
  50. // Mode::kConstantBitrate.
  51. uint32_t peak_bps() const;
  52. std::string ToString() const;
  53. private:
  54. constexpr Bitrate(Mode mode, uint32_t target_bps, uint32_t peak_bps)
  55. : mode_(mode), target_bps_(target_bps), peak_bps_(peak_bps) {}
  56. // These member variables cannot be const (despite the intent that we do not
  57. // change them after creation) because we must have an assignment operator for
  58. // Mojo, and const member variables are incompatible with an assignment
  59. // operator.
  60. // The bitrate mode.
  61. Mode mode_ = Mode::kConstant;
  62. // Target bitrate for the stream in bits per second.
  63. uint32_t target_bps_ = 0u;
  64. // For use with Mode::kVariable. Peak bitrate in bits per second.
  65. uint32_t peak_bps_ = 0u;
  66. };
  67. } // namespace media
  68. #endif // MEDIA_BASE_BITRATE_H_