Sound Manager
R:/SoundManagerBinding/doc/ApplicationGuide.md
Go to the documentation of this file.
1 **Sound Manager Application Guide**
2 ====
3 <div align="right">Revision: 0.2Beta</div>
4 <div align="right">TOYOTA MOTOR CORPORATION</div>
5 <div align="right">Advanced Driver Information Technology</div>
6 <div align="right">2nd/Oct/2017</div>
7 
8 * * *
9 
10 ## **<div id="Table\ of\ content">Table of content</div>**
11 - [Target reader of this document](#Target\ reader\ of\ this\ document)
12 - [Overview](#Overview)
13 - [Getting Start](#Getting\ Start)
14  - [Supported environment](#Supported\ environment)
15  - [Build](#Build)
16  - [Configuring](#Configuring)
17  - [Additional work](#Additional\ work)
18  - [How to call Sound Manager's APIs from your Application?](#How\ to\ call\ Sound\ Manager\ APIs\ from\ your\ Application?)
19 - [Supported usecase](#Supported\ usecase)
20 - [Software Architecture](#Software\ Architecture)
21 - [API reference](#API\ reference)
22  - [APIs](#APIs)
23  - [Events](#Events)
24 - [Sequence](#Sequence)
25  - [StartUp](#StartUp)
26  - [Registration](#Registration)
27  - [Request Sound Right](#Request\ Sound\ Right)
28  - [Connect Sound Route](#Connect\ Sound\ Route)
29  - [Start Sound Streaming](#Start\ Sound\ Streaming)
30  - [Stop Sound Streaming](#Stop\ Sound\ Streaming)
31  - [Disconnect Sound Route](#Disconnect\ Sound\ Route)
32  - [Change Volume](#Change\ Volume)
33  - [Set Mute State](#Set\ Mute\ State)
34  - [Release Sound Right](#Release\ Sound\ Right)
35  - [Audio Domain](#Audio\ Domain)
36 - [Sample code](#Sample\ code)
37 - [Limitation](#Limitation)
38 
39 * * *
40 
41 ## **<div id="Target\ reader\ of\ this\ document">Target reader of this document</div>**
42 Application developer whose software uses sound output.
43 
44 * * *
45 
46 ## **<div id="Overview">Overview</div>**
47 The sound manager is the service which provides **sound-right management** for multiple sound sources.
48 This service based on GENIVI Audio Manager, and this package contains service binder and library for API calling.
49 The reason why this service based on GENIVI Audio Manager is because the sound manager supports highly strong and flexible sound-right management function.
50 
51 In order to understand, the below figure shows the one of typical usecases.
52 In this example, there are four sound mode.
53 1. Audio Off
54 2. Media Player
55 3. Tel (Ring and talking)
56 4. TTS (Text To Speech; typically it's used by Navigation sound)
57 ![Figure: Typical usecase](parts/typical-usecase.png)
58 
59 The important points are:
60 - **There is a priority for each sound source.**
61  In this example, "Tel" and "TTS" is stronger than "MediaPlayer". Therefore when the system got incoming call, all four outputs of MediaPlayer are muted automatically by Sound Manager. And in this timing, Sound Manager will issue the event to Media Player, then Media Player can stop the music. (Because depends on OEM's requirement, "Stop" is required, not "Mute".)
62  "Tel" and "TTS" have the same priority. So if TTS event happened on talking, each sound will output from independent speaker.
63  If on-hook button is touched, Sound Manager will resume previous sound mode. In this example, basically it's MediaPlayer sound. But if TTS still playing, three speaker will output MediaPlayer sound but one speaker will continue to output TTS sound.
64 - **Sound mode transition should be done by Sound Manager not Applications.**
65  Actually application cannot recognize all sound source and its priority, so some centerized manager is required. Sound Manager provides this function. Sound Manager has a database for usecase and priority and in line with this policy Sound Manager controls proper sound mode.
66 
67 
68 The below links show the example of Sound/Window mode transition.
69 * [Single window application](Display_Audio_Transition1.md)
70  This transition assumes target IVI system support only one window on screen. It's a similar transition to CES2017 demo.
71 * [Dual window application](Display_Audio_Transition2.md)
72  This transition assumes target IVI system support two window (split screen) on screen.
73 
74  Of course user can customize shortcut menu, but since it's too many states so this example limits shortcut menu as "Home", "MediaPlayer", "HVAC" and "Navigation".
75 
76 * * *
77 
78 ## **<div id="Getting\ Start">Getting Start</div>**
79 
80 ### **<div id="Supported\ environment">Supported environment</div>**
81 | Item | Description |
82 |:------------|:----------------------------------|
83 | AGL version | Daring Dab |
84 | Hardware | Renesas R-Car Starter Kit Pro(M3) |
85 
86 
87 ### **<div id="Build">Build</div>**
88 
89 You can make Sound Manager object files by the following two stage operations.
90 
91 **Download recipe**
92 If repo is already done, please start with git clone
93 ```
94 $ mkdir WORK
95 $ cd WORK
96 $ repo init -b dab -m dab_4.0.0_xml -u https://gerrit.automotivelinux.org/gerrit/AGL/AGL-repo
97 $ repo sync
98 $ git clone https://gerrit.automotivelinux.org/gerrit/staging/meta-hmi-framework
99 
100 ```
101 
102 Then you can get the following recipe.
103 * `meta-hmi-framework/agl-service-soundmanager-2017`
104 
105 
106 **Bitbake**
107 ```
108 $ source meta-agl/scripts/aglsetup.sh -m m3ulcb agl-demo agl-devel agl-appfw-smack hmi-framework
109 $ bitbake agl-service-soundmanager-2017
110 ```
111 
112 
113 * * *
114 
115 ### **<div id="Configuring">Configuring</div>**
116 To use Sound Manager API, an application shall paste the following configuration definition into "config.xml" of application.
117 ```
118 <feature name="urn:AGL:widget:required-api">
119  <param name="soundmanager" value="ws" />
120 </feature>
121 ```
122 
123 * * *
124 
125 ### **<div id="Additional\ work">Additional work</div>**
126 
127 **Add Policy file**
128 
129 To add Sound Manager Domain into policy, put the following file to /etc/audiomanager/control.
130 
131 ` soundmanager/conf/audiomanager-config-sample/configuration.xml`
132 
133 
134 **Remove Module router of Pulse Audio**
135 
136 Because the module rounter access to Audio manager for getting sound right instead of application in CES2017, after you changed your application code for Sound manager, you should remove the module router.
137 To do this, you have to comment out line.143 of /etc/pulse/default.pa as below.
138 
139 ```
140 .ifexists module-router.so
141 #load-module module-router
142 .endif
143 
144 ```
145 
146 
147 * * *
148 
149 ### **<div id="How\ to\ call\ Sound\ Manager\ APIs\ from\ your\ Application?">How to call Sound Manager APIs from your Application?</div>**
150 Sound Manager provides a library which is called "libsoundmanager".
151 This library provides function style API calling interface. So you should include libsoundmanager.hpp headerfile, and should link this library.
152 Please also refer sample application and template.
153 
154 
155 See also our [Sample code](#Sample\ code).
156 
157 
158 <br />
159 
160 * * *
161 
162 ## **<div id="Supported\ usecase">Supported usecase</div>**
163 1. Active source change
164  - When user choose different audio source with current one, IVI system stop or pause current source and activate new one.
165  - When user connect external device e.g. iPhone, USB memory IVI system change active source automatically to connected one.
166 2. Active source locking
167  - When user is in phone call, IVI restrict to change active source.
168 3. Interrupt source mixing
169  - When car close to cross road IVI system reduce the volume of current source and mix with interrupt source e.g. Navigation Guidance.
170 4. Volume change
171  - User can change the volume of active source or sink.
172  - When user change volume during interruption e.g. Navigation Guidance, IVI system change its volume temporary or permanently.
173 5. Mute/unmute
174  - User can mute/unmute current active source.
175 6. Volume management
176  - When user change active source, IVI system mute/unmute to avoid distortion of sound.
177 7. Volume acceleration
178  - When road noise is increased by speed, IVI system automatically change the volume of active source.
179 8. Routing sound
180  - System needs to route sound stream to proper zones. (driver zone, passenger zone, rear seat zone)
181 
182 [See also this page](https://wiki.automotivelinux.org/eg-ui-graphics-req-audiorouting)
183 
184 * * *
185 
186 ## **<div id="Software\ Architecture">Software Architecture</div>**
187 The architecture of Sound Manager is shown below.
188 Sound Manager is the service designed to be used by multiple applications.
189 Therefore Sound Manager framework consists on two binder layers. Please refer the following figure.
190 The upper binder is for application side security context for applications. The lower binder is for servide side security context.
191 Usually application side binder has some business logic for each application, so the number of binders depend on the number of applications which use Sound Manager.
192 On the other hand, regarding lower binder there is only one module in the system. This binder receives all messages from multiple applications (in detail, it comes from upper layer binder).
193 
194 The communication protocols between libsoundmanager and upper binder, upper binder and lower binder, lower binder (soundmanager-binding) and AudioManager are WebSocket.
195 
196 ![software-stack.png](parts/software-stack.png)
197 
198 * * *
199 
200 ## **<div id="API\ reference">API reference</div>**
201 "libsoundmanager" and "soundmanager_binding" provides several kinds of APIs, and these APIs basically correspond to GENIVI Audio Manager API. (Some APIs are Sound Manager original functions.)
202 
203 For understanding, GENIVI Audio Manager stands for one core module and three plug-ins.
204 1. AudioManagerDaemon
205  This is a core module of Audio Manager.
206 2. AudioManagerCommandPlugin
207  This is a command interface for Audio Manager.
208 3. AudioManagerController
209  This plug-in can be used for sound-right management.
210 4. AudioManagerRountingPlugin
211  This plug-in abstracts the hardware and software. And sometimes there may be multiple plug-ins.
212 
213 *) [See also GENIVI AudioManager Components](http://docs.projects.genivi.org/AudioManager/audiomanagercomponentspage.html)
214 
215 ![See also GENIVI AudioManager Components](parts/am-component.png)
216 (This figure was copied from GENIVI Web page.)
217 
218 
219 ### **<div id="APIs">APIs</div>**
220 
221 - [init(int port, const std::string& token)]()
222 - [registerSource(const std::string& sourceName)](http://docs.projects.genivi.org/AudioManager/a00053.html#acadce23459d94cec496d17700cbde230)
223 - [connect(int sourceID, int sinkID)](http://docs.projects.genivi.org/AudioManager/a00033.html#a62d8f5aee1e601d59f993c5a5561e234)
224 - [connect(int sourceID, const std::string& sinkName = "default")](http://docs.projects.genivi.org/AudioManager/a00033.html#a62d8f5aee1e601d59f993c5a5561e234)
225 - [disconnect(int connectionID)](http://docs.projects.genivi.org/AudioManager/a00033.html#aa24d0146f4e3c75e02d6c0152e246da1)
226 - [ackSetSourceState(int sourceID, int handle, int errno)](http://docs.projects.genivi.org/AudioManager/a00053.html#a11f6b0378a50296a72107d6a1fa7ec21)
227 - [LibSoundmanager ()](api-ref/html/class_lib_soundmanager.html#a8b51e9891813cb62dd12109c017ad106)
228 - [set_event_handler(enum EventType_AsyncSetSourceState et, handler_asyncSetSourceState f)]()
229 - [register_callback( void (*event_cb)(const std::string& event, struct json_object* event_contents), void (*reply_cb)(struct json_object* reply_contents), void (*hangup_cb)(void) = nullptr)]()
230 - [register_callback( void (*reply_cb)(struct json_object* reply_contents), void (*hangup_cb)(void) = nullptr)]()
231 
232 
233 The below APIs will be available at RC2.
234 
235 - [setVolume (const am_sinkID_t sinkID, const am_mainVolume_t volume)](http://docs.projects.genivi.org/AudioManager/a00033.html#a6d47bc67473d75495260abe8c666fc7e)
236 - [volumeStep (const am_sinkID_t sinkID, const int16_t volumeStep)](http://docs.projects.genivi.org/AudioManager/a00033.html#ad7a4c1fe5a2ecfaae5484a14d8820e58)
237 - [setSinkMuteState (const am_sinkID_t sinkID, const am_MuteState_e muteState)](http://docs.projects.genivi.org/AudioManager/a00033.html#afae22041843c5349be16a6593d3ebb9c)
238 - [getListMainConnections (std::vector< am_MainConnectionType_s > &listConnections)](http://docs.projects.genivi.org/AudioManager/a00033.html#a59d10a7178e3227d0b8f415308c71179)
239 - [confirmRoutingReady (const uint16_t handle, const am_Error_e error)](http://docs.projects.genivi.org/AudioManager/a00053.html#a1dd1b89cccffeaafb1a3c11cebd7e48c)
240 
241 
242 ### **<div id="Events">Events</div>**
243 
244 - [EventType_AsyncSetSourceState]()
245 
246 The below Events will be available at RC2.
247 
248 - [EventType_NewMainConnection](http://docs.projects.genivi.org/AudioManager/a00034.html#a69ada9e19c65c1d078d8a5f473d08586)
249 - [EventType_RemovedMainConnection](http://docs.projects.genivi.org/AudioManager/a00034.html#aa3b5906bcf682cff155fb24d402efd89)
250 - [EventType_MainConnectionStateChanged](http://docs.projects.genivi.org/AudioManager/a00034.html#a32aa8ab84632805a876e023a7aead810)
251 - [EventType_VolumeChanged](http://docs.projects.genivi.org/AudioManager/a00034.html#a4494fdd835137e572f2cf4a3aceb6ae5)
252 - [EventType_SinkMuteStateChanged](http://docs.projects.genivi.org/AudioManager/a00034.html#a6068ce59089fbdc63aec81e778aba238)
253 - [EventType_setRoutingReady](http://docs.projects.genivi.org/AudioManager/a00054.html#a7a4d410e30df0e8240d25a57e3c72c6b)
254 - [EventType_asyncConnect](http://docs.projects.genivi.org/AudioManager/a00054.html#a8a81297be9c64511e27d85444c59b0d6)
255 - [EventType_asyncSetSourceState](http://docs.projects.genivi.org/AudioManager/a00054.html#ab02d93d54ee9cd98776a3f2d274ee24d)
256 - [EventType_asyncDisconnect](http://docs.projects.genivi.org/AudioManager/a00054.html#a93ae95515730eb615ab5dfc1316d7862)
257 
258 * * *
259 
260 ## **<div id="Sequence">Sequence</div>**
261 ### **<div id="StartUp">StartUp</div>**
262 ![seq_startup.png](parts/seq_startup.svg)
263 
264 ### **<div id="Registration">Registration</div>**
265 ![seq_registration.png](parts/seq_registration.svg)
266 
267 ### **<div id="Request\ Sound\ Right">Request Sound Right</div>**
268 ![seq_requestsoundmode.png](parts/seq_requestsoundmode.svg)
269 
270 ### **<div id="Connect\ Sound\ Route">Connect Sound Route</div>**
271 ![seq_connectsoundroute.png](parts/seq_connectsoundroute.svg)
272 
273 ### **<div id="Start\ Sound\ Streaming">Start Sound Streaming</div>**
274 ![seq_startsoundstreaming.png](parts/seq_startsoundstreaming.svg)
275 
276 ### **<div id="Stop\ Sound\ Streaming">Stop Sound Streaming</div>**
277 ![seq_stopsoundstreaming.png](parts/seq_stopsoundstreaming.svg)
278 
279 ### **<div id="Disconnect\ Sound\ Route">Disconnect Sound Route</div>**
280 ![seq_disconnectsoundroute.png](parts/seq_disconnectsoundroute.svg)
281 
282 ### **<div id="Change\ Volume">Change Volume</div>**
283 ![seq_changevolume.png](parts/seq_changevolume.svg)
284 
285 ### **<div id="Set\ Mute\ State">Set Mute State</div>**
286 ![seq_setmutestate.png](parts/seq_setmutestate.svg)
287 
288 ### **<div id="Release\ Sound\ Right">Release Sound Right</div>**
289 ![seq_releasesoundmode.png](parts/seq_releasesoundmode.svg)
290 
291 * * *
292 
293 ### **<div id="Audio\ Domain">Audio Domain</div>**
294 
295 One of the most important concept of Audio Manager is Audio Domain.
296 To use GENIVI Audio Manager based system, it may be better to understand this concept.
297 The below document should bring good understanding.
298 
299 [GENIVI Audio Manager: Generic Controller Plug-in](http://events.linuxfoundation.org/sites/events/files/slides/AGL_AMM_presentation_A01.pdf)
300 
301 Although strongly recommended to read whole pages, but you can get quick understanding by page.10 to 14.
302 
303 
304 # **<div id="Sample\ code">Sample code</div>**
305 You can find sample implementation of Sound Manager as below.
306 * `apps/agl-service-homescreen-2017/sample/template`
307 * `apps/agl-service-homescreen-2017/sample/radio`
308 * `apps/agl-service-homescreen-2017/sample/mediaplayer`
309 
310 
311 
312 # **<div id="Limitation">Limitation</div>**
313 * Minimum APIs and Events are prepared for RC1, the following APIs will be available for RC2.
314 
315  * setVolume
316  * volumeStep
317  * setSinkMuteState
318  * getListMainConnections
319  * confirmRoutingReady
320  * EventType_NewMainConnection
321  * EventType_RemovedMainConnection
322  * EventType_MainConnectionStateChanged
323  * EventType_VolumeChanged
324  * EventType_SinkMuteStateChanged
325  * EventType_setRoutingReady
326  * EventType_asyncConnect
327  * EventType_asyncSetSourceState
328  * EventType_asyncDisconnect
329 
330