Line data Source code
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 0 : OutputStreamData::~OutputStreamData()
13 : {
14 0 : MOZ_ASSERT(NS_IsMainThread());
15 : // Break the connection to the input stream if necessary.
16 0 : if (mPort) {
17 0 : mPort->Destroy();
18 : }
19 0 : }
20 :
21 : void
22 0 : OutputStreamData::Init(OutputStreamManager* aOwner, ProcessedMediaStream* aStream)
23 : {
24 0 : mOwner = aOwner;
25 0 : mStream = aStream;
26 0 : }
27 :
28 : bool
29 0 : OutputStreamData::Connect(MediaStream* aStream)
30 : {
31 0 : MOZ_ASSERT(NS_IsMainThread());
32 0 : MOZ_ASSERT(!mPort, "Already connected?");
33 :
34 0 : if (mStream->IsDestroyed()) {
35 0 : return false;
36 : }
37 :
38 0 : mPort = mStream->AllocateInputPort(aStream);
39 0 : return true;
40 : }
41 :
42 : bool
43 0 : OutputStreamData::Disconnect()
44 : {
45 0 : MOZ_ASSERT(NS_IsMainThread());
46 :
47 : // During cycle collection, DOMMediaStream can be destroyed and send
48 : // its Destroy message before this decoder is destroyed. So we have to
49 : // be careful not to send any messages after the Destroy().
50 0 : if (mStream->IsDestroyed()) {
51 0 : return false;
52 : }
53 :
54 : // Disconnect the existing port if necessary.
55 0 : if (mPort) {
56 0 : mPort->Destroy();
57 0 : mPort = nullptr;
58 : }
59 0 : return true;
60 : }
61 :
62 : bool
63 0 : OutputStreamData::Equals(MediaStream* aStream) const
64 : {
65 0 : return mStream == aStream;
66 : }
67 :
68 : MediaStreamGraph*
69 0 : OutputStreamData::Graph() const
70 : {
71 0 : return mStream->Graph();
72 : }
73 :
74 : void
75 0 : OutputStreamManager::Add(ProcessedMediaStream* aStream, bool aFinishWhenEnded)
76 : {
77 0 : MOZ_ASSERT(NS_IsMainThread());
78 : // All streams must belong to the same graph.
79 0 : MOZ_ASSERT(!Graph() || Graph() == aStream->Graph());
80 :
81 : // Ensure that aStream finishes the moment mDecodedStream does.
82 0 : if (aFinishWhenEnded) {
83 0 : aStream->SetAutofinish(true);
84 : }
85 :
86 0 : OutputStreamData* p = mStreams.AppendElement();
87 0 : p->Init(this, aStream);
88 :
89 : // Connect to the input stream if we have one. Otherwise the output stream
90 : // will be connected in Connect().
91 0 : if (mInputStream) {
92 0 : p->Connect(mInputStream);
93 : }
94 0 : }
95 :
96 : void
97 0 : OutputStreamManager::Remove(MediaStream* aStream)
98 : {
99 0 : MOZ_ASSERT(NS_IsMainThread());
100 0 : for (int32_t i = mStreams.Length() - 1; i >= 0; --i) {
101 0 : if (mStreams[i].Equals(aStream)) {
102 0 : mStreams.RemoveElementAt(i);
103 0 : break;
104 : }
105 : }
106 0 : }
107 :
108 : void
109 0 : OutputStreamManager::Connect(MediaStream* aStream)
110 : {
111 0 : MOZ_ASSERT(NS_IsMainThread());
112 0 : mInputStream = aStream;
113 0 : for (int32_t i = mStreams.Length() - 1; i >= 0; --i) {
114 0 : if (!mStreams[i].Connect(aStream)) {
115 : // Probably the DOMMediaStream was GCed. Clean up.
116 0 : mStreams.RemoveElementAt(i);
117 : }
118 : }
119 0 : }
120 :
121 : void
122 0 : OutputStreamManager::Disconnect()
123 : {
124 0 : MOZ_ASSERT(NS_IsMainThread());
125 0 : mInputStream = nullptr;
126 0 : for (int32_t i = mStreams.Length() - 1; i >= 0; --i) {
127 0 : if (!mStreams[i].Disconnect()) {
128 : // Probably the DOMMediaStream was GCed. Clean up.
129 0 : mStreams.RemoveElementAt(i);
130 : }
131 : }
132 0 : }
133 :
134 : } // namespace mozilla
|