summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasource/SourceBufferAttributes.h
blob: 0af80a4fe4a439b325216f3c893015c7f8917919 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_SourceBufferAttributes_h_
#define mozilla_SourceBufferAttributes_h_

#include "TimeUnits.h"
#include "mozilla/dom/SourceBufferBinding.h"
#include "mozilla/Maybe.h"

namespace mozilla {

class SourceBufferAttributes {
public:

  // Current state as per Segment Parser Loop Algorithm
  // http://w3c.github.io/media-source/index.html#sourcebuffer-segment-parser-loop
  enum class AppendState
  {
    WAITING_FOR_SEGMENT,
    PARSING_INIT_SEGMENT,
    PARSING_MEDIA_SEGMENT,
  };

  explicit SourceBufferAttributes(bool aGenerateTimestamp)
  : mGenerateTimestamps(aGenerateTimestamp)
  , mAppendWindowStart(0)
  , mAppendWindowEnd(PositiveInfinity<double>())
  , mAppendMode(dom::SourceBufferAppendMode::Segments)
  , mApparentTimestampOffset(0)
  , mAppendState(AppendState::WAITING_FOR_SEGMENT)
  {}

  SourceBufferAttributes(const SourceBufferAttributes& aOther) = default;

  double GetAppendWindowStart() const
  {
    return mAppendWindowStart;
  }

  double GetAppendWindowEnd() const
  {
    return mAppendWindowEnd;
  }

  void SetAppendWindowStart(double aWindowStart)
  {
    mAppendWindowStart = aWindowStart;
  }

  void SetAppendWindowEnd(double aWindowEnd)
  {
    mAppendWindowEnd = aWindowEnd;
  }

  double GetApparentTimestampOffset() const
  {
    return mApparentTimestampOffset;
  }

  void SetApparentTimestampOffset(double aTimestampOffset)
  {
    mApparentTimestampOffset = aTimestampOffset;
    mTimestampOffset = media::TimeUnit::FromSeconds(aTimestampOffset);
  }

  media::TimeUnit GetTimestampOffset() const
  {
    return mTimestampOffset;
  }

  void SetTimestampOffset(const media::TimeUnit& aTimestampOffset)
  {
    mTimestampOffset = aTimestampOffset;
    mApparentTimestampOffset = aTimestampOffset.ToSeconds();
  }

  dom::SourceBufferAppendMode GetAppendMode() const
  {
    return mAppendMode;
  }

  void SetAppendMode(dom::SourceBufferAppendMode aAppendMode)
  {
    mAppendMode = aAppendMode;
  }

  void SetGroupStartTimestamp(const media::TimeUnit& aGroupStartTimestamp)
  {
    mGroupStartTimestamp = Some(aGroupStartTimestamp);
  }

  media::TimeUnit GetGroupStartTimestamp() const
  {
    return mGroupStartTimestamp.ref();
  }

  bool HaveGroupStartTimestamp() const
  {
    return mGroupStartTimestamp.isSome();
  }

  void ResetGroupStartTimestamp()
  {
    mGroupStartTimestamp.reset();
  }

  void RestartGroupStartTimestamp()
  {
    mGroupStartTimestamp = Some(mGroupEndTimestamp);
  }

  media::TimeUnit GetGroupEndTimestamp() const
  {
    return mGroupEndTimestamp;
  }

  void SetGroupEndTimestamp(const media::TimeUnit& aGroupEndTimestamp)
  {
    mGroupEndTimestamp = aGroupEndTimestamp;
  }

  AppendState GetAppendState() const
  {
    return mAppendState;
  }

  void SetAppendState(AppendState aState)
  {
    mAppendState = aState;
  }

  // mGenerateTimestamp isn't mutable once the source buffer has been constructed
  bool mGenerateTimestamps;

  SourceBufferAttributes& operator=(const SourceBufferAttributes& aOther) = default;

private:
  SourceBufferAttributes() = delete;

  double mAppendWindowStart;
  double mAppendWindowEnd;
  dom::SourceBufferAppendMode mAppendMode;
  double mApparentTimestampOffset;
  media::TimeUnit mTimestampOffset;
  Maybe<media::TimeUnit> mGroupStartTimestamp;
  media::TimeUnit mGroupEndTimestamp;
  // The current append state as per https://w3c.github.io/media-source/#sourcebuffer-append-state
  AppendState mAppendState;
};

} // end namespace mozilla

#endif /* mozilla_SourceBufferAttributes_h_ */