summaryrefslogtreecommitdiff
path: root/server/mm_sound_server.c
blob: 6b1dbb03200cc5361aeaab25c6a526a6a13fb714 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <getopt.h>

#include <mm_error.h>
#include <mm_debug.h>
#include "include/mm_sound_thread_pool.h"
#include "include/mm_sound_mgr_run.h"
#include "include/mm_sound_mgr_codec.h"
#include "include/mm_sound_mgr_ipc.h"
#include "include/mm_sound_mgr_pulse.h"
#include "include/mm_sound_recovery.h"

#include <heynoti.h>

#include <glib.h>

#define PLUGIN_ENV "MM_SOUND_PLUGIN_PATH"
#define PLUGIN_DIR "/usr/lib/soundplugins/"
#define PLUGIN_MAX 30

#define HIBERNATION_CHECK_KEY	"memory/hibernation/sound_ready"
#define HIBERNATION_READY		1
#define USE_SYSTEM_SERVER_PROCESS_MONITORING

typedef struct {
    char *plugdir;
    int startserver;
    int printlist;
    int testmode;
} server_arg;

static int getOption(int argc, char **argv, server_arg *arg);
static int usgae(int argc, char **argv);

static struct sigaction sigint_action;  /* Backup pointer of SIGINT handler */
static struct sigaction sigabrt_action; /* Backup pointer of SIGABRT signal handler */
static struct sigaction sigsegv_action; /* Backup pointer of SIGSEGV fault signal handler */
static struct sigaction sigterm_action; /* Backup pointer of SIGTERM signal handler */
static struct sigaction sigsys_action;  /* Backup pointer of SIGSYS signal handler */
static void _exit_handler(int sig);

GMainLoop *g_mainloop;

GThreadFunc event_loop_thread(gpointer data)
{
	g_mainloop = g_main_loop_new(NULL, TRUE);
	if(g_mainloop == NULL) {
		debug_error("g_main_loop_new() failed\n");
	}
	g_main_loop_run(g_mainloop);
	return NULL;
}

void hibernation_leave_cb()
{
	MMSoundMgrPulseHandleRegisterMonoAudio();

	if(sound_system_bootup_recovery()) {
		debug_error("Audio reset failed\n");
	} else {
		debug_msg("Audio reset success\n");
	}
}


int main(int argc, char **argv)
{
	server_arg serveropt;
	struct sigaction action;
	int ret;
	int heynotifd = -1;

	action.sa_handler = _exit_handler;
	action.sa_flags = 0;
	sigemptyset(&action.sa_mask);

#if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
	int pid;
#endif

	if (getOption(argc, argv, &serveropt))
		return 1;

	/* Daemon process create */
	if (!serveropt.testmode && serveropt.startserver) {
#if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
		daemon(0,0); //chdir to ("/"), and close stdio
#endif
	}

	signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
	/*
	 * Set oom_adj lowest 2010.03.09
	 */

	if(sound_system_bootup_recovery()) {
		debug_error("Audio reset failed\n");
	} else {
		debug_msg("Audio reset success\n");
	}

	heynotifd = heynoti_init();
	if(heynoti_subscribe(heynotifd, "HIBERNATION_LEAVE", hibernation_leave_cb, NULL)) {
		debug_error("heynoti_subscribe failed...\n");
	} else {
		debug_msg("heynoti_subscribe() success\n");
	}

	if(heynoti_attach_handler(heynotifd)) {
		debug_error("heynoti_attach_handler() failed\n");
	} else {
		debug_msg("heynoti_attach_handler() success\n");
	}

#if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
	while(1)
	{
		if ((pid = fork()) < 0)
		{
			fprintf(stderr, "Sub Fork Error\n");
			return 2;
		}
		else if(pid == 0)
		{
			break;
		}
		else if(pid > 0)
		{
			wait(&ret);
			fprintf(stderr, "Killed by signal [%05X]\n", ret);	
			fprintf(stderr, "Daemon is run againg\n");
		}
	}
#endif
	sigaction(SIGABRT, &action, &sigabrt_action);
	sigaction(SIGSEGV, &action, &sigsegv_action);
	sigaction(SIGTERM, &action, &sigterm_action);
	sigaction(SIGSYS, &action, &sigsys_action);

	if (!g_thread_supported ())
		g_thread_init (NULL);

	if(NULL == g_thread_create(event_loop_thread, NULL, FALSE, NULL)) {
		fprintf(stderr,"event loop thread create failed\n");
		return 3;
	}


	if (serveropt.startserver || serveropt.printlist) {
		MMSoundThreadPoolInit();
		MMSoundMgrRunInit(serveropt.plugdir);
		MMSoundMgrCodecInit(serveropt.plugdir);
		if (!serveropt.testmode)
			MMSoundMgrIpcInit();

		MMSoundMgrPulseInit();
	}

	if (serveropt.startserver) {
		/* Start Run types */
		MMSoundMgrRunRunAll();

		/* set hibernation check */
		if(vconf_set_int(HIBERNATION_CHECK_KEY, HIBERNATION_READY)) {
			debug_error("[SoundServer] Hibernation check vconf_set_int fail\n");
		}

		/* Start Ipc mgr */
		MMSoundMgrIpcReady();
	}

	if (serveropt.startserver || serveropt.printlist) {
		MMSoundMgrRunStopAll();
		if (!serveropt.testmode)
			MMSoundMgrIpcFini();

		MMSoundMgrCodecFini();
		MMSoundMgrRunFini();
		MMSoundThreadPoolFini();

		MMSoundMgrPulseFini();

		if(heynoti_unsubscribe(heynotifd, "HIBERNATION_LEAVE", NULL)) {
			debug_error("heynoti_unsubscribe failed..\n");
		}
		heynoti_close(heynotifd);
	}
	return 0;
}

