summaryrefslogtreecommitdiffstats
path: root/media/webrtc/signaling/src/jsep/JsepTrack.h
blob: 5aa37404fff310f64293955f6b93568de6f449d6 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* 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 _JSEPTRACK_H_
#define _JSEPTRACK_H_

#include <algorithm>
#include <string>
#include <map>
#include <set>

#include <mozilla/RefPtr.h>
#include <mozilla/UniquePtr.h>
#include <mozilla/Maybe.h>
#include "nsISupportsImpl.h"
#include "nsError.h"

#include "signaling/src/jsep/JsepTransport.h"
#include "signaling/src/jsep/JsepTrackEncoding.h"
#include "signaling/src/sdp/Sdp.h"
#include "signaling/src/sdp/SdpAttribute.h"
#include "signaling/src/sdp/SdpMediaSection.h"
#include "signaling/src/common/PtrVector.h"

namespace mozilla {

class JsepTrackNegotiatedDetails
{
public:
  size_t
  GetEncodingCount() const
  {
    return mEncodings.values.size();
  }

  const JsepTrackEncoding&
  GetEncoding(size_t index) const
  {
    MOZ_RELEASE_ASSERT(index < mEncodings.values.size());
    return *mEncodings.values[index];
  }

  const SdpExtmapAttributeList::Extmap*
  GetExt(const std::string& ext_name) const
  {
    auto it = mExtmap.find(ext_name);
    if (it != mExtmap.end()) {
      return &it->second;
    }
    return nullptr;
  }

  std::vector<uint8_t> GetUniquePayloadTypes() const
  {
    return mUniquePayloadTypes;
  }

private:
  friend class JsepTrack;

  std::map<std::string, SdpExtmapAttributeList::Extmap> mExtmap;
  std::vector<uint8_t> mUniquePayloadTypes;
  PtrVector<JsepTrackEncoding> mEncodings;
};

class JsepTrack
{
public:
  JsepTrack(mozilla::SdpMediaSection::MediaType type,
            const std::string& streamid,
            const std::string& trackid,
            sdp::Direction direction = sdp::kSend)
      : mType(type),
        mStreamId(streamid),
        mTrackId(trackid),
        mDirection(direction),
        mActive(false)
  {}

  virtual mozilla::SdpMediaSection::MediaType
  GetMediaType() const
  {
    return mType;
  }

  virtual const std::string&
  GetStreamId() const
  {
    return mStreamId;
  }

  virtual void
  SetStreamId(const std::string& id)
  {
    mStreamId = id;
  }

  virtual const std::string&
  GetTrackId() const
  {
    return mTrackId;
  }

  virtual void
  SetTrackId(const std::string& id)
  {
    mTrackId = id;
  }

  virtual const std::string&
  GetCNAME() const
  {
    return mCNAME;
  }

  virtual void
  SetCNAME(const std::string& cname)
  {
    mCNAME = cname;
  }

  virtual sdp::Direction
  GetDirection() const
  {
    return mDirection;
  }

  virtual const std::vector<uint32_t>&
  GetSsrcs() const
  {
    return mSsrcs;
  }

  virtual void
  AddSsrc(uint32_t ssrc)
  {
    mSsrcs.push_back(ssrc);
  }

  bool
  GetActive() const
  {
    return mActive;
  }

  void
  SetActive(bool active)
  {
    mActive = active;
  }

  virtual void PopulateCodecs(
      const std::vector<JsepCodecDescription*>& prototype);

  template <class UnaryFunction>
  void ForEachCodec(UnaryFunction func)
  {
    std::for_each(mPrototypeCodecs.values.begin(),
                  mPrototypeCodecs.values.end(), func);
  }

  template <class BinaryPredicate>
  void SortCodecs(BinaryPredicate sorter)
  {
    std::stable_sort(mPrototypeCodecs.values.begin(),
                     mPrototypeCodecs.values.end(), sorter);
  }

  virtual void AddToOffer(SdpMediaSection* offer) const;
  virtual void AddToAnswer(const SdpMediaSection& offer,
                           SdpMediaSection* answer) const;
  virtual void Negotiate(const SdpMediaSection& answer,
                         const SdpMediaSection& remote);
  static void SetUniquePayloadTypes(
      const std::vector<RefPtr<JsepTrack>>& tracks);
  virtual void GetNegotiatedPayloadTypes(std::vector<uint16_t>* payloadTypes);

