speech-recognition: use a dedicated data type for passing audio samples around.
[profile/ivi/speech-recognition.git] / src / daemon / client.h
1 /*
2  * Copyright (c) 2012, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Intel Corporation nor the names of its contributors
14  *     may be used to endorse or promote products derived from this software
15  *     without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef __SRS_DAEMON_CLIENT_H__
31 #define __SRS_DAEMON_CLIENT_H__
32
33 typedef struct srs_client_s srs_client_t;
34
35 #include "src/daemon/context.h"
36 #include "src/daemon/resourceif.h"
37 #include "src/daemon/audiobuf.h"
38
39
40 /*
41  * client types
42  */
43
44 typedef enum {
45     SRS_CLIENT_TYPE_UNKNOWN = 0,
46     SRS_CLIENT_TYPE_DBUS,                /* external D-BUS client */
47     SRS_CLIENT_TYPE_BUILTIN,             /* builtin client */
48 } srs_client_type_t;
49
50
51 /*
52  * voice focus types
53  */
54
55 typedef enum {
56     SRS_VOICE_FOCUS_NONE = 0,            /* focus released */
57     SRS_VOICE_FOCUS_SHARED,              /* normal shared voice focus */
58     SRS_VOICE_FOCUS_EXCLUSIVE,           /* exclusive voice focus */
59 } srs_voice_focus_t;
60
61
62 /*
63  * client commands
64  */
65
66 #define SRS_MAX_TOKENS 64
67
68 typedef struct {
69     char **tokens;                       /* tokens of this command */
70     int    ntoken;                       /* number of tokens */
71 } srs_command_t;
72
73
74 /*
75  * special command tokens
76  */
77
78 #define SRS_TOKEN_SWITCHDICT "__switch_dict__"
79 #define SRS_TOKEN_PUSHDICT   "__push_dict__"
80 #define SRS_TOKEN_POPDICT    "__pop_dict__"
81 #define SRS_TOKEN_WILDCARD   "*"
82
83
84 /* dictionary pseudo-commands */
85 #define SRS_DICTCMD_SWITCH    "__switch_dict__"
86 #define SRS_DICTCMD_PUSH      "__push_dict__"
87 #define SRS_DICTCMD_POP       "__pop_dict__"
88
89 #define SRS_DICT_SWITCH(dict) SRS_DICTCMD_SWITCH"("dict")"
90 #define SRS_DICT_PUSH(dict)   SRS_DICTCMD_PUSH"("dict")"
91 #define SRS_DICT_POP()        SRS_DICTCMD_POP
92
93
94 /*
95  * special tokens
96  */
97
98 #define SRS_TOKEN_SWITCHDICT "__switch_dict__"
99 #define SRS_TOKEN_PUSHDICT   "__push_dict__"
100 #define SRS_TOKEN_POPDICT    "__pop_dict__"
101
102 #define SRS_TOKEN_WILDCARD "*"           /* match till end of utterance */
103
104 /*
105  * dictionary operations
106  */
107
108 /* dictionary operations */
109 typedef enum {
110     SRS_DICT_OP_UNKNOWN = 0,
111     SRS_DICT_OP_SWITCH,
112     SRS_DICT_OP_PUSH,
113     SRS_DICT_OP_POP
114 } srs_dict_op_t;
115
116
117 /*
118  * connected clients
119  */
120
121 typedef struct {
122     int (*notify_focus)(srs_client_t *c, srs_voice_focus_t focus);
123 #if 1
124     int (*notify_command)(srs_client_t *c, int idx, int ntoken,
125                           char **tokens, uint32_t *start, uint32_t *end,
126                           srs_audiobuf_t *audio);
127 #else
128     int (*notify_command)(srs_client_t *c, int idx, int ntoken,
129                           char **tokens, void *samplebuf, size_t samplelen,
130                           uint32_t *start, uint32_t *end);
131 #endif
132 } srs_client_ops_t;
133
134
135 struct srs_client_s {
136     mrp_list_hook_t         hook;        /* to list of clients */
137     srs_client_type_t       type;        /* client type */
138     char                   *name;        /* client name */
139     char                   *appclass;    /* client application class */
140     srs_command_t          *commands;    /* client command set */
141     int                     ncommand;    /* number of commands */
142     char                   *id;          /* client id */
143     srs_context_t          *srs;         /* context back pointer */
144     mrp_res_resource_set_t *rset;        /* resource set */
145     srs_voice_focus_t       requested;   /* requested voice focus */
146     srs_voice_focus_t       granted;     /* granted voice focus */
147     srs_voice_focus_t       focus;       /* requested voice focus */
148     int                     enabled : 1; /* interested in commands */
149     int                     allowed : 1; /* has resource granted */
150     srs_client_ops_t        ops;         /* client ops (notifications)  */
151     void                   *user_data;   /* opaque client data */
152 };
153
154
155 /** Create a new client. */
156 srs_client_t *client_create(srs_context_t *srs, srs_client_type_t type,
157                             const char *name, const char *appclass,
158                             char **commands, int ncommand,
159                             const char *id, srs_client_ops_t *ops,
160                             void *user_data);
161
162 /** Destroy a client. */
163 void client_destroy(srs_client_t *c);
164
165 /** Look up a client by its id. */
166 srs_client_t *client_lookup_by_id(srs_context_t *srs, const char *id);
167
168 /** Request client focus change. */
169 int client_request_focus(srs_client_t *c, srs_voice_focus_t focus);
170
171 /** Create resources for all registered clients. */
172 void client_create_resources(srs_context_t *srs);
173
174 /** Reset the resource sets of all clients. */
175 void client_reset_resources(srs_context_t *srs);
176
177 /** Deliver a resource notification event to the client. */
178 void client_resource_event(srs_client_t *c, srs_resset_event_t event);
179
180 /** Deliver a command notification event to the client. */
181 void client_notify_command(srs_client_t *c, int idx, int ntoken,
182                            const char **tokens, uint32_t *start, uint32_t *end,
183                            srs_audiobuf_t *audio);
184
185 #endif /* __SRS_DAEMON_CLIENT_H__ */