From 11d2ae972c434453f09a7ffe66a8c202efbce2a5 Mon Sep 17 00:00:00 2001 From: Thierry Bultel Date: Tue, 3 Jul 2018 18:52:33 +0200 Subject: Added role for radio, and mute/unmute actions This defines a new 'radio' role that uses the radio_stream Also implements the new mute/unmute actions, that are forwarded to the softmixer. Now registers to a client (multimedia player, radio service ...) disconnection, to properly free the leased role. Change-Id: Ic15fb63cd90ba28f3978c67d917a77ed6b030e08 Signed-off-by: Thierry Bultel --- ahl-binding/role.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++--- ahl-binding/role.hpp | 6 ++++- 2 files changed, 68 insertions(+), 4 deletions(-) (limited to 'ahl-binding') diff --git a/ahl-binding/role.cpp b/ahl-binding/role.cpp index 5b2f95e..1b60a40 100644 --- a/ahl-binding/role.cpp +++ b/ahl-binding/role.cpp @@ -187,6 +187,8 @@ void role_t::invoke(afb_request* req) else if (action == "close") close(req, arg); else if (action == "volume") volume(req, arg); else if (action == "interrupt") interrupt(req, arg); + else if (action == "mute") mute(req, arg); + else if (action == "unmute") unmute(req, arg); else afb_request_fail(req, "Unknown action!", nullptr); } @@ -200,7 +202,31 @@ void role_t::open(afb_request* r, json_object* o) if (!apply_policy(r)) { - afb_request_context_set(r, this, nullptr); + afb_request_context_set( + r, + this, + [](void* arg) + { + role_t * role = (role_t*) arg; + afb_dynapi * api = ahl_binding_t::instance().handle(); + + AFB_DYNAPI_NOTICE(api, "Released role %s\n", role->uid_.c_str()); + role->opened_ = false; + + // send a mute command to the HAL. We cannot reuse the do_mute function, + // because in the context here, the afb_request is no longer valid. + json_object* a = json_object_new_object(); + json_object_object_add(a, "mute", json_object_new_boolean(true)); + + afb_dynapi_call( + api, + role->hal_.c_str(), + role->stream_.c_str(), + a, + NULL, + NULL); + } + ); opened_ = true; json_object* result = json_object_new_object(); @@ -228,8 +254,42 @@ void role_t::close(afb_request* r, json_object* o) afb_request_success(r, nullptr, "Stream closed!"); } +void role_t::mute(afb_request * r, json_object* o) { + do_mute(r, true); +} + +void role_t::unmute(afb_request *r, json_object *o) { + do_mute(r, false); +} + +void role_t::do_mute(afb_request * r, bool v) { + + json_object* a = json_object_new_object(); + json_object_object_add(a, "mute", json_object_new_boolean(v)); + afb_dynapi * api = ahl_binding_t::instance().handle(); + + afb_dynapi_call( + api, + hal_.c_str(), + stream_.c_str(), + a, + [](void* closure, int status, json_object* result, afb_dynapi* handle) + { + AFB_DYNAPI_DEBUG(handle, "Got the following answer: %s", json_object_to_json_string(result)); + afb_request* r = (afb_request*)closure; + + json_object_get(result); + if (status) afb_request_fail(r, json_object_to_json_string(result), nullptr); + else afb_request_success(r, result, nullptr); + afb_request_unref(r); + }, + afb_request_addref(r)); +} + void role_t::volume(afb_request* r, json_object* o) { + afb_dynapi * api = ahl_binding_t::instance().handle(); + if(!afb_request_has_permission(r, "urn:AGL:permission::public:4a-audio-mixer")) { if (!opened_) @@ -246,7 +306,7 @@ void role_t::volume(afb_request* r, json_object* o) } else { - AFB_DYNAPI_NOTICE(ahl_binding_t::instance().handle(), "Granted special audio-mixer permission to change volume"); + AFB_DYNAPI_NOTICE(api, "Granted special audio-mixer permission to change volume"); } json_object* value; @@ -263,7 +323,7 @@ void role_t::volume(afb_request* r, json_object* o) json_object_object_add(a, "volume", value); afb_dynapi_call( - r->api, + api, hal_.c_str(), stream_.c_str(), a, diff --git a/ahl-binding/role.hpp b/ahl-binding/role.hpp index 6831195..5e64e11 100644 --- a/ahl-binding/role.hpp +++ b/ahl-binding/role.hpp @@ -37,11 +37,13 @@ private: int apply_policy(afb_request* req); + void do_mute(afb_request*, bool); + public: explicit role_t() = default; explicit role_t(const role_t&) = default; explicit role_t(role_t&&) = default; - + ~role_t() = default; role_t& operator=(const role_t&) = default; @@ -75,4 +77,6 @@ public: void close(afb_request* r, json_object* o); void volume(afb_request* r, json_object* o); void interrupt(afb_request* r, json_object* o); + void mute(afb_request* r, json_object* o); + void unmute(afb_request* r, json_object* o); }; -- cgit 1.2.3-korg