f3addc49a5b1a6f91071e890a5a3b10005af07d1
[platform/core/api/tone-player.git] / test / tone_player_test.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <stdio.h>
18 #include <tone_player.h>
19 #include <glib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24 #include <signal.h>
25 #include <sound_manager.h>
26
27 #define TONE_FIRST          TONE_TYPE_DTMF_0
28 #define TONE_LAST           TONE_TYPE_USER_DEFINED_HIGH_FRE
29
30 static GMainLoop *g_mainloop;
31 static GThread *event_thread;
32 struct sigaction sig_int_old_action;
33 static int gid = -1;
34
35 static gpointer _g_mainloop_thread(gpointer data)
36 {
37         g_mainloop = g_main_loop_new(NULL, 0);
38
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);
42
43         g_main_loop_unref(g_mainloop);
44
45         return NULL;
46 }
47
48 static gboolean _g_timeout_cb(gpointer data)
49 {
50         int id = (int)data;
51         int ret = 0;
52
53         g_print("< Stopping id(%d), ", id);
54         ret = tone_player_stop(id);
55         g_print("Done! ret=0x%X >", ret);
56
57         return FALSE;
58 }
59
60
61 static void _help()
62 {
63         g_print("Usage : ");
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");
70 }
71
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)
74 {
75         g_print("stream focus callback invoked...\n");
76         return;
77 }
78
79
80 static void _tone_play_test(int from, int to, int duration, int sleep_time)
81 {
82         int i, j;
83         sound_stream_info_h stream_info = NULL;
84
85         g_print("From : %2d,   To : %2d,    Duration : %4d,  sleep_time : %4d\n", from, to, duration, sleep_time);
86
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");
89                 return;
90         }
91
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");
94                 return;
95         }
96
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);
101
102                 if (duration != sleep_time)
103                         g_timeout_add(sleep_time, _g_timeout_cb, (void *)gid);
104
105                 for (j = 0; j < sleep_time / 100; j++) {
106                         g_print(".");
107                         usleep(100*1000);
108                 }
109
110                 usleep(1000*1000);
111                 g_print(" *\n");
112         }
113
114         sound_manager_destroy_stream_information(stream_info);
115
116         g_main_loop_quit(g_mainloop);
117 }
118
119 void __sig_handler(int signo)
120 {
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);
125
126         printf("sig.num(%d)\n", signo);
127
128         switch (signo) {
129         case SIGINT:
130                 if (gid > -1) {
131                         printf("stop tone-player gid(%d)\n", gid);
132                         tone_player_stop(gid);
133                 }
134                 sigaction(SIGINT, &sig_int_old_action, NULL);
135                 raise(signo);
136                 break;
137         default:
138                 break;
139         }
140 }
141
142 int main(int argc, char **argv)
143 {
144         int from = TONE_TYPE_DTMF_0, to = TONE_TYPE_DTMF_S;
145         int duration = 500, sleep_time = -1;
146
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);
152
153         while (1) {
154                 int opt;
155                 int opt_idx = 0;
156
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'},
163                         {0, 0, 0, 0}
164                 };
165
166                 if ((opt = getopt_long(argc, argv, "f:t:d:s:h", long_options, &opt_idx)) == -1)
167                         break;
168
169                 switch (opt) {
170                 case 'f':
171                         from = atoi(optarg);
172                         if (from < TONE_FIRST)
173                                 from = TONE_FIRST;
174                         else if (from > TONE_LAST)
175                                 from = TONE_LAST;
176                         break;
177                 case 't':
178                         to = atoi(optarg);
179                         if (to < TONE_FIRST)
180                                 to = TONE_FIRST;
181                         else if (to > TONE_LAST)
182                                 to = TONE_LAST;
183                         break;
184                 case 'd':
185                         duration = atoi(optarg);
186                         break;
187                 case 's':
188                         sleep_time = atoi(optarg);
189                         break;
190                 case 'h':
191                 default:
192                         _help();
193                         return 0;
194                 }
195         }
196
197         if (sleep_time == -1)
198                 sleep_time = duration;
199
200         event_thread = g_thread_new(NULL, _g_mainloop_thread, NULL);
201
202         _tone_play_test(from, to, duration, sleep_time);
203
204         g_print("Now join the thread, ");
205         g_thread_join(event_thread);
206         g_print("Done, QUIT testsuite\n");
207
208         return 0;
209 }