summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/transportflow.cpp
blob: 8b263c8cfe835fe9d935cf86fbb7be7549b62e6d (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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/. */

// Original author: ekr@rtfm.com
#include <deque>

#include "transportflow.h"
#include "transportlayer.h"

namespace mozilla {

NS_IMPL_ISUPPORTS0(TransportFlow)

// There are some hacks here to allow destruction off of
// the main thread.
TransportFlow::~TransportFlow() {
  // Push the destruction onto the STS thread. Note that there
  // is still some possibility that someone is accessing this
  // object simultaneously, but as long as smart pointer discipline
  // is maintained, it shouldn't be possible to access and
  // destroy it simultaneously. The conversion to a UniquePtr
  // ensures automatic destruction of the queue at exit of
  // DestroyFinal.
  CheckThread();
  ClearLayers(layers_.get());
}

void TransportFlow::DestroyFinal(
    UniquePtr<std::deque<TransportLayer*>> layers) {
  ClearLayers(layers.get());
}

void TransportFlow::ClearLayers(std::deque<TransportLayer*>* layers) {
  while (!layers->empty()) {
    delete layers->front();
    layers->pop_front();
  }
}

void TransportFlow::PushLayer(TransportLayer* layer) {
  CheckThread();
  layers_->push_front(layer);
  EnsureSameThread(layer);
  layer->SetFlowId(id_);
}

TransportLayer* TransportFlow::GetLayer(const std::string& id) const {
  CheckThread();

  if (layers_) {
    for (TransportLayer* layer : *layers_) {
      if (layer->id() == id) return layer;
    }
  }

  return nullptr;
}

void TransportFlow::EnsureSameThread(TransportLayer* layer) {
  // Enforce that if any of the layers have a thread binding,
  // they all have the same binding.
  if (target_) {
    const nsCOMPtr<nsIEventTarget>& lthread = layer->GetThread();

    if (lthread && (lthread != target_)) MOZ_CRASH();
  } else {
    target_ = layer->GetThread();
  }
}

}  // namespace mozilla