]> sigrok.org Git - libsigrokflow.git/blame - src/main.cpp
Revised implementation without using constructor.
[libsigrokflow.git] / src / main.cpp
CommitLineData
572e76fe
UH
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
f7363af1
ML
23#include <iostream>
24
b903bb0a
UH
25namespace Srf
26{
27
f7363af1 28using namespace std;
d03b3a98 29using namespace std::placeholders;
f7363af1 30
b903bb0a
UH
31void init()
32{
33}
34
64b0db03
ML
35Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
36 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
f7363af1 37{
64b0db03
ML
38 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
39 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
40
41 auto src_template = Gst::PadTemplate::create("src",
42 Gst::PAD_SRC,
43 Gst::PAD_ALWAYS,
44 Gst::Caps::create_any());
45 device->_src_pad = Gst::Pad::create(src_template);
46 device->add_pad(device->_src_pad);
47 device->_libsigrok_device = libsigrok_device;
48 return device;
f7363af1
ML
49}
50
d03b3a98 51shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
f7363af1 52{
64b0db03 53 return _libsigrok_device;
f7363af1
ML
54}
55
d03b3a98
ML
56Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
57{
58 switch (transition)
59 {
60 case Gst::STATE_CHANGE_READY_TO_PAUSED:
61 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
62 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
64b0db03
ML
63 _libsigrok_device->open();
64 _libsigrok_device->config_set(sigrok::ConfigKey::LIMIT_SAMPLES,
d03b3a98
ML
65 Glib::Variant<int>::create(10));
66 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
67 _task->set_lock(_mutex);
68 _src_pad->set_active(true);
69 _task->start();
70 return Gst::STATE_CHANGE_SUCCESS;
71 default:
72 return Gst::STATE_CHANGE_SUCCESS;
73 }
74}
75
76void LegacyCaptureDevice::_datafeed_callback(
77 shared_ptr<sigrok::Device> device,
78 shared_ptr<sigrok::Packet> packet)
79{
80 (void) device;
81 switch (packet->type()->id()) {
82 case SR_DF_LOGIC:
83 {
84 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
85 auto mem = Gst::Memory::create(
86 Gst::MEMORY_FLAG_READONLY,
87 logic->data_pointer(),
88 logic->data_length(),
89 0,
90 logic->data_length());
91 auto buf = Gst::Buffer::create();
92 buf->append_memory(move(mem));
93 _src_pad->push(move(buf));
94 break;
95 }
96 case SR_DF_END:
97 _session->stop();
98 _src_pad->push_event(Gst::EventEos::create());
99 break;
100 default:
101 break;
102 }
103}
104
105void LegacyCaptureDevice::_run()
106{
64b0db03
ML
107 _session = _libsigrok_device->driver()->parent()->create_session();
108 _session->add_device(_libsigrok_device);
d03b3a98
ML
109 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
110 _session->start();
111 _session->run();
112 _task->stop();
113}
114
b903bb0a 115}