Home | History | Annotate | Download | only in server
      1 /* Copyright 2017 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  */
      5 
      6 #include <stdint.h>
      7 #include <string.h>
      8 #include <syslog.h>
      9 
     10 #include "cras_main_message.h"
     11 #include "cras_observer.h"
     12 #include "cras_system_state.h"
     13 
     14 struct non_empty_audio_msg {
     15 	struct cras_main_message header;
     16 	int32_t non_empty;
     17 };
     18 
     19 /* The following functions are called from audio thread. */
     20 
     21 static void init_non_empty_audio_msg(struct non_empty_audio_msg *msg)
     22 {
     23 	memset(msg, 0, sizeof(*msg));
     24 	msg->header.type = CRAS_MAIN_NON_EMPTY_AUDIO_STATE;
     25 	msg->header.length = sizeof(*msg);
     26 }
     27 
     28 int cras_non_empty_audio_send_msg(int32_t non_empty)
     29 {
     30 	struct non_empty_audio_msg msg;
     31 	int rc;
     32 
     33 	init_non_empty_audio_msg(&msg);
     34 	msg.non_empty = non_empty;
     35 
     36 	rc = cras_main_message_send((struct cras_main_message *)&msg);
     37 	if (rc < 0)
     38 		syslog(LOG_ERR, "Failed to send non-empty audio message!");
     39 
     40 	return rc;
     41 }
     42 
     43 /* The following functions are called from main thread. */
     44 
     45 static void handle_non_empty_audio_message(struct cras_main_message *msg,
     46 					   void *arg)
     47 {
     48 	struct non_empty_audio_msg *audio_msg =
     49 			(struct non_empty_audio_msg *)msg;
     50 
     51 	cras_system_state_set_non_empty_status(audio_msg->non_empty);
     52 	cras_observer_notify_non_empty_audio_state_changed(audio_msg->non_empty);
     53 }
     54 
     55 int cras_non_empty_audio_handler_init()
     56 {
     57 	cras_main_message_add_handler(CRAS_MAIN_NON_EMPTY_AUDIO_STATE,
     58 				      handle_non_empty_audio_message, NULL);
     59 	return 0;
     60 }