summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_non_empty_audio_handler.c
blob: 313149ff545a99eaf7e9b5c30f4ac2af6353ee26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Copyright 2017 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stdint.h>
#include <string.h>
#include <syslog.h>

#include "cras_main_message.h"
#include "cras_observer.h"
#include "cras_system_state.h"

struct non_empty_audio_msg {
	struct cras_main_message header;
	int32_t non_empty;
};

/* The following functions are called from audio thread. */

static void init_non_empty_audio_msg(struct non_empty_audio_msg *msg)
{
	memset(msg, 0, sizeof(*msg));
	msg->header.type = CRAS_MAIN_NON_EMPTY_AUDIO_STATE;
	msg->header.length = sizeof(*msg);
}

int cras_non_empty_audio_send_msg(int32_t non_empty)
{
	struct non_empty_audio_msg msg;
	int rc;

	init_non_empty_audio_msg(&msg);
	msg.non_empty = non_empty;

	rc = cras_main_message_send((struct cras_main_message *)&msg);
	if (rc < 0)
		syslog(LOG_ERR, "Failed to send non-empty audio message!");

	return rc;
}

/* The following functions are called from main thread. */

static void handle_non_empty_audio_message(struct cras_main_message *msg,
					   void *arg)
{
	struct non_empty_audio_msg *audio_msg =
		(struct non_empty_audio_msg *)msg;

	cras_system_state_set_non_empty_status(audio_msg->non_empty);
	cras_observer_notify_non_empty_audio_state_changed(
		audio_msg->non_empty);
}

int cras_non_empty_audio_handler_init()
{
	cras_main_message_add_handler(CRAS_MAIN_NON_EMPTY_AUDIO_STATE,
				      handle_non_empty_audio_message, NULL);
	return 0;
}