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