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