Repository navigation
Expand file tree
/
Copy pathfdproxy.dogma
More file actions
233 lines (186 loc) · 10.6 KB
/
Copy pathfdproxy.dogma
File metadata and controls
233 lines (186 loc) · 10.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
dogma_v1 utf-8
- identifier = fdproxy_v1
- description = fdproxy: the read protocol between FFmpeg and the parent that serves its input, version 1
- reference = fdproxy-specification.md
- dogma = https://github.com/kstenerud/dogma/blob/master/v1/dogma_v1.0.md
# A channel is two pipes, one each way (Channels). This grammar describes the bytes on either
# one, from the first byte to the writer's end. Which of the two a pipe is comes from the descriptor
# it was handed, never from what it carries.
#
# Not expressible here, and stated in the specification's prose:
# - the number of READs outstanding at once (READ), which pairs the two pipes in time;
# - that the parent sends nothing more after a violation, and kills FFmpeg instead (Serving);
# - that FFmpeg sends no DONE before a well-formed REQUEST has arrived (When DONE Is Sent);
# - the checks a reader must make without overflowing (Checks Without Overflow).
fdproxy_pipe = byte_order(lsb, [
pipe = written_by_ffmpeg : ffmpeg_to_parent;
pipe = written_by_parent : parent_to_ffmpeg;
]);
written_by_ffmpeg = 1;
written_by_parent = 2;
pipe: uinteger = """
Which pipe of the channel this is: `written_by_ffmpeg` for the one FFmpeg writes and the parent
reads (FFmpeg's -write_fd), `written_by_parent` for the one the parent writes and FFmpeg
reads (FFmpeg's -read_fd).
""";
###############################################################################
# FFmpeg to the parent
###############################################################################
# FFmpeg numbers every frame it sends: HELLO is 0, and each later frame takes the next number,
# wrapping from 127 to 0. A pipe may end after any whole frame: FFmpeg writes each frame with one
# write() of at most 4,096 bytes, so a kill never leaves half of one (Frame Size).
ffmpeg_to_parent = hello
& (capability(1) & reads_then_done(2)?)?
& eod
;
# READs until DONE, or until FFmpeg exits without one (When DONE Is Sent: a kill, a crash, an exit() mid-run).
reads_then_done(sequence) = read(sequence) & reads_then_done(next(sequence))?
| done(sequence)
;
next(sequence) = (sequence + 1) % 128;
###############################################################################
# The parent to FFmpeg
###############################################################################
# Every parent frame is an answer, and echoes the number of the frame it answers, oldest first (Sequence Numbers).
# REQUEST answers CAPABILITY (1). The DATA frames answer FFmpeg's READs in their order, so they
# carry the numbers from 2 on. The parent answers no READ after DONE, and none once it has decided
# to kill FFmpeg, so the DATA frames may stop short of the READs.
parent_to_ffmpeg = hello
& (request(1) & answers(2)?)?
& eod
;
answers(sequence) = data(sequence) & answers(next(sequence))?;
###############################################################################
# The frame (Frame Header): a 6-byte header, then the body
###############################################################################
frame(type, sequence, body_lengths, body) = frame_type(type)
& frame_sequence(sequence)
& frame_body_length(var(body_length, body_lengths))
& sized(body_length * 8, body)
;
frame_type(type) = i8(type); # which message: never 0
frame_sequence(sequence) = i8(sequence); # 0 to 127 (Sequence Numbers)
frame_body_length(length) = i32(length); # the body's length in bytes
type_hello = 1;
type_capability = 2;
type_request = 3;
type_read = 4;
type_data = 5;
type_done = 6;
###############################################################################
# HELLO, from both sides
###############################################################################
# Frozen in every version, its header included. The parent's echoes FFmpeg's sequence, 0.
hello = frame(type_hello, 0, hello_body_length, hello_body);
hello_body_length = 4;
hello_body = lowest_version_spoken(var(lowest, 1~))
& highest_version_spoken(lowest~)
;
lowest_version_spoken(v) = i16(v);
highest_version_spoken(v) = i16(v);
###############################################################################
# CAPABILITY, from FFmpeg
###############################################################################
capability(sequence) = frame(type_capability, sequence, capability_body_length, capability_body);
capability_body_length = 9;
capability_body = preferred_block_size(var(preferred, 1~))
& largest_data_body_accepted(preferred~)
& most_reads_outstanding(1~16)
;
preferred_block_size(v) = i32(v);
largest_data_body_accepted(v) = i32(v);
most_reads_outstanding(v) = i8(v);
###############################################################################
# REQUEST, from the parent: the terms
###############################################################################
request(sequence) = frame(type_request, sequence, request_body_length, request_body);
request_body_length = 27;
request_body = version(negotiated_version)
& run(1~)
& file_size(0~4611686018427387904)
& block_size(var(block, 1~capability_largest_body))
& longest_read(block~capability_largest_body)
& reads_outstanding(1~capability_outstanding)
;
version(v) = i16(v); # the version both sides worked out; a cross-check
run(v) = i64(v); # the parent's identifier for the run, echoed in DONE
file_size(v) = i64(v); # the input's size in bytes, at most 2^62 (Checks Without Overflow)
block_size(v) = i32(v); # the size of the blocks FFmpeg's side asks for
longest_read(v) = i32(v); # the most bytes one READ may ask for
reads_outstanding(v) = i8(v); # the most READs FFmpeg's side may have unanswered
###############################################################################
# READ, from FFmpeg
###############################################################################
read(sequence) = frame(type_read, sequence, read_body_length, read_body);
read_body_length = 12;
read_body = read_offset(var(offset, 0~terms_file_size))
& read_length((1~terms_longest_read) ! ((terms_file_size - offset + 1)~))
;
read_offset(v) = i64(v); # where in the input to read from
read_length(v) = i32(v); # how many bytes; never past the end of the input
###############################################################################
# DATA, from the parent
###############################################################################
data(sequence) = frame(type_data, sequence, length_read(sequence), data_body);
data_body = input_byte*; # exactly the bytes the READ asked for
input_byte = uint(8, ~);
###############################################################################
# DONE, from FFmpeg: its account of the channel
###############################################################################
done(sequence) = frame(type_done, sequence, done_body_lengths, done_body);
done_body_lengths = 9~1040;
done_body = done_run(terms_run | 0) # 0 when REQUEST's run broke its rule
& (done_ok | done_err)
;
done_run(v) = i64(v);
done_ok = result(result_ok);
done_err = result(result_err)
& error_kind(kind_terms | kind_channel | kind_protocol | kind_interrupted)
& error_code(0~)
& message_length(var(length, 0~1024))
& message(length)
;
result(v) = i8(v);
error_kind(v) = i8(v);
error_code(v) = i32(v); # FFmpeg's error code, negated; 0 when it had none to give
message_length(v) = i16(v);
message(length) = message_byte{length}; # for the logs; any bytes, sanitised (Logging Text)
message_byte = uint(8, ~);
result_ok = 0;
result_err = 1;
kind_terms = 1; # FFmpeg's side declined REQUEST's terms, or could not take them up
kind_channel = 2; # the channel to the parent ended: the parent died
kind_protocol = 3; # the parent broke the protocol: a wrong type, number or length
kind_interrupted = 4; # FFmpeg's interrupt callback fired while the channel waited
###############################################################################
# Values one pipe takes from the other
###############################################################################
negotiated_version: uinteger = """
The version both sides worked out from the two HELLOs (Versions): the lower of the two highest, valid
when it is at least the higher of the two lowest. Version 1 is the one this grammar describes.
""";
capability_largest_body: uinteger = """
The largest DATA body FFmpeg's CAPABILITY said it accepts.
""";
capability_outstanding: uinteger = """
The most READs outstanding FFmpeg's CAPABILITY said it can have.
""";
terms_run: uinteger = """
The run REQUEST gave.
""";
terms_file_size: uinteger = """
The file size REQUEST gave.
""";
terms_longest_read: uinteger = """
The longest READ REQUEST allowed.
""";
length_read(sequence: uinteger): uinteger = """
The length of the READ numbered `sequence` that this DATA answers: the oldest READ outstanding.
""";
###############################################################################
# Integers: signed, two's complement, little-endian (Integers)
###############################################################################
i8(v) = sint(8, v);
i16(v) = ordered(sint(16, v));
i32(v) = ordered(sint(32, v));
i64(v) = ordered(sint(64, v));