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