summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libsoundmanager.cpp470
-rw-r--r--src/libsoundmanager.hpp17
2 files changed, 239 insertions, 248 deletions
diff --git a/src/libsoundmanager.cpp b/src/libsoundmanager.cpp
index 25125d2..9eeac92 100644
--- a/src/libsoundmanager.cpp
+++ b/src/libsoundmanager.cpp
@@ -51,7 +51,7 @@ static const std::vector<std::string> api_list{
};
static const std::vector<std::string> event_list{
- std::string("asyncSetSourceState"),
+ std::string("asyncSetSourceState"),
std::string("newMainConnection"),
std::string("volumeChanged"),
std::string("removedMainConnection"),
@@ -64,22 +64,22 @@ static const std::vector<std::string> event_list{
static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
{
- static_cast<Soundmanager*>(closure)->on_hangup(NULL,wsj);
+ static_cast<Soundmanager*>(closure)->on_hangup(NULL,wsj);
}
static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
- /* Soundmanager is not called from other process */
+ /* Soundmanager is not called from other process */
}
static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
{
- static_cast<Soundmanager*>(closure)->on_event(NULL,event,msg);
+ static_cast<Soundmanager*>(closure)->on_event(NULL,event,msg);
}
static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
{
- static_cast<Soundmanager*>(closure)->on_reply(NULL,msg);
+ static_cast<Soundmanager*>(closure)->on_reply(NULL,msg);
}
Soundmanager::Soundmanager()
@@ -88,14 +88,14 @@ Soundmanager::Soundmanager()
Soundmanager::~Soundmanager()
{
- if(mploop)
- {
- sd_event_unref(mploop);
- }
- if(sp_websock != NULL)
- {
- afb_wsj1_unref(sp_websock);
- }
+ if(mploop)
+ {
+ sd_event_unref(mploop);
+ }
+ if(sp_websock != NULL)
+ {
+ afb_wsj1_unref(sp_websock);
+ }
}
@@ -114,70 +114,70 @@ Soundmanager::~Soundmanager()
*/
int Soundmanager::init(int port, const string& token)
{
- int ret;
- if(port > 0 && token.size() > 0)
- {
- mport = port;
- mtoken = token;
- }
- else
- {
- ELOG("port and token should be > 0, Initial port and token uses.");
- return -1;
- }
-
- ret = initialize_websocket();
- if(ret != 0 )
- {
- ELOG("Failed to initialize websocket");
- return -1;
- }
- ret = init_event();
- if(ret != 0 )
- {
- ELOG("Failed to initialize websocket");
- return -1;
- }
- return 0;
+ int ret;
+ if(port > 0 && token.size() > 0)
+ {
+ mport = port;
+ mtoken = token;
+ }
+ else
+ {
+ ELOG("port and token should be > 0, Initial port and token uses.");
+ return -1;
+ }
+
+ ret = initialize_websocket();
+ if(ret != 0 )
+ {
+ ELOG("Failed to initialize websocket");
+ return -1;
+ }
+ ret = init_event();
+ if(ret != 0 )
+ {
+ ELOG("Failed to initialize websocket");
+ return -1;
+ }
+ return 0;
}
int Soundmanager::initialize_websocket()
{
- mploop = NULL;
- onEvent = nullptr;
- onReply = nullptr;
- int ret = sd_event_default(&mploop);
- if(ret < 0)
- {
- ELOG("Failed to create event loop");
- goto END;
- }
- /* Initialize interface from websocket */
- {
- minterface.on_hangup = _on_hangup_static;
- minterface.on_call = _on_call_static;
- minterface.on_event = _on_event_static;
- string muri = "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken;
- sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
- }
- if(sp_websock == NULL)
- {
- ELOG("Failed to create websocket connection");
- goto END;
- }
-
- return 0;
+ mploop = NULL;
+ onEvent = nullptr;
+ onReply = nullptr;
+ int ret = sd_event_default(&mploop);
+ if(ret < 0)
+ {
+ ELOG("Failed to create event loop");
+ goto END;
+ }
+ /* Initialize interface from websocket */
+ {
+ minterface.on_hangup = _on_hangup_static;
+ minterface.on_call = _on_call_static;
+ minterface.on_event = _on_event_static;
+ string muri = "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken;
+ sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
+ }
+ if(sp_websock == NULL)
+ {
+ ELOG("Failed to create websocket connection");
+ goto END;
+ }
+
+ return 0;
END:
- if(mploop)
- {
- sd_event_unref(mploop);
- }
- return -1;
+ if(mploop)
+ {
+ sd_event_unref(mploop);
+ }
+ return -1;
}
int Soundmanager::init_event(){
- /* subscribe most important event for sound right */
- return subscribe(string("asyncSetSourceState"));
+ /* subscribe most important event for sound right */
+ return subscribe(string("asyncSetSourceState"));
}
/**
@@ -195,13 +195,13 @@ int Soundmanager::init_event(){
* If you would like to get event, please call subscribe function before/after this function
*/
void Soundmanager::registerCallback(
- void (*event_cb)(const string& event, struct json_object* event_contents),
- void (*reply_cb)(struct json_object* reply_contents),
- void (*hangup_cb)(void))
+ void (*event_cb)(const string& event, struct json_object* event_contents),
+ void (*reply_cb)(struct json_object* reply_contents),
+ void (*hangup_cb)(void))
{
- onEvent = event_cb;
- onReply = reply_cb;
- onHangup = hangup_cb;
+ onEvent = event_cb;
+ onReply = reply_cb;
+ onHangup = hangup_cb;
}
/**
@@ -221,14 +221,14 @@ void Soundmanager::registerCallback(
*/
int Soundmanager::registerSource(const string& sourceName)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- struct json_object* jsn = json_object_new_string(sourceName.c_str());
- json_object_object_add(j_obj, "appname", jsn);
- return this->call(__FUNCTION__, j_obj);
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jsn = json_object_new_string(sourceName.c_str());
+ json_object_object_add(j_obj, "appname", jsn);
+ return this->call(__FUNCTION__, j_obj);
}
/**
@@ -249,16 +249,16 @@ int Soundmanager::registerSource(const string& sourceName)
*/
int Soundmanager::connect(int sourceID, int sinkID)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- struct json_object* jsource = json_object_new_int(sourceID);
- struct json_object* jsink = json_object_new_int(sinkID);
- json_object_object_add(j_obj, "sourceID", jsource);
- json_object_object_add(j_obj, "sinkID", jsink);
- return this->call(__FUNCTION__, j_obj);
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jsource = json_object_new_int(sourceID);
+ struct json_object* jsink = json_object_new_int(sinkID);
+ json_object_object_add(j_obj, "sourceID", jsource);
+ json_object_object_add(j_obj, "sinkID", jsink);
+ return this->call(__FUNCTION__, j_obj);
}
/**
@@ -281,17 +281,17 @@ int Soundmanager::connect(int sourceID, int sinkID)
*/
int Soundmanager::connect(int sourceID, const string& sinkName)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- struct json_object* jsource = json_object_new_int(sourceID);
- //struct json_object* jsink = json_object_new_int(1);
- struct json_object* jsink = json_object_new_string(sinkName.c_str());
- json_object_object_add(j_obj, "sourceID", jsource);
- json_object_object_add(j_obj, "sinkID", jsink);
- return this->call(__FUNCTION__, j_obj);
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jsource = json_object_new_int(sourceID);
+ //struct json_object* jsink = json_object_new_int(1);
+ struct json_object* jsink = json_object_new_string(sinkName.c_str());
+ json_object_object_add(j_obj, "sourceID", jsource);
+ json_object_object_add(j_obj, "sinkID", jsink);
+ return this->call(__FUNCTION__, j_obj);
}
/**
@@ -309,14 +309,14 @@ int Soundmanager::connect(int sourceID, const string& sinkName)
*/
int Soundmanager::disconnect(int connectionID)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- struct json_object* jconnection = json_object_new_int(connectionID);
- json_object_object_add(j_obj, "mainConnectionID", jconnection);
- return this->call(__FUNCTION__, j_obj);
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jconnection = json_object_new_int(connectionID);
+ json_object_object_add(j_obj, "mainConnectionID", jconnection);
+ return this->call(__FUNCTION__, j_obj);
}
/**
@@ -336,16 +336,16 @@ int Soundmanager::disconnect(int connectionID)
*/
int Soundmanager::ackSetSourceState(int handle, int error)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- struct json_object* jhandle = json_object_new_int(handle);
- struct json_object* jerrno = json_object_new_int(error);
- json_object_object_add(j_obj, "handle", jhandle);
- json_object_object_add(j_obj, "error", jerrno);
- return this->call(__FUNCTION__, j_obj);
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jhandle = json_object_new_int(handle);
+ struct json_object* jerrno = json_object_new_int(error);
+ json_object_object_add(j_obj, "handle", jhandle);
+ json_object_object_add(j_obj, "error", jerrno);
+ return this->call(__FUNCTION__, j_obj);
}
/**
@@ -364,21 +364,21 @@ int Soundmanager::ackSetSourceState(int handle, int error)
*/
int Soundmanager::call(const string& verb, struct json_object* arg)
{
- int ret;
- if(!sp_websock)
- {
- return -1;
- }
- if (!has_verb(verb))
- {
- ELOG("verb doesn't exit");
- return -1;
- }
- ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
- if (ret < 0) {
- ELOG("Failed to call verb:%s",verb.c_str());
- }
- return ret;
+ int ret;
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ if (!has_verb(verb))
+ {
+ ELOG("verb doesn't exit");
+ return -1;
+ }
+ ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",verb.c_str());
+ }
+ return ret;
}
/**
@@ -398,21 +398,21 @@ int Soundmanager::call(const string& verb, struct json_object* arg)
*/
int Soundmanager::call(const char* verb, struct json_object* arg)
{
- int ret;
- if(!sp_websock)
- {
- return -1;
- }
- if (!has_verb(string(verb)))
- {
- ELOG("verb doesn't exit");
- return -1;
- }
- ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
- if (ret < 0) {
- ELOG("Failed to call verb:%s",verb);
- }
- return ret;
+ int ret;
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ if (!has_verb(string(verb)))
+ {
+ ELOG("verb doesn't exit");
+ return -1;
+ }
+ ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",verb);
+ }
+ return ret;
}
/**
@@ -431,18 +431,18 @@ int Soundmanager::call(const char* verb, struct json_object* arg)
*/
int Soundmanager::subscribe(const string& event_name)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
-
- int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
- if (ret < 0) {
- ELOG("Failed to call verb:%s",__FUNCTION__);
- }
- return ret;
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
+
+ int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",__FUNCTION__);
+ }
+ return ret;
}
/**
@@ -460,29 +460,29 @@ int Soundmanager::subscribe(const string& event_name)
*/
int Soundmanager::unsubscribe(const string& event_name)
{
- if(!sp_websock)
- {
- return -1;
- }
- struct json_object* j_obj = json_object_new_object();
- json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
-
- int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
- if (ret < 0) {
- ELOG("Failed to call verb:%s",__FUNCTION__);
- }
- return ret;
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
+
+ int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",__FUNCTION__);
+ }
+ return ret;
}
/************* Callback Function *************/
void Soundmanager::on_hangup(void *closure, struct afb_wsj1 *wsj)
{
- DLOG("%s called", __FUNCTION__);
- if(onHangup != nullptr)
- {
- onHangup();
- }
+ DLOG("%s called", __FUNCTION__);
+ if(onHangup != nullptr)
+ {
+ onHangup();
+ }
}
void Soundmanager::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
@@ -494,79 +494,79 @@ void Soundmanager::on_call(void *closure, const char *api, const char *verb, str
* msg is like {"event":"soundmanager\/newMainConnection","data":{"mainConnectionID":3,"sourceID":101,"sinkID":100,"delay":0,"connectionState":4},"jtype":"afb-event"})}
* ^key^ ^^^^^^^^^^^^ value ^^^^^^^^^^^^
* so you can get
- event name : struct json_object obj = json_object_object_get(msg,"event")
+ event name : struct json_object obj = json_object_object_get(msg,"event")
*/
void Soundmanager::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
- /* check event is for us */
- string ev = string(event);
- if (ev.find(API) == string::npos) {
- /* It's not us */
- return;
- }
- struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
- if((onEvent != nullptr))
- {
- onEvent(ev, ev_contents);
- }
- else{}
-
- json_object_put(ev_contents);
+ /* check event is for us */
+ string ev = string(event);
+ if (ev.find(API) == string::npos) {
+ /* It's not us */
+ return;
+ }
+ struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
+ if((onEvent != nullptr))
+ {
+ onEvent(ev, ev_contents);
+ }
+ else{}
+
+ json_object_put(ev_contents);
}
void Soundmanager::on_reply(void *closure, struct afb_wsj1_msg *msg)
{
- struct json_object* reply = afb_wsj1_msg_object_j(msg);
- /*struct json_object *json_data = json_object_object_get(reply, "response");
- struct json_object *jverb = json_object_object_get(json_data, "verb");
- const char* cverb = json_object_get_string(jverb);
- DLOG("cverb is %s",cverb);
- string verb = string(cverb);
- DLOG("verb is %s",verb.c_str());
-
- if(verb == "registerSource"){
- struct json_object *jsourceID = json_object_object_get(json_data, "sourceID");
- int sourceID = json_object_get_int(jsourceID);
- msourceIDs.push_back(sourceID);
- DLOG("my sourceID is created: %d", sourceID);
- }*/
- if(onReply != nullptr)
- {
- onReply(reply);
- }
- json_object_put(reply);
+ struct json_object* reply = afb_wsj1_msg_object_j(msg);
+ /*struct json_object *json_data = json_object_object_get(reply, "response");
+ struct json_object *jverb = json_object_object_get(json_data, "verb");
+ const char* cverb = json_object_get_string(jverb);
+ DLOG("cverb is %s",cverb);
+ string verb = string(cverb);
+ DLOG("verb is %s",verb.c_str());
+
+ if(verb == "registerSource"){
+ struct json_object *jsourceID = json_object_object_get(json_data, "sourceID");
+ int sourceID = json_object_get_int(jsourceID);
+ msourceIDs.push_back(sourceID);
+ DLOG("my sourceID is created: %d", sourceID);
+ }*/
+ if(onReply != nullptr)
+ {
+ onReply(reply);
+ }
+ json_object_put(reply);
}
/* Internal Function in libsoundmanager */
static void _ELOG(const char* func, const int line, const char* log, ...)
{
- char *message;
- va_list args;
- va_start(args, log);
- if (log == NULL || vasprintf(&message, log, args) < 0)
- message = NULL;
- cout << "[ERROR: soundmanager]" << func << "(" << line << "):" << message << endl;
- va_end(args);
- free(message);
+ char *message;
+ va_list args;
+ va_start(args, log);
+ if (log == NULL || vasprintf(&message, log, args) < 0)
+ message = NULL;
+ cout << "[ERROR: soundmanager]" << func << "(" << line << "):" << message << endl;
+ va_end(args);
+ free(message);
}
static void _DLOG(const char* func, const int line, const char* log, ...)
{
- char *message;
- va_list args;
- va_start(args, log);
- if (log == NULL || vasprintf(&message, log, args) < 0)
- message = NULL;
- cout << "[DEBUG: soundmanager]" << func << "(" << line << "):" << message << endl;
- va_end(args);
- free(message);
+ char *message;
+ va_list args;
+ va_start(args, log);
+ if (log == NULL || vasprintf(&message, log, args) < 0)
+ message = NULL;
+ cout << "[DEBUG: soundmanager]" << func << "(" << line << "):" << message << endl;
+ va_end(args);
+ free(message);
}
static bool has_verb(const string& verb)
{
- if(find(api_list.begin(), api_list.end(), verb) != api_list.end())
- return true;
- else
- return false;
+ if(find(api_list.begin(), api_list.end(), verb) != api_list.end())
+ return true;
+ else
+ return false;
}
diff --git a/src/libsoundmanager.hpp b/src/libsoundmanager.hpp
index 2632f94..96346e8 100644
--- a/src/libsoundmanager.hpp
+++ b/src/libsoundmanager.hpp
@@ -19,7 +19,6 @@
#include <vector>
#include <map>
#include <string>
-#include <functional>
#include <json-c/json.h>
#include <systemd/sd-event.h>
extern "C"
@@ -36,12 +35,10 @@ public:
Soundmanager(const Soundmanager &) = delete;
Soundmanager &operator=(const Soundmanager &) = delete;
int init(int port, const std::string& token);
-
- using handler_fun = std::function<void(struct json_object*)>;
-
- enum EventType_SM {
- Event_AsyncSetSourceState = 1 /*arg key: {sourceID, handle, sourceState}*/
- };
+ void registerCallback(
+ void (*event_cb)(const std::string& event, struct json_object* event_contents),
+ void (*reply_cb)(struct json_object* reply_contents),
+ void (*hangup_cb)(void) = nullptr);
/* Method */
int registerSource(const std::string& sourceName);
@@ -54,10 +51,6 @@ public:
int call(const char* verb, struct json_object* arg);
int subscribe(const std::string& event_name);
int unsubscribe(const std::string& event_name);
- void registerCallback(
- void (*event_cb)(const std::string& event, struct json_object* event_contents),
- void (*reply_cb)(struct json_object* reply_contents),
- void (*hangup_cb)(void) = nullptr);
private:
int init_event();
@@ -73,8 +66,6 @@ private:
int mport;
std::string mtoken;
std::vector<int> msourceIDs;
- std::map<EventType_SM, handler_fun> handlers;
- EventType_SM const NumItems = (EventType_SM)(Event_AsyncSetSourceState + 1);
public:
/* Don't use/ Internal only */