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