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 Sink::Sink(GstBaseSink *gobj) :
36 Device::Device(GstElement *gobj) :
41 CaptureDevice::CaptureDevice(GstElement *gobj) :
46 #ifdef HAVE_LIBSIGROKCXX
47 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
49 klass->set_metadata("sigrok legacy capture device",
50 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
53 klass->add_pad_template(Gst::PadTemplate::create(
57 Gst::Caps::create_any()));
60 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
62 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
63 0, Gst::register_mm_type<LegacyCaptureDevice>(
64 "sigrok_legacy_capture_device"));
69 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
70 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
73 add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
76 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
77 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
79 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
81 throw runtime_error("Failed to create element - plugin not registered?");
82 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
83 device->libsigrok_device_ = libsigrok_device;
88 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
90 return libsigrok_device_;
93 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
96 case Gst::STATE_CHANGE_READY_TO_PAUSED:
97 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
98 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
99 task_ = Gst::Task::create(std::bind(&LegacyCaptureDevice::run_, this));
100 task_->set_lock(mutex_);
101 src_pad_->set_active(true);
103 return Gst::STATE_CHANGE_SUCCESS;
105 return Gst::STATE_CHANGE_SUCCESS;
109 void LegacyCaptureDevice::datafeed_callback_(
110 shared_ptr<sigrok::Device> device,
111 shared_ptr<sigrok::Packet> packet)
115 switch (packet->type()->id()) {
117 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
118 auto mem = Gst::Memory::create(
119 Gst::MEMORY_FLAG_READONLY,
120 logic->data_pointer(),
121 logic->data_length(),
123 logic->data_length());
124 auto buf = Gst::Buffer::create();
125 buf->append_memory(move(mem));
126 src_pad_->push(move(buf));
131 src_pad_->push_event(Gst::EventEos::create());
138 void LegacyCaptureDevice::run_()
140 session_ = libsigrok_device_->driver()->parent()->create_session();
141 session_->add_device(libsigrok_device_);
142 session_->add_datafeed_callback(bind(&LegacyCaptureDevice::datafeed_callback_, this, _1, _2));
148 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
150 klass->set_metadata("sigrok legacy input",
151 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
154 klass->add_pad_template(Gst::PadTemplate::create(
158 Gst::Caps::create_any()));
160 klass->add_pad_template(Gst::PadTemplate::create(
164 Gst::Caps::create_any()));
167 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
169 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
170 0, Gst::register_mm_type<LegacyInput>(
171 "sigrok_legacy_input"));
176 LegacyInput::LegacyInput(GstElement *gobj) :
177 Glib::ObjectBase(typeid(LegacyInput)),
180 add_pad(sink_pad_ = Gst::Pad::create(get_pad_template("sink"), "sink"));
181 add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
182 sink_pad_->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
185 Glib::RefPtr<LegacyInput> LegacyInput::create(
186 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
187 map<string, Glib::VariantBase> options)
189 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
191 throw runtime_error("Failed to create element - plugin not registered?");
192 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
193 input->libsigrok_input_format_ = libsigrok_input_format;
194 input->options_ = options;
199 bool LegacyInput::start_vfunc()
201 libsigrok_input_ = libsigrok_input_format_->create_input(options_);
202 auto context = libsigrok_input_format_->parent();
203 session_ = context->create_session();
204 session_->add_device(libsigrok_input_->device());
205 session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback_, this, _1, _2));
211 void LegacyInput::datafeed_callback_(
212 shared_ptr<sigrok::Device> device,
213 shared_ptr<sigrok::Packet> packet)
217 switch (packet->type()->id()) {
219 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
220 auto mem = Gst::Memory::create(
221 Gst::MEMORY_FLAG_READONLY,
222 logic->data_pointer(),
223 logic->data_length(),
225 logic->data_length());
226 auto buf = Gst::Buffer::create();
227 buf->append_memory(move(mem));
228 src_pad_->push(move(buf));
233 src_pad_->push_event(Gst::EventEos::create());
240 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
241 const Glib::RefPtr<Gst::Buffer> &buf)
244 buf->map(info, Gst::MAP_READ);
245 libsigrok_input_->send(info.get_data(), info.get_size());
251 bool LegacyInput::stop_vfunc()
253 libsigrok_input_->end();
258 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
260 klass->set_metadata("sigrok legacy output",
261 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
264 klass->add_pad_template(Gst::PadTemplate::create(
268 Gst::Caps::create_any()));
271 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
273 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
274 0, Gst::register_mm_type<LegacyOutput>(
275 "sigrok_legacy_output"));
280 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
281 Glib::ObjectBase(typeid(LegacyOutput)),
286 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
287 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
288 shared_ptr<sigrok::Device> libsigrok_device,
289 map<string, Glib::VariantBase> options)
291 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
293 throw runtime_error("Failed to create element - plugin not registered?");
294 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
295 output->libsigrok_output_format_ = libsigrok_output_format;
296 output->libsigrok_device_ = libsigrok_device;
297 output->options_ = options;
302 bool LegacyOutput::start_vfunc()
304 libsigrok_output_ = libsigrok_output_format_->create_output(
305 libsigrok_device_, options_);
310 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
313 buffer->map(info, Gst::MAP_READ);
314 auto context = libsigrok_output_format_->parent();
315 auto packet = context->create_logic_packet(
316 info.get_data(), info.get_size(), 2);
317 auto result = libsigrok_output_->receive(packet);
324 bool LegacyOutput::stop_vfunc()
326 auto context = libsigrok_output_format_->parent();
327 auto end_packet = context->create_end_packet();
328 auto result = libsigrok_output_->receive(end_packet);
335 #ifdef HAVE_LIBSIGROKDECODE
336 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
338 klass->set_metadata("sigrok legacy decoder",
339 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
342 klass->add_pad_template(Gst::PadTemplate::create(
346 Gst::Caps::create_any()));
349 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
351 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
352 0, Gst::register_mm_type<LegacyDecoder>(
353 "sigrok_legacy_decoder"));
358 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
359 Glib::ObjectBase(typeid(LegacyDecoder)),
364 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
365 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
367 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
369 throw runtime_error("Failed to create element - plugin not registered?");
370 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
371 decoder->session_ = libsigrokdecode_session;
372 decoder->unitsite_ = unitsize;
377 struct srd_session *LegacyDecoder::libsigrokdecode_session()
382 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
385 buffer->map(info, Gst::MAP_READ);
386 uint64_t num_samples = info.get_size() / unitsite_;
387 srd_session_send(session_, abs_ss_, abs_ss_ + num_samples,
388 info.get_data(), info.get_size(), unitsite_);
389 abs_ss_ += num_samples;
395 bool LegacyDecoder::start_vfunc()
398 srd_session_start(session_);
403 bool LegacyDecoder::stop_vfunc()
405 srd_session_terminate_reset(session_);