]> sigrok.org Git - libsigrokflow.git/blob - src/legacy_capture_device.cpp
Add tests/legacy_decoder.cpp and a simple unit test.
[libsigrokflow.git] / src / legacy_capture_device.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/legacy_capture_device.hpp>
23 #include <functional>
24 #include <memory>
25 #include <stdexcept>
26 #include <utility>
27 #include <gstreamermm/private/element_p.h>
28
29 namespace Srf
30 {
31
32 using std::move;
33 using std::placeholders::_1;
34 using std::placeholders::_2;
35 using std::runtime_error;
36 using std::shared_ptr;
37 using std::static_pointer_cast;
38
39 #ifdef HAVE_LIBSIGROKCXX
40 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
41 {
42         klass->set_metadata("sigrok legacy capture device",
43                         "Source", "Wrapper for capture devices using legacy libsigrok APIs",
44                         "Martin Ling");
45
46         klass->add_pad_template(Gst::PadTemplate::create(
47                         "src",
48                         Gst::PAD_SRC,
49                         Gst::PAD_ALWAYS,
50                         Gst::Caps::create_any()));
51 }
52
53 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
54 {
55         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
56                         0, Gst::register_mm_type<LegacyCaptureDevice>(
57                                 "sigrok_legacy_capture_device"));
58
59         return true;
60 }
61
62 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
63         Glib::ObjectBase(typeid(LegacyCaptureDevice)),
64         CaptureDevice(gobj)
65 {
66         add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
67 }
68
69 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
70         shared_ptr<sigrok::HardwareDevice> libsigrok_device)
71 {
72         auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
73         if (!element)
74                 throw runtime_error("Failed to create element - plugin not registered?");
75         auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
76         device->libsigrok_device_ = libsigrok_device;
77
78         return device;
79 }
80
81 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
82 {
83         return libsigrok_device_;
84 }
85
86 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
87 {
88         switch (transition) {
89         case Gst::STATE_CHANGE_READY_TO_PAUSED:
90                 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
91         case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
92                 task_ = Gst::Task::create(std::bind(&LegacyCaptureDevice::run, this));
93                 task_->set_lock(mutex_);
94                 src_pad_->set_active(true);
95                 task_->start();
96                 return Gst::STATE_CHANGE_SUCCESS;
97         default:
98                 return Gst::STATE_CHANGE_SUCCESS;
99         }
100 }
101
102 void LegacyCaptureDevice::datafeed_callback(
103         shared_ptr<sigrok::Device> device,
104         shared_ptr<sigrok::Packet> packet)
105 {
106         (void)device;
107
108         switch (packet->type()->id()) {
109         case SR_DF_LOGIC: {
110                 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
111                 auto mem = Gst::Memory::create(
112                                 Gst::MEMORY_FLAG_READONLY,
113                                 logic->data_pointer(),
114                                 logic->data_length(),
115                                 0,
116                                 logic->data_length());
117                 auto buf = Gst::Buffer::create();
118                 buf->append_memory(move(mem));
119                 src_pad_->push(move(buf));
120                 break;
121         }
122         case SR_DF_END:
123                 session_->stop();
124                 src_pad_->push_event(Gst::EventEos::create());
125                 break;
126         default:
127                 break;
128         }
129 }
130
131 void LegacyCaptureDevice::run()
132 {
133         session_ = libsigrok_device_->driver()->parent()->create_session();
134         session_->add_device(libsigrok_device_);
135         session_->add_datafeed_callback(bind(&LegacyCaptureDevice::datafeed_callback, this, _1, _2));
136         session_->start();
137         session_->run();
138         task_->stop();
139 }
140 #endif
141
142 }