2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <tone_player.h>
25 #include <sound_manager.h>
27 #define TONE_FIRST TONE_TYPE_DTMF_0
28 #define TONE_LAST TONE_TYPE_USER_DEFINED_HIGH_FRE
30 static GMainLoop *g_mainloop;
31 static GThread *event_thread;
32 struct sigaction sig_int_old_action;
35 static gpointer _g_mainloop_thread(gpointer data)
37 g_mainloop = g_main_loop_new(NULL, 0);
39 g_print("now run mainloop %p\n", g_mainloop);
40 g_main_loop_run(g_mainloop);
41 g_print("mainloop %p quit...\n", g_mainloop);
43 g_main_loop_unref(g_mainloop);
48 static gboolean _g_timeout_cb(gpointer data)
53 g_print("< Stopping id(%d), ", id);
54 ret = tone_player_stop(id);
55 g_print("Done! ret=0x%X >", ret);
64 g_print("tone_player_test [OPTION]\n\n"
65 " -f, --from from which tone type\n"
66 " -t, --to to which tone type\n"
67 " -d, --duration duration(ms)\n"
68 " -s, --sleep sleep time after play start(ms)\n"
69 " -h, --help help\n");
72 static void _stream_focus_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
73 sound_stream_focus_change_reason_e reason, int sound_behavior, const char *extra_info, void *user_data)
75 g_print("stream focus callback invoked...\n");
80 static void _tone_play_test(int from, int to, int duration, int sleep_time)
83 sound_stream_info_h stream_info = NULL;
85 g_print("From : %2d, To : %2d, Duration : %4d, sleep_time : %4d\n", from, to, duration, sleep_time);
87 if (from < TONE_FIRST || to < TONE_FIRST || duration < 0 || from > to || from > TONE_LAST || to > TONE_LAST || sleep_time < 0) {
88 g_print("Wrong Parameter\n");
92 if (sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _stream_focus_cb, NULL, &stream_info)) {
93 g_print("failed to create stream info\n");
97 for (i = from; i <= to; i++) {
98 g_print("* Playing Tone : %3d -> ", i);
99 tone_player_start_new(i, stream_info, duration, &gid);
100 g_print("gid(%d) ", gid);
102 if (duration != sleep_time)
103 g_timeout_add(sleep_time, _g_timeout_cb, (void *)gid);
105 for (j = 0; j < sleep_time / 100; j++) {
114 sound_manager_destroy_stream_information(stream_info);
116 g_main_loop_quit(g_mainloop);
119 void __sig_handler(int signo)
121 sigset_t old_mask, all_mask;
122 sigfillset(&all_mask);
123 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
124 sigprocmask(SIG_SETMASK, &old_mask, NULL);
126 printf("sig.num(%d)\n", signo);
131 printf("stop tone-player gid(%d)\n", gid);
132 tone_player_stop(gid);
134 sigaction(SIGINT, &sig_int_old_action, NULL);
142 int main(int argc, char **argv)
144 int from = TONE_TYPE_DTMF_0, to = TONE_TYPE_DTMF_S;
145 int duration = 500, sleep_time = -1;
147 struct sigaction sig_action;
148 sig_action.sa_handler = __sig_handler;
149 sig_action.sa_flags = SA_NOCLDSTOP;
150 sigemptyset(&sig_action.sa_mask);
151 sigaction(SIGINT, &sig_action, &sig_int_old_action);
157 static struct option long_options[] = {
158 {"from", required_argument, 0, 'f'},
159 {"to", required_argument, 0, 't'},
160 {"duration", required_argument, 0, 'd'},
161 {"sleep", required_argument, 0, 's'},
162 {"help", no_argument, 0, 'h'},
166 if ((opt = getopt_long(argc, argv, "f:t:d:s:h", long_options, &opt_idx)) == -1)
172 if (from < TONE_FIRST)
174 else if (from > TONE_LAST)
181 else if (to > TONE_LAST)
185 duration = atoi(optarg);
188 sleep_time = atoi(optarg);
197 if (sleep_time == -1)
198 sleep_time = duration;
200 event_thread = g_thread_new(NULL, _g_mainloop_thread, NULL);
202 _tone_play_test(from, to, duration, sleep_time);
204 g_print("Now join the thread, ");
205 g_thread_join(event_thread);
206 g_print("Done, QUIT testsuite\n");