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 if (!Gst::is_initialized())
39 throw runtime_error("Gst::init() has not run yet");
41 #ifdef HAVE_LIBSIGROKCXX
42 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
43 "sigrok_legacy_capture_device",
44 "Wrapper for capture devices using legacy libsigrok APIs",
45 sigc::ptr_fun(&LegacyCaptureDevice::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_input",
49 "Wrapper for inputs using legacy libsigrok APIs",
50 sigc::ptr_fun(&LegacyInput::register_element),
51 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
52 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
53 "sigrok_legacy_output",
54 "Wrapper for outputs using legacy libsigrok APIs",
55 sigc::ptr_fun(&LegacyOutput::register_element),
56 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
58 #ifdef HAVE_LIBSIGROKDECODE
59 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
60 "sigrok_legacy_decoder",
61 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
62 sigc::ptr_fun(&LegacyDecoder::register_element),
63 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
66 srf_initialized_ = true;
71 if (!srf_initialized_)
72 throw runtime_error("libsigrokflow is not initialized");
74 srf_initialized_ = false;
77 Sink::Sink(GstBaseSink *gobj) :
82 Device::Device(GstElement *gobj) :
87 CaptureDevice::CaptureDevice(GstElement *gobj) :
92 #ifdef HAVE_LIBSIGROKCXX
93 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
95 klass->set_metadata("sigrok legacy capture device",
96 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
99 klass->add_pad_template(Gst::PadTemplate::create(
103 Gst::Caps::create_any()));
106 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
108 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
109 0, Gst::register_mm_type<LegacyCaptureDevice>(
110 "sigrok_legacy_capture_device"));
115 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
116 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
119 add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
122 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
123 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
125 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
127 throw runtime_error("Failed to create element - plugin not registered?");
128 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
129 device->libsigrok_device_ = libsigrok_device;
134 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
136 return libsigrok_device_;
139 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
141 switch (transition) {
142 case Gst::STATE_CHANGE_READY_TO_PAUSED:
143 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
144 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
145 task_ = Gst::Task::create(std::bind(&LegacyCaptureDevice::run_, this));
146 task_->set_lock(mutex_);
147 src_pad_->set_active(true);
149 return Gst::STATE_CHANGE_SUCCESS;
151 return Gst::STATE_CHANGE_SUCCESS;
155 void LegacyCaptureDevice::datafeed_callback_(
156 shared_ptr<sigrok::Device> device,
157 shared_ptr<sigrok::Packet> packet)
161 switch (packet->type()->id()) {
163 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
164 auto mem = Gst::Memory::create(
165 Gst::MEMORY_FLAG_READONLY,
166 logic->data_pointer(),
167 logic->data_length(),
169 logic->data_length());
170 auto buf = Gst::Buffer::create();
171 buf->append_memory(move(mem));
172 src_pad_->push(move(buf));
177 src_pad_->push_event(Gst::EventEos::create());
184 void LegacyCaptureDevice::run_()
186 session_ = libsigrok_device_->driver()->parent()->create_session();
187 session_->add_device(libsigrok_device_);
188 session_->add_datafeed_callback(bind(&LegacyCaptureDevice::datafeed_callback_, this, _1, _2));
194 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
196 klass->set_metadata("sigrok legacy input",
197 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
200 klass->add_pad_template(Gst::PadTemplate::create(
204 Gst::Caps::create_any()));
206 klass->add_pad_template(Gst::PadTemplate::create(
210 Gst::Caps::create_any()));
213 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
215 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
216 0, Gst::register_mm_type<LegacyInput>(
217 "sigrok_legacy_input"));
222 LegacyInput::LegacyInput(GstElement *gobj) :
223 Glib::ObjectBase(typeid(LegacyInput)),
226 add_pad(sink_pad_ = Gst::Pad::create(get_pad_template("sink"), "sink"));
227 add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
228 sink_pad_->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
231 Glib::RefPtr<LegacyInput> LegacyInput::create(
232 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
233 map<string, Glib::VariantBase> options)
235 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
237 throw runtime_error("Failed to create element - plugin not registered?");
238 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
239 input->libsigrok_input_format_ = libsigrok_input_format;
240 input->options_ = options;
245 bool LegacyInput::start_vfunc()
247 libsigrok_input_ = libsigrok_input_format_->create_input(options_);
248 auto context = libsigrok_input_format_->parent();
249 session_ = context->create_session();
250 session_->add_device(libsigrok_input_->device());
251 session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback_, this, _1, _2));
257 void LegacyInput::datafeed_callback_(
258 shared_ptr<sigrok::Device> device,
259 shared_ptr<sigrok::Packet> packet)
263 switch (packet->type()->id()) {
265 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
266 auto mem = Gst::Memory::create(
267 Gst::MEMORY_FLAG_READONLY,
268 logic->data_pointer(),
269 logic->data_length(),
271 logic->data_length());
272 auto buf = Gst::Buffer::create();
273 buf->append_memory(move(mem));
274 src_pad_->push(move(buf));
279 src_pad_->push_event(Gst::EventEos::create());
286 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
287 const Glib::RefPtr<Gst::Buffer> &buf)
290 buf->map(info, Gst::MAP_READ);
291 libsigrok_input_->send(info.get_data(), info.get_size());
297 bool LegacyInput::stop_vfunc()
299 libsigrok_input_->end();
304 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
306 klass->set_metadata("sigrok legacy output",
307 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
310 klass->add_pad_template(Gst::PadTemplate::create(
314 Gst::Caps::create_any()));
317 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
319 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
320 0, Gst::register_mm_type<LegacyOutput>(
321 "sigrok_legacy_output"));
326 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
327 Glib::ObjectBase(typeid(LegacyOutput)),
332 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
333 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
334 shared_ptr<sigrok::Device> libsigrok_device,
335 map<string, Glib::VariantBase> options)
337 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
339 throw runtime_error("Failed to create element - plugin not registered?");
340 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
341 output->libsigrok_output_format_ = libsigrok_output_format;
342 output->libsigrok_device_ = libsigrok_device;
343 output->options_ = options;
348 bool LegacyOutput::start_vfunc()
350 libsigrok_output_ = libsigrok_output_format_->create_output(
351 libsigrok_device_, options_);
356 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
359 buffer->map(info, Gst::MAP_READ);
360 auto context = libsigrok_output_format_->parent();
361 auto packet = context->create_logic_packet(
362 info.get_data(), info.get_size(), 2);
363 auto result = libsigrok_output_->receive(packet);
370 bool LegacyOutput::stop_vfunc()
372 auto context = libsigrok_output_format_->parent();
373 auto end_packet = context->create_end_packet();
374 auto result = libsigrok_output_->receive(end_packet);
381 #ifdef HAVE_LIBSIGROKDECODE
382 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
384 klass->set_metadata("sigrok legacy decoder",
385 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
388 klass->add_pad_template(Gst::PadTemplate::create(
392 Gst::Caps::create_any()));
395 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
397 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
398 0, Gst::register_mm_type<LegacyDecoder>(
399 "sigrok_legacy_decoder"));
404 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
405 Glib::ObjectBase(typeid(LegacyDecoder)),
410 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
411 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
413 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
415 throw runtime_error("Failed to create element - plugin not registered?");
416 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
417 decoder->session_ = libsigrokdecode_session;
418 decoder->unitsite_ = unitsize;
423 struct srd_session *LegacyDecoder::libsigrokdecode_session()
428 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
431 buffer->map(info, Gst::MAP_READ);
432 uint64_t num_samples = info.get_size() / unitsite_;
433 srd_session_send(session_, abs_ss_, abs_ss_ + num_samples,
434 info.get_data(), info.get_size(), unitsite_);
435 abs_ss_ += num_samples;
441 bool LegacyDecoder::start_vfunc()
444 srd_session_start(session_);
449 bool LegacyDecoder::stop_vfunc()
451 srd_session_terminate_reset(session_);