19 #include "acm_session_mgr.h"
20 #include "audiocapturemgr_iarm.h"
27 #include "safec_lib.h"
29 using namespace audiocapturemgr;
31 static const unsigned int MAX_SUPPORTED_SOURCES = 1;
34 static unsigned int ticker = 0;
35 std::string audio_filename_prefix = AUDIOCAPTUREMGR_FILENAME_PREFIX;
36 std::string audio_file_path = AUDIOCAPTUREMGR_FILE_PATH;
38 static void request_callback(
void * data, std::string &file,
int result)
43 ERROR(
"Failed to grab sample.\n");
48 rc = strcpy_s(payload.dataLocator,
sizeof(payload.dataLocator), file.c_str());
51 int ret =
IARM_Bus_BroadcastEvent(IARMBUS_AUDIOCAPTUREMGR_NAME, DATA_CAPTURE_IARM_EVENT_AUDIO_CLIP_READY, &payload,
sizeof(payload));
52 REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
57 WARN(
"Incoming filename is too big for payload buffer.\n");
62 static IARM_Result_t request_sample(
void * arg)
65 return IARM_RESULT_SUCCESS;
68 static IARM_Result_t open(
void * arg)
71 return IARM_RESULT_SUCCESS;
73 static IARM_Result_t close(
void * arg)
76 return IARM_RESULT_SUCCESS;
78 static IARM_Result_t start(
void * arg)
81 return IARM_RESULT_SUCCESS;
83 static IARM_Result_t stop(
void * arg)
86 return IARM_RESULT_SUCCESS;
88 static IARM_Result_t get_audio_props(
void * arg)
91 return IARM_RESULT_SUCCESS;
93 static IARM_Result_t get_default_audio_props(
void * arg)
96 return IARM_RESULT_SUCCESS;
98 static IARM_Result_t get_output_props(
void * arg)
101 return IARM_RESULT_SUCCESS;
103 static IARM_Result_t set_output_props(
void * arg)
106 return IARM_RESULT_SUCCESS;
108 static IARM_Result_t set_audio_props(
void * arg)
111 return IARM_RESULT_SUCCESS;
113 acm_session_mgr::acm_session_mgr() : m_session_counter(0)
117 for(
unsigned int i = 0; i < MAX_SUPPORTED_SOURCES; i++)
119 m_sources.push_back((
q_mgr *)NULL);
123 m_sources[0] =
new q_mgr;
125 pthread_mutexattr_t mutex_attribute;
126 REPORT_IF_UNEQUAL(0, pthread_mutexattr_init(&mutex_attribute));
127 REPORT_IF_UNEQUAL(0, pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_ERRORCHECK));
128 REPORT_IF_UNEQUAL(0, pthread_mutex_init(&m_mutex, &mutex_attribute));
131 acm_session_mgr::~acm_session_mgr()
134 REPORT_IF_UNEQUAL(0, pthread_mutex_destroy(&m_mutex));
137 void acm_session_mgr::lock()
139 REPORT_IF_UNEQUAL(0, pthread_mutex_lock(&m_mutex));
142 void acm_session_mgr::unlock()
144 REPORT_IF_UNEQUAL(0, pthread_mutex_unlock(&m_mutex));
159 REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
162 REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
165 REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
167 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_REQUEST_SAMPLE, request_sample); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
168 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_OPEN, open); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
169 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_CLOSE, close); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
170 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_START, start); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
171 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_STOP, stop); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
172 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_GET_DEFAULT_AUDIO_PROPS, get_default_audio_props); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
173 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_GET_AUDIO_PROPS, get_audio_props); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
174 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_GET_OUTPUT_PROPS, get_output_props); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
175 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_SET_AUDIO_PROPERTIES, set_audio_props); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
176 ret =
IARM_Bus_RegisterCall(IARMBUS_AUDIOCAPTUREMGR_SET_OUTPUT_PROPERTIES, set_output_props); REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
184 REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
186 REPORT_IF_UNEQUAL(IARM_RESULT_SUCCESS, ret);
190 static bool get_rfc_output_conversion_config()
192 int ret = system(
". /lib/rdk/isFeatureEnabled.sh AcmEnableOpConv");
193 if((
true == WEXITSTATUS(ret)) && (
true == WIFEXITED(ret)))
195 INFO(
"RFC: enable song-id output conversion\n");
200 INFO(
"RFC: disable song-id output conversion.\n");
208 INFO(
"source: 0x%x, output type: 0x%x\n", param->details.arg_open.source, param->details.arg_open.output_type);
209 if( (MAX_SUPPORTED_SOURCES <= param->details.arg_open.source) || (MAX_SUPPORTED_OUTPUT_TYPES <= param->details.arg_open.output_type))
211 ERROR(
"Unsupported paramters.\n");
212 param->result = ACM_RESULT_INVALID_ARGUMENTS;
213 return param->result;
217 new_session->source = m_sources[param->details.arg_open.source];
218 switch(param->details.arg_open.output_type)
220 case BUFFERED_FILE_OUTPUT:
221 new_session->client =
new music_id_client(new_session->source, music_id_client::SOCKET_OUTPUT);
222 static_cast <music_id_client *
> (new_session->client)->enable_output_conversion(get_rfc_output_conversion_config());
226 case REALTIME_SOCKET:
227 new_session->client =
new ip_out_client(new_session->source);
232 ERROR(
"Unrecognized output type.\n");
234 new_session->enable =
false;
235 new_session->output_type = param->details.arg_open.output_type;
240 new_session->session_id = m_session_counter++;
245 if(BUFFERED_FILE_OUTPUT == new_session->output_type)
247 std::list <acm_session_t *>::iterator iter;
248 for(iter = m_sessions.begin(); iter != m_sessions.end(); iter++)
250 if((BUFFERED_FILE_OUTPUT == (*iter)->output_type) && (new_session->source == (*iter)->source))
252 duplicate_session = *iter;
253 m_sessions.erase(iter);
258 m_sessions.push_back(new_session);
261 if(duplicate_session)
263 WARN(
"Detroying existing music id session (id %d) for the same source.\n", duplicate_session->session_id);
264 duplicate_session->client->stop();
265 delete duplicate_session->client;
266 delete duplicate_session;
269 INFO(
"Created session 0x%x\n", new_session->session_id);
270 param->session_id = new_session->session_id;
272 return param->result;
278 INFO(
"session_id 0x%x\n", param->session_id);
283 std::list <acm_session_t *>::iterator iter;
284 for(iter = m_sessions.begin(); iter != m_sessions.end(); iter++)
286 if(param->session_id == (*iter)->session_id)
289 m_sessions.erase(iter);
301 INFO(
"Stopped session 0x%x.\n", param->session_id);
306 INFO(
"Session destroyed.\n");
310 WARN(
"Session not found!\n");
311 param->result = ACM_RESULT_BAD_SESSION_ID;
313 return param->result;
320 std::list<acm_session_t *>::iterator iter;
321 for(iter = m_sessions.begin(); iter != m_sessions.end(); iter++)
323 if(session_id == (*iter)->session_id)
336 INFO(
"session_id 0x%x\n", param->session_id);
343 ptr->client->start();
345 INFO(
"Started session 0x%x.\n", param->session_id);
351 ERROR(
"Session not found!\n")
352 param->result = ACM_RESULT_BAD_SESSION_ID;
355 return param->result;
361 INFO(
"session_id 0x%x\n", param->session_id);
370 INFO(
"Stopped session 0x%x.\n", param->session_id);
376 ERROR(
"Session not found!\n")
377 param->result = ACM_RESULT_BAD_SESSION_ID;
380 return param->result;
386 INFO(
"session_id 0x%x\n", param->session_id);
391 ptr->client->get_default_audio_properties(props);
393 switch (props.format) {
395 param->details.arg_audio_properties.format = acmFormate16BitStereo;
398 param->details.arg_audio_properties.format = acmFormate24BitStereo;
401 param->details.arg_audio_properties.format = acmFormate16BitMonoLeft;
404 param->details.arg_audio_properties.format = acmFormate16BitMonoRight;
407 param->details.arg_audio_properties.format = acmFormate16BitMono;
410 param->details.arg_audio_properties.format = acmFormate24Bit5_1;
413 param->details.arg_audio_properties.format = acmFormateMax;
416 param->details.arg_audio_properties.format = acmFormate16BitStereo;
420 switch (props.sampling_frequency) {
422 param->details.arg_audio_properties.sampling_frequency =
acmFreqe16000;
425 param->details.arg_audio_properties.sampling_frequency =
acmFreqe32000;
428 param->details.arg_audio_properties.sampling_frequency =
acmFreqe44100;
431 param->details.arg_audio_properties.sampling_frequency =
acmFreqe48000;
434 param->details.arg_audio_properties.sampling_frequency = acmFreqeMax;
437 param->details.arg_audio_properties.sampling_frequency =
acmFreqe48000;
441 param->details.arg_audio_properties.fifo_size = props.fifo_size;
442 param->details.arg_audio_properties.threshold = props.threshold;
443 param->details.arg_audio_properties.delay_compensation_ms = props.delay_compensation_ms;
449 ERROR(
"Session not found!\n")
450 param->result = ACM_RESULT_BAD_SESSION_ID;
452 return param->result;
459 INFO(
"session_id 0x%x\n", param->session_id);
464 ptr->client->get_audio_properties(props);
466 switch (props.format) {
468 param->details.arg_audio_properties.format = acmFormate16BitStereo;
471 param->details.arg_audio_properties.format = acmFormate24BitStereo;
474 param->details.arg_audio_properties.format = acmFormate16BitMonoLeft;
477 param->details.arg_audio_properties.format = acmFormate16BitMonoRight;
480 param->details.arg_audio_properties.format = acmFormate16BitMono;
483 param->details.arg_audio_properties.format = acmFormate24Bit5_1;
486 param->details.arg_audio_properties.format = acmFormateMax;
489 param->details.arg_audio_properties.format = acmFormate16BitStereo;
493 switch (props.sampling_frequency) {
495 param->details.arg_audio_properties.sampling_frequency =
acmFreqe16000;
498 param->details.arg_audio_properties.sampling_frequency =
acmFreqe24000;
501 param->details.arg_audio_properties.sampling_frequency =
acmFreqe32000;
504 param->details.arg_audio_properties.sampling_frequency =
acmFreqe44100;
507 param->details.arg_audio_properties.sampling_frequency =
acmFreqe48000;
510 param->details.arg_audio_properties.sampling_frequency = acmFreqeMax;
513 param->details.arg_audio_properties.sampling_frequency =
acmFreqe48000;
517 param->details.arg_audio_properties.fifo_size = props.fifo_size;
518 param->details.arg_audio_properties.threshold = props.threshold;
519 param->details.arg_audio_properties.delay_compensation_ms = props.delay_compensation_ms;
525 ERROR(
"Session not found!\n")
526 param->result = ACM_RESULT_BAD_SESSION_ID;
528 return param->result;
534 INFO(
"session_id 0x%x\n", param->session_id);
538 if(REALTIME_SOCKET == ptr->output_type)
541 std::string sock_path = client->get_data_path();
542 if(sock_path.empty())
544 param->result = ACM_RESULT_GENERAL_FAILURE;
548 int i32FilePathLen =
sizeof(param->details.arg_output_props.output.file_path);
549 rc = strcpy_s(param->details.arg_output_props.output.file_path, i32FilePathLen, sock_path.c_str());
557 else if(BUFFERED_FILE_OUTPUT == ptr->output_type)
566 ERROR(
"Session not found!\n")
567 param->result = ACM_RESULT_BAD_SESSION_ID;
569 return param->result;
575 INFO(
"session_id 0x%x\n", param->session_id);
582 switch (param->details.arg_audio_properties.format) {
583 case acmFormate16BitStereo:
586 case acmFormate24BitStereo:
589 case acmFormate16BitMonoLeft:
592 case acmFormate16BitMonoRight:
595 case acmFormate16BitMono:
598 case acmFormate24Bit5_1:
602 props.format = racFormat_eMax;
609 switch (param->details.arg_audio_properties.sampling_frequency) {
626 props.sampling_frequency = racFreq_eMax;
634 props.fifo_size = param->details.arg_audio_properties.fifo_size;
635 props.threshold = param->details.arg_audio_properties.threshold;
636 props.delay_compensation_ms = param->details.arg_audio_properties.delay_compensation_ms;
638 param->result = ptr->client->set_audio_properties(props);
642 ERROR(
"Session not found!\n")
643 param->result = ACM_RESULT_BAD_SESSION_ID;
645 return param->result;
652 INFO(
"session_id 0x%x\n", param->session_id);
656 if(BUFFERED_FILE_OUTPUT == ptr->output_type)
659 unsigned int duration = param->details.arg_output_props.output.buffer_duration;
660 if(duration < client->get_max_supported_duration())
666 ERROR(
"Duration out of bounds!\n");
667 param->result = ACM_RESULT_DURATION_OUT_OF_BOUNDS;
672 WARN(
"Not implemented for this type of output.\n");
673 param->result = ACM_RESULT_UNSUPPORTED_API;
678 ERROR(
"Session not found!\n")
679 param->result = ACM_RESULT_BAD_SESSION_ID;
681 return param->result;
687 INFO(
"session_id 0x%x\n", param->session_id);
691 if(BUFFERED_FILE_OUTPUT == ptr->output_type)
695 if(
false == ptr->enable)
697 ERROR(
"Audio capture is currently disabled!\n");
698 param->result = ACM_RESULT_DURATION_OUT_OF_BOUNDS;
703 param->result = ACM_RESULT_SUCCESS;
704 std::string filename = client->get_sock_path();
706 if(param->details.arg_sample_request.is_precapture)
712 request_callback(NULL, filename, 0);
717 unsigned int duration = param->details.arg_sample_request.duration;
718 if(duration <= client->get_max_supported_duration())
724 ERROR(
"Duration out of bounds!\n");
725 param->result = ACM_RESULT_DURATION_OUT_OF_BOUNDS;
732 param->result = ACM_RESULT_GENERAL_FAILURE;
739 WARN(
"Not supported with this output type.\n");
740 param->result = ACM_RESULT_UNSUPPORTED_API;
745 ERROR(
"Session not found!\n")
746 param->result = ACM_RESULT_BAD_SESSION_ID;
748 return param->result;
753 audio_filename_prefix = prefix;