]> sigrok.org Git - libsigrokflow.git/blame - src/main.cpp
Create LegacyOutput from format, device and options.
[libsigrokflow.git] / src / main.cpp
CommitLineData
572e76fe
UH
1/*
2 * This file is part of the libsigrokflow project.
3 *
4 * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include <libsigrokflow/libsigrokflow.hpp>
22
f7363af1
ML
23#include <iostream>
24
b903bb0a
UH
25namespace Srf
26{
27
f7363af1 28using namespace std;
d03b3a98 29using namespace std::placeholders;
f7363af1 30
b903bb0a
UH
31void init()
32{
6d71d36a
ML
33 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
34 "sigrok_legacy_capture_device",
35 "Wrapper for capture devices using legacy libsigrok APIs",
36 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
97513ff0 37 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
e2cfc0ef
ML
38 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
39 "sigrok_legacy_output",
40 "Wrapper for outputs using legacy libsigrok APIs",
41 sigc::ptr_fun(&LegacyOutput::register_element),
42 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
6d71d36a
ML
43}
44
e2cfc0ef
ML
45Sink::Sink(GstBaseSink *gobj) :
46 Gst::BaseSink(gobj)
4b7d782a
ML
47{
48}
49
50Device::Device(GstElement *gobj) :
e2cfc0ef 51 Gst::Element(gobj)
4b7d782a
ML
52{
53}
54
55CaptureDevice::CaptureDevice(GstElement *gobj) :
56 Device(gobj)
57{
58}
59
6d71d36a
ML
60void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
61{
62 klass->set_metadata("sigrok legacy capture device",
63 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
64 "Martin Ling");
65
66 klass->add_pad_template(Gst::PadTemplate::create(
67 "src",
68 Gst::PAD_SRC,
69 Gst::PAD_ALWAYS,
70 Gst::Caps::create_any()));
71}
72
73bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
74{
75 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
76 0, Gst::register_mm_type<LegacyCaptureDevice>(
77 "sigrok_legacy_capture_device"));
78 return true;
79}
80
81LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
82 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
83 CaptureDevice(gobj)
84{
85 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
b903bb0a
UH
86}
87
64b0db03
ML
88Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
89 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
f7363af1 90{
64b0db03 91 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
d75c9a6a
ML
92 if (!element)
93 throw runtime_error("Failed to create element - plugin not registered?");
64b0db03 94 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
64b0db03
ML
95 device->_libsigrok_device = libsigrok_device;
96 return device;
f7363af1
ML
97}
98
d03b3a98 99shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
f7363af1 100{
64b0db03 101 return _libsigrok_device;
f7363af1
ML
102}
103
d03b3a98
ML
104Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
105{
106 switch (transition)
107 {
108 case Gst::STATE_CHANGE_READY_TO_PAUSED:
109 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
110 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
d03b3a98
ML
111 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
112 _task->set_lock(_mutex);
113 _src_pad->set_active(true);
114 _task->start();
115 return Gst::STATE_CHANGE_SUCCESS;
116 default:
117 return Gst::STATE_CHANGE_SUCCESS;
118 }
119}
120
121void LegacyCaptureDevice::_datafeed_callback(
122 shared_ptr<sigrok::Device> device,
123 shared_ptr<sigrok::Packet> packet)
124{
125 (void) device;
126 switch (packet->type()->id()) {
127 case SR_DF_LOGIC:
128 {
129 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
130 auto mem = Gst::Memory::create(
131 Gst::MEMORY_FLAG_READONLY,
132 logic->data_pointer(),
133 logic->data_length(),
134 0,
135 logic->data_length());
136 auto buf = Gst::Buffer::create();
137 buf->append_memory(move(mem));
138 _src_pad->push(move(buf));
139 break;
140 }
141 case SR_DF_END:
142 _session->stop();
143 _src_pad->push_event(Gst::EventEos::create());
144 break;
145 default:
146 break;
147 }
148}
149
150void LegacyCaptureDevice::_run()
151{
64b0db03
ML
152 _session = _libsigrok_device->driver()->parent()->create_session();
153 _session->add_device(_libsigrok_device);
d03b3a98
ML
154 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
155 _session->start();
156 _session->run();
157 _task->stop();
158}
159
e2cfc0ef
ML
160void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
161{
162 klass->set_metadata("sigrok legacy output",
163 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
164 "Martin Ling");
165
166 klass->add_pad_template(Gst::PadTemplate::create(
167 "sink",
168 Gst::PAD_SINK,
169 Gst::PAD_ALWAYS,
170 Gst::Caps::create_any()));
171}
172
173bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
174{
175 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
176 0, Gst::register_mm_type<LegacyOutput>(
177 "sigrok_legacy_output"));
178 return true;
179}
180
181LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
182 Glib::ObjectBase(typeid(LegacyOutput)),
183 Sink(gobj)
184{
e2cfc0ef
ML
185}
186
187Glib::RefPtr<LegacyOutput>LegacyOutput::create(
726122c2
ML
188 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
189 shared_ptr<sigrok::Device> libsigrok_device,
190 map<string, Glib::VariantBase> options)
e2cfc0ef
ML
191{
192 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
193 if (!element)
194 throw runtime_error("Failed to create element - plugin not registered?");
195 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
726122c2
ML
196 output->_libsigrok_output_format = libsigrok_output_format;
197 output->_libsigrok_device = libsigrok_device;
198 output->_options = options;
e2cfc0ef
ML
199 return output;
200}
201
726122c2 202bool LegacyOutput::start_vfunc()
e2cfc0ef 203{
726122c2
ML
204 _libsigrok_output = _libsigrok_output_format->create_output(
205 _libsigrok_device, _options);
206 return true;
e2cfc0ef
ML
207}
208
209Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
210{
211 Gst::MapInfo info;
212 buffer->map(info, Gst::MAP_READ);
726122c2 213 auto context = _libsigrok_output_format->parent();
e2cfc0ef
ML
214 auto packet = context->create_logic_packet(
215 info.get_data(), info.get_size(), 2);
216 auto result = _libsigrok_output->receive(packet);
217 cout << result;
218 buffer->unmap(info);
219 return Gst::FLOW_OK;
220}
221
222bool LegacyOutput::stop_vfunc()
223{
726122c2 224 auto context = _libsigrok_output_format->parent();
e2cfc0ef
ML
225 auto end_packet = context->create_end_packet();
226 auto result = _libsigrok_output->receive(end_packet);
227 cout << result;
228 return true;
229}
230
b903bb0a 231}