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