Merge "res_musiconhold: Start playlist after initial announcement"
[asterisk/asterisk.git] / apps / app_waitforring.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Wait for Ring Application
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>extended</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/file.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/lock.h"
39
40 /*** DOCUMENTATION
41         <application name="WaitForRing" language="en_US">
42                 <synopsis>
43                         Wait for Ring Application.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="timeout" required="true" />
47                 </syntax>
48                 <description>
49                         <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
50                         and only after the next ring has completed. Returns <literal>0</literal> on success or
51                         <literal>-1</literal> on hangup.</para>
52                 </description>
53         </application>
54  ***/
55
56 static char *app = "WaitForRing";
57
58 static int waitforring_exec(struct ast_channel *chan, const char *data)
59 {
60         struct ast_frame *f;
61         struct ast_silence_generator *silgen = NULL;
62         int res = 0;
63         double s;
64         int timeout_ms;
65         int ms;
66         struct timeval start = ast_tvnow();
67
68         if (!data || (sscanf(data, "%30lg", &s) != 1)) {
69                 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
70                 return 0;
71         }
72
73         if (s < 0.0) {
74                 ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s);
75                 return 0;
76         }
77
78         if (ast_opt_transmit_silence) {
79                 silgen = ast_channel_start_silence_generator(chan);
80         }
81
82         timeout_ms = s * 1000.0;
83         while ((ms = ast_remaining_ms(start, timeout_ms))) {
84                 ms = ast_waitfor(chan, ms);
85                 if (ms < 0) {
86                         res = -1;
87                         break;
88                 }
89                 if (ms > 0) {
90                         f = ast_read(chan);
91                         if (!f) {
92                                 res = -1;
93                                 break;
94                         }
95                         if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
96                                 ast_verb(3, "Got a ring but still waiting for timeout\n");
97                         }
98                         ast_frfree(f);
99                 }
100         }
101         /* Now we're really ready for the ring */
102         if (!res) {
103                 for (;;) {
104                         int wait_res = ast_waitfor(chan, -1);
105                         if (wait_res < 0) {
106                                 res = -1;
107                                 break;
108                         } else {
109                                 f = ast_read(chan);
110                                 if (!f) {
111                                         res = -1;
112                                         break;
113                                 }
114                                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
115                                         ast_verb(3, "Got a ring after the timeout\n");
116                                         ast_frfree(f);
117                                         break;
118                                 }
119                                 ast_frfree(f);
120                         }
121                 }
122         }
123
124         if (silgen) {
125                 ast_channel_stop_silence_generator(chan, silgen);
126         }
127
128         return res;
129 }
130
131 static int unload_module(void)
132 {
133         return ast_unregister_application(app);
134 }
135
136 static int load_module(void)
137 {
138         return ast_register_application_xml(app, waitforring_exec);
139 }
140
141 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time");
142