summaryrefslogtreecommitdiffstats
path: root/src/ahl-binding.c
diff options
context:
space:
mode:
authorTai Vuong <tvuong@audiokinetic.com>2017-10-30 15:18:33 -0400
committerTai Vuong <tvuong@audiokinetic.com>2017-10-30 15:18:33 -0400
commitc46955de2731563d1991691cd1a7986f05f019e3 (patch)
treee0375428c2c9ad60bbfb8cc7f53f157770232ea6 /src/ahl-binding.c
parent53aea8f073c5b4ad859c39f903fdbcaccd2fc5b8 (diff)
parent8a584f01b46d251fdc5de8b071eff755d99f0090 (diff)
isolate structure use in policy util from HLB
Diffstat (limited to 'src/ahl-binding.c')
-rw-r--r--src/ahl-binding.c1547
1 files changed, 1240 insertions, 307 deletions
diff --git a/src/ahl-binding.c b/src/ahl-binding.c
index 4b075c0..264930f 100644
--- a/src/ahl-binding.c
+++ b/src/ahl-binding.c
@@ -1,6 +1,5 @@
/*
* Copyright (C) 2017 "Audiokinetic Inc"
- * Author Francois Thibault <fthibault@audiokinetic.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,43 +21,160 @@
#include "ahl-binding.h"
#include "ahl-apidef.h" // Generated from JSON OpenAPI
#include "wrap-json.h"
+#include "ahl-policy.h"
+#include "ahl-policy-utils.h"
// Global high-level binding context
-AHLCtxT g_AHLCtx;
-
-// Workshop development topics:
-// - Associating streamID with a client id of afb. Has to be done with sessions?
-// - Declaring dependencies on other binding services (examples)
-// - json_object_put to free JSON objects? potential leaks currently
-// - no events on new HAL registered to alsacore?
-// - unregistering event subscription examples?
-// - discussion how to use property and event system to dispatch external parameterization (e.g. Wwise/Fiberdyne)
-// - discussion where to best isolate policy (controller or HLB plugin)
-// - Other HLB attributes to pass through (e.g. interrupted behavior)
-// - DBScale TLV warning
-// - GLib (internal) dependency
-// - HAL registration dependency (initialization order)
-// - Binding startup arguments for config file path
-// - Can we use the same HAL for different card numbers?
-// - Example use of volume ramping in HAL?
-// - Binding termination function
-// - AGL persistence framework?
-// - How to provide API services with config.xml (secrets and all)
-
-
-// Helper macros/func for packaging JSON objects from C structures
-#define EndpointInfoStructToJSON(__JSON_OBJECT__, __ENDPOINTINFOSTRUCT__) \
- wrap_json_pack(&__JSON_OBJECT__, "{s:i,s:i,s:s,s:i}",\
- "endpoint_id", __ENDPOINTINFOSTRUCT__.endpointID, \
- "endpoint_type", __ENDPOINTINFOSTRUCT__.type, \
- "device_name", __ENDPOINTINFOSTRUCT__.deviceName, \
- "device_uri_type", __ENDPOINTINFOSTRUCT__.deviceURIType);
+AHLCtxT g_AHLCtx;
+
+// TODO: Helpers that could be common
+static EndpointTypeT EndpointTypeToEnum(char * in_pEndpointTypeStr)
+{
+ if (in_pEndpointTypeStr == NULL) {
+ return ENDPOINTTYPE_MAXVALUE;
+ }
+ else if (strcasecmp(in_pEndpointTypeStr,AHL_ENDPOINTTYPE_SOURCE)==0) {
+ return ENDPOINTTYPE_SOURCE;
+ }
+ else if (strcasecmp(in_pEndpointTypeStr,AHL_ENDPOINTTYPE_SINK)==0) {
+ return ENDPOINTTYPE_SINK;
+ }
+ else
+ return ENDPOINTTYPE_MAXVALUE;
+}
+
+static StreamStateT StreamStateToEnum(char * in_pStreamStateStr)
+{
+ if (in_pStreamStateStr == NULL) {
+ return STREAM_STATE_MAXVALUE;
+ }
+ else if (strcasecmp(in_pStreamStateStr,AHL_STREAM_STATE_IDLE)==0) {
+ return STREAM_STATE_IDLE;
+ }
+ else if (strcasecmp(in_pStreamStateStr,AHL_STREAM_STATE_RUNNING)==0) {
+ return STREAM_STATE_RUNNING;
+ }
+ else if (strcasecmp(in_pStreamStateStr,AHL_STREAM_STATE_PAUSED)==0) {
+ return STREAM_STATE_PAUSED;
+ }
+ else
+ return STREAM_STATE_MAXVALUE;
+}
+
+static StreamMuteT StreamMuteToEnum(char * in_pStreamMuteStr)
+{
+ if (in_pStreamMuteStr == NULL) {
+ return STREAM_MUTE_MAXVALUE;
+ }
+ else if (strcasecmp(in_pStreamMuteStr,AHL_STREAM_UNMUTED)==0) {
+ return STREAM_UNMUTED;
+ }
+ else if (strcasecmp(in_pStreamMuteStr,AHL_STREAM_MUTED)==0) {
+ return STREAM_MUTED;
+ }
+ else
+ return STREAM_MUTE_MAXVALUE;
+}
+
+static char * DeviceURITypeEnumToStr(DeviceURITypeT in_eDeviceURIType) {
+ switch(in_eDeviceURIType) {
+ case DEVICEURITYPE_ALSA_HW: // Alsa hardware device URI
+ return AHL_DEVICEURITYPE_ALSA_HW;
+ case DEVICEURITYPE_ALSA_DMIX: // Alsa Dmix device URI (only for playback devices)
+ return AHL_DEVICEURITYPE_ALSA_DMIX;
+ case DEVICEURITYPE_ALSA_DSNOOP: // Alsa DSnoop device URI (only for capture devices)
+ return AHL_DEVICEURITYPE_ALSA_DSNOOP;
+ case DEVICEURITYPE_ALSA_SOFTVOL: // Alsa softvol device URI
+ return AHL_DEVICEURITYPE_ALSA_SOFTVOL;
+ case DEVICEURITYPE_ALSA_PLUG: // Alsa plug device URI
+ return AHL_DEVICEURITYPE_ALSA_PLUG;
+ case DEVICEURITYPE_ALSA_OTHER: // Alsa domain URI device of unspecified type
+ return AHL_DEVICEURITYPE_ALSA_OTHER;
+ case DEVICEURITYPE_NOT_ALSA: // Unknown (not ALSA domain)
+ return AHL_DEVICEURITYPE_NOT_ALSA;
+ default:
+ return "Unknown";
+ }
+}
+
+static char * StreamStateEnumToStr(StreamStateT in_eStreamState) {
+ switch(in_eStreamState) {
+ case STREAM_STATE_IDLE:
+ return AHL_STREAM_STATE_IDLE;
+ case STREAM_STATE_RUNNING:
+ return AHL_STREAM_STATE_RUNNING;
+ case STREAM_STATE_PAUSED:
+ return AHL_STREAM_STATE_PAUSED;
+ default:
+ return "Unknown";
+ }
+}
+
+static char * StreamMuteEnumToStr(StreamMuteT in_eStreamMute) {
+ switch(in_eStreamMute) {
+ case STREAM_UNMUTED:
+ return AHL_STREAM_UNMUTED;
+ case STREAM_MUTED:
+ return AHL_STREAM_MUTED;
+ default:
+ return "Unknown";
+ }
+}
+
+static void AudioFormatStructToJSON(json_object **audioFormatJ, AudioFormatT * pAudioFormat)
+{
+ wrap_json_pack(audioFormatJ, "{s:i,s:i,s:i}",
+ "sample_rate", pAudioFormat->sampleRate,
+ "num_channels", pAudioFormat->numChannels,
+ "sample_type", pAudioFormat->sampleType);
+}
+
+// Package only information that can useful to application clients when selecting endpoint
+static void EndpointInfoStructToJSON(json_object **endpointInfoJ, EndpointInfoT * pEndpointInfo)
+{
+ json_object *formatInfoJ = NULL;
+ wrap_json_pack(endpointInfoJ, "{s:i,s:s,s:s,s:s,s:s,s:s,s:s}",
+ "endpoint_id", pEndpointInfo->endpointID,
+ "endpoint_type", (pEndpointInfo->type == ENDPOINTTYPE_SOURCE) ? AHL_ENDPOINTTYPE_SOURCE : AHL_ENDPOINTTYPE_SINK,
+ "device_name", pEndpointInfo->gsDeviceName,
+ "display_name", pEndpointInfo->gsDisplayName,
+ "audio_role", pEndpointInfo->pRoleName,
+ "device_domain",pEndpointInfo->gsDeviceDomain,
+ "device_uri_type", DeviceURITypeEnumToStr(pEndpointInfo->deviceURIType));
+ AudioFormatStructToJSON(&formatInfoJ,&pEndpointInfo->format);
+ json_object_object_add(*endpointInfoJ,"format",formatInfoJ);
+
+ // Properties
+ if (pEndpointInfo->pPropTable) {
+ json_object *pPropTableJ = json_object_new_array();
+
+ GHashTableIter iter;
+ gpointer key, value;
+ g_hash_table_iter_init (&iter, pEndpointInfo->pPropTable);
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ {
+ if((key!=NULL) && (value!=NULL))
+ {
+ json_object *pPropertyJ = NULL;
+ json_object_get((json_object*)value); // Don't let the framework free our object when the request is done
+ wrap_json_pack(&pPropertyJ, "{s:s,s:o}","property_name", (char*)key, "property_value", (json_object*)value);
+ json_object_array_add(pPropTableJ, pPropertyJ);
+ }
+ }
+ json_object_object_add(*endpointInfoJ,"properties",pPropTableJ);
+ }
+}
-static void StreamInfoStructToJSON(json_object **streamInfoJ, StreamInfoT streamInfo)
+// Package only information that can useful to application clients when opening a stream
+static void StreamInfoStructToJSON(json_object **streamInfoJ, StreamInfoT * pStreamInfo)
{
- json_object *endpointInfoJ;
- EndpointInfoStructToJSON(endpointInfoJ,streamInfo.endpointInfo);
- wrap_json_pack(streamInfoJ, "{s:i}", "stream_id", streamInfo.streamID);
+ json_object *endpointInfoJ = NULL;
+ EndpointInfoStructToJSON(&endpointInfoJ,pStreamInfo->pEndpointInfo);
+ wrap_json_pack(streamInfoJ, "{s:i,s:s,s:s,s:s}",
+ "stream_id", pStreamInfo->streamID,
+ "state", StreamStateEnumToStr(pStreamInfo->streamState),
+ "mute", StreamMuteEnumToStr(pStreamInfo->streamMute),
+ "device_uri",pStreamInfo->pEndpointInfo->gsDeviceURI); // Need to open a stream to have access to the device URI
json_object_object_add(*streamInfoJ,"endpoint_info",endpointInfoJ);
}
@@ -69,86 +185,378 @@ static streamID_t CreateNewStreamID()
return newID;
}
-static int FindRoleIndex( const char * in_pAudioRole)
+static EndpointInfoT * GetEndpointInfoWithRole(endpointID_t in_endpointID, EndpointTypeT in_endpointType, RoleInfoT * in_pRole)
{
- int index = -1; // Not found
- for (int i = 0; i < g_AHLCtx.policyCtx.iNumberRoles; i++)
- {
- GString gs = g_array_index( g_AHLCtx.policyCtx.pAudioRoles, GString, i );
- if ( strcasecmp(gs.str,in_pAudioRole) == 0 )
- index = i;
+ EndpointInfoT * pEndpointInfo = NULL;
+ GPtrArray * pDeviceArray = NULL;
+ if (in_endpointType == ENDPOINTTYPE_SOURCE){
+ pDeviceArray = in_pRole->pSourceEndpoints;
+ }
+ else {
+ pDeviceArray = in_pRole->pSinkEndpoints;
}
- return index;
+ g_assert_nonnull(pDeviceArray);
+
+ for (int j = 0; j < pDeviceArray->len; j++) {
+ EndpointInfoT * pCurEndpointInfo = g_ptr_array_index(pDeviceArray,j);
+ g_assert_nonnull(pCurEndpointInfo);
+ if (pCurEndpointInfo->endpointID == in_endpointID) {
+ pEndpointInfo = pCurEndpointInfo;
+ break;
+ }
+ }
+
+ return pEndpointInfo;
+}
+
+static int ReplaceEndpointInfoWithRole(endpointID_t in_endpointID, EndpointTypeT in_endpointType, RoleInfoT * in_pRole, EndpointInfoT * in_pNewEndpoint)
+{
+ GPtrArray * pDeviceArray = NULL;
+ if (in_endpointType == ENDPOINTTYPE_SOURCE){
+ pDeviceArray = in_pRole->pSourceEndpoints;
+ }
+ else {
+ pDeviceArray = in_pRole->pSinkEndpoints;
+ }
+ g_assert_nonnull(pDeviceArray);
+
+ for (int j = 0; j < pDeviceArray->len; j++) {
+ EndpointInfoT * pCurEndpointInfo = g_ptr_array_index(pDeviceArray,j);
+ g_assert_nonnull(pCurEndpointInfo);
+ if (pCurEndpointInfo->endpointID == in_endpointID) {
+ g_ptr_array_insert(pDeviceArray,j,in_pNewEndpoint);
+ g_ptr_array_remove_index(pDeviceArray,j+1);
+ TermEndpointInfo(pCurEndpointInfo);
+ // GLib automatically frees item upon array removal
+ return AHL_SUCCESS;
+ }
+ }
+
+ return AHL_FAIL;
}
static EndpointInfoT * GetEndpointInfo(endpointID_t in_endpointID, EndpointTypeT in_endpointType)
{
EndpointInfoT * pEndpointInfo = NULL;
- for (int i = 0; i < g_AHLCtx.policyCtx.iNumberRoles; i++)
+
+ GHashTableIter iter;
+ gpointer key, value;
+ g_hash_table_iter_init (&iter, g_AHLCtx.policyCtx.pRoleInfo);
+ while (pEndpointInfo == NULL && g_hash_table_iter_next (&iter, &key, &value))
{
- GArray * pRoleDeviceArray = NULL;
- if (in_endpointType == ENDPOINTTYPE_SOURCE){
- pRoleDeviceArray = g_ptr_array_index( g_AHLCtx.policyCtx.pSourceEndpoints, i );
+ RoleInfoT * pRoleInfo = (RoleInfoT*)value;
+ pEndpointInfo = GetEndpointInfoWithRole(in_endpointID,in_endpointType,pRoleInfo);
+ }
+
+ return pEndpointInfo;
+}
+
+static StreamInfoT * GetStream(streamID_t in_streamID)
+{
+ if (g_AHLCtx.policyCtx.pStreams == NULL)
+ return NULL;
+
+ return g_hash_table_lookup(g_AHLCtx.policyCtx.pStreams,GINT_TO_POINTER(&in_streamID));
+}
+
+static RoleInfoT * GetRole(char * in_pAudioRoleName)
+{
+ if (g_AHLCtx.policyCtx.pRoleInfo == NULL)
+ return NULL;
+
+ return g_hash_table_lookup(g_AHLCtx.policyCtx.pRoleInfo,in_pAudioRoleName);
+}
+
+static AHLClientCtxT * AllocateClientContext()
+{
+ AHLClientCtxT * pClientCtx = malloc(sizeof(AHLClientCtxT));
+ pClientCtx->pStreamAccessList = g_array_new(FALSE, TRUE, sizeof(streamID_t));
+ return pClientCtx;
+}
+
+static void TerminateClientContext(void * ptr)
+{
+ AHLClientCtxT * pClientCtx = (AHLClientCtxT *) ptr;
+ if (pClientCtx != NULL) {
+
+ // Avoid having policy in bad state if client loses WS connection (e.g. app termination without close stream)
+ // Force close streams in those cases.
+
+ if (pClientCtx->pStreamAccessList != NULL) {
+#ifndef AHL_DISCONNECT_POLICY
+ for (int i = 0; i < pClientCtx->pStreamAccessList->len; i++)
+ {
+ streamID_t streamID = g_array_index(pClientCtx->pStreamAccessList,streamID_t,i);
+ // Call policy to verify whether creating a new audio stream is allowed in current context and possibly take other actions
+ StreamInfoT * pStreamInfo = GetStream(streamID);
+ if (pStreamInfo == NULL) {
+ AFB_ERROR("Specified stream not currently active stream_id -> %d",streamID);
+ return;
+ }
+
+ json_object *pPolicyStreamJ = NULL;
+ int err = PolicyStreamStructToJSON(pStreamInfo, &pPolicyStreamJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ AFB_ERROR("Audio policy violation, Unable to get JSON object for Policy_CloseStream");
+ return;
+ }
+ Policy_CloseStream(pPolicyStreamJ);
+ }
+#endif
+ g_array_free( pClientCtx->pStreamAccessList, TRUE);
+ pClientCtx->pStreamAccessList = NULL;
}
- else{
- pRoleDeviceArray = g_ptr_array_index( g_AHLCtx.policyCtx.pSinkEndpoints, i );
+ free(pClientCtx);
+ }
+}
+
+static int CheckStreamAccessControl(AHLClientCtxT * pClientCtx, streamID_t streamID)
+{
+ int iAccessControl = AHL_ACCESS_CONTROL_DENIED;
+ if (pClientCtx && pClientCtx->pStreamAccessList) {
+ for (int i = 0; i < pClientCtx->pStreamAccessList->len ; i++) {
+ streamID_t iID = g_array_index(pClientCtx->pStreamAccessList,streamID_t,i);
+ if (iID == streamID) {
+ iAccessControl = AHL_ACCESS_CONTROL_GRANTED;
+ }
}
- for (int j = 0; j < pRoleDeviceArray->len; j++) {
- EndpointInfoT * pCurEndpointInfo = &g_array_index(pRoleDeviceArray,EndpointInfoT,j);
- if (pCurEndpointInfo->endpointID == in_endpointID) {
- pEndpointInfo = pCurEndpointInfo;
- break;
+ }
+ return iAccessControl;
+}
+
+static int CreateEvents()
+{
+ int err = 0;
+
+ g_AHLCtx.policyCtx.propertyEvent = afb_daemon_make_event(AHL_ENDPOINT_PROPERTY_EVENT);
+ err = !afb_event_is_valid(g_AHLCtx.policyCtx.propertyEvent);
+ if (err) {
+ AFB_ERROR("Could not create endpoint property change event");
+ return err;
+ }
+
+ g_AHLCtx.policyCtx.volumeEvent = afb_daemon_make_event(AHL_ENDPOINT_VOLUME_EVENT);
+ err = !afb_event_is_valid(g_AHLCtx.policyCtx.volumeEvent);
+ if (err) {
+ AFB_ERROR("Could not create endpoint volume change event");
+ return err;
+ }
+
+ g_AHLCtx.policyCtx.postActionEvent = afb_daemon_make_event(AHL_POST_ACTION_EVENT);
+ err = !afb_event_is_valid(g_AHLCtx.policyCtx.postActionEvent);
+ if (err) {
+ AFB_ERROR("Could not create post action event call event");
+ return err;
+ }
+
+ return err;
+}
+
+static void AhlBindingTerm()
+{
+#ifndef AHL_DISCONNECT_POLICY
+ // Policy termination
+ Policy_Term();
+#endif
+
+ // Roles
+ if (g_AHLCtx.policyCtx.pRoleInfo != NULL) {
+ GHashTableIter iter;
+ gpointer key, value;
+ g_hash_table_iter_init(&iter, g_AHLCtx.policyCtx.pRoleInfo);
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ {
+ RoleInfoT * pRole = (RoleInfoT *)value;
+ if (pRole)
+ {
+ if(pRole->pRoleName) {
+ g_free(pRole->pRoleName);
+ pRole->pRoleName = NULL;
+ }
+ // Actions
+ if (pRole->pActionList) {
+ for (int i = 0; i < pRole->pActionList->len; i++)
+ {
+ g_ptr_array_remove_index( pRole->pActionList, i ); // Free char * is called by GLib
+ }
+ }
+ // Source endpoints
+ if (pRole->pSourceEndpoints) {
+ for (int i = 0; i < pRole->pSourceEndpoints->len; i++)
+ {
+ EndpointInfoT * pEndpoint = g_ptr_array_remove_index( pRole->pSourceEndpoints, i ); // Free endpoint * is called by GLib
+ if (pEndpoint) {
+ TermEndpointInfo(pEndpoint);
+ }
+ }
+ }
+ // Sink endpoints
+ if (pRole->pSinkEndpoints) {
+ for (int i = 0; i < pRole->pSinkEndpoints->len; i++)
+ {
+ EndpointInfoT * pEndpoint = g_ptr_array_remove_index( pRole->pSinkEndpoints, i ); // Free endpoint * is called by GLib
+ if (pEndpoint) {
+ TermEndpointInfo(pEndpoint);
+ }
+ }
+ }
+ free(pRole);
}
}
+ g_hash_table_remove_all(g_AHLCtx.policyCtx.pRoleInfo);
+ g_hash_table_destroy(g_AHLCtx.policyCtx.pRoleInfo);
+ g_AHLCtx.policyCtx.pRoleInfo = NULL;
}
- return pEndpointInfo;
+
+ if (g_AHLCtx.policyCtx.pStreams) {
+ GHashTableIter iter;
+ gpointer key, value;
+ g_hash_table_iter_init (&iter, g_AHLCtx.policyCtx.pStreams);
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ {
+ if (value)
+ free(value);
+ }
+ g_hash_table_remove_all(g_AHLCtx.policyCtx.pStreams);
+ g_hash_table_destroy(g_AHLCtx.policyCtx.pStreams);
+ g_AHLCtx.policyCtx.pStreams = NULL;
+ }
+
+ if (g_AHLCtx.policyCtx.pHALList) {
+ g_ptr_array_free(g_AHLCtx.policyCtx.pHALList,TRUE);
+ g_AHLCtx.policyCtx.pHALList = NULL;
+ }
+
+ AFB_INFO("Audio high-level binding termination success");
}
// Binding initialization
PUBLIC int AhlBindingInit()
{
- int errcount = 0;
int err = 0;
- // This API uses services from low level audio
- err = afb_daemon_require_api_v2("alsacore",1) ;
- if( err != 0 )
+ memset(&g_AHLCtx,0,sizeof(g_AHLCtx));
+
+ // Register exit function
+ atexit(AhlBindingTerm);
+
+ //Create AGL Events
+ err=CreateEvents();
+ if(err)
{
- AFB_ERROR("Audio high level API requires alsacore API to be available");
- return 1;
+ //Error messages already reported inside CreateEvents
+ return err;
}
// Parse high-level binding JSON configuration file (will build device lists)
- errcount += ParseHLBConfig();
+ err = ParseHLBConfig();
+ if(err)
+ {
+ //Error messages already reported inside ParseHLBConfig
+ return err;
+ }
+
+#ifndef AHL_DISCONNECT_POLICY
+ // Policy initialization
+ err = Policy_Init();
+ if(err == AHL_POLICY_REJECT)
+ {
+ //Error messages already reported inside PolicyInit
+ return err;
+ }
- // Initialize list of active streams
- g_AHLCtx.policyCtx.pActiveStreams = g_array_new(FALSE,TRUE,sizeof(StreamInfoT));
+ // for all audio Roles
+ GHashTableIter iter;
+ gpointer key, value;
+
+ g_hash_table_iter_init (&iter, g_AHLCtx.policyCtx.pRoleInfo);
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ {
+ RoleInfoT * pRoleInfo = (RoleInfoT*)value;
+ if (pRoleInfo->pSourceEndpoints){
+ // for all source endpoints
+ for (int j = 0; j < pRoleInfo->pSourceEndpoints->len; j++) {
+ EndpointInfoT * pCurEndpointInfo = g_ptr_array_index(pRoleInfo->pSourceEndpoints,j);
+ g_assert_nonnull(pCurEndpointInfo);
+ json_object *pPolicyEndpointJ = NULL;
+ err = PolicyEndpointStructToJSON(pCurEndpointInfo, &pPolicyEndpointJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ AFB_ERROR("Unable to Create Endpoint Json object error:%s ",wrap_json_get_error_string(err));
+ return err;
+ }
+ else
+ {
+ err = Policy_Endpoint_Init(pPolicyEndpointJ);
+ if (err == AHL_POLICY_REJECT) {
+ AFB_ERROR("Policy endpoint properties initalization failed for endpoint_id :%d type:%d",pCurEndpointInfo->endpointID, pCurEndpointInfo->type);
+ }
+ //free pPolicyEndpointJ
+ json_object_put(pPolicyEndpointJ);
+ }
+ }
+ }
+ if (pRoleInfo->pSinkEndpoints){
+ // for all sink endpoints
+ for (int j = 0; j < pRoleInfo->pSinkEndpoints->len; j++) {
+ EndpointInfoT * pCurEndpointInfo = g_ptr_array_index(pRoleInfo->pSinkEndpoints,j);
+ g_assert_nonnull(pCurEndpointInfo);
+ json_object *pPolicyEndpointJ = NULL;
+ err = PolicyEndpointStructToJSON(pCurEndpointInfo, &pPolicyEndpointJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ AFB_ERROR("Unable to Create Endpoint Json object error:%s ",wrap_json_get_error_string(err));
+ return err;
+ }
+ else
+ {
+ err = Policy_Endpoint_Init(pPolicyEndpointJ);
+ if (err== AHL_POLICY_REJECT) {
+ AFB_ERROR("Policy endpoint properties initalization failed for endpoint_id :%d type:%d",pCurEndpointInfo->endpointID, pCurEndpointInfo->type);
+ }
+ //free pPolicyEndpointJ
+ json_object_put(pPolicyEndpointJ);
+ }
- // TODO: Register for device changes ALSA low level audio service
+ }
+ }
+ }
+#endif
- // TODO: Perform other binding initialization tasks (e.g. broadcast service ready event?)
+ // Initialize list of active streams
+ g_AHLCtx.policyCtx.pStreams = g_hash_table_new(g_int_hash, g_int_equal);
+ if(g_AHLCtx.policyCtx.pStreams == NULL)
+ {
+ AFB_ERROR("Unable to create Active Stream List");
+ return err;
+ }
- // TODO: Use AGL persistence framework to retrieve and set inital state/volumes/properties
+ // TODO: Use AGL persistence framework to retrieve and set initial volumes/properties
- AFB_DEBUG("Audio high-level Binding success errcount=%d", errcount);
- return errcount;
+ AFB_DEBUG("Audio high-level Binding success");
+ return err;
}
-// TODO: AhlBindingTerm()?
-// // TODO: Use AGL persistence framework to retrieve and set inital state/volumes
+PUBLIC void AhlOnEvent(const char *evtname, json_object *eventJ)
+{
+ AFB_DEBUG("AHL received event %s", evtname);
-// TODO: OnEventFunction
-// if ALSA device availability change -> update source / sink list
-// Call policy to attempt to assign role based on information (e.g. device name)
-// Policy should also determine priority (insert device in right spot in the list)
+ // TODO: Handle event from the policy to update internal information (currently not possible since within the same binding)
+
+#ifndef AHL_DISCONNECT_POLICY
+ // Temp: currently forward to policy to handle events (they will be received directly when disconnected into separate binding)
+ Policy_OnEvent(evtname, eventJ);
+#endif
+}
PUBLIC void audiohlapi_get_sources(struct afb_req req)
{
- json_object *sourcesJ = NULL;
- json_object *sourceJ = NULL;
+ json_object *devicesJ = NULL;
+ json_object *deviceJ = NULL;
json_object *queryJ = NULL;
char * audioRole = NULL;
-
+
queryJ = afb_req_json(req);
int err = wrap_json_unpack(queryJ, "{s:s}", "audio_role", &audioRole);
if (err) {
@@ -156,40 +564,41 @@ PUBLIC void audiohlapi_get_sources(struct afb_req req)
return;
}
- sourcesJ = json_object_new_array();
-
AFB_DEBUG("Filtering devices according to specified audio role=%s", audioRole);
- // Check that the role requested exists in current configuration
- int roleIndex = FindRoleIndex(audioRole);
- if ( roleIndex == -1)
+
+ RoleInfoT * pRole = GetRole(audioRole);
+ if ( pRole == NULL )
{
afb_req_fail_f(req, "Invalid arguments", "Requested audio role does not exist in current configuration -> %s", json_object_get_string(queryJ));
return;
}
- // List device only for current audio role and device type (pick the role device list)
- int iRoleIndex = FindRoleIndex(audioRole);
- if (iRoleIndex >= 0)
+ else
{
- GArray * pRoleSourceDeviceArray = g_ptr_array_index( g_AHLCtx.policyCtx.pSourceEndpoints, iRoleIndex );
- int iNumberDevices = pRoleSourceDeviceArray->len;
- for ( int j = 0 ; j < iNumberDevices; j++)
- {
- EndpointInfoT sourceInfo = g_array_index(pRoleSourceDeviceArray,EndpointInfoT,j);
- EndpointInfoStructToJSON(sourceJ, sourceInfo);
- json_object_array_add(sourcesJ, sourceJ);
+ devicesJ = json_object_new_array();
+ GPtrArray * pDeviceArray = pRole->pSourceEndpoints;
+ if (pDeviceArray) {
+ int iNumberDevices = pDeviceArray->len;
+ for ( int j = 0 ; j < iNumberDevices; j++)
+ {
+ EndpointInfoT * pEndpointInfo = g_ptr_array_index(pDeviceArray,j);
+ if (pEndpointInfo) {
+ EndpointInfoStructToJSON(&deviceJ, pEndpointInfo);
+ json_object_array_add(devicesJ, deviceJ);
+ }
+ }
}
}
- afb_req_success(req, sourcesJ, "List of sources");
+ afb_req_success(req, devicesJ, "List of sources");
}
PUBLIC void audiohlapi_get_sinks(struct afb_req req)
{
- json_object *sinksJ = NULL;
- json_object *sinkJ = NULL;
+ json_object *devicesJ = NULL;
+ json_object *deviceJ = NULL;
json_object *queryJ = NULL;
char * audioRole = NULL;
-
+
queryJ = afb_req_json(req);
int err = wrap_json_unpack(queryJ, "{s:s}", "audio_role", &audioRole);
if (err) {
@@ -197,109 +606,163 @@ PUBLIC void audiohlapi_get_sinks(struct afb_req req)
return;
}
- sinksJ = json_object_new_array();
-
AFB_DEBUG("Filtering devices according to specified audio role=%s", audioRole);
- // Check that the role requested exists in current configuration
- int roleIndex = FindRoleIndex(audioRole);
- if ( roleIndex == -1)
+
+ RoleInfoT * pRole = GetRole(audioRole);
+ if ( pRole == NULL )
{
afb_req_fail_f(req, "Invalid arguments", "Requested audio role does not exist in current configuration -> %s", json_object_get_string(queryJ));
return;
}
- // List device only for current audio role and device type (pick the role device list)
- int iRoleIndex = FindRoleIndex(audioRole);
- if (iRoleIndex >= 0)
+ else
{
- GArray * pRoleSinkDeviceArray = g_ptr_array_index( g_AHLCtx.policyCtx.pSinkEndpoints, iRoleIndex );
- int iNumberDevices = pRoleSinkDeviceArray->len;
- for ( int j = 0 ; j < iNumberDevices; j++)
- {
- EndpointInfoT sinkInfo = g_array_index(pRoleSinkDeviceArray,EndpointInfoT,j);
- EndpointInfoStructToJSON(sinkJ, sinkInfo);
- json_object_array_add(sinksJ, sinkJ);
+ devicesJ = json_object_new_array();
+ GPtrArray * pDeviceArray = pRole->pSinkEndpoints;
+ if (pDeviceArray) {
+ int iNumberDevices = pDeviceArray->len;
+ for ( int j = 0 ; j < iNumberDevices; j++)
+ {
+ EndpointInfoT * pEndpointInfo = g_ptr_array_index(pDeviceArray,j);
+ EndpointInfoStructToJSON(&deviceJ, pEndpointInfo);
+ json_object_array_add(devicesJ, deviceJ);
+ }
}
}
- afb_req_success(req, sinksJ, "List of sinks");
+ afb_req_success(req, devicesJ, "List of sinks");
}
PUBLIC void audiohlapi_stream_open(struct afb_req req)
{
json_object *streamInfoJ = NULL;
- StreamInfoT streamInfo;
+ StreamInfoT * pStreamInfo = NULL;
json_object *queryJ = NULL;
char * audioRole = NULL;
- EndpointTypeT endpointType;
- endpointID_t endpointID = UNDEFINED_ID;
- int policyAllowed = 0;
- EndpointInfoT endpointInfo;
+ char * endpointTypeStr = NULL;
+ EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ EndpointInfoT * pEndpointInfo = NULL;
+ EndpointSelectionModeT endpointSelMode = ENDPOINTSELMODEMAXVALUE;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:s,s:i,s?i}", "audio_role", &audioRole, "endpoint_type", &endpointType, "endpoint_id", &endpointID);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:s,s?i}", "audio_role", &audioRole, "endpoint_type", &endpointTypeStr, "endpoint_id", &endpointID);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = audio_role:%s endpoint_type:%d endpoint_id:%d", audioRole,endpointType,endpointID);
+ AFB_DEBUG("Parsed input arguments = audio_role:%s endpoint_type:%s endpoint_id:%d", audioRole,endpointTypeStr,endpointID);
+ endpointType = EndpointTypeToEnum(endpointTypeStr);
- int iRoleIndex = FindRoleIndex(audioRole);
- if (iRoleIndex < 0) {
+ // Check if there is already an existing context for this client
+ AHLClientCtxT * pClientCtx = afb_req_context_get(req); // Retrieve client-specific data structure
+ if (pClientCtx == NULL)
+ {
+ pClientCtx = AllocateClientContext();
+ afb_req_context_set(req, pClientCtx, TerminateClientContext);
+ }
+
+ RoleInfoT * pRole = GetRole(audioRole);
+ if ( pRole == NULL )
+ {
afb_req_fail_f(req, "Invalid audio role", "Audio role was not found in configuration -> %s",audioRole);
return;
}
- GArray * pRoleDeviceArray = NULL;
+ GPtrArray * pDeviceArray = NULL;
if (endpointType == ENDPOINTTYPE_SOURCE){
- pRoleDeviceArray = g_ptr_array_index( g_AHLCtx.policyCtx.pSourceEndpoints, iRoleIndex );
+ pDeviceArray = pRole->pSourceEndpoints;
}
else{
- pRoleDeviceArray = g_ptr_array_index( g_AHLCtx.policyCtx.pSinkEndpoints, iRoleIndex );
+ pDeviceArray = pRole->pSinkEndpoints;
}
- if (pRoleDeviceArray->len == 0) {
- afb_req_fail_f(req, "No available devices", "No available devices for role:%s and device type:%d",audioRole,endpointType);
+ if (pDeviceArray == NULL || pDeviceArray->len == 0) {
+ afb_req_fail_f(req, "No available devices", "No available devices for role:%s and device type:%s",audioRole,endpointTypeStr);
return;
}
- if (endpointID == UNDEFINED_ID)
+ if (endpointID == AHL_UNDEFINED)
{
// Assign a device based on configuration priority (first in the list for requested role and endpoint type)
- endpointInfo = g_array_index(pRoleDeviceArray,EndpointInfoT,0);
+ pEndpointInfo = g_ptr_array_index(pDeviceArray,0);
+ endpointSelMode = ENDPOINTSELMODE_AUTO;
+
}
else{
+ endpointSelMode = ENDPOINTSELMODE_MANUAL;
// Find specified endpoint ID in list of devices
- int iNumberDevices = pRoleDeviceArray->len;
- int iEndpointFound = 0;
+ int iNumberDevices = pDeviceArray->len;
for ( int j = 0 ; j < iNumberDevices; j++)
{
- endpointInfo = g_array_index(pRoleDeviceArray,EndpointInfoT,j);
- if (endpointInfo.endpointID == endpointID) {
- iEndpointFound = 1;
+ pEndpointInfo = g_ptr_array_index(pDeviceArray,j);
+ if (pEndpointInfo && pEndpointInfo->endpointID == endpointID) {
break;
}
+ pEndpointInfo = NULL;
}
- if (iEndpointFound == 0) {
- afb_req_fail_f(req, "Endpoint not available", "Specified endpoint not available for role:%s and device type:%d endpoint id %d",audioRole,endpointType,endpointID);
- return;
- }
+
}
+ if (pEndpointInfo == NULL) {
+ afb_req_fail_f(req, "Endpoint not available", "Specified endpoint not available for role:%s and device type:%d endpoint id %d",audioRole,endpointType,endpointID);
+ return;
+ }
+
+ pStreamInfo = (StreamInfoT*) malloc(sizeof(StreamInfoT));
+ memset(pStreamInfo,0,sizeof(StreamInfoT));
+
+ // Create stream
+ pStreamInfo->streamID = CreateNewStreamID(); // create new ID
+ pStreamInfo->streamState = STREAM_STATE_IDLE;
+ pStreamInfo->streamMute = STREAM_UNMUTED;
+ pStreamInfo->pEndpointInfo = pEndpointInfo;
+ pStreamInfo->endpointSelMode = endpointSelMode;
+ // Directly from role config for now, but could be programmatically overriden in the future
+ pStreamInfo->pRoleName = pRole->pRoleName;
+ pStreamInfo->iPriority = pRole->iPriority;
+ pStreamInfo->eInterruptBehavior = pRole->eInterruptBehavior;
+
+#ifndef AHL_DISCONNECT_POLICY
// Call policy to verify whether creating a new audio stream is allowed in current context and possibly take other actions
- policyAllowed = Policy_OpenStream(audioRole, endpointType, endpointID);
- if (policyAllowed == AUDIOHL_POLICY_REJECT)
+ json_object *pPolicyStreamJ = NULL;
+ err = PolicyStreamStructToJSON(pStreamInfo, &pPolicyStreamJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ afb_req_fail(req, "Audio policy violation", "Unable to get JSON object for Policy_OpenStream");
+ return;
+ }
+
+ int policyAllowed = Policy_OpenStream(pPolicyStreamJ);
+ if (policyAllowed == AHL_POLICY_REJECT)
{
afb_req_fail(req, "Audio policy violation", "Open stream not allowed in current context");
return;
+ }
+#endif
+
+ char streamEventName[128];
+ snprintf(streamEventName,128,"ahl_streamstate_%d",pStreamInfo->streamID);
+
+ pStreamInfo->streamStateEvent = afb_daemon_make_event(streamEventName);
+ err = !afb_event_is_valid(pStreamInfo->streamStateEvent);
+ if (err) {
+ afb_req_fail(req, "Stream event creation failure", "Could not create stream specific state change event");
+ return;
}
- // Create stream
- streamInfo.streamID = CreateNewStreamID(); // create new ID
- streamInfo.endpointInfo = endpointInfo;
+ err = afb_req_subscribe(req,pStreamInfo->streamStateEvent);
+ if (err) {
+ afb_req_fail(req, "Stream event subscription failure", "Could not subscribe to stream specific state change event");
+ return;
+ }
+
+ // Add to client context stream ID access rights
+ g_array_append_val(pClientCtx->pStreamAccessList, pStreamInfo->streamID);
// Push stream on active stream list
- g_array_append_val( g_AHLCtx.policyCtx.pActiveStreams, streamInfo );
+ if (g_AHLCtx.policyCtx.pStreams)
+ g_hash_table_insert( g_AHLCtx.policyCtx.pStreams, GINT_TO_POINTER(&pStreamInfo->streamID), pStreamInfo );
- StreamInfoStructToJSON(&streamInfoJ,streamInfo);
+ StreamInfoStructToJSON(&streamInfoJ,pStreamInfo);
afb_req_success(req, streamInfoJ, "Stream info structure");
}
@@ -307,7 +770,7 @@ PUBLIC void audiohlapi_stream_open(struct afb_req req)
PUBLIC void audiohlapi_stream_close(struct afb_req req)
{
json_object *queryJ = NULL;
- streamID_t streamID = UNDEFINED_ID;
+ streamID_t streamID = AHL_UNDEFINED;
queryJ = afb_req_json(req);
int err = wrap_json_unpack(queryJ, "{s:i}", "stream_id", &streamID);
@@ -317,106 +780,252 @@ PUBLIC void audiohlapi_stream_close(struct afb_req req)
}
AFB_DEBUG("Parsed input arguments = stream_id:%d", streamID);
- // TODO: Validate that the application ID from which the stream close is coming is the one that opened it, otherwise fail and do nothing
+
+ StreamInfoT * pStreamInfo = GetStream(streamID);
+ if (pStreamInfo == NULL) {
+ afb_req_fail_f(req, "Stream not found", "Specified stream not currently active stream_id -> %d",streamID);
+ return;
+ }
- // Remove from active stream list (if present)
- int iNumActiveStreams = g_AHLCtx.policyCtx.pActiveStreams->len;
- int iStreamFound = 0;
- for ( int i = 0; i < iNumActiveStreams ; i++ ) {
- StreamInfoT streamInfo = g_array_index(g_AHLCtx.policyCtx.pActiveStreams,StreamInfoT,i);
- if (streamInfo.streamID == streamID){
- g_array_remove_index(g_AHLCtx.policyCtx.pActiveStreams,i);
- iStreamFound = 1;
- break;
- }
+ // Check if there is already an existing context for this client
+ AHLClientCtxT * pClientCtx = afb_req_context_get(req); // Retrieve client-specific data structure
+ if (pClientCtx == NULL)
+ {
+ afb_req_fail(req, "Bad state", "No client context associated with the request (is there an opened stream by this client?)");
+ return;
}
- if (iStreamFound == 0) {
- afb_req_fail_f(req, "Stream not found", "Specified stream not currently active stream_id -> %d",streamID);
+ // Verify that this client can control the stream
+ int iStreamAccessControl = CheckStreamAccessControl( pClientCtx, streamID );
+ if (iStreamAccessControl == AHL_ACCESS_CONTROL_DENIED)
+ {
+ afb_req_fail(req, "Access control denied", "Close stream not allowed in current client context");
return;
}
+#ifndef AHL_DISCONNECT_POLICY
+ json_object *pPolicyStreamJ = NULL;
+ err = PolicyStreamStructToJSON(pStreamInfo, &pPolicyStreamJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ afb_req_fail(req, "Audio policy violation", "Unable to get JSON object for Policy_CloseStream");
+ return;
+ }
+ // Call policy to verify whether creating a new audio stream is allowed in current context and possibly take other actions
+ int policyAllowed = Policy_CloseStream(pPolicyStreamJ);
+ if (policyAllowed == AHL_POLICY_REJECT)
+ {
+ afb_req_fail(req, "Audio policy violation", "Close stream not allowed in current context");
+ return;
+ }
+#endif
+
+ // Unsubscribe client from stream events
+ char streamEventName[128];
+ snprintf(streamEventName,128,"ahl_streamstate_%d",streamID);
+ int iValid = afb_event_is_valid(pStreamInfo->streamStateEvent);
+ if (iValid) {
+ err = afb_req_unsubscribe(req,pStreamInfo->streamStateEvent);
+ if (err) {
+ afb_req_fail(req, "Stream event subscription failure", "Could not unsubscribe to stream specific state change event");
+ return;
+ }
+ }
+
+ // Remove from stream list (if present)
+ if (g_AHLCtx.policyCtx.pStreams)
+ g_hash_table_remove(g_AHLCtx.policyCtx.pStreams,GINT_TO_POINTER(&pStreamInfo->streamID));
+ free(pStreamInfo);
+ pStreamInfo = NULL;
+
+ // Find index for cases where there are multiple streams per client
+ // Remove from client context stream ID and endpoint ID access rights
+ if (pClientCtx->pStreamAccessList) {
+ for (int i = 0; i < pClientCtx->pStreamAccessList->len ; i++) {
+ streamID_t iID = g_array_index(pClientCtx->pStreamAccessList,streamID_t,i);
+ if (iID == streamID) {
+ g_array_remove_index(pClientCtx->pStreamAccessList, i);
+ }
+ }
+
+ if (pClientCtx->pStreamAccessList->len == 0) {
+ // If no more streams/endpoints owner, clear session
+ afb_req_context_clear(req);
+ }
+ }
+
afb_req_success(req, NULL, "Stream close completed");
}
-// Endpoints
-PUBLIC void audiohlapi_set_volume(struct afb_req req)
-{
+ PUBLIC void audiohlapi_set_stream_state(struct afb_req req)
+ {
json_object *queryJ = NULL;
- endpointID_t endpointID = UNDEFINED_ID;
- EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
- char * volumeStr = NULL;
- int rampTimeMS = 0;
- int policyAllowed = 0;
+ streamID_t streamID = AHL_UNDEFINED;
+ char * streamStateStr = NULL;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:i,s:i,s:s,s?i}", "endpoint_type", &endpointType,"endpoint_id",&endpointID,"volume",&volumeStr,"ramp_time_ms",&rampTimeMS);
+ int err = wrap_json_unpack(queryJ, "{s:i,s:s}", "stream_id", &streamID,"state",&streamStateStr);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = endpoint_type:%d endpoint_id:%d volume:%s ramp_time_ms: %d", endpointType,endpointID,volumeStr,rampTimeMS);
-
- // TODO: Parse volume string to support increment/absolute/percent notation
- int vol = atoi(volumeStr);
+ AFB_DEBUG("Parsed input arguments = stream_id:%d, state:%s", streamID,streamStateStr);
+
+ StreamInfoT * pStreamInfo = GetStream(streamID);
+ if (pStreamInfo == NULL) {
+ afb_req_fail_f(req, "Stream not found", "Specified stream not found stream_id -> %d",streamID);
+ return;
+ }
- policyAllowed = Policy_SetVolume(endpointType, endpointID, volumeStr, rampTimeMS); // TODO: Potentially retrieve modified value by policy (e.g. volume limit)
- if (!policyAllowed)
+ // Check if there is already an existing context for this client
+ AHLClientCtxT * pClientCtx = afb_req_context_get(req); // Retrieve client-specific data structure
+ if (pClientCtx == NULL)
{
- afb_req_fail(req, "Audio policy violation", "Set volume not allowed in current context");
+ afb_req_fail(req, "Bad state", "No client context associated with the request (is there an opened stream by this client?)");
return;
}
- // TODO: Cache during device enumeration for efficiency
- EndpointInfoT * pEndpointInfo = GetEndpointInfo(endpointID,endpointType);
- if (pEndpointInfo == NULL)
+ // Verify that this client can control the stream
+ int iStreamAccessControl = CheckStreamAccessControl( pClientCtx, streamID );
+ if (iStreamAccessControl == AHL_ACCESS_CONTROL_DENIED)
{
- afb_req_fail_f(req, "Endpoint not found", "Endpoint information not found for id:%d type%d",endpointID,endpointType);
+ afb_req_fail(req, "Access control denied", "Set stream state not allowed in current client context");
return;
}
- // Using audio role available from endpoint to target the right HAL control (build string based on convention)
- char halControlName[AUDIOHL_MAXHALCONTROLNAME_LENGTH];
- strncpy(halControlName,pEndpointInfo->audioRole,AUDIOHL_MAXHALCONTROLNAME_LENGTH);
- strcat(halControlName,"_Ramp"); // Or _Vol for direct control (no ramping)
+ StreamStateT streamState = StreamStateToEnum(streamStateStr);
+#ifndef AHL_DISCONNECT_POLICY
+ json_object *pPolicyStreamJ = NULL;
+ err = PolicyStreamStructToJSON(pStreamInfo, &pPolicyStreamJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ afb_req_fail(req, "Audio policy violation", "Unable to get JSON object for Policy_SetStreamState");
+ return;
+ }
- // Set endpoint volume using HAL services (leveraging ramps etc.)
- json_object *j_response, *j_query = NULL;
-
- // Package query
- err = wrap_json_pack(&j_query,"{s:s,s:i}","label",halControlName, "val",vol);
+ json_object *paramJ= json_object_new_int(streamState);
+ json_object_object_add(pPolicyStreamJ, "arg_stream_state", paramJ);
+
+ int policyAllowed = Policy_SetStreamState(pPolicyStreamJ);
+ if (policyAllowed == AHL_POLICY_REJECT)
+ {
+ afb_req_fail(req, "Audio policy violation", "Change stream state not allowed in current context");
+ return;
+ }
+#else
+ // Simulate that policy returns target state (accepted)
+ pStreamInfo->streamState = streamState;
+#endif
+
+ afb_req_success(req, NULL, "Set stream state");
+ }
+
+ PUBLIC void audiohlapi_set_stream_mute(struct afb_req req)
+ {
+ json_object *queryJ = NULL;
+ streamID_t streamID = AHL_UNDEFINED;
+ char * pMuteStr = NULL;
+
+ queryJ = afb_req_json(req);
+ int err = wrap_json_unpack(queryJ, "{s:i,s:s}", "stream_id", &streamID,"mute",&pMuteStr);
if (err) {
- afb_req_fail_f(req, "Invalid query for HAL ctlset", "Invalid query for HAL ctlset: %s",json_object_to_json_string(j_query));
+ afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
+ return;
+ }
+ AFB_DEBUG("Parsed input arguments = stream_id:%d, mute:%s", streamID,pMuteStr);
+
+ StreamInfoT * pStreamInfo = GetStream(streamID);
+ if (pStreamInfo == NULL) {
+ afb_req_fail_f(req, "Stream not found", "Specified stream not currently active stream_id -> %d",streamID);
+ return;
+ }
+
+ // Check if there is already an existing context for this client
+ AHLClientCtxT * pClientCtx = afb_req_context_get(req); // Retrieve client-specific data structure
+ if (pClientCtx == NULL)
+ {
+ afb_req_fail(req, "Bad state", "No client context associated with the request (is there an opened stream by this client?)");
+ return;
+ }
+
+ // Verify that this client can control the stream
+ int iStreamAccessControl = CheckStreamAccessControl( pClientCtx, streamID );
+ if (iStreamAccessControl == AHL_ACCESS_CONTROL_DENIED)
+ {
+ afb_req_fail(req, "Access control denied", "Set stream mute state not allowed in current client context");
+ return;
+ }
+
+
+ StreamMuteT muteState = StreamMuteToEnum(pMuteStr);
+#ifndef AHL_DISCONNECT_POLICY
+ json_object *pPolicyStreamJ = NULL;
+ err = PolicyStreamStructToJSON(pStreamInfo, &pPolicyStreamJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ afb_req_fail(req, "Audio policy violation", "Unable to get JSON object for Policy_SetStreamMute");
+ return;
+ }
+
+ json_object *paramJ= json_object_new_int(muteState);
+ json_object_object_add(pPolicyStreamJ, "mute_state", paramJ);
+
+ int policyAllowed = Policy_SetStreamMute(pPolicyStreamJ);
+ if (policyAllowed == AHL_POLICY_REJECT)
+ {
+ afb_req_fail(req, "Audio policy violation", "Mute stream not allowed in current context");
return;
}
+#else
+ // Simulate that policy returns target state (accepted)
+ pStreamInfo->streamMute = muteState;
+#endif
+
+ afb_req_success(req, NULL, "Set stream mute completed");
+ }
- // Set the volume using the HAL
- err = afb_service_call_sync(pEndpointInfo->halAPIName, "ctlset", j_query, &j_response);
+ PUBLIC void audiohlapi_get_stream_info(struct afb_req req)
+ {
+ json_object *queryJ = NULL;
+ streamID_t streamID = AHL_UNDEFINED;
+ json_object * streamInfoJ = NULL;
+
+ queryJ = afb_req_json(req);
+ int err = wrap_json_unpack(queryJ, "{s:i}", "stream_id", &streamID);
if (err) {
- afb_req_fail_f(req, "HAL ctlset failure", "Could not ctlset on HAL: %s",pEndpointInfo->halAPIName);
+ afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("HAL ctlset response=%s", json_object_to_json_string(j_response));
-
- afb_req_success(req, NULL, "Set volume completed");
-}
+ AFB_DEBUG("Parsed input arguments = stream_id:%d", streamID);
-PUBLIC void audiohlapi_get_volume(struct afb_req req)
+ StreamInfoT * pStreamInfo = GetStream(streamID);
+ if (pStreamInfo == NULL) {
+ afb_req_fail_f(req, "Stream not found", "Specified stream not currently active stream_id -> %d",streamID);
+ return;
+ }
+
+ StreamInfoStructToJSON(&streamInfoJ,pStreamInfo);
+
+ afb_req_success(req, streamInfoJ, "Get stream info completed");
+ }
+
+PUBLIC void audiohlapi_set_volume(struct afb_req req)
{
json_object *queryJ = NULL;
- endpointID_t endpointID = UNDEFINED_ID;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ char * pEndpointTypeStr = NULL;
EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
- json_object *volumeJ;
+ char * volumeStr = NULL;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:i,s:i}", "endpoint_type", &endpointType,"endpoint_id",&endpointID);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:i,s:s}", "endpoint_type", &pEndpointTypeStr,"endpoint_id",&endpointID,"volume",&volumeStr);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = endpoint_type:%d endpoint_id:%d", endpointType,endpointID);
+ AFB_DEBUG("Parsed input arguments = endpoint_type:%s endpoint_id:%d volume:%s", pEndpointTypeStr,endpointID,volumeStr);
+ endpointType = EndpointTypeToEnum(pEndpointTypeStr);
- // TODO: Cache during device enumeration for efficiency
EndpointInfoT * pEndpointInfo = GetEndpointInfo(endpointID,endpointType);
if (pEndpointInfo == NULL)
{
@@ -424,221 +1033,545 @@ PUBLIC void audiohlapi_get_volume(struct afb_req req)
return;
}
- // Using audio role available from endpoint to target the right HAL control (build string based on convention)
- char halControlName[AUDIOHL_MAXHALCONTROLNAME_LENGTH];
- strncpy(halControlName,pEndpointInfo->audioRole,AUDIOHL_MAXHALCONTROLNAME_LENGTH);
- strcat(halControlName,"_Vol"); // Use current value, not ramp target
+#ifndef AHL_DISCONNECT_POLICY
+ json_object *pPolicyEndpointJ = NULL;
+ err = PolicyEndpointStructToJSON(pEndpointInfo, &pPolicyEndpointJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ afb_req_fail(req, "Audio policy violation", "Unable to get JSON object for Policy_SetVolume");
+ return;
+ }
- // Set endpoint volume using HAL services (leveraging ramps etc.)
- json_object *j_response, *j_query = NULL;
-
- // Package query
- err = wrap_json_pack(&j_query,"{s:s}","label",halControlName);
- if (err) {
- afb_req_fail_f(req, "Invalid query for HAL ctlget", "Invalid query for HAL ctlget: %s",json_object_to_json_string(j_query));
+ json_object *paramJ= json_object_new_string(volumeStr);
+ json_object_object_add(pPolicyEndpointJ, "arg_volume", paramJ);
+
+ int policyAllowed = Policy_SetVolume(pPolicyEndpointJ);
+ if (!policyAllowed)
+ {
+ afb_req_fail(req, "Audio policy violation", "Set volume not allowed in current context");
return;
}
+#else
+ // Simulate that policy returns target state (accepted)
+ pEndpointInfo->iVolume = atoi(volumeStr);
+#endif
+
+ afb_req_success(req, NULL, "Set volume completed");
+}
- // Set the volume using the HAL
- err = afb_service_call_sync(pEndpointInfo->halAPIName, "ctlget", j_query, &j_response);
+PUBLIC void audiohlapi_get_volume(struct afb_req req)
+{
+ json_object *queryJ = NULL;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ char * pEndpointTypeStr = NULL;
+ EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
+ json_object * volumeJ = NULL;
+
+ queryJ = afb_req_json(req);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:i}", "endpoint_type", &pEndpointTypeStr,"endpoint_id",&endpointID);
if (err) {
- afb_req_fail_f(req, "HAL ctlget failure", "Could not ctlget on HAL: %s",pEndpointInfo->halAPIName);
+ afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_INFO("HAL ctlget response=%s", json_object_to_json_string(j_response));
+ AFB_DEBUG("Parsed input arguments = endpoint_type:%s endpoint_id:%d", pEndpointTypeStr,endpointID);
+ endpointType = EndpointTypeToEnum(pEndpointTypeStr);
- // Parse response
- json_object * jRespObj = NULL;
- json_object_object_get_ex(j_response, "response", &jRespObj);
- json_object * jVal = NULL;
- json_object_object_get_ex(jRespObj, "val", &jVal);
- int val1 = 0, val2 = 0; // Why 2 values?
- err = wrap_json_unpack(jVal, "[ii]", &val1, &val2);
- if (err) {
- afb_req_fail_f(req,"Volume retrieve failed", "Could not retrieve volume value -> %s", json_object_get_string(jVal));
+ EndpointInfoT * pEndpointInfo = GetEndpointInfo(endpointID,endpointType);
+ if (pEndpointInfo == NULL)
+ {
+ afb_req_fail_f(req, "Endpoint not found", "Endpoint information not found for id:%d type%d",endpointID,endpointType);
return;
}
- volumeJ = json_object_new_double((double)val1);
+ volumeJ = json_object_new_int(pEndpointInfo->iVolume);
afb_req_success(req, volumeJ, "Retrieved volume value");
}
-PUBLIC void audiohlapi_set_property(struct afb_req req)
+// Properties
+PUBLIC void audiohlapi_get_endpoint_info(struct afb_req req)
{
json_object *queryJ = NULL;
- endpointID_t endpointID = UNDEFINED_ID;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ char * pEndpointTypeStr = NULL;
EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
- char * propertyName = NULL;
- char * propValueStr = NULL;
- int rampTimeMS = 0;
- int policyAllowed = 0;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:i,s:i,s:s,s:s,s?i}", "endpoint_type", &endpointType,"endpoint_id",&endpointID,"property_name",&propertyName,"value",&propValueStr,"ramp_time_ms",&rampTimeMS);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:i}", "endpoint_type", &pEndpointTypeStr,"endpoint_id",&endpointID);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = endpoint_type:%d endpoint_id:%d property_name:%s value:%s ramp_time_ms:%d", endpointType,endpointID,propertyName,propValueStr,rampTimeMS);
-
- // TODO: Parse property value string to support increment/absolute/percent notation
+ AFB_DEBUG("Parsed input arguments = endpoint_type:%s endpoint_id:%d", pEndpointTypeStr,endpointID);
+ endpointType = EndpointTypeToEnum(pEndpointTypeStr);
- // Call policy to allow custom policy actions in current context
- policyAllowed = Policy_SetProperty(endpointType, endpointID, propertyName, propValueStr, rampTimeMS); // TODO: Potentially retrieve modified value by policy (e.g. parameter limit)
- if (!policyAllowed)
+ EndpointInfoT * pEndpointInfo = GetEndpointInfo(endpointID,endpointType);
+ if (pEndpointInfo == NULL)
{
- afb_req_fail(req, "Audio policy violation", "Set endpoint property not allowed in current context");
+ afb_req_fail_f(req, "Endpoint not found", "Endpoint information not found for id:%d type%d",endpointID,endpointType);
return;
}
- // TODO: Set endpoint property (dispatch on right service target)
- // Property targets (Internal,Wwise,Fiberdyne) (e.g. Wwise.ParamX, Fiberdyne.ParamY, Internal.ParamZ)
- // Cache value in property list
- // TBD
+ json_object *endpointInfoJ = NULL;
+ EndpointInfoStructToJSON(&endpointInfoJ,pEndpointInfo);
- afb_req_success(req, NULL, "Set property completed");
+ afb_req_success(req, endpointInfoJ, "Retrieved endpoint information and properties");
}
-PUBLIC void audiohlapi_get_property(struct afb_req req)
+PUBLIC void audiohlapi_set_property(struct afb_req req)
{
json_object *queryJ = NULL;
- endpointID_t endpointID = UNDEFINED_ID;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ char * pEndpointTypeStr = NULL;
EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
char * propertyName = NULL;
- json_object *propertyValJ;
- double value = 0.0;
+ json_object * propValueJ = NULL;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:i,s?i,s:s}", "endpoint_type", &endpointType,"endpoint_id",&endpointID,"property_name",&propertyName);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:i,s:s,s:o}", "endpoint_type", &pEndpointTypeStr,"endpoint_id",&endpointID,"property_name",&propertyName,"value",&propValueJ);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = endpoint_type:%d endpoint_id:%d property_name:%s", endpointType,endpointID,propertyName);
+ AFB_DEBUG("Parsed input arguments = endpoint_type:%s endpoint_id:%d property_name:%s", pEndpointTypeStr,endpointID,propertyName);
+ endpointType = EndpointTypeToEnum(pEndpointTypeStr);
- // TODO: Retrieve cached property value
+ EndpointInfoT * pEndpointInfo = GetEndpointInfo(endpointID,endpointType);
+ if (pEndpointInfo == NULL)
+ {
+ afb_req_fail_f(req, "Endpoint not found", "Endpoint information not found for id:%d type%d",endpointID,endpointType);
+ return;
+ }
- value = 93.0; // TODO: Get actual property value
- propertyValJ = json_object_new_double(value);
+#ifndef AHL_DISCONNECT_POLICY
+ json_object *pPolicyEndpointJ = NULL;
+ err = PolicyEndpointStructToJSON(pEndpointInfo, &pPolicyEndpointJ);
+ if (err == AHL_POLICY_UTIL_FAIL)
+ {
+ afb_req_fail(req, "Audio policy violation", "Unable to get JSON object for Policy_SetVolume");
+ return;
+ }
- afb_req_success(req, propertyValJ, "Retrieved property value");
+ json_object *paramJ= json_object_new_string(propertyName);
+ json_object_object_add(pPolicyEndpointJ, "arg_property_name", paramJ);
+ json_object_object_add(pPolicyEndpointJ, "arg_property_value", propValueJ);
+
+ // Call policy to allow custom policy actions in current context
+ int policyAllowed = Policy_SetProperty(pPolicyEndpointJ);
+ if (!policyAllowed)
+ {
+ afb_req_fail(req, "Audio policy violation", "Set endpoint property not allowed in current context");
+ return;
+ }
+#else
+ // Simulate that policy returns target state (accepted)
+ if (pEndpointInfo->pPropTable)
+ g_hash_table_insert(pEndpointInfo->pPropTable, propertyName, propValueJ);
+#endif
+
+ //afb_event_push(g_AHLCtx.policyCtx.propertyEvent,queryJ);
+
+ afb_req_success(req, NULL, "Set property completed");
}
-PUBLIC void audiohlapi_set_state(struct afb_req req)
+PUBLIC void audiohlapi_get_property(struct afb_req req)
{
json_object *queryJ = NULL;
- endpointID_t endpointID = UNDEFINED_ID;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ char * pEndpointTypeStr = NULL;
EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
- char * stateName = NULL;
- char * stateValue = NULL;
- int policyAllowed = 0;
+ char * propertyName = NULL;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:i,s:i,s:s,s:s}", "endpoint_type", &endpointType,"endpoint_id",&endpointID,"state_name",&stateName,"state_value",&stateValue);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:i,s:s}", "endpoint_type", &pEndpointTypeStr,"endpoint_id",&endpointID,"property_name",&propertyName);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = endpoint_type:%d endpoint_id:%d state_name:%s state_value:%s", endpointType,endpointID,stateName,stateValue);
+ AFB_DEBUG("Parsed input arguments = endpoint_type:%s endpoint_id:%d property_name:%s", pEndpointTypeStr,endpointID,propertyName);
+ endpointType = EndpointTypeToEnum(pEndpointTypeStr);
- // Check that state provided is within list of known state for this config
- char * pDefaultStateValue = g_hash_table_lookup(g_AHLCtx.pDefaultStatesHT, stateName);
- if (pDefaultStateValue == NULL)
+ EndpointInfoT * pEndpointInfo = GetEndpointInfo(endpointID,endpointType);
+ if (pEndpointInfo == NULL)
{
- afb_req_fail_f(req, "Invalid arguments", "State provided is not known to configuration query=%s", stateName);
+ afb_req_fail_f(req, "Endpoint not found", "Endpoint information not found for id:%d type%d",endpointID,endpointType);
return;
}
- // Call policy to allow custom policy actions in current context
- policyAllowed = Policy_SetState(endpointType, endpointID, stateName, stateValue); // TODO: Potentially retrieve modified value by policy (e.g. state change)
- if (!policyAllowed)
- {
- afb_req_fail(req, "Audio policy violation", "Set endpoint state not allowed in current context");
+ // Retrieve cached property value
+ json_object * propertyValJ = (json_object *)g_hash_table_lookup(pEndpointInfo->pPropTable,propertyName);
+ if (propertyValJ == NULL) {
+ afb_req_fail_f(req, "Property not found", "Property information not found: %s",propertyName);
return;
}
- // Change the state of the endpoint as requested
+ json_object_get(propertyValJ); // Increase ref count so that framework does not free our JSON object
+ //AFB_WARNING("properties %s", json_object_get_string(propertyValJ));
+ //json_object_get(propertyValJ); // Increase ref count so that framework does not free our JSON object
- afb_req_success(req, NULL, "Set endpoint state completed");
+ afb_req_success(req, propertyValJ, "Retrieved property value");
}
-PUBLIC void audiohlapi_get_state(struct afb_req req)
+// Audio related events
+
+PUBLIC void audiohlapi_get_list_actions(struct afb_req req)
{
json_object *queryJ = NULL;
- endpointID_t endpointID = UNDEFINED_ID;
- EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
- json_object *stateValJ;
- char * stateName = NULL;
- char * stateValue = NULL;
+ char * audioRole = NULL;
+ json_object * roleActionsJ = NULL;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:i,s:i,s:s}", "endpoint_type", &endpointType,"endpoint_id",&endpointID,"state_name",&stateName);
+ int err = wrap_json_unpack(queryJ, "{s:s}", "audio_role",&audioRole);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = endpoint_type:%d endpoint_id:%d state_name:%s", endpointType,endpointID,stateName);
+ AFB_DEBUG("Parsed input arguments = audio_role:%s",audioRole);
- stateValJ = json_object_new_string(stateValue);
- // return cached value
+ // Build and return list of actions for specific audio role
+ RoleInfoT * pRole = GetRole(audioRole);
+ if ( pRole == NULL )
+ {
+ afb_req_fail_f(req, "Invalid audio role", "Audio role was not found in configuration -> %s",audioRole);
+ return;
+ }
- afb_req_success(req, stateValJ, "Retrieved state value");
+ roleActionsJ = json_object_new_array();
+ if (pRole->pActionList) {
+ int iNumberActions = pRole->pActionList->len;
+ for ( int i = 0 ; i < iNumberActions; i++)
+ {
+ char * pActionName = g_ptr_array_index(pRole->pActionList,i);
+ json_object * actionJ = json_object_new_string(pActionName);
+ json_object_array_add(roleActionsJ, actionJ);
+ }
+ }
+
+ afb_req_success(req, roleActionsJ, "Retrieved action list for audio role");
}
-// Sound events
-PUBLIC void audiohlapi_post_sound_event(struct afb_req req)
+PUBLIC void audiohlapi_post_action(struct afb_req req)
{
json_object *queryJ = NULL;
- char * eventName = NULL;
- char * mediaName = NULL;
+ char * actionName = NULL;
char * audioRole = NULL;
- json_object *audioContext = NULL;
- int policyAllowed = 0;
+ char * mediaName = NULL;
+ json_object *actionContext = NULL;
queryJ = afb_req_json(req);
- int err = wrap_json_unpack(queryJ, "{s:s,s:s,s?s,s?o}", "event_name", &eventName,"audio_role",&audioRole,"media_name",&mediaName,"audio_context",&audioContext);
+ int err = wrap_json_unpack(queryJ, "{s:s,s:s,s?s,s?o}", "action_name", &actionName,"audio_role",&audioRole,"media_name",&mediaName,"action_context",&actionContext);
if (err) {
afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
return;
}
- AFB_DEBUG("Parsed input arguments = event_name:%s audio_role:%s media_name:%s", eventName,audioRole,mediaName);
+ AFB_DEBUG("Parsed input arguments = action_name:%s audio_role:%s", actionName,audioRole);
+ // Verify if known action for audio role
+ RoleInfoT * pRole = GetRole(audioRole);
+ if ( pRole == NULL )
+ {
+ afb_req_fail_f(req, "Invalid audio role", "Audio role was not found in configuration -> %s",audioRole);
+ return;
+ }
+
+ // Check to find specific action
+ int iActionFound = 0;
+ if (pRole->pActionList) {
+ int iNumberActions = pRole->pActionList->len;
+ char * pTargetActionName = NULL;
+ for ( int i = 0 ; i < iNumberActions; i++)
+ {
+ pTargetActionName = g_ptr_array_index(pRole->pActionList,i);
+ if ( strcasecmp(pTargetActionName,actionName)==0) {
+ iActionFound = 1;
+ break;
+ }
+ }
+ }
+
+ if (!iActionFound) {
+ afb_req_fail_f(req, "Event not found for audio role", "Event -> %s not found for role:%s",actionName,audioRole);
+ return;
+ }
+
+#ifndef AHL_DISCONNECT_POLICY
// Call policy to allow custom policy actions in current context (e.g. cancel playback)
- policyAllowed = Policy_PostSoundEvent(eventName, audioRole, mediaName, (void*)audioContext); // TODO: Potentially retrieve modified value by policy (e.g. change media)
+ int policyAllowed = Policy_PostAction(queryJ);
if (!policyAllowed)
{
- afb_req_fail(req, "Audio policy violation", "Post sound event not allowed in current context");
+ afb_req_fail(req, "Audio policy violation", "Post sound action not allowed in current context");
return;
}
+#endif
- // TODO: Post sound event to rendering services (e.g. gstreamer file player wrapper or simple ALSA player)
+ //afb_event_push(g_AHLCtx.policyCtx.postEvent,queryJ);
- afb_req_success(req, NULL, "Posted sound event");
+ afb_req_success(req, NULL, "Posted sound action");
}
// Monitoring
PUBLIC void audiohlapi_subscribe(struct afb_req req)
{
- // json_object *queryJ = NULL;
-
- // queryJ = afb_req_json(req);
+ json_object *queryJ = NULL;
+ json_object * eventArrayJ = NULL;
- // TODO: Iterate through array length, parsing the string value to actual events
- // TODO: Subscribe to appropriate events from other services
+ queryJ = afb_req_json(req);
+ int err = wrap_json_unpack(queryJ, "{s:o}", "events", &eventArrayJ);
+ if (err) {
+ afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
+ return;
+ }
+
+ int iNumEvents = json_object_array_length(eventArrayJ);
+ for (int i = 0; i < iNumEvents; i++)
+ {
+ char * pEventName = NULL;
+ json_object * jEvent = json_object_array_get_idx(eventArrayJ,i);
+ pEventName = (char *)json_object_get_string(jEvent);
+ if(pEventName == NULL) {
+ afb_req_fail(req, "failed", "Invalid event");
+ return;
+ }
+ else if(!strcasecmp(pEventName, AHL_ENDPOINT_PROPERTY_EVENT)) {
+ afb_req_subscribe(req, g_AHLCtx.policyCtx.propertyEvent);
+ AFB_DEBUG("Client subscribed to endpoint property events");
+ }
+ else if(!strcasecmp(pEventName, AHL_ENDPOINT_VOLUME_EVENT)) {
+ afb_req_subscribe(req, g_AHLCtx.policyCtx.volumeEvent);
+ AFB_DEBUG("Client subscribed to endpoint volume events");
+ }
+ else if(!strcasecmp(pEventName, AHL_POST_ACTION_EVENT)) {
+ afb_req_subscribe(req, g_AHLCtx.policyCtx.postActionEvent);
+ AFB_DEBUG("Client subscribed to post event calls events");
+ }
+ else {
+ afb_req_fail(req, "failed", "Invalid event");
+ return;
+ }
+ }
afb_req_success(req, NULL, "Subscribe to events finished");
}
PUBLIC void audiohlapi_unsubscribe(struct afb_req req)
{
- // json_object *queryJ = NULL;
-
- // queryJ = afb_req_json(req);
+ json_object *queryJ = NULL;
+ json_object * eventArrayJ = NULL;
- // TODO: Iterate through array length, parsing the string value to actual events
- // TODO: Unsubscribe to appropriate events from other services
+ queryJ = afb_req_json(req);
+ int err = wrap_json_unpack(queryJ, "{s:o}", "events", &eventArrayJ);
+ if (err) {
+ afb_req_fail_f(req, "Invalid arguments", "Args not a valid json object query=%s", json_object_get_string(queryJ));
+ return;
+ }
+
+ int iNumEvents = json_object_array_length(eventArrayJ);
+ for (int i = 0; i < iNumEvents; i++)
+ {
+ char * pEventName = NULL;
+ json_object * jEvent = json_object_array_get_idx(eventArrayJ,i);
+ pEventName = (char *)json_object_get_string(jEvent);
+ if(pEventName == NULL) {
+ afb_req_fail(req, "failed", "Invalid event");
+ return;
+ }
+ else if(!strcasecmp(pEventName, AHL_ENDPOINT_PROPERTY_EVENT)) {
+ afb_req_unsubscribe(req, g_AHLCtx.policyCtx.propertyEvent);
+ AFB_DEBUG("Client unsubscribed to endpoint property events");
+ }
+ else if(!strcasecmp(pEventName, AHL_ENDPOINT_VOLUME_EVENT)) {
+ afb_req_unsubscribe(req, g_AHLCtx.policyCtx.volumeEvent);
+ AFB_DEBUG("Client unsubscribed to endpoint volume events");
+ }
+ else if(!strcasecmp(pEventName, AHL_POST_ACTION_EVENT)) {
+ afb_req_unsubscribe(req, g_AHLCtx.policyCtx.postActionEvent);
+ AFB_DEBUG("Client unsubscribed to post event calls events");
+ }
+ else {
+ afb_req_fail(req, "failed", "Invalid event");
+ return;
+ }
+ }
- afb_req_success(req, NULL, "Subscribe to events finished");
+ afb_req_success(req, NULL, "Unsubscribe to events finished");
}
+
+// Since the policy is currently in the same binding, it cannot raise events on its own
+// This is a first step toward isolation, when policy is migrated in its own binding it can simply raise AGL events
+// This binding will register for these policy events and will execute the code below upon event reception
+PUBLIC void audiohlapi_raise_event(json_object * pEventDataJ)
+{
+ char * pEventName = NULL;
+
+ int err = wrap_json_unpack(pEventDataJ,"{s:s}","event_name", &pEventName);
+ if(err)
+ {
+ AFB_ERROR("Unable to retrieve event name");
+ return;
+ }
+
+ if(strcasecmp(pEventName, AHL_ENDPOINT_INIT_EVENT)==0) {
+ char * pAudioRole = NULL;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
+ int err = wrap_json_unpack(pEventDataJ,"{s:i,s:i,s:s}",
+ "endpoint_id", &endpointID,
+ "endpoint_type", &endpointType,
+ "audio_role", &pAudioRole);
+ if(err)
+ {
+ AFB_ERROR("Unable to unpack property init event");
+ return;
+ }
+
+ RoleInfoT * pRole = GetRole(pAudioRole);
+ if ( pRole == NULL ){
+ AFB_ERROR("Requested audio role does not exist in current configuration -> %s", pAudioRole);
+ return;
+ }
+
+ EndpointInfoT * pEndpointInfo = InitEndpointInfo();
+ g_assert_nonnull(pEndpointInfo);
+ PolicyCtxJSONToEndpoint(pEventDataJ,pEndpointInfo);
+
+ err = ReplaceEndpointInfoWithRole(endpointID,endpointType,pRole,pEndpointInfo);
+ if(err == AHL_FAIL)
+ {
+ AFB_ERROR("Can't update EndpointInfo aborting");
+ return;
+ }
+
+ // Remove event name from object
+ // json_object_object_del(pEventDataJ,"event_name");
+ //afb_event_push(g_AHLCtx.policyCtx.propertyEvent,pEventDataJ); // Not broadcasted to application at this time
+ }
+ else if(strcasecmp(pEventName, AHL_ENDPOINT_PROPERTY_EVENT)==0) {
+ char * pAudioRole = NULL;
+ char * pPropertyName = NULL;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
+ json_object * propValueJ = NULL;
+ int err = wrap_json_unpack(pEventDataJ,"{s:i,s:i,s:s,s:o,s:s}",
+ "endpoint_id", &endpointID,
+ "endpoint_type", &endpointType,
+ "property_name", &pPropertyName,
+ "value",&propValueJ,
+ "audio_role", &pAudioRole);
+ if(err)
+ {
+ AFB_ERROR("Unable to unpack property event");
+ return;
+ }
+ RoleInfoT * pRole = GetRole(pAudioRole);
+ if ( pRole == NULL ){
+ AFB_ERROR("Requested audio role does not exist in current configuration -> %s", pAudioRole);
+ return;
+ }
+ EndpointInfoT * pEndpointInfo = GetEndpointInfoWithRole(endpointID,endpointType,pRole);
+ // update property value
+ if (pEndpointInfo->pPropTable)
+ {
+ json_type jType = json_object_get_type(propValueJ);
+ switch (jType) {
+ case json_type_double:
+ Add_Endpoint_Property_Double(pEndpointInfo,pPropertyName,json_object_get_double(propValueJ));
+ break;
+ case json_type_int:
+ Add_Endpoint_Property_Int(pEndpointInfo,pPropertyName,json_object_get_int(propValueJ));
+ break;
+ case json_type_string:
+ Add_Endpoint_Property_String(pEndpointInfo,pPropertyName,json_object_get_string(propValueJ));
+ break;
+ default:
+ AFB_ERROR("Invalid property argument Property value not a valid json object query=%s", json_object_get_string(propValueJ));
+ return ;
+ }
+ }
+ // Remove event name from object
+ json_object_object_del(pEventDataJ,"event_name");
+ afb_event_push(g_AHLCtx.policyCtx.propertyEvent,pEventDataJ);
+ }
+ else if(strcasecmp(pEventName, AHL_ENDPOINT_VOLUME_EVENT)==0) {
+ char * pAudioRole = NULL;
+ endpointID_t endpointID = AHL_UNDEFINED;
+ EndpointTypeT endpointType = ENDPOINTTYPE_MAXVALUE;
+ int iVolume = 0;
+ int err = wrap_json_unpack(pEventDataJ,"{s:i,s:i,s:i,s:s}",
+ "endpoint_id", &endpointID,
+ "endpoint_type", &endpointType,
+ "value",&iVolume,
+ "audio_role", &pAudioRole);
+ if(err)
+ {
+ AFB_ERROR("Unable to unpack volume event data");
+ return;
+ }
+ RoleInfoT * pRole = GetRole(pAudioRole);
+ if ( pRole == NULL ){
+ AFB_ERROR("Requested audio role does not exist in current configuration -> %s", pAudioRole);
+ return;
+ }
+ EndpointInfoT * pEndpointInfo = GetEndpointInfoWithRole(endpointID,endpointType,pRole);
+ // update volume value
+ pEndpointInfo->iVolume = iVolume;
+ // Remove event name from object
+ json_object_object_del(pEventDataJ,"event_name");
+ afb_event_push(g_AHLCtx.policyCtx.volumeEvent,pEventDataJ);
+ }
+ else if(strcasecmp(pEventName, AHL_POST_ACTION_EVENT)==0) {
+ // Remove event name from object
+ json_object_object_del(pEventDataJ,"event_name");
+ afb_event_push(g_AHLCtx.policyCtx.postActionEvent,pEventDataJ);
+ }
+ else if(strcasecmp(pEventName, AHL_STREAM_STATE_EVENT)==0) {
+ streamID_t streamID = AHL_UNDEFINED;
+ StreamEventT streamEvent = STREAM_EVENT_MAXVALUE;
+ int err = wrap_json_unpack(pEventDataJ,"{s:i,s:i}",
+ "stream_id", &streamID,
+ "state_event", &streamEvent);
+ if(err)
+ {
+ AFB_ERROR("Unable to unpack stream event data");
+ return;
+ }
+
+ StreamInfoT * pStreamInfo = GetStream(streamID);
+ if (pStreamInfo == NULL) {
+ AFB_ERROR("Specified stream not currently active stream_id -> %d",streamID);
+ return;
+ }
+
+ // update streamstate value
+ switch (streamEvent) {
+ case STREAM_EVENT_START:
+ pStreamInfo->streamState = STREAM_STATE_RUNNING;
+ break;
+ case STREAM_EVENT_STOP:
+ pStreamInfo->streamState = STREAM_STATE_IDLE;
+ break;
+ case STREAM_EVENT_PAUSE:
+ pStreamInfo->streamState = STREAM_STATE_PAUSED;
+ break;
+ case STREAM_EVENT_RESUME:
+ pStreamInfo->streamState = STREAM_STATE_RUNNING;
+ break;
+ case STREAM_EVENT_MUTED:
+ pStreamInfo->streamMute = STREAM_MUTED;
+ break;
+ case STREAM_EVENT_UNMUTED:
+ pStreamInfo->streamMute = STREAM_UNMUTED;
+ break;
+ default:
+ AFB_ERROR("Unknown stream event");
+ }
+
+ // Remove event name from object
+ json_object_object_del(pEventDataJ,"event_name");
+ AFB_ERROR("pEventDataJ=%s", json_object_get_string(pEventDataJ));
+ afb_event_push(pStreamInfo->streamStateEvent,pEventDataJ);
+ }
+ else {
+ AFB_ERROR("Unknown event name");
+ }
+} \ No newline at end of file