]> sigrok.org Git - libsigrokflow.git/blame - src/legacy_input.cpp
Factor out legacy_capture_device.hpp.
[libsigrokflow.git] / src / legacy_input.cpp
CommitLineData
aa03d5d8
UH
1/*
2 * This file is part of the libsigrokflow project.
3 *
4 * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
5 * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <config.h>
22#include <libsigrokflow/libsigrokflow.hpp>
23
24namespace Srf
25{
26
27using namespace std;
28using namespace std::placeholders;
29
30#ifdef HAVE_LIBSIGROKCXX
31void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
32{
33 klass->set_metadata("sigrok legacy input",
34 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
35 "Martin Ling");
36
37 klass->add_pad_template(Gst::PadTemplate::create(
38 "sink",
39 Gst::PAD_SINK,
40 Gst::PAD_ALWAYS,
41 Gst::Caps::create_any()));
42
43 klass->add_pad_template(Gst::PadTemplate::create(
44 "src",
45 Gst::PAD_SRC,
46 Gst::PAD_ALWAYS,
47 Gst::Caps::create_any()));
48}
49
50bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
51{
52 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
53 0, Gst::register_mm_type<LegacyInput>(
54 "sigrok_legacy_input"));
55
56 return true;
57}
58
59LegacyInput::LegacyInput(GstElement *gobj) :
60 Glib::ObjectBase(typeid(LegacyInput)),
61 Gst::Element(gobj)
62{
63 add_pad(sink_pad_ = Gst::Pad::create(get_pad_template("sink"), "sink"));
64 add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
65 sink_pad_->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
fce9342e 66 sink_pad_->set_event_function(sigc::mem_fun(*this, &LegacyInput::event));
aa03d5d8
UH
67}
68
69Glib::RefPtr<LegacyInput> LegacyInput::create(
70 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
71 map<string, Glib::VariantBase> options)
72{
73 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
74 if (!element)
75 throw runtime_error("Failed to create element - plugin not registered?");
76 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
77 input->libsigrok_input_format_ = libsigrok_input_format;
fce9342e 78 input->libsigrok_input_ = libsigrok_input_format->create_input(options);
aa03d5d8
UH
79 return input;
80}
81
897cb509 82void LegacyInput::datafeed_callback(
aa03d5d8
UH
83 shared_ptr<sigrok::Device> device,
84 shared_ptr<sigrok::Packet> packet)
85{
86 (void)device;
87
88 switch (packet->type()->id()) {
89 case SR_DF_LOGIC: {
90 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
91 auto mem = Gst::Memory::create(
92 Gst::MEMORY_FLAG_READONLY,
93 logic->data_pointer(),
94 logic->data_length(),
95 0,
96 logic->data_length());
97 auto buf = Gst::Buffer::create();
98 buf->append_memory(move(mem));
99 src_pad_->push(move(buf));
100 break;
101 }
102 case SR_DF_END:
103 session_->stop();
104 src_pad_->push_event(Gst::EventEos::create());
105 break;
106 default:
107 break;
108 }
109}
110
111Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
112 const Glib::RefPtr<Gst::Buffer> &buf)
113{
114 Gst::MapInfo info;
115 buf->map(info, Gst::MAP_READ);
116 libsigrok_input_->send(info.get_data(), info.get_size());
fce9342e
ML
117 auto device = libsigrok_input_->device();
118 if (!session_ && device) {
119 auto context = libsigrok_input_format_->parent();
120 session_ = context->create_session();
121 session_->add_device(device);
122 session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback, this, _1, _2));
123 }
aa03d5d8
UH
124 buf->unmap(info);
125
126 return Gst::FLOW_OK;
127}
128
fce9342e 129bool LegacyInput::event(const Glib::RefPtr<Gst::Pad>&pad, Glib::RefPtr<Gst::Event>&event)
aa03d5d8 130{
fce9342e
ML
131 (void)pad;
132
133 if (event->get_event_type() == Gst::EVENT_EOS) {
134 libsigrok_input_->end();
135 session_->stop();
136 }
aa03d5d8
UH
137
138 return true;
139}
140#endif
141
142}