ADTF Qt5
Loading...
Searching...
No Matches
reader.h
Go to the documentation of this file.
1
7
8/*
9 * This file depends on Qt which is licensed under LGPLv3.
10 * See ADTF_DIR/3rdparty/qt5 and doc/license for detailed information.
11 */
12#pragma once
13#include <adtfstreaming3/samplereader.h>
14#include <adtfmediadescription/sample_codec.h>
15#include <adtfstreaming3/streammetatypeimage.h>
16
17#include <QJsonObject>
18#include <QImage>
19#include <QJSEngine>
20
21#include <unordered_map>
22#include <mutex>
23#include <string>
24
25namespace adtf
26{
27namespace javascript
28{
29namespace scripting
30{
31
33class cImageConverter
34{
35public:
36 cImageConverter(const adtf::streaming::IStreamType& oType);
37 QImage convert(const adtf::streaming::ISample& oSample);
38
39private:
40 adtf::streaming::tStreamImageFormat m_sInputFormat;
41};
42
43inline tResult script_error_to_result(const QJSValue& oScriptResult)
44{
45 if (oScriptResult.isError())
46 {
47 RETURN_ERROR_DESC(ERR_FAILED, "Uncaught script exception at %s:%d: %s",
48 oScriptResult.property("fileName").toString().toLocal8Bit().data(),
49 oScriptResult.property("lineNumber").toInt(), oScriptResult.toString().toLocal8Bit().data());
50 }
51 else
52 {
53 RETURN_ERROR_DESC(ERR_FAILED, "Uncaught script exception: %s", oScriptResult.toString().toLocal8Bit().data());
54 }
55}
56
57class cSyncSignalRecievers : public QObject
58{
59 Q_OBJECT
60
61public:
62 Q_INVOKABLE void connect(QJSValue oReciever)
63 {
64 m_oRecievers.push_back(oReciever);
65 }
66
67 template<typename... Arguments>
68 tResult call(Arguments&&... arguments)
69 {
70 QJSValueList oJSArgs{std::forward<Arguments>(arguments)...};
71 for (auto& oReciever : m_oRecievers)
72 {
73 auto oScriptResult = oReciever.call(oJSArgs);
74 if (!oScriptResult.isNull() && !oScriptResult.isUndefined())
75 {
76 return script_error_to_result(oScriptResult);
77 }
78 }
79
80 RETURN_NOERROR;
81 }
82
83 bool isConnected() const
84 {
85 return !m_oRecievers.empty();
86 }
87
88private:
89 std::vector<QJSValue> m_oRecievers;
90};
92
112class cScriptReader : public QObject
113{
114 Q_OBJECT
115
116public:
118 cScriptReader(adtf::streaming::cDynamicSampleReader* pReader, QJSEngine& oEngine);
119 tResult Check();
121
130 Q_INVOKABLE QVariant getNextSample();
131
141 Q_INVOKABLE QVariant getLastSample();
142
151 Q_INVOKABLE void requestSamples(const QVariant& oSubStreamFilter);
152
160 Q_INVOKABLE void cancelRequest(const QVariant& oSubStreamFilter);
161
166 Q_PROPERTY(QVariantList requests READ requests NOTIFY requestsChanged)
167
168signals:
170#ifdef DOXYGEN
179 void streamTypeChanged(QObject* type);
180
192 void trigger(qint64 timestamp);
193
204 void sample(const QVariant& sample);
205#endif
206
208 void requestsChanged();
209
210public:
211 const QVariantList& requests() const noexcept;
212
213 virtual tResult OnTrigger(adtf::base::tNanoSeconds tmTrigger) = 0;
214
215protected:
216 void DiscardSamples();
217 std::string GetName();
218 virtual tResult ForwardStreamTypeToScriptHandler(QObject* oType) = 0;
219 virtual std::pair<bool, bool> TriggerAndSampleCallbacks() const = 0;
220 QJSEngine& m_oEngine;
221
222private:
223 QVariantMap Transform(const adtf::ucom::iobject_ptr<const adtf::streaming::ISample>& pSample);
224
225 adtf::streaming::cDynamicSampleReader* m_pReader = nullptr;
226 mutable std::mutex m_oSubStreamsLock;
227
228 // Empty state indicates that decoder initialization has failed.
229 using tSubsStreamDecoder =
230 std::variant<std::monostate, adtf::mediadescription::cSampleCodecFactory, cImageConverter>;
231
232 struct tDecodableSubStream
233 {
234 tDecodableSubStream(const QString strName,
235 const adtf::ucom::iobject_ptr<const adtf::streaming::IStreamType>& pType):
236 strName(strName), pType(pType)
237 {
238 }
239 QString strName;
240 adtf::ucom::object_ptr<const adtf::streaming::IStreamType> pType;
241 // Non-zero pointer indicates that stream is currently matches by requests.
242 std::unique_ptr<tSubsStreamDecoder> pDecoder;
243 };
244
245 struct tRequestableSubStream
246 {
247 tRequestableSubStream(uint32_t nId): nId(nId)
248 {
249 }
250 uint32_t nId;
251 adtf::ucom::object_ptr<adtf::streaming::IStreamingRequest> pRequest;
252 };
253
254 // Can only contain QString and QRegularExpression. Specifies substream names.
255 QVariantList m_oRequestFilters;
256
257 bool m_bUseSampleSubStreamId = false;
258 // Built from type received in AcceptType
259 // Contains only an entry for id==0, if m_bUseSampleSubStreamId is not set.
260 std::unordered_map<uint32_t, tDecodableSubStream> m_oDecodableSubStreams;
261 // Built from type received in SynchronousTypeUpdate
262 std::unordered_map<QString, tRequestableSubStream> m_oRequestableSubStream;
263
264 static std::unique_ptr<tSubsStreamDecoder>
265 GetStreamDecoder(const adtf::ucom::iobject_ptr<const adtf::streaming::IStreamType>& pType);
266
267 static bool MatchesFilter(const QVariant& oRequestFilter, const QString& strSubStream) noexcept;
268 bool MatchesCurrentFilters(const QString& strSubStream) const noexcept;
269
271};
272
278class cAsyncScriptReader : public cScriptReader
279{
280 Q_OBJECT
281
282public:
283 using cScriptReader::cScriptReader;
284 tResult OnTrigger(adtf::base::tNanoSeconds tmTrigger) override;
285
286signals:
287 void streamTypeChanged(QObject* type);
288 void trigger(qint64 timestamp);
289 void sample(const QVariant& sample);
290 void triggerProcessed(qint64 timestamp);
291
292protected:
293 tResult ForwardStreamTypeToScriptHandler(QObject* oType) override;
294 std::pair<bool, bool> TriggerAndSampleCallbacks() const override;
295};
297
302class cSyncScriptReader : public cScriptReader
303{
304 Q_OBJECT
305 Q_PROPERTY(QObject* streamTypeChanged READ getSyncStreamTypes)
306 Q_PROPERTY(QObject* trigger READ getSyncTrigger)
307 Q_PROPERTY(QObject* sample READ getSyncSample)
308
309public:
310 cSyncScriptReader(adtf::streaming::cDynamicSampleReader* pReader, QJSEngine& oEngine);
311 tResult OnTrigger(adtf::base::tNanoSeconds tmTrigger) override;
312
313protected:
314 tResult ForwardStreamTypeToScriptHandler(QObject* oType) override;
315 std::pair<bool, bool> TriggerAndSampleCallbacks() const override;
316
317private:
318 QObject* getSyncStreamTypes()
319 {
320 return &m_oSyncStreamTypeRecievers;
321 }
322
323 QObject* getSyncTrigger()
324 {
325 return &m_oSyncTriggerRecievers;
326 }
327
328 QObject* getSyncSample()
329 {
330 return &m_oSyncSampleRecievers;
331 }
332
333 cSyncSignalRecievers m_oSyncStreamTypeRecievers;
334 cSyncSignalRecievers m_oSyncTriggerRecievers;
335 cSyncSignalRecievers m_oSyncSampleRecievers;
336};
338
339} // namespace scripting
340} // namespace javascript
341} // namespace adtf
Q_INVOKABLE void cancelRequest(const QVariant &oSubStreamFilter)
QVariantList requests
Definition reader.h:166
Q_INVOKABLE void requestSamples(const QVariant &oSubStreamFilter)
Helper namespace for extension of adtf_string_forward interfaces.
Definition qt_string_intf.h:11
Definition clock.h:25
Namespace for the ADTF JavaScript Filter SDK.
Definition clock.h:22
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtfui_pkg.h:13