]> sigrok.org Git - libsigrokflow.git/blob - src/main.cpp
Implement LegacyOutput.
[libsigrokflow.git] / src / main.cpp
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
23 #include <iostream>
24
25 namespace Srf
26 {
27
28 using namespace std;
29 using namespace std::placeholders;
30
31 void init()
32 {
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),
37                         "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
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");
43 }
44
45 Sink::Sink(GstBaseSink *gobj) :
46         Gst::BaseSink(gobj)
47 {
48 }
49
50 Device::Device(GstElement *gobj) :
51         Gst::Element(gobj)
52 {
53 }
54
55 CaptureDevice::CaptureDevice(GstElement *gobj) :
56         Device(gobj)
57 {
58 }
59
60 void 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
73 bool 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
81 LegacyCaptureDevice::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"));
86 }
87
88 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
89         shared_ptr<sigrok::HardwareDevice> libsigrok_device)
90 {
91         auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
92         if (!element)
93                 throw runtime_error("Failed to create element - plugin not registered?");
94         auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
95         device->_libsigrok_device = libsigrok_device;
96         return device;
97 }
98
99 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
100 {
101         return _libsigrok_device;
102 }
103
104 Gst::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:
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
121 void 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
150 void LegacyCaptureDevice::_run()
151 {
152         _session = _libsigrok_device->driver()->parent()->create_session();
153         _session->add_device(_libsigrok_device);
154         _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
155         _session->start();
156         _session->run();
157         _task->stop();
158 }
159
160 void 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
173 bool 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
181 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
182         Glib::ObjectBase(typeid(LegacyOutput)),
183         Sink(gobj)
184 {
185         add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
186 }
187
188 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
189         shared_ptr<sigrok::Output> libsigrok_output)
190 {
191         auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
192         if (!element)
193                 throw runtime_error("Failed to create element - plugin not registered?");
194         auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
195         output->_libsigrok_output = libsigrok_output;
196         return output;
197 }
198
199 shared_ptr<sigrok::Output> LegacyOutput::libsigrok_output()
200 {
201         return _libsigrok_output;
202 }
203
204 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
205 {
206         Gst::MapInfo info;
207         buffer->map(info, Gst::MAP_READ);
208         auto context = _libsigrok_output->format()->parent();
209         auto packet = context->create_logic_packet(
210                         info.get_data(), info.get_size(), 2);
211         auto result = _libsigrok_output->receive(packet);
212         cout << result;
213         buffer->unmap(info);
214         return Gst::FLOW_OK;
215 }
216
217 bool LegacyOutput::stop_vfunc()
218 {
219         auto context = _libsigrok_output->format()->parent();
220         auto end_packet = context->create_end_packet();
221         auto result = _libsigrok_output->receive(end_packet);
222         cout << result;
223         return true;
224 }
225
226 }