1 | /* |
---|---|

2 | * Copyright (C) 2012 Apple Inc. All rights reserved. |

3 | * |

4 | * Redistribution and use in source and binary forms, with or without |

5 | * modification, are permitted provided that the following conditions |

6 | * are met: |

7 | * |

8 | * 1. Redistributions of source code must retain the above copyright |

9 | * notice, this list of conditions and the following disclaimer. |

10 | * 2. Redistributions in binary form must reproduce the above copyright |

11 | * notice, this list of conditions and the following disclaimer in the |

12 | * documentation and/or other materials provided with the distribution. |

13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |

14 | * its contributors may be used to endorse or promote products derived |

15 | * from this software without specific prior written permission. |

16 | * |

17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |

18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |

19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |

20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |

21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |

22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |

23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |

24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |

25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |

26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |

27 | */ |

28 | |

29 | #ifndef WTF_MediaTime_h |

30 | #define WTF_MediaTime_h |

31 | |

32 | #include <wtf/FastMalloc.h> |

33 | |

34 | #include <cmath> |

35 | #include <limits> |

36 | #include <math.h> |

37 | #include <stdint.h> |

38 | |

39 | namespace WTF { |

40 | |

41 | class PrintStream; |

42 | |

43 | class WTF_EXPORT_PRIVATE MediaTime { |

44 | WTF_MAKE_FAST_ALLOCATED; |

45 | public: |

46 | enum { |

47 | Valid = 1 << 0, |

48 | HasBeenRounded = 1 << 1, |

49 | PositiveInfinite = 1 << 2, |

50 | NegativeInfinite = 1 << 3, |

51 | Indefinite = 1 << 4, |

52 | DoubleValue = 1 << 5, |

53 | }; |

54 | |

55 | MediaTime(); |

56 | MediaTime(int64_t value, int32_t scale, uint32_t flags = Valid); |

57 | MediaTime(const MediaTime& rhs); |

58 | ~MediaTime(); |

59 | |

60 | static MediaTime createWithFloat(float floatTime); |

61 | static MediaTime createWithFloat(float floatTime, int32_t timeScale); |

62 | static MediaTime createWithDouble(double doubleTime); |

63 | static MediaTime createWithDouble(double doubleTime, int32_t timeScale); |

64 | |

65 | float toFloat() const; |

66 | double toDouble() const; |

67 | |

68 | MediaTime& operator=(const MediaTime& rhs); |

69 | MediaTime& operator+=(const MediaTime& rhs) { return *this = *this + rhs; } |

70 | MediaTime& operator-=(const MediaTime& rhs) { return *this = *this - rhs; } |

71 | MediaTime operator+(const MediaTime& rhs) const; |

72 | MediaTime operator-(const MediaTime& rhs) const; |

73 | MediaTime operator-() const; |

74 | MediaTime operator*(int32_t) const; |

75 | bool operator<(const MediaTime& rhs) const; |

76 | bool operator>(const MediaTime& rhs) const; |

77 | bool operator!=(const MediaTime& rhs) const; |

78 | bool operator==(const MediaTime& rhs) const; |

79 | bool operator>=(const MediaTime& rhs) const; |

80 | bool operator<=(const MediaTime& rhs) const; |

81 | bool operator!() const; |

82 | explicit operator bool() const; |

83 | |

84 | typedef enum { |

85 | LessThan = -1, |

86 | EqualTo = 0, |

87 | GreaterThan = 1, |

88 | } ComparisonFlags; |

89 | |

90 | ComparisonFlags compare(const MediaTime& rhs) const; |

91 | |

92 | bool isValid() const { return m_timeFlags & Valid; } |

93 | bool isInvalid() const { return !isValid(); } |

94 | bool hasBeenRounded() const { return m_timeFlags & HasBeenRounded; } |

95 | bool isPositiveInfinite() const { return m_timeFlags & PositiveInfinite; } |

96 | bool isNegativeInfinite() const { return m_timeFlags & NegativeInfinite; } |

97 | bool isIndefinite() const { return m_timeFlags & Indefinite; } |

98 | bool hasDoubleValue() const { return m_timeFlags & DoubleValue; } |

99 | |

100 | static const MediaTime& zeroTime(); |

101 | static const MediaTime& invalidTime(); |

102 | static const MediaTime& positiveInfiniteTime(); |

103 | static const MediaTime& negativeInfiniteTime(); |

104 | static const MediaTime& indefiniteTime(); |

105 | |

106 | const int64_t& timeValue() const { return m_timeValue; } |

107 | const int32_t& timeScale() const { return m_timeScale; } |

108 | |

109 | void dump(PrintStream& out) const; |

110 | |

111 | // Make the following casts errors: |

112 | operator double() const = delete; |

113 | MediaTime(double) = delete; |

114 | operator int() const = delete; |

115 | MediaTime(int) = delete; |

116 | |

117 | friend WTF_EXPORT_PRIVATE MediaTime abs(const MediaTime& rhs); |

118 | |

119 | static const int32_t DefaultTimeScale = 10000000; |

120 | static const int32_t MaximumTimeScale; |

121 | |

122 | private: |

123 | void setTimeScale(int32_t); |

124 | |

125 | union { |

126 | int64_t m_timeValue; |

127 | double m_timeValueAsDouble; |

128 | }; |

129 | int32_t m_timeScale; |

130 | uint32_t m_timeFlags; |

131 | }; |

132 | |

133 | inline MediaTime operator*(int32_t lhs, const MediaTime& rhs) { return rhs.operator*(lhs); } |

134 | |

135 | WTF_EXPORT_PRIVATE extern MediaTime abs(const MediaTime& rhs); |

136 | } |

137 | |

138 | using WTF::MediaTime; |

139 | using WTF::abs; |

140 | |

141 | #endif |

142 |