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