OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SERVICES_MEDIA_FRAMEWORK_GRAPH_H_ | |
6 #define SERVICES_MEDIA_FRAMEWORK_GRAPH_H_ | |
7 | |
8 #include <list> | |
9 | |
10 #include "services/media/framework/engine.h" | |
11 #include "services/media/framework/refs.h" | |
12 #include "services/media/framework/stages/active_sink_stage.h" | |
13 #include "services/media/framework/stages/active_source_stage.h" | |
14 #include "services/media/framework/stages/multistream_source_stage.h" | |
15 #include "services/media/framework/stages/stage.h" | |
16 #include "services/media/framework/stages/transform_stage.h" | |
17 | |
18 namespace mojo { | |
19 namespace media { | |
20 | |
21 namespace { | |
22 | |
23 // StageCreator::Create creates a stage for a part. DEFINE_STAGE_CREATOR defines | |
24 // a specialization for a particular model/stage type pair. Every new | |
25 // model/stage type pair that's defined will need an entry here. | |
26 template<typename T, typename Enable = void> class StageCreator; | |
27 | |
28 #define DEFINE_STAGE_CREATOR(TModel, TStage) \ | |
29 template<typename T> \ | |
30 class StageCreator<T, typename std::enable_if< \ | |
31 std::is_base_of<TModel, T>::value>::type> { \ | |
32 public: \ | |
33 static inline Stage* Create(std::shared_ptr<T> t_ptr) { \ | |
34 return new TStage(std::shared_ptr<TModel>(t_ptr)); \ | |
35 } \ | |
36 }; | |
37 | |
38 DEFINE_STAGE_CREATOR(MultistreamSource, MultistreamSourceStage); | |
39 DEFINE_STAGE_CREATOR(Transform, TransformStage); | |
40 DEFINE_STAGE_CREATOR(ActiveSource, ActiveSourceStage); | |
41 DEFINE_STAGE_CREATOR(ActiveSink, ActiveSinkStage); | |
42 | |
43 #undef DEFINE_STAGE_CREATOR | |
44 | |
45 } // namespace | |
46 | |
47 // | |
48 // USAGE | |
49 // | |
50 // Graph is a container for sources, sinks and transforms ('parts') connected | |
51 // in a graph. PartRef, InputRef and OutputRef are all | |
52 // references to parts and their inputs and outputs. Graph provides a variety | |
53 // of methods for adding and removing parts and for connecting inputs and | |
54 // outputs to form a graph. | |
55 // | |
56 // The graph isn't thread-safe. If the graph is to be modified and/or | |
57 // interrogated on multiple threads, the caller must provide its own lock | |
58 // to prevent collisions. In this case, the caller must also acquire the same | |
59 // lock when making calls that cause parts to add or remove inputs or outputs. | |
60 // | |
61 // The graph prevents the disconnection of prepared inputs and outputs. Once | |
62 // a connected input/output pair is prepared, it must be unprepared before | |
63 // disconnection. This allows the engine to operate freely over prepared | |
64 // portions of the graph (prepare and unprepare are synchronized with the | |
65 // engine). | |
66 // | |
67 // Parts added to the graph are referenced using shared pointers. The graph | |
68 // holds pointers to the parts it contains, and the application, in many cases, | |
69 // also holds pointers to the parts so it can call methods that are outside the | |
70 // graph's scope. When a part is added, the graph returns a PartRef | |
71 // object, which can be used to reference the part when the graph is modified. | |
72 // PartRef objects can be interrogated to retrieve inputs (as | |
73 // InputRef objects) and outputs (as OutputRef objects). | |
74 // | |
75 // Parts come in various flavors, defined by 'model' abstract classes. The | |
76 // current list of supported models is: | |
77 // | |
78 // ActiveSink - a sink that consumes packets asynchronously | |
79 // ActiveSource - a source that produces packets asynchronously | |
johngro
2016/02/08 22:33:36
<nit> indentation </nit>
dalesat
2016/02/09 00:34:11
Hazard of grep
| |
80 // MultistreamSource - a source that produces multiple streams of packets | |
81 // synchronously | |
82 // Transform - a synchronous transform that consumes and produces | |
83 // packets via one input and one output | |
84 // | |
85 // Other models will be defined in the future as needed. | |
86 // | |
87 | |
88 // | |
89 // DESIGN | |
90 // | |
91 // The Graph is implemented as a system of cooperating objects. Of those | |
92 // objects, only the graph itself is of relevance to code that uses Graph and | |
93 // to part implementations. The other objects are: | |
94 // | |
95 // Stage | |
96 // A stage hosts a single part. There are many subclasses of Stage, one for | |
97 // each supported part model. The stage's job is to implement the contract | |
98 // represented by the model so the parts that conform to the model can | |
99 // participate in the operation of the graph. Stages are uniform with respect | |
100 // to how they interact with graph. PartRef references a stage. | |
101 // | |
102 // Input | |
103 // A stage possesses zero or more Input instances. Input objects | |
104 // implement the supply of media into the stage and demand for media signalled | |
105 // upstream. Inputs recieve media from Outputs in the form of packets | |
106 // (type Packet). | |
107 // | |
108 // Output | |
109 // A stage possesses zero or more Output instances. Output objects | |
110 // implement the supply of media output of the stage to a downstream input and | |
111 // demand for media signalled from that input. | |
112 // | |
113 | |
114 // Host for a source, sink or transform. | |
115 class Graph { | |
116 public: | |
117 Graph(); | |
118 | |
119 ~Graph(); | |
120 | |
121 // Adds a part to the graph. | |
122 template<typename T> | |
123 PartRef Add(std::shared_ptr<T> t_ptr) { | |
124 DCHECK(t_ptr); | |
125 return Add(StageCreator<T>::Create(t_ptr)); | |
126 } | |
127 | |
128 // Removes a part from the graph after disconnecting it from other parts. | |
129 void RemovePart(PartRef part); | |
130 | |
131 // Connects an output connector to an input connector. Returns the dowstream | |
132 // part. | |
133 PartRef Connect(const OutputRef& output, const InputRef& input); | |
134 | |
135 // Connects a part with exactly one output to a part with exactly one input. | |
136 // Returns the downstream part. | |
137 PartRef ConnectParts(PartRef upstream_part, PartRef downstream_part); | |
138 | |
139 // Connects an output connector to a part that has exactly one input. Returns | |
140 // the downstream part. | |
141 PartRef ConnectOutputToPart(const OutputRef& output, PartRef downstream_part); | |
142 | |
143 // Connects a part with exactly one output to an input connector. Returns the | |
144 // downstream part. | |
145 PartRef ConnectPartToInput(PartRef upstream_part, const InputRef& input); | |
146 | |
147 // Disconnects an output connector and the input connector to which it's | |
148 // connected. | |
149 void DisconnectOutput(const OutputRef& output); | |
150 | |
151 // Disconnects an input connector and the output connector to which it's | |
152 // connected. | |
153 void DisconnectInput(const InputRef& input); | |
154 | |
155 // Disconnects and removes part and everything connected to it. | |
156 void RemovePartsConnectedToPart(PartRef part); | |
157 | |
158 // Disconnects and removes everything connected to output. | |
159 void RemovePartsConnectedToOutput(const OutputRef& output); | |
160 | |
161 // Disconnects and removes everything connected to input. | |
162 void RemovePartsConnectedToInput(const InputRef& input); | |
163 | |
164 // Adds all the parts in t (which must all have one input and one output) and | |
165 // connects them in sequence to the output connector. Returns the output | |
166 // connector of the last part or the output parameter if it is empty. | |
167 template<typename T> | |
168 OutputRef AddAndConnectAll(OutputRef output, const T& t) { | |
169 for (auto& element : t) { | |
johngro
2016/02/08 22:33:36
const auto&
dalesat
2016/02/09 00:34:11
Done.
| |
170 PartRef part = Add(StageCreator<T>::Create(element)); | |
171 Connect(output, part.input()); | |
172 output = part.output(); | |
173 } | |
174 return output; | |
175 } | |
176 | |
177 // Removes all parts from the graph. | |
178 void Reset(); | |
179 | |
180 // Prepares the graph for operation. | |
181 void Prepare(); | |
182 | |
183 // Prepares the input and everything upstream of it. This method is used to | |
184 // prepare subgraphs added when the rest of the graph is already prepared. | |
185 void PrepareInput(const InputRef& input); | |
186 | |
187 // Primes all the sinks in the graph. | |
188 void PrimeSinks(); | |
189 | |
190 private: | |
191 // Adds a stage to the graph. | |
192 PartRef Add(Stage* stage); | |
193 | |
194 std::list<Stage*> stages_; | |
195 std::list<Stage*> sources_; | |
196 std::list<Stage*> sinks_; | |
197 | |
198 Engine engine_; | |
199 Stage::UpdateCallback update_function_; | |
200 }; | |
201 | |
202 } // namespace media | |
203 } // namespace mojo | |
204 | |
205 #endif // SERVICES_MEDIA_FRAMEWORK_GRAPH_H_ | |
OLD | NEW |