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 if (!srf_initialized_)
69 throw runtime_error("libsigrokflow is not initialized");
71 srf_initialized_ = false;
74 Sink::Sink(GstBaseSink *gobj) :
79 Device::Device(GstElement *gobj) :
84 CaptureDevice::CaptureDevice(GstElement *gobj) :
89 #ifdef HAVE_LIBSIGROKCXX
90 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
92 klass->set_metadata("sigrok legacy capture device",
93 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
96 klass->add_pad_template(Gst::PadTemplate::create(
100 Gst::Caps::create_any()));
103 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
105 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
106 0, Gst::register_mm_type<LegacyCaptureDevice>(
107 "sigrok_legacy_capture_device"));
111 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
112 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
115 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
118 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
119 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
121 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
123 throw runtime_error("Failed to create element - plugin not registered?");
124 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
125 device->_libsigrok_device = libsigrok_device;
129 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
131 return _libsigrok_device;
134 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
138 case Gst::STATE_CHANGE_READY_TO_PAUSED:
139 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
140 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
141 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
142 _task->set_lock(_mutex);
143 _src_pad->set_active(true);
145 return Gst::STATE_CHANGE_SUCCESS;
147 return Gst::STATE_CHANGE_SUCCESS;
151 void LegacyCaptureDevice::_datafeed_callback(
152 shared_ptr<sigrok::Device> device,
153 shared_ptr<sigrok::Packet> packet)
156 switch (packet->type()->id()) {
159 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
160 auto mem = Gst::Memory::create(
161 Gst::MEMORY_FLAG_READONLY,
162 logic->data_pointer(),
163 logic->data_length(),
165 logic->data_length());
166 auto buf = Gst::Buffer::create();
167 buf->append_memory(move(mem));
168 _src_pad->push(move(buf));
173 _src_pad->push_event(Gst::EventEos::create());
180 void LegacyCaptureDevice::_run()
182 _session = _libsigrok_device->driver()->parent()->create_session();
183 _session->add_device(_libsigrok_device);
184 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
190 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
192 klass->set_metadata("sigrok legacy input",
193 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
196 klass->add_pad_template(Gst::PadTemplate::create(
200 Gst::Caps::create_any()));
202 klass->add_pad_template(Gst::PadTemplate::create(
206 Gst::Caps::create_any()));
209 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
211 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
212 0, Gst::register_mm_type<LegacyInput>(
213 "sigrok_legacy_input"));
217 LegacyInput::LegacyInput(GstElement *gobj) :
218 Glib::ObjectBase(typeid(LegacyInput)),
221 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
222 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
223 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
226 Glib::RefPtr<LegacyInput> LegacyInput::create(
227 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
228 map<string, Glib::VariantBase> options)
230 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
232 throw runtime_error("Failed to create element - plugin not registered?");
233 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
234 input->_libsigrok_input_format = libsigrok_input_format;
235 input->_options = options;
239 bool LegacyInput::start_vfunc()
241 _libsigrok_input = _libsigrok_input_format->create_input(_options);
242 auto context = _libsigrok_input_format->parent();
243 _session = context->create_session();
244 _session->add_device(_libsigrok_input->device());
245 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
250 void LegacyInput::_datafeed_callback(
251 shared_ptr<sigrok::Device> device,
252 shared_ptr<sigrok::Packet> packet)
255 switch (packet->type()->id()) {
258 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
259 auto mem = Gst::Memory::create(
260 Gst::MEMORY_FLAG_READONLY,
261 logic->data_pointer(),
262 logic->data_length(),
264 logic->data_length());
265 auto buf = Gst::Buffer::create();
266 buf->append_memory(move(mem));
267 _src_pad->push(move(buf));
272 _src_pad->push_event(Gst::EventEos::create());
279 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
280 const Glib::RefPtr<Gst::Buffer> &buf)
283 buf->map(info, Gst::MAP_READ);
284 _libsigrok_input->send(info.get_data(), info.get_size());
289 bool LegacyInput::stop_vfunc()
291 _libsigrok_input->end();
295 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
297 klass->set_metadata("sigrok legacy output",
298 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
301 klass->add_pad_template(Gst::PadTemplate::create(
305 Gst::Caps::create_any()));
308 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
310 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
311 0, Gst::register_mm_type<LegacyOutput>(
312 "sigrok_legacy_output"));
316 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
317 Glib::ObjectBase(typeid(LegacyOutput)),
322 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
323 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
324 shared_ptr<sigrok::Device> libsigrok_device,
325 map<string, Glib::VariantBase> options)
327 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
329 throw runtime_error("Failed to create element - plugin not registered?");
330 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
331 output->_libsigrok_output_format = libsigrok_output_format;
332 output->_libsigrok_device = libsigrok_device;
333 output->_options = options;
337 bool LegacyOutput::start_vfunc()
339 _libsigrok_output = _libsigrok_output_format->create_output(
340 _libsigrok_device, _options);
344 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
347 buffer->map(info, Gst::MAP_READ);
348 auto context = _libsigrok_output_format->parent();
349 auto packet = context->create_logic_packet(
350 info.get_data(), info.get_size(), 2);
351 auto result = _libsigrok_output->receive(packet);
357 bool LegacyOutput::stop_vfunc()
359 auto context = _libsigrok_output_format->parent();
360 auto end_packet = context->create_end_packet();
361 auto result = _libsigrok_output->receive(end_packet);
367 #ifdef HAVE_LIBSIGROKDECODE
368 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
370 klass->set_metadata("sigrok legacy decoder",
371 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
374 klass->add_pad_template(Gst::PadTemplate::create(
378 Gst::Caps::create_any()));
381 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
383 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
384 0, Gst::register_mm_type<LegacyDecoder>(
385 "sigrok_legacy_decoder"));
389 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
390 Glib::ObjectBase(typeid(LegacyDecoder)),
395 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
396 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
398 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
400 throw runtime_error("Failed to create element - plugin not registered?");
401 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
402 decoder->_session = libsigrokdecode_session;
403 decoder->_unitsize = unitsize;
407 struct srd_session *LegacyDecoder::libsigrokdecode_session()
412 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
415 buffer->map(info, Gst::MAP_READ);
416 uint64_t num_samples = info.get_size() / _unitsize;
417 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
418 info.get_data(), info.get_size(), _unitsize);
419 _abs_ss += num_samples;
424 bool LegacyDecoder::start_vfunc()
427 srd_session_start(_session);
431 bool LegacyDecoder::stop_vfunc()
433 srd_session_terminate_reset(_session);