summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/jsapi/PacketDumper.cpp
blob: 2b62e0806ed42cc4b62a3214385d4562e6fbd4a3 (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
/* 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/. */

#include "jsapi/PacketDumper.h"
#include "jsapi/PeerConnectionImpl.h"
#include "mozilla/media/MediaUtils.h"  // NewRunnableFrom
#include "nsThreadUtils.h"             // NS_DispatchToMainThread

namespace mozilla {

/* static */
RefPtr<PacketDumper> PacketDumper::GetPacketDumper(
    const std::string& aPcHandle) {
  MOZ_ASSERT(NS_IsMainThread());
  PeerConnectionWrapper pcw(aPcHandle);
  if (pcw.impl()) {
    return pcw.impl()->GetPacketDumper();
  }

  return new PacketDumper("");
}

PacketDumper::PacketDumper(const std::string& aPcHandle)
    : mPcHandle(aPcHandle),
      mPacketDumpEnabled(false),
      mPacketDumpFlagsMutex("Packet dump flags mutex") {}

void PacketDumper::Dump(size_t aLevel, dom::mozPacketDumpType aType,
                        bool aSending, const void* aData, size_t aSize) {
  // Optimization; avoids making a copy of the buffer, but we need to lock a
  // mutex and check the flags. Could be optimized further, if we really want to
  if (!ShouldDumpPacket(aLevel, aType, aSending)) {
    return;
  }

  UniquePtr<uint8_t[]> ownedPacket = MakeUnique<uint8_t[]>(aSize);
  memcpy(ownedPacket.get(), aData, aSize);

  RefPtr<Runnable> dumpRunnable = media::NewRunnableFrom(std::bind(
      [this, self = RefPtr<PacketDumper>(this), aLevel, aType, aSending,
       aSize](UniquePtr<uint8_t[]>& aPacket) -> nsresult {
        // Check again; packet dump might have been disabled since the dispatch
        if (ShouldDumpPacket(aLevel, aType, aSending)) {
          PeerConnectionWrapper pcw(mPcHandle);
          RefPtr<PeerConnectionImpl> pc = pcw.impl();
          if (pc) {
            pc->DumpPacket_m(aLevel, aType, aSending, aPacket, aSize);
          }
        }
        return NS_OK;
      },
      std::move(ownedPacket)));

  NS_DispatchToMainThread(dumpRunnable);
}

nsresult PacketDumper::EnablePacketDump(unsigned long aLevel,
                                        dom::mozPacketDumpType aType,
                                        bool aSending) {
  mPacketDumpEnabled = true;
  std::vector<unsigned>* packetDumpFlags;
  if (aSending) {
    packetDumpFlags = &mSendPacketDumpFlags;
  } else {
    packetDumpFlags = &mRecvPacketDumpFlags;
  }

  unsigned flag = 1 << (unsigned)aType;

  MutexAutoLock lock(mPacketDumpFlagsMutex);
  if (aLevel >= packetDumpFlags->size()) {
    packetDumpFlags->resize(aLevel + 1);
  }

  (*packetDumpFlags)[aLevel] |= flag;
  return NS_OK;
}

nsresult PacketDumper::DisablePacketDump(unsigned long aLevel,
                                         dom::mozPacketDumpType aType,
                                         bool aSending) {
  std::vector<unsigned>* packetDumpFlags;
  if (aSending) {
    packetDumpFlags = &mSendPacketDumpFlags;
  } else {
    packetDumpFlags = &mRecvPacketDumpFlags;
  }

  unsigned flag = 1 << (unsigned)aType;

  MutexAutoLock lock(mPacketDumpFlagsMutex);
  if (aLevel < packetDumpFlags->size()) {
    (*packetDumpFlags)[aLevel] &= ~flag;
  }

  return NS_OK;
}

bool PacketDumper::ShouldDumpPacket(size_t aLevel, dom::mozPacketDumpType aType,
                                    bool aSending) const {
  if (!mPacketDumpEnabled) {
    return false;
  }

  MutexAutoLock lock(mPacketDumpFlagsMutex);

  const std::vector<unsigned>* packetDumpFlags;

  if (aSending) {
    packetDumpFlags = &mSendPacketDumpFlags;
  } else {
    packetDumpFlags = &mRecvPacketDumpFlags;
  }

  if (aLevel < packetDumpFlags->size()) {
    unsigned flag = 1 << (unsigned)aType;
    return flag & packetDumpFlags->at(aLevel);
  }

  return false;
}

}  // namespace mozilla