1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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 #ifndef ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_FOR_C_H
18 #define ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_FOR_C_H
19 
20 #include <stdint.h>
21 #include <sys/cdefs.h>
22 #include <sys/types.h>
23 #include <errno.h>
24 
25 #include <hardware/hardware.h>
26 #include <utils/Timers.h>
27 
28 __BEGIN_DECLS
29 
30 /**
31  * Container to hold all objects / bookkeeping stuffs.
32  * Audio HAL is not supposed to touch the contents.
33  */
34 typedef struct vehicle_network_audio_helper
35 {
36     void* obj;
37 } vehicle_network_audio_helper_t;
38 
39 enum vehicle_network_audio_helper_stream {
40     VEHICLE_NETWORK_AUDIO_HELPER_STREAM_0 = 0x1,
41     VEHICLE_NETWORK_AUDIO_HELPER_STREAM_1 = 0x2,
42     VEHICLE_NETWORK_AUDIO_HELPER_STREAM_2 = 0x4,
43     VEHICLE_NETWORK_AUDIO_HELPER_STREAM_3 = 0x8,
44 };
45 
46 #define FOCUS_WAIT_DEFAULT_TIMEOUT_NS 1000000000
47 
48 /**
49  * Create helper instance with default timeout. Timer is reset when
50  * vehicle_network_audio_helper_notify_stream_started is called, and subsequent call to
51  * vehicle_network_audio_helper_get_stream_focus_state can return timeout if focus is not
52  * granted within given time.
53  * Timeout timer will also reset if focus is taken away while having focus and stream is started.
54  */
55 vehicle_network_audio_helper_t* vehicle_network_audio_helper_create(int64_t timeout);
56 
57 vehicle_network_audio_helper_t* vehicle_network_audio_helper_create_with_default_timeout();
58 
59 void vehicle_network_audio_helper_destroy(vehicle_network_audio_helper_t* helper);
60 
61 /**
62  * Notify stream start and reset focus timeout timer if it is not reset already.
63  */
64 void vehicle_network_audio_helper_notify_stream_started(vehicle_network_audio_helper_t* helper,
65         int32_t stream);
66 
67 /**
68  * Notify stream stop.
69  */
70 void vehicle_network_audio_helper_notify_stream_stopped(vehicle_network_audio_helper_t* helper,
71         int32_t stream);
72 
73 enum vehicle_network_audio_helper_focus_state {
74     VEHICLE_NETWORK_AUDIO_HELPER_FOCUS_STATE_TIMEOUT = -1,
75     VEHICLE_NETWORK_AUDIO_HELPER_FOCUS_STATE_NO_FOCUS = 0,
76     VEHICLE_NETWORK_AUDIO_HELPER_FOCUS_STATE_FOCUS = 1,
77 };
78 
79 /**
80  * Check if target stream has focus or not. This function also checks if default timeout has passed
81  * since stream was started or since focus was lost last time.
82  * @return VEHICLE_NETWORK_AUDIO_HELPER_FOCUS_STATE_FOCUS if there is focus.
83  *         VEHICLE_NETWORK_AUDIO_HELPER_FOCUS_STATE_NO_FOCUS no focus, no timeout
84  *         VEHICLE_NETWORK_AUDIO_HELPER_FOCUS_STATE_FOCUS no focus, timed out
85  */
86 int vehicle_network_audio_helper_get_stream_focus_state(
87         vehicle_network_audio_helper_t* helper, int32_t stream);
88 
89 /**
90  * Wait for focus until given timeout. It will return immediately if given stream has focus.
91  * Otherwise, it will be waiting for focus for given waitTimeNs.
92  * @return 1 if focus is available
93  *         0 if focus is not available and timeout has happened.
94  */
95 int vehicle_network_audio_helper_wait_for_stream_focus(vehicle_network_audio_helper_t* helper,
96         int32_t stream, nsecs_t waitTimeNs);
97 
98 __END_DECLS
99 
100 #endif /* ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_FOR_C_H */
101