Fix SVACE defect
[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_internal.h>
21 #include <sound_manager.h>
22 #include <glib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27
28 #define FILE_PATH_MAX   128
29 #define DEFAULT_FILE    "/usr/share/sounds/alsa/Front_Center.wav"
30
31 static GMainLoop *g_mainloop = NULL;
32
33 void help()
34 {
35         printf("Usage : ");
36         printf("wav_player_test [OPTION]\n\n"
37                    "  -f, --file                file path to play\n"
38                    "  -i, --iterate             how many times to play\n"
39                    "  -h, --help                help\n");
40 }
41
42 void _player_stop_cb(int id, void *user_data)
43 {
44         printf("complete id = %d,%p\n", id, user_data);
45         sound_manager_destroy_stream_information((sound_stream_info_h)user_data);
46         g_main_loop_quit(g_mainloop);
47 }
48
49 void stream_focus_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
50 {
51         return;
52 }
53
54 void wav_play_test(const char* file_path, int iterate)
55 {
56         int ret = 0;
57         int id;
58         int i;
59         sound_stream_info_h stream_info;
60
61         if (iterate <= 0 || file_path == NULL) {
62                 printf("invalid param, iterate(%d), file_path(%s)\n", iterate, file_path);
63                 return;
64         }
65
66         if (sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, stream_focus_cb, NULL, &stream_info)) {
67                 printf("failed to create stream info\n");
68                 return;
69         }
70
71         printf("Play Wav, File Path : %s, Iterate : %d\n", file_path, iterate);
72         ret = wav_player_start_loop(file_path, stream_info, iterate, _player_stop_cb, (void*)stream_info, &id);
73         printf("wav_player_start(%d)(id=%d) ret = %d\n", i, id, ret);
74         if (ret) {
75                 sound_manager_destroy_stream_information(stream_info);
76                 return;
77         }
78
79         g_mainloop = g_main_loop_new(NULL, 0);
80         g_main_loop_run(g_mainloop);
81 }
82
83 int main(int argc, char**argv)
84 {
85         int iterate = 1;
86         char file_path[FILE_PATH_MAX] = DEFAULT_FILE;
87
88         while (1) {
89                 int opt;
90                 int opt_idx = 0;
91
92                 static struct option long_options[] = {
93                         {"iterate"    , required_argument, 0, 'i'},
94                         {"file"       , required_argument, 0, 'f'},
95                         { 0, 0, 0, 0 }
96                 };
97
98                 if ((opt = getopt_long(argc, argv, "i:f:", long_options, &opt_idx)) == -1)
99                         break;
100
101                 switch (opt) {
102                 case 'f':
103                         strncpy(file_path, optarg, FILE_PATH_MAX - 1);
104                         file_path[FILE_PATH_MAX - 1] = '\0';
105                         break;
106                 case 'i':
107                         iterate = atoi(optarg);
108                         break;
109                 case 'h':
110                 default:
111                         help();
112                         return 0;
113                 }
114         }
115
116         wav_play_test(file_path, iterate);
117
118         return 0;
119 }