aboutsummaryrefslogtreecommitdiff
path: root/cast/streaming/frame_id.h
blob: 3625242257acfb5df15012df1e64a58000eb17c1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2016 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 CAST_STREAMING_FRAME_ID_H_
#define CAST_STREAMING_FRAME_ID_H_

#include <stdint.h>

#include <sstream>

#include "cast/streaming/expanded_value_base.h"

namespace cast {
namespace streaming {

// Forward declaration (see below).
class FrameId;

// Convenience operator overloads for logging.
std::ostream& operator<<(std::ostream& out, const FrameId rhs);

// Unique identifier for a frame in a RTP media stream.  FrameIds are truncated
// to 8-bit values in RTP and RTCP headers, and then expanded back by the other
// endpoint when parsing the headers.
//
// Usage example:
//
//   // Distance/offset math.
//   FrameId first = FrameId::first();
//   FrameId second = first + 1;
//   FrameId third = second + 1;
//   int64_t offset = third - first;
//   FrameId fourth = second + offset;
//
//   // Logging convenience.
//   OSP_DLOG_INFO << "The current frame is " << fourth;
class FrameId : public ExpandedValueBase<int64_t, FrameId> {
 public:
  // The "null" FrameId constructor.  Represents a FrameId field that has not
  // been set and/or a "not applicable" indicator.
  constexpr FrameId() : FrameId(std::numeric_limits<int64_t>::min()) {}

  // Allow copy construction and assignment.
  constexpr FrameId(const FrameId&) = default;
  constexpr FrameId& operator=(const FrameId&) = default;

  // Returns true if this is the special value representing null.
  constexpr bool is_null() const { return *this == FrameId(); }

  // Distance operator.
  int64_t operator-(FrameId rhs) const {
    OSP_DCHECK(!is_null());
    OSP_DCHECK(!rhs.is_null());
    return value_ - rhs.value_;
  }

  // Operators to compute advancement by incremental amounts.
  FrameId operator+(int64_t rhs) const {
    OSP_DCHECK(!is_null());
    return FrameId(value_ + rhs);
  }
  FrameId operator-(int64_t rhs) const {
    OSP_DCHECK(!is_null());
    return FrameId(value_ - rhs);
  }
  FrameId& operator+=(int64_t rhs) {
    OSP_DCHECK(!is_null());
    return (*this = (*this + rhs));
  }
  FrameId& operator-=(int64_t rhs) {
    OSP_DCHECK(!is_null());
    return (*this = (*this - rhs));
  }
  FrameId& operator++() {
    OSP_DCHECK(!is_null());
    ++value_;
    return *this;
  }
  FrameId& operator--() {
    OSP_DCHECK(!is_null());
    --value_;
    return *this;
  }
  FrameId operator++(int) {
    OSP_DCHECK(!is_null());
    return FrameId(value_++);
  }
  FrameId operator--(int) {
    OSP_DCHECK(!is_null());
    return FrameId(value_--);
  }

  // The identifier for the first frame in a stream.
  static constexpr FrameId first() { return FrameId(0); }

 private:
  friend class ExpandedValueBase<int64_t, FrameId>;
  friend std::ostream& operator<<(std::ostream& out, const FrameId rhs);

  constexpr explicit FrameId(int64_t value) : ExpandedValueBase(value) {}

  // Accessor used by ostream output function.
  constexpr int64_t value() const { return value_; }
};

}  // namespace streaming
}  // namespace cast

#endif  // CAST_STREAMING_FRAME_ID_H_