summaryrefslogtreecommitdiff
path: root/chromium/media/base/bitrate.h
blob: 1d189621908e55fc0279928838df46cf6873160e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_BITRATE_H_
#define MEDIA_BASE_BITRATE_H_

#include <stdint.h>
#include <string>

#include "media/base/media_export.h"

namespace media {

class MEDIA_EXPORT Bitrate {
 public:
  // Indicates whether constant bitrate (CBR) or variable bitrate (VBR) should
  // be used for encoding.
  enum class Mode { kConstant, kVariable };

  // Required by Mojo for serialization and de-serialization. Creates an
  // invalid constant bitrate with |target_| and |peak_| set to 0u. Prefer
  // to use the Bitrate::ConstantBitrate() method.
  constexpr Bitrate() = default;

  constexpr Bitrate(const Bitrate& other) = default;
  constexpr Bitrate& operator=(const Bitrate& other) = default;

  // Do not use int or uint64_t variations of these. If you have a signed
  // or 64-bit value you want to use as input, you must explicitly convert to
  // uint32_t before calling. This is intended to prevent implicit and unsafe
  // type conversion.
  static constexpr Bitrate ConstantBitrate(uint32_t target_bitrate) {
    return Bitrate(Mode::kConstant, target_bitrate, 0);
  }
  static Bitrate VariableBitrate(uint32_t target_bitrate,
                                 uint32_t peak_bitrate);

  // Deleted variants: you must SAFELY convert to uint32_t before calling.
  // See base/numerics/safe_conversions.h for functions to safely convert
  // between types.
  static Bitrate ConstantBitrate(int target_bitrate) = delete;
  static Bitrate VariableBitrate(int target_bitrate, int peak_bitrate) = delete;
  static Bitrate VariableBitrate(int target_bitrate,
                                 uint32_t peak_bitrate) = delete;
  static Bitrate VariableBitrate(uint32_t target_bitrate,
                                 int peak_bitrate) = delete;
  static Bitrate ConstantBitrate(uint64_t target_bitrate) = delete;
  static Bitrate VariableBitrate(uint64_t target_bitrate,
                                 uint64_t peak_bitrate) = delete;
  static Bitrate VariableBitrate(uint64_t target_bitrate,
                                 uint32_t peak_bitrate) = delete;
  static Bitrate VariableBitrate(uint32_t target_bitrate,
                                 uint64_t peak_bitrate) = delete;

  bool operator==(const Bitrate& right) const;
  bool operator!=(const Bitrate& right) const;

  // Accessor for |mode_|.
  constexpr Mode mode() const { return mode_; }

  // Accessor for |target_|.
  constexpr uint32_t target() const { return target_; }

  // Accessor for |peak_|. Returns 0 if |mode_| is
  // Mode::kConstantBitrate.
  uint32_t peak() const;

  std::string ToString() const;

 private:
  constexpr Bitrate(Mode mode, uint32_t target_bitrate, uint32_t peak_bitrate)
      : mode_(mode), target_(target_bitrate), peak_(peak_bitrate) {}

  // These member variables cannot be const (despite the intent that we do not
  // change them after creation) because we must have an assignment operator for
  // Mojo, and const member variables are incompatible with an assignment
  // operator.

  // The bitrate mode.
  Mode mode_ = Mode::kConstant;

  // Target bitrate for the stream in bits per second.
  uint32_t target_ = 0u;

  // For use with Mode::kVariable. Peak bitrate in bits per second.
  uint32_t peak_ = 0u;
};

}  // namespace media

#endif  // MEDIA_BASE_BITRATE_H_