2 * This file is part of the libsigrokflow project.
4 * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
5 * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
22 #include <libsigrokflow/libsigrokflow.hpp>
24 #include <libsigrokdecode/libsigrokdecode.h>
32 using namespace std::placeholders;
36 #ifdef HAVE_LIBSIGROKCXX
37 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
38 "sigrok_legacy_capture_device",
39 "Wrapper for capture devices using legacy libsigrok APIs",
40 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
41 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
42 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
43 "sigrok_legacy_input",
44 "Wrapper for inputs using legacy libsigrok APIs",
45 sigc::ptr_fun(&LegacyInput::register_element),
46 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
47 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
48 "sigrok_legacy_output",
49 "Wrapper for outputs using legacy libsigrok APIs",
50 sigc::ptr_fun(&LegacyOutput::register_element),
51 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
53 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
54 "sigrok_legacy_decoder",
55 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
56 sigc::ptr_fun(&LegacyDecoder::register_element),
57 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
60 Sink::Sink(GstBaseSink *gobj) :
65 Device::Device(GstElement *gobj) :
70 CaptureDevice::CaptureDevice(GstElement *gobj) :
75 #ifdef HAVE_LIBSIGROKCXX
76 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
78 klass->set_metadata("sigrok legacy capture device",
79 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
82 klass->add_pad_template(Gst::PadTemplate::create(
86 Gst::Caps::create_any()));
89 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
91 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
92 0, Gst::register_mm_type<LegacyCaptureDevice>(
93 "sigrok_legacy_capture_device"));
97 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
98 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
101 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
104 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
105 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
107 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
109 throw runtime_error("Failed to create element - plugin not registered?");
110 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
111 device->_libsigrok_device = libsigrok_device;
115 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
117 return _libsigrok_device;
120 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
124 case Gst::STATE_CHANGE_READY_TO_PAUSED:
125 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
126 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
127 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
128 _task->set_lock(_mutex);
129 _src_pad->set_active(true);
131 return Gst::STATE_CHANGE_SUCCESS;
133 return Gst::STATE_CHANGE_SUCCESS;
137 void LegacyCaptureDevice::_datafeed_callback(
138 shared_ptr<sigrok::Device> device,
139 shared_ptr<sigrok::Packet> packet)
142 switch (packet->type()->id()) {
145 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
146 auto mem = Gst::Memory::create(
147 Gst::MEMORY_FLAG_READONLY,
148 logic->data_pointer(),
149 logic->data_length(),
151 logic->data_length());
152 auto buf = Gst::Buffer::create();
153 buf->append_memory(move(mem));
154 _src_pad->push(move(buf));
159 _src_pad->push_event(Gst::EventEos::create());
166 void LegacyCaptureDevice::_run()
168 _session = _libsigrok_device->driver()->parent()->create_session();
169 _session->add_device(_libsigrok_device);
170 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
176 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
178 klass->set_metadata("sigrok legacy input",
179 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
182 klass->add_pad_template(Gst::PadTemplate::create(
186 Gst::Caps::create_any()));
188 klass->add_pad_template(Gst::PadTemplate::create(
192 Gst::Caps::create_any()));
195 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
197 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
198 0, Gst::register_mm_type<LegacyInput>(
199 "sigrok_legacy_input"));
203 LegacyInput::LegacyInput(GstElement *gobj) :
204 Glib::ObjectBase(typeid(LegacyInput)),
207 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
208 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
209 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
212 Glib::RefPtr<LegacyInput> LegacyInput::create(
213 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
214 map<string, Glib::VariantBase> options)
216 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
218 throw runtime_error("Failed to create element - plugin not registered?");
219 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
220 input->_libsigrok_input_format = libsigrok_input_format;
221 input->_options = options;
225 bool LegacyInput::start_vfunc()
227 _libsigrok_input = _libsigrok_input_format->create_input(_options);
228 auto context = _libsigrok_input_format->parent();
229 _session = context->create_session();
230 _session->add_device(_libsigrok_input->device());
231 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
236 void LegacyInput::_datafeed_callback(
237 shared_ptr<sigrok::Device> device,
238 shared_ptr<sigrok::Packet> packet)
241 switch (packet->type()->id()) {
244 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
245 auto mem = Gst::Memory::create(
246 Gst::MEMORY_FLAG_READONLY,
247 logic->data_pointer(),
248 logic->data_length(),
250 logic->data_length());
251 auto buf = Gst::Buffer::create();
252 buf->append_memory(move(mem));
253 _src_pad->push(move(buf));
258 _src_pad->push_event(Gst::EventEos::create());
265 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
266 const Glib::RefPtr<Gst::Buffer> &buf)
269 buf->map(info, Gst::MAP_READ);
270 _libsigrok_input->send(info.get_data(), info.get_size());
275 bool LegacyInput::stop_vfunc()
277 _libsigrok_input->end();
281 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
283 klass->set_metadata("sigrok legacy output",
284 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
287 klass->add_pad_template(Gst::PadTemplate::create(
291 Gst::Caps::create_any()));
294 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
296 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
297 0, Gst::register_mm_type<LegacyOutput>(
298 "sigrok_legacy_output"));
302 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
303 Glib::ObjectBase(typeid(LegacyOutput)),
308 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
309 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
310 shared_ptr<sigrok::Device> libsigrok_device,
311 map<string, Glib::VariantBase> options)
313 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
315 throw runtime_error("Failed to create element - plugin not registered?");
316 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
317 output->_libsigrok_output_format = libsigrok_output_format;
318 output->_libsigrok_device = libsigrok_device;
319 output->_options = options;
323 bool LegacyOutput::start_vfunc()
325 _libsigrok_output = _libsigrok_output_format->create_output(
326 _libsigrok_device, _options);
330 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
333 buffer->map(info, Gst::MAP_READ);
334 auto context = _libsigrok_output_format->parent();
335 auto packet = context->create_logic_packet(
336 info.get_data(), info.get_size(), 2);
337 auto result = _libsigrok_output->receive(packet);
343 bool LegacyOutput::stop_vfunc()
345 auto context = _libsigrok_output_format->parent();
346 auto end_packet = context->create_end_packet();
347 auto result = _libsigrok_output->receive(end_packet);
353 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
355 klass->set_metadata("sigrok legacy decoder",
356 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
359 klass->add_pad_template(Gst::PadTemplate::create(
363 Gst::Caps::create_any()));
366 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
368 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
369 0, Gst::register_mm_type<LegacyDecoder>(
370 "sigrok_legacy_decoder"));
374 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
375 Glib::ObjectBase(typeid(LegacyDecoder)),
380 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
381 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
383 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
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;
392 struct srd_session *LegacyDecoder::libsigrokdecode_session()
397 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
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;
409 bool LegacyDecoder::start_vfunc()
412 srd_session_start(_session);
416 bool LegacyDecoder::stop_vfunc()
418 srd_session_terminate_reset(_session);