]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrokflow project. | |
3 | * | |
4 | * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li> | |
5 | * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <iostream> | |
23 | #include <libsigrokflow/libsigrokflow.hpp> | |
24 | ||
25 | namespace Srf | |
26 | { | |
27 | ||
28 | using namespace std; | |
29 | using namespace std::placeholders; | |
30 | ||
31 | void init() | |
32 | { | |
33 | #ifdef HAVE_LIBSIGROKCXX | |
34 | Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, | |
35 | "sigrok_legacy_capture_device", | |
36 | "Wrapper for capture devices using legacy libsigrok APIs", | |
37 | sigc::ptr_fun(&LegacyCaptureDevice::register_element), | |
38 | "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); | |
39 | Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, | |
40 | "sigrok_legacy_input", | |
41 | "Wrapper for inputs using legacy libsigrok APIs", | |
42 | sigc::ptr_fun(&LegacyInput::register_element), | |
43 | "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); | |
44 | Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, | |
45 | "sigrok_legacy_output", | |
46 | "Wrapper for outputs using legacy libsigrok APIs", | |
47 | sigc::ptr_fun(&LegacyOutput::register_element), | |
48 | "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); | |
49 | #endif | |
50 | #ifdef HAVE_LIBSIGROKDECODE | |
51 | Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, | |
52 | "sigrok_legacy_decoder", | |
53 | "Wrapper for protocol decoders using legacy libsigrokdecode APIs", | |
54 | sigc::ptr_fun(&LegacyDecoder::register_element), | |
55 | "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); | |
56 | #endif | |
57 | } | |
58 | ||
59 | Sink::Sink(GstBaseSink *gobj) : | |
60 | Gst::BaseSink(gobj) | |
61 | { | |
62 | } | |
63 | ||
64 | Device::Device(GstElement *gobj) : | |
65 | Gst::Element(gobj) | |
66 | { | |
67 | } | |
68 | ||
69 | CaptureDevice::CaptureDevice(GstElement *gobj) : | |
70 | Device(gobj) | |
71 | { | |
72 | } | |
73 | ||
74 | #ifdef HAVE_LIBSIGROKCXX | |
75 | void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass) | |
76 | { | |
77 | klass->set_metadata("sigrok legacy capture device", | |
78 | "Source", "Wrapper for capture devices using legacy libsigrok APIs", | |
79 | "Martin Ling"); | |
80 | ||
81 | klass->add_pad_template(Gst::PadTemplate::create( | |
82 | "src", | |
83 | Gst::PAD_SRC, | |
84 | Gst::PAD_ALWAYS, | |
85 | Gst::Caps::create_any())); | |
86 | } | |
87 | ||
88 | bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
89 | { | |
90 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device", | |
91 | 0, Gst::register_mm_type<LegacyCaptureDevice>( | |
92 | "sigrok_legacy_capture_device")); | |
93 | return true; | |
94 | } | |
95 | ||
96 | LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) : | |
97 | Glib::ObjectBase(typeid(LegacyCaptureDevice)), | |
98 | CaptureDevice(gobj) | |
99 | { | |
100 | add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src")); | |
101 | } | |
102 | ||
103 | Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create( | |
104 | shared_ptr<sigrok::HardwareDevice> libsigrok_device) | |
105 | { | |
106 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device"); | |
107 | if (!element) | |
108 | throw runtime_error("Failed to create element - plugin not registered?"); | |
109 | auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element); | |
110 | device->_libsigrok_device = libsigrok_device; | |
111 | return device; | |
112 | } | |
113 | ||
114 | shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device() | |
115 | { | |
116 | return _libsigrok_device; | |
117 | } | |
118 | ||
119 | Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition) | |
120 | { | |
121 | switch (transition) | |
122 | { | |
123 | case Gst::STATE_CHANGE_READY_TO_PAUSED: | |
124 | return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL; | |
125 | case Gst::STATE_CHANGE_PAUSED_TO_PLAYING: | |
126 | _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this)); | |
127 | _task->set_lock(_mutex); | |
128 | _src_pad->set_active(true); | |
129 | _task->start(); | |
130 | return Gst::STATE_CHANGE_SUCCESS; | |
131 | default: | |
132 | return Gst::STATE_CHANGE_SUCCESS; | |
133 | } | |
134 | } | |
135 | ||
136 | void LegacyCaptureDevice::_datafeed_callback( | |
137 | shared_ptr<sigrok::Device> device, | |
138 | shared_ptr<sigrok::Packet> packet) | |
139 | { | |
140 | (void) device; | |
141 | switch (packet->type()->id()) { | |
142 | case SR_DF_LOGIC: | |
143 | { | |
144 | auto logic = static_pointer_cast<sigrok::Logic>(packet->payload()); | |
145 | auto mem = Gst::Memory::create( | |
146 | Gst::MEMORY_FLAG_READONLY, | |
147 | logic->data_pointer(), | |
148 | logic->data_length(), | |
149 | 0, | |
150 | logic->data_length()); | |
151 | auto buf = Gst::Buffer::create(); | |
152 | buf->append_memory(move(mem)); | |
153 | _src_pad->push(move(buf)); | |
154 | break; | |
155 | } | |
156 | case SR_DF_END: | |
157 | _session->stop(); | |
158 | _src_pad->push_event(Gst::EventEos::create()); | |
159 | break; | |
160 | default: | |
161 | break; | |
162 | } | |
163 | } | |
164 | ||
165 | void LegacyCaptureDevice::_run() | |
166 | { | |
167 | _session = _libsigrok_device->driver()->parent()->create_session(); | |
168 | _session->add_device(_libsigrok_device); | |
169 | _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2)); | |
170 | _session->start(); | |
171 | _session->run(); | |
172 | _task->stop(); | |
173 | } | |
174 | ||
175 | void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass) | |
176 | { | |
177 | klass->set_metadata("sigrok legacy input", | |
178 | "Transform", "Wrapper for inputs using legacy libsigrok APIs", | |
179 | "Martin Ling"); | |
180 | ||
181 | klass->add_pad_template(Gst::PadTemplate::create( | |
182 | "sink", | |
183 | Gst::PAD_SINK, | |
184 | Gst::PAD_ALWAYS, | |
185 | Gst::Caps::create_any())); | |
186 | ||
187 | klass->add_pad_template(Gst::PadTemplate::create( | |
188 | "src", | |
189 | Gst::PAD_SRC, | |
190 | Gst::PAD_ALWAYS, | |
191 | Gst::Caps::create_any())); | |
192 | } | |
193 | ||
194 | bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
195 | { | |
196 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input", | |
197 | 0, Gst::register_mm_type<LegacyInput>( | |
198 | "sigrok_legacy_input")); | |
199 | return true; | |
200 | } | |
201 | ||
202 | LegacyInput::LegacyInput(GstElement *gobj) : | |
203 | Glib::ObjectBase(typeid(LegacyInput)), | |
204 | Gst::Element(gobj) | |
205 | { | |
206 | add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink")); | |
207 | add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src")); | |
208 | _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain)); | |
209 | } | |
210 | ||
211 | Glib::RefPtr<LegacyInput> LegacyInput::create( | |
212 | shared_ptr<sigrok::InputFormat> libsigrok_input_format, | |
213 | map<string, Glib::VariantBase> options) | |
214 | { | |
215 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_input"); | |
216 | if (!element) | |
217 | throw runtime_error("Failed to create element - plugin not registered?"); | |
218 | auto input = Glib::RefPtr<LegacyInput>::cast_static(element); | |
219 | input->_libsigrok_input_format = libsigrok_input_format; | |
220 | input->_options = options; | |
221 | return input; | |
222 | } | |
223 | ||
224 | bool LegacyInput::start_vfunc() | |
225 | { | |
226 | _libsigrok_input = _libsigrok_input_format->create_input(_options); | |
227 | auto context = _libsigrok_input_format->parent(); | |
228 | _session = context->create_session(); | |
229 | _session->add_device(_libsigrok_input->device()); | |
230 | _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2)); | |
231 | _session->start(); | |
232 | return true; | |
233 | } | |
234 | ||
235 | void LegacyInput::_datafeed_callback( | |
236 | shared_ptr<sigrok::Device> device, | |
237 | shared_ptr<sigrok::Packet> packet) | |
238 | { | |
239 | (void) device; | |
240 | switch (packet->type()->id()) { | |
241 | case SR_DF_LOGIC: | |
242 | { | |
243 | auto logic = static_pointer_cast<sigrok::Logic>(packet->payload()); | |
244 | auto mem = Gst::Memory::create( | |
245 | Gst::MEMORY_FLAG_READONLY, | |
246 | logic->data_pointer(), | |
247 | logic->data_length(), | |
248 | 0, | |
249 | logic->data_length()); | |
250 | auto buf = Gst::Buffer::create(); | |
251 | buf->append_memory(move(mem)); | |
252 | _src_pad->push(move(buf)); | |
253 | break; | |
254 | } | |
255 | case SR_DF_END: | |
256 | _session->stop(); | |
257 | _src_pad->push_event(Gst::EventEos::create()); | |
258 | break; | |
259 | default: | |
260 | break; | |
261 | } | |
262 | } | |
263 | ||
264 | Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &, | |
265 | const Glib::RefPtr<Gst::Buffer> &buf) | |
266 | { | |
267 | Gst::MapInfo info; | |
268 | buf->map(info, Gst::MAP_READ); | |
269 | _libsigrok_input->send(info.get_data(), info.get_size()); | |
270 | buf->unmap(info); | |
271 | return Gst::FLOW_OK; | |
272 | } | |
273 | ||
274 | bool LegacyInput::stop_vfunc() | |
275 | { | |
276 | _libsigrok_input->end(); | |
277 | return true; | |
278 | } | |
279 | ||
280 | void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass) | |
281 | { | |
282 | klass->set_metadata("sigrok legacy output", | |
283 | "Sink", "Wrapper for outputs using legacy libsigrok APIs", | |
284 | "Martin Ling"); | |
285 | ||
286 | klass->add_pad_template(Gst::PadTemplate::create( | |
287 | "sink", | |
288 | Gst::PAD_SINK, | |
289 | Gst::PAD_ALWAYS, | |
290 | Gst::Caps::create_any())); | |
291 | } | |
292 | ||
293 | bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
294 | { | |
295 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output", | |
296 | 0, Gst::register_mm_type<LegacyOutput>( | |
297 | "sigrok_legacy_output")); | |
298 | return true; | |
299 | } | |
300 | ||
301 | LegacyOutput::LegacyOutput(GstBaseSink *gobj) : | |
302 | Glib::ObjectBase(typeid(LegacyOutput)), | |
303 | Sink(gobj) | |
304 | { | |
305 | } | |
306 | ||
307 | Glib::RefPtr<LegacyOutput>LegacyOutput::create( | |
308 | shared_ptr<sigrok::OutputFormat> libsigrok_output_format, | |
309 | shared_ptr<sigrok::Device> libsigrok_device, | |
310 | map<string, Glib::VariantBase> options) | |
311 | { | |
312 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_output"); | |
313 | if (!element) | |
314 | throw runtime_error("Failed to create element - plugin not registered?"); | |
315 | auto output = Glib::RefPtr<LegacyOutput>::cast_static(element); | |
316 | output->_libsigrok_output_format = libsigrok_output_format; | |
317 | output->_libsigrok_device = libsigrok_device; | |
318 | output->_options = options; | |
319 | return output; | |
320 | } | |
321 | ||
322 | bool LegacyOutput::start_vfunc() | |
323 | { | |
324 | _libsigrok_output = _libsigrok_output_format->create_output( | |
325 | _libsigrok_device, _options); | |
326 | return true; | |
327 | } | |
328 | ||
329 | Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
330 | { | |
331 | Gst::MapInfo info; | |
332 | buffer->map(info, Gst::MAP_READ); | |
333 | auto context = _libsigrok_output_format->parent(); | |
334 | auto packet = context->create_logic_packet( | |
335 | info.get_data(), info.get_size(), 2); | |
336 | auto result = _libsigrok_output->receive(packet); | |
337 | cout << result; | |
338 | buffer->unmap(info); | |
339 | return Gst::FLOW_OK; | |
340 | } | |
341 | ||
342 | bool LegacyOutput::stop_vfunc() | |
343 | { | |
344 | auto context = _libsigrok_output_format->parent(); | |
345 | auto end_packet = context->create_end_packet(); | |
346 | auto result = _libsigrok_output->receive(end_packet); | |
347 | cout << result; | |
348 | return true; | |
349 | } | |
350 | #endif | |
351 | ||
352 | #ifdef HAVE_LIBSIGROKDECODE | |
353 | void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass) | |
354 | { | |
355 | klass->set_metadata("sigrok legacy decoder", | |
356 | "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs", | |
357 | "Uwe Hermann"); | |
358 | ||
359 | klass->add_pad_template(Gst::PadTemplate::create( | |
360 | "sink", | |
361 | Gst::PAD_SINK, | |
362 | Gst::PAD_ALWAYS, | |
363 | Gst::Caps::create_any())); | |
364 | } | |
365 | ||
366 | bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
367 | { | |
368 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder", | |
369 | 0, Gst::register_mm_type<LegacyDecoder>( | |
370 | "sigrok_legacy_decoder")); | |
371 | return true; | |
372 | } | |
373 | ||
374 | LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) : | |
375 | Glib::ObjectBase(typeid(LegacyDecoder)), | |
376 | Sink(gobj) | |
377 | { | |
378 | } | |
379 | ||
380 | Glib::RefPtr<LegacyDecoder>LegacyDecoder::create( | |
381 | struct srd_session *libsigrokdecode_session, uint64_t unitsize) | |
382 | { | |
383 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder"); | |
384 | if (!element) | |
385 | throw runtime_error("Failed to create element - plugin not registered?"); | |
386 | auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element); | |
387 | decoder->_session = libsigrokdecode_session; | |
388 | decoder->_unitsize = unitsize; | |
389 | return decoder; | |
390 | } | |
391 | ||
392 | struct srd_session *LegacyDecoder::libsigrokdecode_session() | |
393 | { | |
394 | return _session; | |
395 | } | |
396 | ||
397 | Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
398 | { | |
399 | Gst::MapInfo info; | |
400 | buffer->map(info, Gst::MAP_READ); | |
401 | uint64_t num_samples = info.get_size() / _unitsize; | |
402 | srd_session_send(_session, _abs_ss, _abs_ss + num_samples, | |
403 | info.get_data(), info.get_size(), _unitsize); | |
404 | _abs_ss += num_samples; | |
405 | buffer->unmap(info); | |
406 | return Gst::FLOW_OK; | |
407 | } | |
408 | ||
409 | bool LegacyDecoder::start_vfunc() | |
410 | { | |
411 | _abs_ss = 0; | |
412 | srd_session_start(_session); | |
413 | return true; | |
414 | } | |
415 | ||
416 | bool LegacyDecoder::stop_vfunc() | |
417 | { | |
418 | srd_session_terminate_reset(_session); | |
419 | return true; | |
420 | } | |
421 | #endif | |
422 | ||
423 | } |