libvisual  0.5.0
lv_input.cpp
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012-2013 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include "config.h"
25 #include "lv_input.h"
26 #include "lv_common.h"
27 #include "lv_plugin_registry.h"
28 #include <stdexcept>
29 
30 namespace LV {
31 
32  class Input::Impl
33  {
34  public:
35  VisPluginData* plugin;
36  Audio audio;
37  std::function<bool(Audio&)> callback;
38 
39  Impl ();
40  ~Impl ();
41 
42  VisInputPlugin* get_input_plugin () const;
43  };
44 
45  Input::Impl::Impl ()
46  : plugin {nullptr}
47  {
48  // nothing
49  }
50 
51  Input::Impl::~Impl ()
52  {
53  if (plugin) {
54  visual_plugin_unload (plugin);
55  }
56  }
57 
58  VisInputPlugin* Input::Impl::get_input_plugin () const
59  {
60  return static_cast<VisInputPlugin*> (visual_plugin_get_info (plugin)->plugin);
61  }
62 
63  InputPtr Input::load (std::string const& name)
64  {
65  try {
66  return {new Input{name}, false};
67  }
68  catch (std::exception& error) {
69  visual_log (VISUAL_LOG_ERROR, "%s", error.what ());
70  return nullptr;
71  }
72  }
73 
74  Input::Input (std::string const& name)
75  : m_impl {new Impl}
76  , m_ref_count {1}
77  {
78  if (!LV::PluginRegistry::instance()->has_plugin (VISUAL_PLUGIN_TYPE_INPUT, name)) {
79  throw std::runtime_error {"Input plugin not found"};
80  }
81 
82  m_impl->plugin = visual_plugin_load (VISUAL_PLUGIN_TYPE_INPUT, name.c_str ());
83  if (!m_impl->plugin) {
84  throw std::runtime_error {"Failed to load input plugin"};
85  }
86  }
87 
88  Input::~Input ()
89  {
90  // nothing
91  }
92 
94  {
95  if (m_impl->callback) {
96  return true;
97  }
98 
99  return visual_plugin_realize (m_impl->plugin);
100  }
101 
102  VisPluginData* Input::get_plugin ()
103  {
104  return m_impl->plugin;
105  }
106 
107  void Input::set_callback (std::function<bool(Audio&)> const& callback)
108  {
109  m_impl->callback = callback;
110  }
111 
112  Audio const& Input::get_audio ()
113  {
114  return m_impl->audio;
115  }
116 
117  bool Input::run ()
118  {
119  if (m_impl->callback) {
120  m_impl->callback (m_impl->audio);
121  return true;
122  }
123 
124  auto input_plugin = m_impl->get_input_plugin ();
125 
126  if (!input_plugin) {
127  visual_log (VISUAL_LOG_ERROR, "The input plugin is not loaded correctly.");
128  return false;
129  }
130 
131  input_plugin->upload (m_impl->plugin, &m_impl->audio);
132 
133  return true;
134  }
135 }