]> sigrok.org Git - libsigrokflow.git/blame - src/legacy_decoder.cpp
Add tests/legacy_decoder.cpp and a simple unit test.
[libsigrokflow.git] / src / legacy_decoder.cpp
CommitLineData
6cb4dbbf
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>
d59dd5ff 22#include <libsigrokflow/legacy_decoder.hpp>
4f0c2351 23#include <stdexcept>
d59dd5ff 24#include <gstreamermm/private/basesink_p.h>
6cb4dbbf
UH
25
26namespace Srf
27{
28
f638442b 29using std::runtime_error;
6cb4dbbf
UH
30
31#ifdef HAVE_LIBSIGROKDECODE
32void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
33{
34 klass->set_metadata("sigrok legacy decoder",
35 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
36 "Uwe Hermann");
37
38 klass->add_pad_template(Gst::PadTemplate::create(
39 "sink",
40 Gst::PAD_SINK,
41 Gst::PAD_ALWAYS,
42 Gst::Caps::create_any()));
43}
44
45bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
46{
47 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
48 0, Gst::register_mm_type<LegacyDecoder>(
49 "sigrok_legacy_decoder"));
50
51 return true;
52}
53
54LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
55 Glib::ObjectBase(typeid(LegacyDecoder)),
56 Sink(gobj)
57{
58}
59
60Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
61 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
62{
63 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
64 if (!element)
65 throw runtime_error("Failed to create element - plugin not registered?");
66 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
67 decoder->session_ = libsigrokdecode_session;
2f7d45de 68 decoder->unitsize_ = unitsize;
6cb4dbbf
UH
69
70 return decoder;
71}
72
73struct srd_session *LegacyDecoder::libsigrokdecode_session()
74{
75 return session_;
76}
77
66ddf1f1
UH
78uint64_t LegacyDecoder::unitsize()
79{
80 return unitsize_;
81}
82
6cb4dbbf
UH
83Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
84{
85 Gst::MapInfo info;
86 buffer->map(info, Gst::MAP_READ);
2f7d45de 87 uint64_t num_samples = info.get_size() / unitsize_;
6cb4dbbf 88 srd_session_send(session_, abs_ss_, abs_ss_ + num_samples,
2f7d45de 89 info.get_data(), info.get_size(), unitsize_);
6cb4dbbf
UH
90 abs_ss_ += num_samples;
91 buffer->unmap(info);
92
93 return Gst::FLOW_OK;
94}
95
96bool LegacyDecoder::start_vfunc()
97{
98 abs_ss_ = 0;
99 srd_session_start(session_);
100
101 return true;
102}
103
104bool LegacyDecoder::stop_vfunc()
105{
106 srd_session_terminate_reset(session_);
107
108 return true;
109}
110#endif
111
112}