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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/*
* libmm-session
*
* 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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <mm_session_private.h>
#include <mm_error.h>
#include <errno.h>
#include <audio-session-manager.h>
#include <dlog.h>
#include <glib.h>
#define EXPORT_API __attribute__((__visibility__("default")))
#define MAX_FILE_LENGTH 256
#define LOG_TAG "MMFW_SESSION"
#define debug_log(fmt, arg...) SLOG(LOG_VERBOSE, LOG_TAG, "[%s:%d] "fmt"\n", __FUNCTION__,__LINE__,##arg)
#define debug_warning(fmt, arg...) SLOG(LOG_WARN, LOG_TAG, "[%s:%d] "fmt"\n", __FUNCTION__,__LINE__,##arg)
#define debug_error(fmt, arg...) SLOG(LOG_ERROR, LOG_TAG, "[%s:%d] "fmt"\n", __FUNCTION__,__LINE__,##arg)
typedef struct {
session_callback_fn fn;
void* data;
session_msg_t param;
}session_monitor_t;
int g_call_asm_handle = -1;
int g_monitor_asm_handle = -1;
session_monitor_t g_monitor_data;
ASM_cb_result_t asm_monitor_callback(int handle, ASM_event_sources_t event_src, ASM_sound_commands_t command, unsigned int sound_status, void* cb_data);
EXPORT_API
int mm_session_init(int sessiontype)
{
debug_log("");
return mm_session_init_ex(sessiontype, NULL, NULL);
}
EXPORT_API
int mm_session_init_ex(int sessiontype, session_callback_fn callback, void* user_param)
{
int error = 0;
int result = MM_ERROR_NONE;
int ltype = 0;
debug_log("type : %d", sessiontype);
if(sessiontype != MM_SESSION_TYPE_SHARE && sessiontype != MM_SESSION_TYPE_EXCLUSIVE &&
sessiontype != MM_SESSION_TYPE_NOTIFY && sessiontype != MM_SESSION_TYPE_CALL && sessiontype != MM_SESSION_TYPE_ALARM && sessiontype != MM_SESSION_TYPE_VIDEOCALL)
{
debug_error("Invalid argument %d",sessiontype);
return MM_ERROR_INVALID_ARGUMENT;
}
result = _mm_session_util_read_type(-1, <ype);
if(MM_ERROR_INVALID_HANDLE != result)
{
debug_error("Session already initialized. Please finish current session first");
return MM_ERROR_POLICY_DUPLICATED;
}
if(sessiontype == MM_SESSION_TYPE_CALL)
{
if(!ASM_register_sound(-1, &g_call_asm_handle, ASM_EVENT_CALL, ASM_STATE_PLAYING, NULL, NULL, ASM_RESOURCE_NONE, &error))
{
debug_error("Can not register sound");
return MM_ERROR_INVALID_HANDLE;
}
}
else if(sessiontype == MM_SESSION_TYPE_VIDEOCALL)
{
if(!ASM_register_sound(-1, &g_call_asm_handle, ASM_EVENT_VIDEOCALL, ASM_STATE_PLAYING, NULL, NULL, ASM_RESOURCE_CAMERA|ASM_RESOURCE_VIDEO_OVERLAY, &error))
{
debug_error("Can not register sound");
return MM_ERROR_INVALID_HANDLE;
}
}
else
{
if(NULL == callback)
{
debug_warning("Null callback function");
}
else
{
g_monitor_data.fn = callback;
g_monitor_data.data = user_param;
if(!ASM_register_sound(-1, &g_monitor_asm_handle, ASM_EVENT_MONITOR, ASM_STATE_NONE, asm_monitor_callback, (void*)&g_monitor_data, ASM_RESOURCE_NONE, &error))
{
debug_error("Can not register monitor");
return MM_ERROR_INVALID_HANDLE;
}
}
}
result = _mm_session_util_write_type(-1, sessiontype);
if(MM_ERROR_NONE != result)
{
debug_error("Write type failed");
if(sessiontype == MM_SESSION_TYPE_CALL) {
ASM_unregister_sound(g_call_asm_handle, ASM_EVENT_CALL, &error);
}
else if(sessiontype == MM_SESSION_TYPE_VIDEOCALL) {
ASM_unregister_sound(g_call_asm_handle, ASM_EVENT_VIDEOCALL, &error);
}
else {
ASM_unregister_sound(g_monitor_asm_handle, ASM_EVENT_MONITOR, &error);
}
return result;
}
return MM_ERROR_NONE;
}
EXPORT_API
int mm_session_finish()
{
int error = 0;
int result = MM_ERROR_NONE;
int sessiontype = MM_SESSION_TYPE_SHARE;
ASM_sound_states_t state = ASM_STATE_NONE;
debug_log("");
if(g_call_asm_handle == -1)
{
if(g_monitor_asm_handle == -1)
{
//register monitor handle to get MSL status of caller process
if(!ASM_register_sound(-1, &g_monitor_asm_handle, ASM_EVENT_MONITOR, ASM_STATE_NONE, NULL, NULL, ASM_RESOURCE_NONE, &error))
{
debug_error("[%s] Can not register monitor", __func__);
return MM_ERROR_INVALID_HANDLE;
}
}
if(!ASM_get_process_session_state(g_monitor_asm_handle, &state, &error))
{
debug_error("[%s] Can not get process status", __func__);
return MM_ERROR_POLICY_INTERNAL;
}
else
{
switch(state)
{
case ASM_STATE_IGNORE:
case ASM_STATE_NONE:
break;
case ASM_STATE_PLAYING:
case ASM_STATE_WAITING:
case ASM_STATE_STOP:
case ASM_STATE_PAUSE:
case ASM_STATE_PAUSE_BY_APP:
debug_error("[%s] MSL instance still alive", __func__);
return MM_ERROR_POLICY_BLOCKED;
}
}
}
result = _mm_session_util_read_type(-1, &sessiontype);
if(MM_ERROR_NONE != result)
{
debug_error("Can not read current type");
return result;
}
if(sessiontype == MM_SESSION_TYPE_CALL)
{
if(!ASM_unregister_sound(g_call_asm_handle, ASM_EVENT_CALL, &error))
{
debug_error("\"CALL\" ASM unregister failed");
return MM_ERROR_INVALID_HANDLE;
}
g_call_asm_handle = -1;
}
else if(sessiontype == MM_SESSION_TYPE_VIDEOCALL)
{
if(!ASM_unregister_sound(g_call_asm_handle, ASM_EVENT_VIDEOCALL, &error))
{
debug_error("\"VIDEOCALL\" ASM unregister failed");
return MM_ERROR_INVALID_HANDLE;
}
g_call_asm_handle = -1;
}
else
{
if(g_monitor_asm_handle != -1) //TODO :: this is trivial check. this should be removed later.
{
if(!ASM_unregister_sound(g_monitor_asm_handle, ASM_EVENT_MONITOR, &error))
{
debug_error("ASM unregister failed");
return MM_ERROR_INVALID_HANDLE;
}
g_monitor_asm_handle = -1;
}
}
result = _mm_session_util_delete_type(-1);
if(result != MM_ERROR_NONE)
return result;
return MM_ERROR_NONE;
}
EXPORT_API
int _mm_session_util_delete_type(int app_pid)
{
pid_t mypid;
char filename[MAX_FILE_LENGTH];
if(app_pid == -1)
mypid = getpid();
else
mypid = (pid_t)app_pid;
////// DELETE SESSION TYPE /////////
snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
if(-1 == unlink(filename))
return MM_ERROR_FILE_NOT_FOUND;
////// DELETE SESSION TYPE /////////
EXPORT_API
int mm_session_finish()
{
int error = 0;
int result = MM_ERROR_NONE;
int sessiontype = MM_SESSION_TYPE_SHARE;
result = _mm_session_util_read_type(-1, &sessiontype);
if(MM_ERROR_NONE != result)
{
return result;
}
if(sessiontype == MM_SESSION_TYPE_CALL)
{
if(!ASM_unregister_sound(g_call_asm_handle, ASM_EVENT_CALL, &error))
{
return MM_ERROR_INVALID_HANDLE;
}
}
else if(sessiontype == MM_SESSION_TYPE_VIDEOCALL)
{
if(!ASM_unregister_sound(g_call_asm_handle, ASM_EVENT_VIDEOCALL, &error))
{
return MM_ERROR_INVALID_HANDLE;
}
}
result = _mm_session_util_delete_type(-1);
if(result != MM_ERROR_NONE)
return result;
return MM_ERROR_NONE;
}
return MM_ERROR_NONE;
}
EXPORT_API
int _mm_session_util_write_type(int app_pid, int sessiontype)
{
pid_t mypid;
int fd = -1;
char filename[MAX_FILE_LENGTH];
int res=0;
if(sessiontype != MM_SESSION_TYPE_SHARE && sessiontype != MM_SESSION_TYPE_EXCLUSIVE &&
sessiontype != MM_SESSION_TYPE_NOTIFY && sessiontype != MM_SESSION_TYPE_CALL && sessiontype != MM_SESSION_TYPE_ALARM && sessiontype != MM_SESSION_TYPE_VIDEOCALL)
{
return MM_ERROR_INVALID_ARGUMENT;
}
if(app_pid == -1)
mypid = getpid();
else
mypid = (pid_t)app_pid;
////// WRITE SESSION TYPE /////////
snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
fd = open(filename, O_WRONLY | O_CREAT, 0644 );
if(fd < 0)
{
debug_error("open() failed with %d",errno);
return MM_ERROR_FILE_WRITE;
}
write(fd, &sessiontype, sizeof(int));
if(0 > fchmod (fd, 00777)) {
debug_log("fchmod failed with %d", errno);
}
close(fd);
////// WRITE SESSION TYPE /////////
return MM_ERROR_NONE;
}
EXPORT_API
int _mm_session_util_read_type(int app_pid, int *sessiontype)
{
pid_t mypid;
int fd = -1;
char filename[MAX_FILE_LENGTH];
if(sessiontype == NULL)
return MM_ERROR_INVALID_ARGUMENT;
if(app_pid == -1)
mypid = getpid();
else
mypid = (pid_t)app_pid;
////// READ SESSION TYPE /////////
snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d",mypid);
fd = open(filename, O_RDONLY);
if(fd < 0)
{
return MM_ERROR_INVALID_HANDLE;
}
read(fd, sessiontype, sizeof(int));
close(fd);
////// READ SESSION TYPE /////////
return MM_ERROR_NONE;
}
gboolean _asm_monitor_cb(gpointer *data)
{
session_monitor_t* monitor = (session_monitor_t*)data;
monitor->fn(monitor->param, monitor->data);
return FALSE;
}
ASM_cb_result_t
asm_monitor_callback(int handle, ASM_event_sources_t event_src, ASM_sound_commands_t command, unsigned int sound_status, void* cb_data)
{
ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
session_monitor_t *monitor = (session_monitor_t*)cb_data;
debug_log("monitor callback called for handle %d, event_src %d", handle, event_src);
if(!monitor)
{
debug_log("monitor instance is null\n");
return ASM_CB_RES_IGNORE;
}
switch(command)
{
case ASM_COMMAND_STOP:
case ASM_COMMAND_PAUSE:
//call session_callback_fn for stop here
if(monitor->fn != NULL) {
monitor->param = MM_SESSION_MSG_STOP;
g_idle_add((GSourceFunc)_asm_monitor_cb, (gpointer)monitor);
}
cb_res = ASM_CB_RES_STOP;
break;
case ASM_COMMAND_RESUME:
case ASM_COMMAND_PLAY:
//call session_callback_fn for resume here
if(monitor->fn != NULL) {
monitor->param = MM_SESSION_MSG_RESUME;
g_idle_add((GSourceFunc)_asm_monitor_cb, (gpointer)monitor);
}
cb_res = ASM_CB_RES_NONE;;
default:
break;
}
return cb_res;
}
__attribute__ ((destructor))
void __mmsession_finalize(void)
{
_mm_session_util_delete_type(-1);
}
__attribute__ ((constructor))
void __mmsession_initialize(void)
{
}
|