]> sigrok.org Git - libsigrokflow.git/blob - src/legacy_input.cpp
Avoid underscore suffix in member function names.
[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/libsigrokflow.hpp>
23
24 namespace Srf
25 {
26
27 using namespace std;
28 using namespace std::placeholders;
29
30 #ifdef HAVE_LIBSIGROKCXX
31 void 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
50 bool 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
59 LegacyInput::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));
66 }
67
68 Glib::RefPtr<LegacyInput> LegacyInput::create(
69         shared_ptr<sigrok::InputFormat> libsigrok_input_format,
70         map<string, Glib::VariantBase> options)
71 {
72         auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
73         if (!element)
74                 throw runtime_error("Failed to create element - plugin not registered?");
75         auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
76         input->libsigrok_input_format_ = libsigrok_input_format;
77         input->options_ = options;
78
79         return input;
80 }
81
82 bool LegacyInput::start_vfunc()
83 {
84         libsigrok_input_ = libsigrok_input_format_->create_input(options_);
85         auto context = libsigrok_input_format_->parent();
86         session_ = context->create_session();
87         session_->add_device(libsigrok_input_->device());
88         session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback, this, _1, _2));
89         session_->start();
90
91         return true;
92 }
93
94 void LegacyInput::datafeed_callback(
95         shared_ptr<sigrok::Device> device,
96         shared_ptr<sigrok::Packet> packet)
97 {
98         (void)device;
99
100         switch (packet->type()->id()) {
101         case SR_DF_LOGIC: {
102                 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
103                 auto mem = Gst::Memory::create(
104                                 Gst::MEMORY_FLAG_READONLY,
105                                 logic->data_pointer(),
106                                 logic->data_length(),
107                                 0,
108                                 logic->data_length());
109                 auto buf = Gst::Buffer::create();
110                 buf->append_memory(move(mem));
111                 src_pad_->push(move(buf));
112                 break;
113         }
114         case SR_DF_END:
115                 session_->stop();
116                 src_pad_->push_event(Gst::EventEos::create());
117                 break;
118         default:
119                 break;
120         }
121 }
122
123 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
124         const Glib::RefPtr<Gst::Buffer> &buf)
125 {
126         Gst::MapInfo info;
127         buf->map(info, Gst::MAP_READ);
128         libsigrok_input_->send(info.get_data(), info.get_size());
129         buf->unmap(info);
130
131         return Gst::FLOW_OK;
132 }
133
134 bool LegacyInput::stop_vfunc()
135 {
136         libsigrok_input_->end();
137
138         return true;
139 }
140 #endif
141
142 }