  // This will be set when negotiation is carried out.
  virtual const JsepTrackNegotiatedDetails*
  GetNegotiatedDetails() const
  {
    if (mNegotiatedDetails) {
      return mNegotiatedDetails.get();
    }
    return nullptr;
  }

  virtual JsepTrackNegotiatedDetails*
  GetNegotiatedDetails()
  {
    if (mNegotiatedDetails) {
      return mNegotiatedDetails.get();
    }
    return nullptr;
  }

  virtual void
  ClearNegotiatedDetails()
  {
    mNegotiatedDetails.reset();
  }

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(JsepTrack);

  struct JsConstraints
  {
    std::string rid;
    EncodingConstraints constraints;
  };

  void SetJsConstraints(const std::vector<JsConstraints>& constraintsList)
  {
    mJsEncodeConstraints = constraintsList;
  }

  void GetJsConstraints(std::vector<JsConstraints>* outConstraintsList) const
  {
    MOZ_ASSERT(outConstraintsList);
    *outConstraintsList = mJsEncodeConstraints;
  }

  static void AddToMsection(const std::vector<JsConstraints>& constraintsList,
                            sdp::Direction direction,
                            SdpMediaSection* msection);

protected:
  virtual ~JsepTrack() {}

private:
  std::vector<JsepCodecDescription*> GetCodecClones() const;
  static void EnsureNoDuplicatePayloadTypes(
      std::vector<JsepCodecDescription*>* codecs);
  static void GetPayloadTypes(
      const std::vector<JsepCodecDescription*>& codecs,
      std::vector<uint16_t>* pts);
  static void EnsurePayloadTypeIsUnique(std::set<uint16_t>* uniquePayloadTypes,
                                        JsepCodecDescription* codec);
  void AddToMsection(const std::vector<JsepCodecDescription*>& codecs,
                     SdpMediaSection* msection) const;
  void GetRids(const SdpMediaSection& msection,
               sdp::Direction direction,
               std::vector<SdpRidAttributeList::Rid>* rids) const;
  void CreateEncodings(
      const SdpMediaSection& remote,
      const std::vector<JsepCodecDescription*>& negotiatedCodecs,
      JsepTrackNegotiatedDetails* details);

  // |formatChanges| is set on completion of offer/answer, and records how the
  // formats in |codecs| were changed, which is used by |Negotiate| to update
  // |mPrototypeCodecs|.
  virtual void NegotiateCodecs(
      const SdpMediaSection& remote,
      std::vector<JsepCodecDescription*>* codecs,
      std::map<std::string, std::string>* formatChanges = nullptr) const;

  JsConstraints* FindConstraints(
      const std::string& rid,
      std::vector<JsConstraints>& constraintsList) const;
  void NegotiateRids(const std::vector<SdpRidAttributeList::Rid>& rids,
                     std::vector<JsConstraints>* constraints) const;

  const mozilla::SdpMediaSection::MediaType mType;
  std::string mStreamId;
  std::string mTrackId;
  std::string mCNAME;
  const sdp::Direction mDirection;
  PtrVector<JsepCodecDescription> mPrototypeCodecs;
  // Holds encoding params/constraints from JS. Simulcast happens when there are
  // multiple of these. If there are none, we assume unconstrained unicast with
  // no rid.
  std::vector<JsConstraints> mJsEncodeConstraints;
  UniquePtr<JsepTrackNegotiatedDetails> mNegotiatedDetails;
  std::vector<uint32_t> mSsrcs;
  bool mActive;
};

// Need a better name for this.
struct JsepTrackPair {
  size_t mLevel;
  // Is this track pair sharing a transport with another?
  Maybe<size_t> mBundleLevel;
  uint32_t mRecvonlySsrc;
  RefPtr<JsepTrack> mSending;
  RefPtr<JsepTrack> mReceiving;
  RefPtr<JsepTransport> mRtpTransport;
  RefPtr<JsepTransport> mRtcpTransport;
};

} // namespace mozilla

#endif