Fix GlobalVariable violation
[platform/core/api/wav-player.git] / test / wav_player_test.c
1 /*
2 * Copyright (c) 2011-2016 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 <wav_player.h>
19 #include <wav_player_internal.h>
20 #include <sound_manager.h>
21 #include <sound_manager_internal.h>
22 #include <glib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27 #include <signal.h>
28
29 #define FILE_PATH_MAX   128
30 #define DEFAULT_FILE    "/usr/share/sounds/alsa/Front_Center.wav"
31
32 static GMainLoop *g_mainloop = NULL;
33 static int gid = -1;
34 static struct sigaction sig_int_old_action;
35
36 void help(void)
37 {
38         g_message("Usage : ");
39         g_message("wav_player_test [OPTION]\n\n"
40                    "  -f, --file                File path to play\n"
41                    "  -s, --streamtype          Stream type:\n"
42                    "                               0: media (default)\n"
43                    "                               1: system\n"
44                    "                               2: alarm\n"
45                    "                               3: notification\n"
46                    "                               4: emergency\n"
47                    "                               5: voice information\n"
48                    "                               7: ringtone voip\n"
49                    "                               8: voip\n"
50                    "                               9: ringtone voip\n"
51                    "                             107: solo (internal)\n"
52                    "  -i, --iterate             Number of times to play (default:1)\n"
53                    "  -q, --quickplay           Play quick\n"
54                    "  -o, --stopothers          Play after stopping others\n"
55                    "  -h, --help                Help\n");
56 }
57
58 static void __player_stop_cb(int id, void *user_data)
59 {
60         g_message("complete id = %d,%p\n", id, user_data);
61         sound_manager_destroy_stream_information((sound_stream_info_h)user_data);
62         g_main_loop_quit(g_mainloop);
63 }
64
65 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,
66                         sound_stream_focus_change_reason_e reason, int sound_behavior, const char *extra_info, void *user_data)
67 {
68         g_message("*** FOCUS callback is called, stream_info(%p) ***", stream_info);
69
70         g_message(" - changed focus: mask(%x), state(%d) (0:released, 1:acquired)", focus_mask, focus_state);
71         g_message(" - change_reason(%d), behavior(0x%x), extra_info(%s), user_data(%p)", reason, sound_behavior, extra_info, user_data);
72 }
73
74 static void __sig_handler(int signo)
75 {
76         sigset_t old_mask, all_mask;
77         sigfillset(&all_mask);
78         sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
79         sigprocmask(SIG_SETMASK, &old_mask, NULL);
80
81         g_message("sig.num(%d)", signo);
82
83         switch (signo) {
84         case SIGINT:
85                 if (gid > -1) {
86                         g_message("stop wav-player");
87                         wav_player_stop(gid);
88                 }
89                 sigaction(SIGINT, &sig_int_old_action, NULL);
90                 raise(signo);
91                 break;
92         default:
93                 break;
94         }
95 }
96
97 int wav_play_test(const char *file_path, int iterate, int stream_type, bool stop_others)
98 {
99         int ret = 0;
100         sound_stream_info_h stream_info;
101
102         if (!file_path) {
103                 g_message("invalid param, null file_path");
104                 return -1;
105         }
106
107         if (stream_type >= SOUND_STREAM_TYPE_RINGTONE_CALL) {
108                 ret = sound_manager_create_stream_information_internal((sound_stream_type_internal_e)stream_type, __stream_focus_cb, NULL, &stream_info);
109                 if (ret) {
110                         g_message("failed to create stream info internal");
111                         return ret;
112                 }
113         } else {
114                 ret = sound_manager_create_stream_information((sound_stream_type_e)stream_type, __stream_focus_cb, NULL, &stream_info);
115                 if (ret) {
116                         g_message("failed to create stream info");
117                         return ret;
118                 }
119         }
120
121         g_message("Play Wav, File Path : %s, Iterate : %d", file_path, iterate);
122         if (stop_others) {
123                 ret = wav_player_start_loop_stop_others(file_path, stream_info, iterate, __player_stop_cb, (void*)stream_info, &gid);
124                 g_message("wav_player_start_loop_stop_others(id=%d) ret = %d", gid, ret);
125         } else {
126                 ret = wav_player_start_loop(file_path, stream_info, iterate, __player_stop_cb, (void*)stream_info, &gid);
127                 g_message("wav_player_start_loop(id=%d) ret = %d", gid, ret);
128         }
129
130         if (ret) {
131                 sound_manager_destroy_stream_information(stream_info);
132                 return ret;
133         }
134
135         g_mainloop = g_main_loop_new(NULL, 0);
136         g_assert(g_mainloop);
137
138         g_main_loop_run(g_mainloop);
139
140         g_main_loop_unref(g_mainloop);
141
142         return ret;
143 }
144
145 int wav_play_test_quick(const char *file_path)
146 {
147         int ret = 0;
148
149         if (!file_path) {
150                 g_message("invalid file path");
151                 return -1;
152         }
153
154         g_message("Play Wav, File Path : %s", file_path);
155
156         ret = wav_player_play_simple(file_path, "system");
157         g_message("wav_player_play_simple() ret = 0x%X", ret);
158
159         return ret;
160 }
161
162 int main(int argc, char**argv)
163 {
164         int iterate = 1;
165         int stream_type = 0;
166         bool quickplay = false;
167         bool stop_others = false;
168         char file_path[FILE_PATH_MAX] = DEFAULT_FILE;
169         struct sigaction sig_action;
170         sig_action.sa_handler = __sig_handler;
171         sig_action.sa_flags = SA_NOCLDSTOP;
172         sigemptyset(&sig_action.sa_mask);
173         sigaction(SIGINT, &sig_action, &sig_int_old_action);
174
175         while (1) {
176                 int opt;
177                 int opt_idx = 0;
178
179                 static struct option long_options[] = {
180                         {"iterate"    , required_argument, 0, 'i'},
181                         {"file"       , required_argument, 0, 'f'},
182                         {"streamtype" , required_argument, 0, 's'},
183                         {"quickplay"  , no_argument,       0, 'q'},
184                         {"stopothers" , no_argument,       0, 'o'},
185                         {"help"       , no_argument,       0, 'h'},
186                         { 0, 0, 0, 0 }
187                 };
188
189                 if ((opt = getopt_long(argc, argv, "i:f:s:qoh", long_options, &opt_idx)) == -1)
190                         break;
191
192                 g_message("opt = %d(%c), opt_idx = %d", opt, opt, opt_idx);
193
194                 switch (opt) {
195                 case 'i':
196                         iterate = atoi(optarg);
197                         break;
198                 case 'f':
199                         snprintf(file_path, FILE_PATH_MAX, "%s", optarg);
200                         break;
201                 case 's':
202                         stream_type = atoi(optarg);
203                         break;
204                 case 'q':
205                         quickplay = true;
206                         break;
207                 case 'o':
208                         stop_others = true;
209                         break;
210                 case 'h':
211                 default:
212                         help();
213                         return 0;
214                 }
215         }
216
217         if (quickplay)
218                 return wav_play_test_quick(file_path);
219
220         return wav_play_test(file_path, iterate, stream_type, stop_others);
221 }