Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/mediasink/OutputStreamManager.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "MediaStreamGraph.h"
8
#include "OutputStreamManager.h"
9
10
namespace mozilla {
11
12
OutputStreamData::~OutputStreamData()
13
0
{
14
0
  MOZ_ASSERT(NS_IsMainThread());
15
0
  // Break the connection to the input stream if necessary.
16
0
  for (RefPtr<MediaInputPort>& port : mPorts) {
17
0
    port->Destroy();
18
0
  }
19
0
}
20
21
void
22
OutputStreamData::Init(OutputStreamManager* aOwner,
23
                       ProcessedMediaStream* aStream,
24
                       TrackID aNextAvailableTrackID)
25
0
{
26
0
  mOwner = aOwner;
27
0
  mStream = aStream;
28
0
  mNextAvailableTrackID = aNextAvailableTrackID;
29
0
}
30
31
bool
32
OutputStreamData::Connect(MediaStream* aStream,
33
                          TrackID aInputAudioTrackID,
34
                          TrackID aInputVideoTrackID)
35
0
{
36
0
  MOZ_ASSERT(NS_IsMainThread());
37
0
  MOZ_ASSERT(mPorts.IsEmpty(), "Already connected?");
38
0
39
0
  if (mStream->IsDestroyed()) {
40
0
    return false;
41
0
  }
42
0
43
0
  for (TrackID tid : {aInputAudioTrackID, aInputVideoTrackID}) {
44
0
    if (tid == TRACK_NONE) {
45
0
      continue;
46
0
    }
47
0
    MOZ_ASSERT(IsTrackIDExplicit(tid));
48
0
    mPorts.AppendElement(mStream->AllocateInputPort(
49
0
        aStream, tid, mNextAvailableTrackID++));
50
0
  }
51
0
  return true;
52
0
}
53
54
bool
55
OutputStreamData::Disconnect()
56
0
{
57
0
  MOZ_ASSERT(NS_IsMainThread());
58
0
59
0
  // During cycle collection, DOMMediaStream can be destroyed and send
60
0
  // its Destroy message before this decoder is destroyed. So we have to
61
0
  // be careful not to send any messages after the Destroy().
62
0
  if (mStream->IsDestroyed()) {
63
0
    return false;
64
0
  }
65
0
66
0
  // Disconnect any existing port.
67
0
  for (RefPtr<MediaInputPort>& port : mPorts) {
68
0
    port->Destroy();
69
0
  }
70
0
  mPorts.Clear();
71
0
  return true;
72
0
}
73
74
bool
75
OutputStreamData::Equals(MediaStream* aStream) const
76
0
{
77
0
  return mStream == aStream;
78
0
}
79
80
MediaStreamGraph*
81
OutputStreamData::Graph() const
82
0
{
83
0
  return mStream->Graph();
84
0
}
85
86
TrackID
87
OutputStreamData::NextAvailableTrackID() const
88
0
{
89
0
  return mNextAvailableTrackID;
90
0
}
91
92
void
93
OutputStreamManager::Add(ProcessedMediaStream* aStream,
94
                         TrackID aNextAvailableTrackID,
95
                         bool aFinishWhenEnded)
96
0
{
97
0
  MOZ_ASSERT(NS_IsMainThread());
98
0
  // All streams must belong to the same graph.
99
0
  MOZ_ASSERT(!Graph() || Graph() == aStream->Graph());
100
0
101
0
  // Ensure that aStream finishes the moment mDecodedStream does.
102
0
  if (aFinishWhenEnded) {
103
0
    aStream->QueueSetAutofinish(true);
104
0
  }
105
0
106
0
  OutputStreamData* p = mStreams.AppendElement();
107
0
  p->Init(this, aStream, aNextAvailableTrackID);
108
0
109
0
  // Connect to the input stream if we have one. Otherwise the output stream
110
0
  // will be connected in Connect().
111
0
  if (mInputStream) {
112
0
    p->Connect(mInputStream, mInputAudioTrackID, mInputVideoTrackID);
113
0
  }
114
0
}
115
116
void
117
OutputStreamManager::Remove(MediaStream* aStream)
118
0
{
119
0
  MOZ_ASSERT(NS_IsMainThread());
120
0
  for (int32_t i = mStreams.Length() - 1; i >= 0; --i) {
121
0
    if (mStreams[i].Equals(aStream)) {
122
0
      mStreams.RemoveElementAt(i);
123
0
      break;
124
0
    }
125
0
  }
126
0
}
127
128
void
129
OutputStreamManager::Clear()
130
0
{
131
0
  MOZ_ASSERT(NS_IsMainThread());
132
0
  mStreams.Clear();
133
0
}
134
135
TrackID
136
OutputStreamManager::NextAvailableTrackIDFor(MediaStream* aOutputStream) const
137
0
{
138
0
  MOZ_ASSERT(NS_IsMainThread());
139
0
  for (const OutputStreamData& out : mStreams) {
140
0
    if (out.Equals(aOutputStream)) {
141
0
      return out.NextAvailableTrackID();
142
0
    }
143
0
  }
144
0
  return TRACK_INVALID;
145
0
}
146
147
void
148
OutputStreamManager::Connect(MediaStream* aStream,
149
                             TrackID aAudioTrackID,
150
                             TrackID aVideoTrackID)
151
0
{
152
0
  MOZ_ASSERT(NS_IsMainThread());
153
0
  mInputStream = aStream;
154
0
  mInputAudioTrackID = aAudioTrackID;
155
0
  mInputVideoTrackID = aVideoTrackID;
156
0
  for (int32_t i = mStreams.Length() - 1; i >= 0; --i) {
157
0
    if (!mStreams[i].Connect(aStream, mInputAudioTrackID, mInputVideoTrackID)) {
158
0
      // Probably the DOMMediaStream was GCed. Clean up.
159
0
      mStreams.RemoveElementAt(i);
160
0
    }
161
0
  }
162
0
}
163
164
void
165
OutputStreamManager::Disconnect()
166
0
{
167
0
  MOZ_ASSERT(NS_IsMainThread());
168
0
  mInputStream = nullptr;
169
0
  mInputAudioTrackID = TRACK_INVALID;
170
0
  mInputVideoTrackID = TRACK_INVALID;
171
0
  for (int32_t i = mStreams.Length() - 1; i >= 0; --i) {
172
0
    if (!mStreams[i].Disconnect()) {
173
0
      // Probably the DOMMediaStream was GCed. Clean up.
174
0
      mStreams.RemoveElementAt(i);
175
0
    }
176
0
  }
177
0
}
178
179
} // namespace mozilla