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