2 * This file is part of the libsigrokflow project.
4 * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
21 #include <libsigrokflow/libsigrokflow.hpp>
29 using namespace std::placeholders;
35 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
36 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
38 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
40 throw runtime_error("Failed to create element - plugin not registered?");
41 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
43 auto src_template = Gst::PadTemplate::create("src",
46 Gst::Caps::create_any());
47 device->_src_pad = Gst::Pad::create(src_template);
48 device->add_pad(device->_src_pad);
49 device->_libsigrok_device = libsigrok_device;
53 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
55 return _libsigrok_device;
58 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
62 case Gst::STATE_CHANGE_READY_TO_PAUSED:
63 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
64 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
65 _libsigrok_device->open();
66 _libsigrok_device->config_set(sigrok::ConfigKey::LIMIT_SAMPLES,
67 Glib::Variant<int>::create(10));
68 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
69 _task->set_lock(_mutex);
70 _src_pad->set_active(true);
72 return Gst::STATE_CHANGE_SUCCESS;
74 return Gst::STATE_CHANGE_SUCCESS;
78 void LegacyCaptureDevice::_datafeed_callback(
79 shared_ptr<sigrok::Device> device,
80 shared_ptr<sigrok::Packet> packet)
83 switch (packet->type()->id()) {
86 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
87 auto mem = Gst::Memory::create(
88 Gst::MEMORY_FLAG_READONLY,
89 logic->data_pointer(),
92 logic->data_length());
93 auto buf = Gst::Buffer::create();
94 buf->append_memory(move(mem));
95 _src_pad->push(move(buf));
100 _src_pad->push_event(Gst::EventEos::create());
107 void LegacyCaptureDevice::_run()
109 _session = _libsigrok_device->driver()->parent()->create_session();
110 _session->add_device(_libsigrok_device);
111 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));