static int getOption(int argc, char **argv, server_arg *arg)
{
	int c;
	static struct option long_options[] = {
		{"start", 0, 0, 'S'},
		{"list", 0, 0, 'L'},
		{"help", 0, 0, 'H'},
		{"plugdir", 1, 0, 'P'},
		{"testmode", 0, 0, 'T'},
		{0, 0, 0, 0}
	};
	memset(arg, 0, sizeof(server_arg));

	arg->plugdir = getenv(PLUGIN_ENV);
	if (arg->plugdir == NULL)
		arg->plugdir = PLUGIN_DIR;
		
	arg->testmode = 0;

	while (1)
	{
		int opt_idx = 0;

		c = getopt_long (argc, argv, "SLHRP:T", long_options, &opt_idx);
		if (c == -1)
			break;
		switch (c)
		{
		case 'S': /* Start daemon */
			arg->startserver = 1;
			break;
		case 'L': /* list of plugins */
			arg->printlist = 1;
			break;
		case 'R':
			MMSoundMgrCodecInit(arg->plugdir);
			break;
		case 'P': /* Custom plugindir */
			arg->plugdir = optarg;
			break;
		case 'T': /* Test mode */
			arg->testmode = 1;
			break;
		case 'H': /* help msg */
		default:
		return usgae(argc, argv);
		}
	}
	if (argc == 1)
		return usgae(argc, argv);
	return 0;
}

//__attribute__ ((destructor))
static void _exit_handler(int sig)
{
	int ret = MM_ERROR_NONE;
	
	ret = MMSoundMgrRunStopAll();
	if (ret != MM_ERROR_NONE) {
		debug_error("Fail to stop run-plugin\n");
	} else {
		debug_log("All run-type plugin stopped\n");
	}

	switch(sig)
	{
	case SIGINT:
		sigaction(SIGINT, &sigint_action, NULL);
		debug_error("signal(SIGINT) error");
		break;
	case SIGABRT:
		sigaction(SIGABRT, &sigabrt_action, NULL);
		debug_error("signal(SIGABRT) error");
		break;
	case SIGSEGV:
		sigaction(SIGSEGV, &sigsegv_action, NULL);
		debug_error("signal(SIGSEGV) error");
		break;
	case SIGTERM:
		sigaction(SIGTERM, &sigterm_action, NULL);
		debug_error("signal(SIGTERM) error");
		break;
	case SIGSYS:
		sigaction(SIGSYS, &sigsys_action, NULL);
		debug_error("signal(SIGSYS) error");
		break;
	default:
	    ;
	}
	raise(sig);
}

static int usgae(int argc, char **argv)
{
	fprintf(stderr, "Usage: %s [Options]\n", argv[0]);
	fprintf(stderr, "\t%-20s: start sound server.\n", "--start,-S");
	fprintf(stderr, "\t%-20s: print plugin list.\n", "--list,-L");
	fprintf(stderr, "\t%-20s: print this message.\n", "--help,-H");
	fprintf(stderr, "\t%-20s: print this message.\n", "--plugdir,-P");

	return 1;
}