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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
mod external_stream_handler;
mod external_stream_reader;
pub mod factory;
mod ffmpeg_handler;
pub mod ffmpeg_hls;
pub mod ffmpeg_pull;
pub mod ffmpeg_rtmp_push;
pub mod ffmpeg_transcode;
pub mod rtmp_receive;
pub mod rtmp_watch;
pub mod workflow_forwarder;
use super::MediaNotification;
use crate::workflows::definitions::WorkflowStepDefinition;
use downcast_rs::{impl_downcast, Downcast};
use futures::future::BoxFuture;
pub use external_stream_handler::*;
pub use external_stream_reader::*;
pub trait StepFutureResult: Downcast {}
impl_downcast!(StepFutureResult);
pub type FutureList = Vec<BoxFuture<'static, Box<dyn StepFutureResult>>>;
pub type StepCreationResult = Result<
(Box<dyn WorkflowStep + Sync + Send>, FutureList),
Box<dyn std::error::Error + Sync + Send>,
>;
pub type CreateFactoryFnResult =
Box<dyn Fn(&WorkflowStepDefinition) -> StepCreationResult + Send + Sync>;
#[derive(Clone, Debug, PartialEq)]
pub enum StepStatus {
Created,
Active,
Error { message: String },
Shutdown,
}
pub struct StepInputs {
pub media: Vec<MediaNotification>,
pub notifications: Vec<Box<dyn StepFutureResult>>,
}
impl StepInputs {
pub fn new() -> Self {
StepInputs {
media: Vec::new(),
notifications: Vec::new(),
}
}
pub fn clear(&mut self) {
self.media.clear();
self.notifications.clear();
}
}
pub struct StepOutputs {
pub media: Vec<MediaNotification>,
pub futures: Vec<BoxFuture<'static, Box<dyn StepFutureResult>>>,
}
impl StepOutputs {
pub fn new() -> Self {
StepOutputs {
media: Vec::new(),
futures: Vec::new(),
}
}
pub fn clear(&mut self) {
self.futures.clear();
self.media.clear();
}
}
pub trait WorkflowStep {
fn get_status(&self) -> &StepStatus;
fn get_definition(&self) -> &WorkflowStepDefinition;
fn execute(&mut self, inputs: &mut StepInputs, outputs: &mut StepOutputs);
fn shutdown(&mut self);
}
#[cfg(test)]
use crate::workflows::steps::factory::StepGenerator;
#[cfg(test)]
use futures::stream::FuturesUnordered;
#[cfg(test)]
use futures::StreamExt;
#[cfg(test)]
use std::iter::FromIterator;
#[cfg(test)]
use std::time::Duration;
#[cfg(test)]
struct StepTestContext {
step: Box<dyn WorkflowStep>,
futures: FuturesUnordered<BoxFuture<'static, Box<dyn StepFutureResult>>>,
media_outputs: Vec<MediaNotification>,
}
#[cfg(test)]
impl StepTestContext {
fn new(generator: Box<dyn StepGenerator>, definition: WorkflowStepDefinition) -> Self {
let (step, futures) = generator
.generate(definition)
.expect("Failed to generate workflow step");
StepTestContext {
step,
futures: FuturesUnordered::from_iter(futures),
media_outputs: Vec::new(),
}
}
fn execute_with_media(&mut self, media: MediaNotification) {
let mut outputs = StepOutputs::new();
let mut inputs = StepInputs::new();
inputs.media.push(media);
self.step.execute(&mut inputs, &mut outputs);
self.futures.extend(outputs.futures.drain(..));
self.media_outputs = outputs.media;
}
async fn execute_notification(&mut self, notification: Box<dyn StepFutureResult>) {
let mut outputs = StepOutputs::new();
let mut inputs = StepInputs::new();
inputs.notifications.push(notification);
self.step.execute(&mut inputs, &mut outputs);
self.futures.extend(outputs.futures.drain(..));
self.media_outputs = outputs.media;
self.execute_pending_notifications().await;
}
async fn execute_pending_notifications(&mut self) {
loop {
let notification =
match tokio::time::timeout(Duration::from_millis(10), self.futures.next()).await {
Ok(Some(notification)) => notification,
_ => break,
};
let mut outputs = StepOutputs::new();
let mut inputs = StepInputs::new();
inputs.notifications.push(notification);
self.step.execute(&mut inputs, &mut outputs);
self.futures.extend(outputs.futures.drain(..));
self.media_outputs = outputs.media;
}
}
fn assert_media_passed_through(&mut self, media: MediaNotification) {
self.execute_with_media(media.clone());
assert_eq!(
self.media_outputs.len(),
1,
"Unexpected number of media outputs"
);
assert_eq!(self.media_outputs[0], media, "Unexpected media message");
}
fn assert_media_not_passed_through(&mut self, media: MediaNotification) {
self.execute_with_media(media.clone());
assert!(self.media_outputs.is_empty(), "Expected no media outputs");
}
}