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 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
37 "sigrok_legacy_capture_device",
38 "Wrapper for capture devices using legacy libsigrok APIs",
39 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
40 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
41 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
42 "sigrok_legacy_output",
43 "Wrapper for outputs using legacy libsigrok APIs",
44 sigc::ptr_fun(&LegacyOutput::register_element),
45 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
46 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
47 "sigrok_legacy_decoder",
48 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
49 sigc::ptr_fun(&LegacyDecoder::register_element),
50 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
53 Sink::Sink(GstBaseSink *gobj) :
58 Device::Device(GstElement *gobj) :
63 CaptureDevice::CaptureDevice(GstElement *gobj) :
68 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
70 klass->set_metadata("sigrok legacy capture device",
71 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
74 klass->add_pad_template(Gst::PadTemplate::create(
78 Gst::Caps::create_any()));
81 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
83 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
84 0, Gst::register_mm_type<LegacyCaptureDevice>(
85 "sigrok_legacy_capture_device"));
89 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
90 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
93 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
96 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
97 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
99 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
101 throw runtime_error("Failed to create element - plugin not registered?");
102 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
103 device->_libsigrok_device = libsigrok_device;
107 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
109 return _libsigrok_device;
112 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
116 case Gst::STATE_CHANGE_READY_TO_PAUSED:
117 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
118 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
119 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
120 _task->set_lock(_mutex);
121 _src_pad->set_active(true);
123 return Gst::STATE_CHANGE_SUCCESS;
125 return Gst::STATE_CHANGE_SUCCESS;
129 void LegacyCaptureDevice::_datafeed_callback(
130 shared_ptr<sigrok::Device> device,
131 shared_ptr<sigrok::Packet> packet)
134 switch (packet->type()->id()) {
137 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
138 auto mem = Gst::Memory::create(
139 Gst::MEMORY_FLAG_READONLY,
140 logic->data_pointer(),
141 logic->data_length(),
143 logic->data_length());
144 auto buf = Gst::Buffer::create();
145 buf->append_memory(move(mem));
146 _src_pad->push(move(buf));
151 _src_pad->push_event(Gst::EventEos::create());
158 void LegacyCaptureDevice::_run()
160 _session = _libsigrok_device->driver()->parent()->create_session();
161 _session->add_device(_libsigrok_device);
162 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
168 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
170 klass->set_metadata("sigrok legacy output",
171 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
174 klass->add_pad_template(Gst::PadTemplate::create(
178 Gst::Caps::create_any()));
181 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
183 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
184 0, Gst::register_mm_type<LegacyOutput>(
185 "sigrok_legacy_output"));
189 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
190 Glib::ObjectBase(typeid(LegacyOutput)),
195 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
196 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
197 shared_ptr<sigrok::Device> libsigrok_device,
198 map<string, Glib::VariantBase> options)
200 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
202 throw runtime_error("Failed to create element - plugin not registered?");
203 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
204 output->_libsigrok_output_format = libsigrok_output_format;
205 output->_libsigrok_device = libsigrok_device;
206 output->_options = options;
210 bool LegacyOutput::start_vfunc()
212 _libsigrok_output = _libsigrok_output_format->create_output(
213 _libsigrok_device, _options);
217 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
220 buffer->map(info, Gst::MAP_READ);
221 auto context = _libsigrok_output_format->parent();
222 auto packet = context->create_logic_packet(
223 info.get_data(), info.get_size(), 2);
224 auto result = _libsigrok_output->receive(packet);
230 bool LegacyOutput::stop_vfunc()
232 auto context = _libsigrok_output_format->parent();
233 auto end_packet = context->create_end_packet();
234 auto result = _libsigrok_output->receive(end_packet);
239 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
241 klass->set_metadata("sigrok legacy decoder",
242 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
245 klass->add_pad_template(Gst::PadTemplate::create(
249 Gst::Caps::create_any()));
252 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
254 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
255 0, Gst::register_mm_type<LegacyDecoder>(
256 "sigrok_legacy_decoder"));
260 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
261 Glib::ObjectBase(typeid(LegacyDecoder)),
266 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
267 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
269 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
271 throw runtime_error("Failed to create element - plugin not registered?");
272 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
273 decoder->_session = libsigrokdecode_session;
274 decoder->_unitsize = unitsize;
278 struct srd_session *LegacyDecoder::libsigrokdecode_session()
283 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
286 buffer->map(info, Gst::MAP_READ);
287 uint64_t num_samples = info.get_size() / _unitsize;
288 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
289 info.get_data(), info.get_size(), _unitsize);
290 _abs_ss += num_samples;
295 bool LegacyDecoder::start_vfunc()
298 srd_session_start(_session);
302 bool LegacyDecoder::stop_vfunc()
304 srd_session_terminate_reset(_session);