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 LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
49 klass->set_metadata("sigrok legacy input",
50 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
53 klass->add_pad_template(Gst::PadTemplate::create(
57 Gst::Caps::create_any()));
59 klass->add_pad_template(Gst::PadTemplate::create(
63 Gst::Caps::create_any()));
66 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
68 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
69 0, Gst::register_mm_type<LegacyInput>(
70 "sigrok_legacy_input"));
75 LegacyInput::LegacyInput(GstElement *gobj) :
76 Glib::ObjectBase(typeid(LegacyInput)),
79 add_pad(sink_pad_ = Gst::Pad::create(get_pad_template("sink"), "sink"));
80 add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
81 sink_pad_->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
84 Glib::RefPtr<LegacyInput> LegacyInput::create(
85 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
86 map<string, Glib::VariantBase> options)
88 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
90 throw runtime_error("Failed to create element - plugin not registered?");
91 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
92 input->libsigrok_input_format_ = libsigrok_input_format;
93 input->options_ = options;
98 bool LegacyInput::start_vfunc()
100 libsigrok_input_ = libsigrok_input_format_->create_input(options_);
101 auto context = libsigrok_input_format_->parent();
102 session_ = context->create_session();
103 session_->add_device(libsigrok_input_->device());
104 session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback_, this, _1, _2));
110 void LegacyInput::datafeed_callback_(
111 shared_ptr<sigrok::Device> device,
112 shared_ptr<sigrok::Packet> packet)
116 switch (packet->type()->id()) {
118 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
119 auto mem = Gst::Memory::create(
120 Gst::MEMORY_FLAG_READONLY,
121 logic->data_pointer(),
122 logic->data_length(),
124 logic->data_length());
125 auto buf = Gst::Buffer::create();
126 buf->append_memory(move(mem));
127 src_pad_->push(move(buf));
132 src_pad_->push_event(Gst::EventEos::create());
139 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
140 const Glib::RefPtr<Gst::Buffer> &buf)
143 buf->map(info, Gst::MAP_READ);
144 libsigrok_input_->send(info.get_data(), info.get_size());
150 bool LegacyInput::stop_vfunc()
152 libsigrok_input_->end();
157 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
159 klass->set_metadata("sigrok legacy output",
160 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
163 klass->add_pad_template(Gst::PadTemplate::create(
167 Gst::Caps::create_any()));
170 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
172 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
173 0, Gst::register_mm_type<LegacyOutput>(
174 "sigrok_legacy_output"));
179 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
180 Glib::ObjectBase(typeid(LegacyOutput)),
185 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
186 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
187 shared_ptr<sigrok::Device> libsigrok_device,
188 map<string, Glib::VariantBase> options)
190 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
192 throw runtime_error("Failed to create element - plugin not registered?");
193 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
194 output->libsigrok_output_format_ = libsigrok_output_format;
195 output->libsigrok_device_ = libsigrok_device;
196 output->options_ = options;
201 bool LegacyOutput::start_vfunc()
203 libsigrok_output_ = libsigrok_output_format_->create_output(
204 libsigrok_device_, options_);
209 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
212 buffer->map(info, Gst::MAP_READ);
213 auto context = libsigrok_output_format_->parent();
214 auto packet = context->create_logic_packet(
215 info.get_data(), info.get_size(), 2);
216 auto result = libsigrok_output_->receive(packet);
223 bool LegacyOutput::stop_vfunc()
225 auto context = libsigrok_output_format_->parent();
226 auto end_packet = context->create_end_packet();
227 auto result = libsigrok_output_->receive(end_packet);
234 #ifdef HAVE_LIBSIGROKDECODE
235 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
237 klass->set_metadata("sigrok legacy decoder",
238 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
241 klass->add_pad_template(Gst::PadTemplate::create(
245 Gst::Caps::create_any()));
248 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
250 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
251 0, Gst::register_mm_type<LegacyDecoder>(
252 "sigrok_legacy_decoder"));
257 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
258 Glib::ObjectBase(typeid(LegacyDecoder)),
263 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
264 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
266 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
268 throw runtime_error("Failed to create element - plugin not registered?");
269 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
270 decoder->session_ = libsigrokdecode_session;
271 decoder->unitsite_ = unitsize;
276 struct srd_session *LegacyDecoder::libsigrokdecode_session()
281 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
284 buffer->map(info, Gst::MAP_READ);
285 uint64_t num_samples = info.get_size() / unitsite_;
286 srd_session_send(session_, abs_ss_, abs_ss_ + num_samples,
287 info.get_data(), info.get_size(), unitsite_);
288 abs_ss_ += num_samples;
294 bool LegacyDecoder::start_vfunc()
297 srd_session_start(session_);
302 bool LegacyDecoder::stop_vfunc()
304 srd_session_terminate_reset(session_);