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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include "frameBuffer.hpp"
#include "stitchOutput.hpp"
#include "stereoOutput.hpp"
#include "libvideostitch/controller.hpp"
#include <atomic>
#include <deque>
#include <memory>
#include <mutex>
#include <thread>
namespace VideoStitch {
namespace Output {
class VideoWriter;
class StereoWriter;
} // namespace Output
namespace Core {
/**
* A set of host panoramic buffers that can be filled asynchronously.
*
* It buffers N frames and waits when no frame is available.
* Warning: on destruction, waits for all pending frames to be written.
* This can effectively block forever if there are missing frames.
*
* Frames should start at 0.
*
* Past frames will be ignored, current frame undergoes a
* special treatment if refilled (for restitch).
* Future frames are buffered when possible, if not fill()
* becomes blocking.
*
* 3 threads are manipulating the panoramic buffers.
* - "register callbacks" thread : read all buffers, don't change their state (stitched / blank / in use),
* modify them
* - "stitcher" thread : pop one from blanks, put one in stitched
* - "consumer" thread : pop one from stitched, put one in blank
*/
template <typename FrameBuffer>
class AsyncBuffer {
public:
typedef FrameBuffer* Frame;
typedef FrameBuffer FB;
Status initialize(const std::vector<std::shared_ptr<typename FrameBuffer::Surface>>&,
const std::vector<std::shared_ptr<Output::VideoWriter>>& writers);
virtual ~AsyncBuffer();
Status pushVideo(mtime_t date);
Status registerWriters(const std::vector<std::shared_ptr<Output::VideoWriter>>&);
Status registerWriter(std::shared_ptr<Output::VideoWriter>);
protected:
void synchronize(FrameBuffer* frame) { frame->streamSynchronize(); }
FrameBuffer* getUsedFrame(mtime_t date);
FrameBuffer* getCurrentFrame(mtime_t date);
// Frames
std::mutex bkMu;
std::condition_variable bkCond;
std::deque<FrameBuffer*> blankFrames;
std::mutex stMu;
std::condition_variable stCond;
std::deque<std::pair<mtime_t, FrameBuffer*>> stitchedFrames;
bool shutDown = false;
// Hold the frames while the stitcher schedules everything
// The stitcher asks first for the device buffer in which
// to schedule the stitching, then, once everything is scheduled,
// it asks for the host buffer where to render the results.
//
// Since multiple frames can be scheduled at the same time, if more
// than double buffering is used, we ask
// to store the correspondence in between.
// Unlocked, since only the "stitcher" thread
// accesses it.
std::map<mtime_t, FrameBuffer*> inUse;
// used by the "register" thread only
std::vector<FrameBuffer*> allFrames;
};
class AsyncSourceBuffer : public AsyncBuffer<SourceFrameBuffer> {
public:
static Potential<AsyncSourceBuffer> create(const std::vector<std::shared_ptr<SourceFrameBuffer::Surface>>&,
const std::vector<std::shared_ptr<Output::VideoWriter>>&);
GPU::Surface& acquireFrame(mtime_t date, GPU::Stream& stream);
};
class AsyncPanoBuffer : public AsyncBuffer<PanoFrameBuffer> {
public:
static Potential<AsyncPanoBuffer> create(const std::vector<std::shared_ptr<PanoFrameBuffer::Surface>>&,
const std::vector<std::shared_ptr<Output::VideoWriter>>&);
PanoSurface& acquireFrame(mtime_t date);
};
/**
* A set of host stereoscopic buffers that can be filled asynchronously.
*
* It buffers N frames and waits when no frame is available.
* Warning: on destruction, waits for all pending frames to be written.
* This can effectively block forever if there are missing frames.
*
* Frames should start at 0.
*
* Past frames will be ignored, current frame undergoes a
* special treatment if refilled (for restitch).
* Future frames are buffered when possible, if not fill()
* becomes blocking.
* 3 types of threads are manipulating the panoramic buffers.
* - "register callbacks" thread : read all buffers, don't change their state (stitched / blank / in use),
* modify them
* - 2 "stitcher" threads : pop one from blanks, put one in stitched, collaborate (one pop - one push)
* - "consumer" thread : pop one from stitched, put one in blank
*/
class AsyncStereoBuffer {
public:
typedef std::pair<StereoFrameBuffer*, StereoFrameBuffer*> Frame;
typedef PanoFrameBuffer FB;
static Potential<AsyncStereoBuffer> create(const std::vector<std::shared_ptr<PanoSurface>>&,
const std::vector<std::shared_ptr<Output::StereoWriter>>& writers);
Status initialize(const std::vector<std::shared_ptr<PanoSurface>>&,
const std::vector<std::shared_ptr<Output::StereoWriter>>& writers);
virtual ~AsyncStereoBuffer();
PanoSurface& acquireLeftFrame(mtime_t);
PanoSurface& acquireRightFrame(mtime_t);
Status pushVideo(mtime_t date, Eye eye);
Status registerWriters(const std::vector<std::shared_ptr<Output::StereoWriter>>&);
Status registerWriter(std::shared_ptr<Output::StereoWriter>);
protected:
AsyncStereoBuffer() {}
PanoSurface& acquireFrame(mtime_t date, Eye eye);
void synchronize(Frame frame);
Frame getUsedFrame(mtime_t date);
Frame getCurrentFrame(mtime_t date);
// Frames
std::mutex bkMu;
std::condition_variable bkCond;
std::deque<Frame> blankFrames;
std::mutex stMu;
std::condition_variable stCond;
std::deque<std::pair<mtime_t, Frame>> stitchedFrames;
bool shutDown = false;
// Hold the frames while the stitcher schedules everything
// The stitcher asks first for the device buffer in which
// to schedule the stitching, then, once everything is scheduled,
// it asks for the host buffer where to render the results.
//
// Since multiple frames can be scheduled at the same time, if more
// than double buffering is used, we ask
// to store the correspondence in between.
std::map<mtime_t, Frame> inUse;
// used by the "register" thread only
std::vector<Frame> allFrames;
};
template <typename Pimpl, typename AsyncBuffer, typename Pusher, typename Device>
class AsyncBufferedOutput : public Pimpl, protected AsyncBuffer, protected Pusher {
public:
typedef typename Pimpl::Writer Writer;
AsyncBufferedOutput(const std::vector<std::shared_ptr<typename AsyncBuffer::FB::Surface>>&,
const std::vector<std::shared_ptr<Writer>>& writers);
virtual ~AsyncBufferedOutput();
virtual bool setRenderers(const std::vector<std::shared_ptr<typename AsyncBuffer::FB::Renderer>>&) override;
virtual bool addRenderer(std::shared_ptr<typename AsyncBuffer::FB::Renderer> renderer) override {
return Pusher::addRenderer(renderer);
}
virtual bool removeRenderer(const std::string& name) override { return Pusher::removeRenderer(name); }
virtual bool setWriters(const std::vector<std::shared_ptr<Writer>>&) override;
virtual bool addWriter(std::shared_ptr<Writer>) override;
virtual bool removeWriter(const std::string&) override;
virtual bool updateWriter(const std::string&, const Ptv::Value&) override;
protected:
Status initialize(const std::vector<std::shared_ptr<typename AsyncBuffer::FB::Surface>>&,
const std::vector<std::shared_ptr<Writer>>& writers);
private:
static void consumerThread(AsyncBufferedOutput* that);
std::thread* worker;
std::atomic<bool> shutdown;
};
class AsyncSourceOutput : public AsyncBufferedOutput<ExtractOutput::Pimpl, AsyncSourceBuffer,
WriterPusher<SourceFrameBuffer>, PanoDeviceDefinition> {
public:
typedef AsyncBufferedOutput<ExtractOutput::Pimpl, AsyncSourceBuffer, WriterPusher<SourceFrameBuffer>,
PanoDeviceDefinition>
Base;
static Potential<AsyncSourceOutput> create(const std::vector<std::shared_ptr<SourceSurface>>& surfs,
const std::vector<std::shared_ptr<SourceRenderer>>& renderers,
const std::vector<std::shared_ptr<Output::VideoWriter>>& writers,
int source) {
AsyncSourceOutput* aso = new AsyncSourceOutput(surfs, renderers, writers, source);
FAIL_RETURN(aso->initialize(surfs, writers));
return aso;
}
Status pushVideo(mtime_t date) override { return AsyncBuffer<SourceFrameBuffer>::pushVideo(date); }
GPU::Surface& acquireFrame(mtime_t date, GPU::Stream& stream) override {
return AsyncSourceBuffer::acquireFrame(date, stream);
}
private:
AsyncSourceOutput(const std::vector<std::shared_ptr<SourceSurface>>& surfs,
const std::vector<std::shared_ptr<SourceRenderer>>& renderers,
const std::vector<std::shared_ptr<Output::VideoWriter>>& writers, int source)
: Base(surfs, writers) {
sourceIdx = source;
setRenderers(renderers);
}
};
class AsyncStitchOutput : public AsyncBufferedOutput<StitchOutput::Pimpl, AsyncPanoBuffer,
WriterPusher<PanoFrameBuffer>, PanoDeviceDefinition> {
public:
static Potential<AsyncStitchOutput> create(const std::vector<std::shared_ptr<PanoSurface>>& surfs,
const std::vector<std::shared_ptr<PanoRenderer>>& renderers,
const std::vector<std::shared_ptr<Output::VideoWriter>>& writers) {
AsyncStitchOutput* aso = new AsyncStitchOutput(surfs, renderers, writers);
FAIL_RETURN(aso->initialize(surfs, writers));
return aso;
}
Status pushVideo(mtime_t date) override { return AsyncBuffer<PanoFrameBuffer>::pushVideo(date); }
virtual void setCompositor(const std::shared_ptr<GPU::Overlayer>& c) override {
WriterPusher<PanoFrameBuffer>::setCompositor(c);
}
PanoSurface& acquireFrame(mtime_t date) override { return AsyncPanoBuffer::acquireFrame(date); }
protected:
typedef AsyncBufferedOutput<StitchOutput::Pimpl, AsyncPanoBuffer, WriterPusher<PanoFrameBuffer>, PanoDeviceDefinition>
Base;
AsyncStitchOutput(const std::vector<std::shared_ptr<PanoSurface>>& surfs,
const std::vector<std::shared_ptr<PanoRenderer>>& renderers,
const std::vector<std::shared_ptr<Output::VideoWriter>>& writers)
: Base(surfs, writers) {
setRenderers(renderers);
}
};
class AsyncStereoOutput
: public AsyncBufferedOutput<StereoOutput::Pimpl, AsyncStereoBuffer, StereoWriterPusher, StereoDeviceDefinition> {
public:
static Potential<AsyncStereoOutput> create(const std::vector<std::shared_ptr<PanoSurface>>& surfs,
const std::vector<std::shared_ptr<PanoRenderer>>& renderers,
const std::vector<std::shared_ptr<Output::StereoWriter>>& writers) {
AsyncStereoOutput* ret = new AsyncStereoOutput(surfs, renderers, writers);
FAIL_RETURN(ret->initialize(surfs, writers));
return ret;
}
virtual ~AsyncStereoOutput() {}
Status pushVideo(mtime_t date, Eye eye) override { return AsyncStereoBuffer::pushVideo(date, eye); }
virtual PanoSurface& acquireLeftFrame(mtime_t date) override { return AsyncStereoBuffer::acquireLeftFrame(date); }
virtual PanoSurface& acquireRightFrame(mtime_t date) override { return AsyncStereoBuffer::acquireRightFrame(date); }
protected:
AsyncStereoOutput(const std::vector<std::shared_ptr<PanoSurface>>& surfs,
const std::vector<std::shared_ptr<PanoRenderer>>& renderers,
const std::vector<std::shared_ptr<Output::StereoWriter>>& writers)
: AsyncBufferedOutput(surfs, writers) {
setRenderers(renderers);
}
};
} // namespace Core
} // namespace VideoStitch