]>
Commit | Line | Data |
---|---|---|
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", "GPLv3+", "sigrok", "libsigrokflow", "http://sigrok.org"); | |
38 | } | |
39 | ||
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 | return true; | |
59 | } | |
60 | ||
61 | LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) : | |
62 | Glib::ObjectBase(typeid(LegacyCaptureDevice)), | |
63 | CaptureDevice(gobj) | |
64 | { | |
65 | add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src")); | |
66 | } | |
67 | ||
68 | Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create( | |
69 | shared_ptr<sigrok::HardwareDevice> libsigrok_device) | |
70 | { | |
71 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device"); | |
72 | if (!element) | |
73 | throw runtime_error("Failed to create element - plugin not registered?"); | |
74 | auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element); | |
75 | device->_libsigrok_device = libsigrok_device; | |
76 | return device; | |
77 | } | |
78 | ||
79 | shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device() | |
80 | { | |
81 | return _libsigrok_device; | |
82 | } | |
83 | ||
84 | Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition) | |
85 | { | |
86 | switch (transition) | |
87 | { | |
88 | case Gst::STATE_CHANGE_READY_TO_PAUSED: | |
89 | return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL; | |
90 | case Gst::STATE_CHANGE_PAUSED_TO_PLAYING: | |
91 | _libsigrok_device->open(); | |
92 | _libsigrok_device->config_set(sigrok::ConfigKey::LIMIT_SAMPLES, | |
93 | Glib::Variant<int>::create(10)); | |
94 | _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this)); | |
95 | _task->set_lock(_mutex); | |
96 | _src_pad->set_active(true); | |
97 | _task->start(); | |
98 | return Gst::STATE_CHANGE_SUCCESS; | |
99 | default: | |
100 | return Gst::STATE_CHANGE_SUCCESS; | |
101 | } | |
102 | } | |
103 | ||
104 | void LegacyCaptureDevice::_datafeed_callback( | |
105 | shared_ptr<sigrok::Device> device, | |
106 | shared_ptr<sigrok::Packet> packet) | |
107 | { | |
108 | (void) device; | |
109 | switch (packet->type()->id()) { | |
110 | case SR_DF_LOGIC: | |
111 | { | |
112 | auto logic = static_pointer_cast<sigrok::Logic>(packet->payload()); | |
113 | auto mem = Gst::Memory::create( | |
114 | Gst::MEMORY_FLAG_READONLY, | |
115 | logic->data_pointer(), | |
116 | logic->data_length(), | |
117 | 0, | |
118 | logic->data_length()); | |
119 | auto buf = Gst::Buffer::create(); | |
120 | buf->append_memory(move(mem)); | |
121 | _src_pad->push(move(buf)); | |
122 | break; | |
123 | } | |
124 | case SR_DF_END: | |
125 | _session->stop(); | |
126 | _src_pad->push_event(Gst::EventEos::create()); | |
127 | break; | |
128 | default: | |
129 | break; | |
130 | } | |
131 | } | |
132 | ||
133 | void LegacyCaptureDevice::_run() | |
134 | { | |
135 | _session = _libsigrok_device->driver()->parent()->create_session(); | |
136 | _session->add_device(_libsigrok_device); | |
137 | _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2)); | |
138 | _session->start(); | |
139 | _session->run(); | |
140 | _task->stop(); | |
141 | } | |
142 | ||
143 | } |