softwarecontainer  0.18.0-739e8d7 2017-05-04
pulsegateway.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include "pulsegateway.h"
21 
22 namespace softwarecontainer {
23 
24 PulseGateway::PulseGateway(std::shared_ptr<ContainerAbstractInterface> container) :
25  Gateway(ID, container),
26  m_enableAudio(false)
27 {
28 }
29 
30 PulseGateway::~PulseGateway()
31 {
32 }
33 
34 bool PulseGateway::readConfigElement(const json_t *element)
35 {
36  bool configValue = false;
37 
38  if (!JSONParser::read(element, "audio", configValue)) {
39  log_error() << "Either \"audio\" key is missing, or not a bool in json configuration";
40  return false;
41  }
42 
43  if (!m_enableAudio) {
44  m_enableAudio = configValue;
45  }
46 
47  return true;
48 }
49 
50 bool PulseGateway::enablePulseAudio() {
51  bool hasPulse = false;
52  std::string dir = Glib::getenv(PULSE_AUDIO_SERVER_ENVIRONMENT_VARIABLE_NAME, hasPulse);
53 
54  if (!hasPulse) {
55  log_error() << "Should enable pulseaudio gateway, but "
56  << std::string(PULSE_AUDIO_SERVER_ENVIRONMENT_VARIABLE_NAME)
57  << " is not defined";
58  return false;
59  }
60 
61  log_info() << "Enabling pulseaudio gateway. Socket location : " << dir;
62  std::string pathInContainer = buildPath("/gateways/", SOCKET_FILE_NAME);
63 
64  std::shared_ptr<ContainerAbstractInterface> container = getContainer();
65  if (!container->bindMountInContainer(std::string(dir), pathInContainer, false)) {
66  log_error() << "Could not bind mount pulseaudio socket in container";
67  return false;
68  }
69 
70  std::string unixPath = "unix:" + pathInContainer;
71  container->setEnvironmentVariable(PULSE_AUDIO_SERVER_ENVIRONMENT_VARIABLE_NAME, unixPath);
72  return true;
73 }
74 
76 {
77  if (!m_enableAudio) {
78  log_debug() << "Audio will be disabled";
79  return true;
80  }
81  log_debug() << "Audio will be enabled";
82 
83  return enablePulseAudio();
84 }
85 
87 {
88  return true;
89 }
90 
91 } // namespace softwarecontainer
virtual bool activateGateway() override
Implements Gateway::activateGateway.
bool readConfigElement(const json_t *element) override
Gateway specific parsing of config elements.
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51
virtual bool teardownGateway() override
Implements Gateway::teardownGateway.