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_ENGINE_H_ | |
6 #define SERVICES_MEDIA_FRAMEWORK_ENGINE_H_ | |
7 | |
8 #include <deque> | |
9 #include <list> | |
10 | |
11 #include "base/synchronization/lock.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/distributor_stage.h" | |
15 #include "services/media/framework/stages/lpcm_transform_stage.h" | |
16 #include "services/media/framework/stages/packet_transform_stage.h" | |
17 #include "services/media/framework/stages/stage.h" | |
18 | |
19 namespace mojo { | |
20 namespace media { | |
21 | |
22 // Host for a source, sink or transform. | |
23 class Engine { | |
24 public: | |
25 class Input; | |
26 class Output; | |
27 | |
28 // Opaque Stage pointer used for graph building. | |
29 class Part { | |
30 public: | |
31 Part() : stage_(nullptr) {} | |
32 | |
33 uint32_t input_count(); | |
34 Input input(uint32_t index); | |
35 Input input(); | |
36 uint32_t output_count(); | |
37 Output output(uint32_t index); | |
38 Output output(); | |
39 Part upstream_part(uint32_t index); | |
40 Part upstream_part(); | |
41 Part downstream_part(uint32_t index); | |
42 Part downstream_part(); | |
43 | |
44 private: | |
45 explicit Part(Stage* stage) : stage_(stage) {} | |
46 | |
47 explicit operator bool() const { return stage_ != nullptr; } | |
48 | |
49 Stage* stage_; | |
50 | |
51 friend Engine; | |
52 friend Input; | |
53 friend Output; | |
54 }; | |
55 | |
56 // Opaque StageInput pointer used for graph building. | |
57 class Input { | |
58 public: | |
59 Input() : stage_(nullptr), index_(0) {} | |
60 | |
61 explicit operator bool() const { return stage_ != nullptr; } | |
62 | |
63 Part part() { return Part(stage_); } | |
64 | |
65 bool connected() { | |
66 DCHECK(stage_); | |
67 return stage_input().upstream_stage() != nullptr; | |
68 } | |
69 | |
70 Part upstream_part() { | |
71 DCHECK(connected()); | |
72 return Part(stage_input().upstream_stage()); | |
73 } | |
74 | |
75 private: | |
76 Input(Stage* stage, uint32_t index) : | |
77 stage_(stage), index_(index) { | |
78 DCHECK(stage_); | |
79 DCHECK(index_ < stage_->input_count()); | |
80 } | |
81 | |
82 StageInput& stage_input() { | |
83 DCHECK(stage_); | |
84 return stage_->input(index_); | |
85 } | |
86 | |
87 Stage* stage_; | |
88 uint32_t index_; | |
89 | |
90 friend Engine; | |
91 friend Part; | |
92 friend Output; | |
93 }; | |
94 | |
95 // Opaque StageOutput pointer used for graph building. | |
96 class Output { | |
97 public: | |
98 Output() : stage_(nullptr), index_(0) {} | |
99 | |
100 explicit operator bool() const { return stage_ != nullptr; } | |
101 | |
102 Part part() { return Part(stage_); } | |
103 | |
104 bool connected() { | |
105 DCHECK(stage_); | |
106 return stage_output().downstream_stage() != nullptr; | |
107 } | |
108 | |
109 Part downstream_part() { | |
110 DCHECK(connected()); | |
111 return Part(stage_output().downstream_stage()); | |
112 } | |
113 | |
114 private: | |
115 Output(Stage* stage, uint32_t index) : | |
116 stage_(stage), index_(index) { | |
117 DCHECK(stage_); | |
118 DCHECK(index_ < stage_->output_count()); | |
119 } | |
120 | |
121 StageOutput& stage_output() { | |
122 DCHECK(stage_); | |
123 return stage_->output(index_); | |
124 } | |
125 | |
126 Stage* stage_; | |
127 uint32_t index_; | |
128 | |
129 friend Engine; | |
130 friend Part; | |
131 friend Input; | |
132 }; | |
133 | |
134 Engine(); | |
135 | |
136 ~Engine(); | |
137 | |
138 // Adds a part to the engine. | |
139 template<typename T, typename TBase> | |
140 Part Add(SharedPtr<T, TBase> t) { | |
141 DCHECK(t); | |
142 return Add(CreateStage(std::shared_ptr<TBase>(t))); | |
143 } | |
144 | |
145 // Removes a part from the engine after disconnecting it from other parts. | |
146 void Remove(Part part); | |
147 | |
148 // Connects an output connector to an input connector. Returns the dowstream | |
149 // part. | |
150 Part Connect(Output output, Input input); | |
151 | |
152 // Connects a part with exactly one output to a part with exactly one input. | |
153 // Returns the downstream part. | |
154 Part Connect(Part upstream_part, Part downstream_part); | |
155 | |
156 // Connects an output connector to a part that has exactly one input. Returns | |
157 // the downstream part. | |
158 Part Connect(Output output, Part downstream_part); | |
159 | |
160 // Connects a part with exactly one output to an input connector. Returns the | |
161 // downstream part. | |
162 Part Connect(Part upstream_part, Input input); | |
163 | |
164 // Disconnects an output connector and the input connector to which it's | |
165 // connected. | |
166 void Disconnect(Output output); | |
167 | |
168 // Disconnects an input connector and the output connector to which it's | |
169 // connected. | |
170 void Disconnect(Input input); | |
171 | |
172 // Disconnects and removes part and everything connected to it. | |
173 void RemoveAll(Part part); | |
174 | |
175 // Disconnects and removes everything connected to output. | |
176 void RemoveAll(Output output); | |
177 | |
178 // Disconnects and removes everything connected to input. | |
179 void RemoveAll(Input input); | |
180 | |
181 // Adds all the parts in t (which must all have one input and one output) and | |
182 // connects them in sequence to the output connector. Returns the output | |
183 // connector of the last part or the output parameter if it is empty. | |
184 template<typename T> | |
185 Output AddAndConnectAll( | |
186 Output output, | |
187 const T& t) { | |
188 for (auto& element : t) { | |
189 Part part = Add(CreateStage(element)); | |
190 Connect(output, part.input()); | |
191 output = part.output(); | |
192 } | |
193 return output; | |
194 } | |
195 | |
196 // Prepares the engine. | |
197 void Prepare(); | |
198 | |
199 // Prepares the part and everything upstream of it. This method is used to | |
200 // prepare subgraphs added when the rest of the graph is already prepared. | |
201 void Prepare(Part part); | |
202 | |
203 // Primes all the sinks in the graph. | |
204 void PrimeSinks(); | |
205 | |
206 // Removes all parts from the engine. | |
207 void Reset(); | |
208 | |
209 // Pushes the stage to the supply backlog if it isn't already there. | |
210 void PushToSupplyBacklogUnsafe(Stage* stage); | |
211 | |
212 // Pushes the stage to the demand backlog if it isn't already there. | |
213 void PushToDemandBacklogUnsafe(Stage* stage); | |
johngro
2016/01/28 19:14:55
The unsafe methods are the methods which assume th
dalesat
2016/01/29 01:08:30
These are called by inputs and outputs, which are
johngro
2016/02/01 22:38:17
Acknowledged.
| |
214 | |
215 private: | |
216 // Adds a stage to the engine. | |
217 Part Add(Stage* stage); | |
218 | |
219 // Disconnects an output. | |
220 void DisconnectOutputUnsafe(Stage* stage, uint32_t index); | |
221 | |
222 // Disconnects an input. | |
223 void DisconnectInputUnsafe(Stage* stage, uint32_t index); | |
224 | |
225 // Removes a stage. | |
226 void RemoveUnsafe(Stage* stage); | |
227 | |
228 // Creates a stage from a source, sink or transform. A specialization of this | |
229 // template is defined for each type of source, sink or transform that can be | |
230 // added to the engine. | |
231 template<typename T> | |
232 static Stage* CreateStage(std::shared_ptr<T> t); | |
233 | |
234 // CreateStage template specialization for MultiStreamPacketSource. | |
235 static Stage* CreateStage(MultiStreamPacketSourcePtr source); | |
236 | |
237 // CreateStage template specialization for PacketTransform. | |
238 static Stage* CreateStage(PacketTransformPtr transform); | |
239 | |
240 // CreateStage template specialization for ActiveSource. | |
241 static Stage* CreateStage(ActiveSourcePtr source); | |
242 | |
243 // CreateStage template specialization for ActiveSink. | |
244 static Stage* CreateStage(ActiveSinkPtr sink); | |
245 | |
246 // CreateStage template specialization for LpcmTransform. | |
247 static Stage* CreateStage(LpcmTransformPtr transform); | |
248 | |
249 // Prepares a stage. | |
250 void PrepareUnsafe(Stage* stage); | |
251 | |
252 // Processes the entire backlog. | |
253 void UpdateUnsafe(); | |
254 | |
255 // Performs processing for a single stage, updating the backlog accordingly. | |
256 void UpdateUnsafe(Stage *stage); | |
257 | |
258 // Pops a stage from the supply backlog and returns it or returns nullptr if | |
259 // the supply backlog is empty. | |
260 Stage* PopFromSupplyBacklogUnsafe(); | |
261 | |
262 // Pops a stage from the demand backlog and returns it or returns nullptr if | |
263 // the demand backlog is empty. | |
264 Stage* PopFromDemandBacklogUnsafe(); | |
265 | |
266 mutable base::Lock lock_; | |
johngro
2016/01/28 19:14:55
Now that you are adding thread safety and multi th
dalesat
2016/01/29 01:08:30
Done.
| |
267 std::list<Stage*> stages_; | |
268 std::list<Stage*> sources_; | |
269 std::list<Stage*> sinks_; | |
270 std::deque<Stage*> supply_backlog_; | |
271 std::deque<Stage*> demand_backlog_; | |
272 Stage::UpdateCallback update_function_; | |
273 bool packets_produced_; | |
274 }; | |
275 | |
276 } // namespace media | |
277 } // namespace mojo | |
278 | |
279 #endif // SERVICES_MEDIA_FRAMEWORK_ENGINE_ENGINE_H_ | |
OLD | NEW |