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/>.
23 #include <libsigrokflow/libsigrokflow.hpp>
29 using namespace std::placeholders;
31 static bool srf_initialized_ = false;
36 throw runtime_error("libsigrokflow is already initialized");
38 #ifdef HAVE_LIBSIGROKCXX
39 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
40 "sigrok_legacy_capture_device",
41 "Wrapper for capture devices using legacy libsigrok APIs",
42 sigc::ptr_fun(&LegacyCaptureDevice::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_input",
46 "Wrapper for inputs using legacy libsigrok APIs",
47 sigc::ptr_fun(&LegacyInput::register_element),
48 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
49 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
50 "sigrok_legacy_output",
51 "Wrapper for outputs using legacy libsigrok APIs",
52 sigc::ptr_fun(&LegacyOutput::register_element),
53 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
55 #ifdef HAVE_LIBSIGROKDECODE
56 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
57 "sigrok_legacy_decoder",
58 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
59 sigc::ptr_fun(&LegacyDecoder::register_element),
60 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
63 srf_initialized_ = true;
68 srf_initialized_ = false;
71 Sink::Sink(GstBaseSink *gobj) :
76 Device::Device(GstElement *gobj) :
81 CaptureDevice::CaptureDevice(GstElement *gobj) :
86 #ifdef HAVE_LIBSIGROKCXX
87 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
89 klass->set_metadata("sigrok legacy capture device",
90 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
93 klass->add_pad_template(Gst::PadTemplate::create(
97 Gst::Caps::create_any()));
100 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
102 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
103 0, Gst::register_mm_type<LegacyCaptureDevice>(
104 "sigrok_legacy_capture_device"));
108 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
109 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
112 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
115 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
116 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
118 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
120 throw runtime_error("Failed to create element - plugin not registered?");
121 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
122 device->_libsigrok_device = libsigrok_device;
126 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
128 return _libsigrok_device;
131 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
135 case Gst::STATE_CHANGE_READY_TO_PAUSED:
136 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
137 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
138 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
139 _task->set_lock(_mutex);
140 _src_pad->set_active(true);
142 return Gst::STATE_CHANGE_SUCCESS;
144 return Gst::STATE_CHANGE_SUCCESS;
148 void LegacyCaptureDevice::_datafeed_callback(
149 shared_ptr<sigrok::Device> device,
150 shared_ptr<sigrok::Packet> packet)
153 switch (packet->type()->id()) {
156 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
157 auto mem = Gst::Memory::create(
158 Gst::MEMORY_FLAG_READONLY,
159 logic->data_pointer(),
160 logic->data_length(),
162 logic->data_length());
163 auto buf = Gst::Buffer::create();
164 buf->append_memory(move(mem));
165 _src_pad->push(move(buf));
170 _src_pad->push_event(Gst::EventEos::create());
177 void LegacyCaptureDevice::_run()
179 _session = _libsigrok_device->driver()->parent()->create_session();
180 _session->add_device(_libsigrok_device);
181 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
187 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
189 klass->set_metadata("sigrok legacy input",
190 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
193 klass->add_pad_template(Gst::PadTemplate::create(
197 Gst::Caps::create_any()));
199 klass->add_pad_template(Gst::PadTemplate::create(
203 Gst::Caps::create_any()));
206 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
208 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
209 0, Gst::register_mm_type<LegacyInput>(
210 "sigrok_legacy_input"));
214 LegacyInput::LegacyInput(GstElement *gobj) :
215 Glib::ObjectBase(typeid(LegacyInput)),
218 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
219 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
220 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
223 Glib::RefPtr<LegacyInput> LegacyInput::create(
224 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
225 map<string, Glib::VariantBase> options)
227 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
229 throw runtime_error("Failed to create element - plugin not registered?");
230 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
231 input->_libsigrok_input_format = libsigrok_input_format;
232 input->_options = options;
236 bool LegacyInput::start_vfunc()
238 _libsigrok_input = _libsigrok_input_format->create_input(_options);
239 auto context = _libsigrok_input_format->parent();
240 _session = context->create_session();
241 _session->add_device(_libsigrok_input->device());
242 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
247 void LegacyInput::_datafeed_callback(
248 shared_ptr<sigrok::Device> device,
249 shared_ptr<sigrok::Packet> packet)
252 switch (packet->type()->id()) {
255 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
256 auto mem = Gst::Memory::create(
257 Gst::MEMORY_FLAG_READONLY,
258 logic->data_pointer(),
259 logic->data_length(),
261 logic->data_length());
262 auto buf = Gst::Buffer::create();
263 buf->append_memory(move(mem));
264 _src_pad->push(move(buf));
269 _src_pad->push_event(Gst::EventEos::create());
276 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
277 const Glib::RefPtr<Gst::Buffer> &buf)
280 buf->map(info, Gst::MAP_READ);
281 _libsigrok_input->send(info.get_data(), info.get_size());
286 bool LegacyInput::stop_vfunc()
288 _libsigrok_input->end();
292 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
294 klass->set_metadata("sigrok legacy output",
295 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
298 klass->add_pad_template(Gst::PadTemplate::create(
302 Gst::Caps::create_any()));
305 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
307 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
308 0, Gst::register_mm_type<LegacyOutput>(
309 "sigrok_legacy_output"));
313 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
314 Glib::ObjectBase(typeid(LegacyOutput)),
319 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
320 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
321 shared_ptr<sigrok::Device> libsigrok_device,
322 map<string, Glib::VariantBase> options)
324 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
326 throw runtime_error("Failed to create element - plugin not registered?");
327 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
328 output->_libsigrok_output_format = libsigrok_output_format;
329 output->_libsigrok_device = libsigrok_device;
330 output->_options = options;
334 bool LegacyOutput::start_vfunc()
336 _libsigrok_output = _libsigrok_output_format->create_output(
337 _libsigrok_device, _options);
341 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
344 buffer->map(info, Gst::MAP_READ);
345 auto context = _libsigrok_output_format->parent();
346 auto packet = context->create_logic_packet(
347 info.get_data(), info.get_size(), 2);
348 auto result = _libsigrok_output->receive(packet);
354 bool LegacyOutput::stop_vfunc()
356 auto context = _libsigrok_output_format->parent();
357 auto end_packet = context->create_end_packet();
358 auto result = _libsigrok_output->receive(end_packet);
364 #ifdef HAVE_LIBSIGROKDECODE
365 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
367 klass->set_metadata("sigrok legacy decoder",
368 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
371 klass->add_pad_template(Gst::PadTemplate::create(
375 Gst::Caps::create_any()));
378 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
380 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
381 0, Gst::register_mm_type<LegacyDecoder>(
382 "sigrok_legacy_decoder"));
386 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
387 Glib::ObjectBase(typeid(LegacyDecoder)),
392 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
393 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
395 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
397 throw runtime_error("Failed to create element - plugin not registered?");
398 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
399 decoder->_session = libsigrokdecode_session;
400 decoder->_unitsize = unitsize;
404 struct srd_session *LegacyDecoder::libsigrokdecode_session()
409 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
412 buffer->map(info, Gst::MAP_READ);
413 uint64_t num_samples = info.get_size() / _unitsize;
414 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
415 info.get_data(), info.get_size(), _unitsize);
416 _abs_ss += num_samples;
421 bool LegacyDecoder::start_vfunc()
424 srd_session_start(_session);
428 bool LegacyDecoder::stop_vfunc()
430 srd_session_terminate_reset(_session);