1 /******************************************************************************
2  *
3  *  Copyright 2016 The Android Open Source Project
4  *  Copyright 2009-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #ifndef BTIF_A2DP_SINK_H
21 #define BTIF_A2DP_SINK_H
22 
23 #include <bluetooth/log.h>
24 
25 #include <cstdint>
26 #include <future>
27 
28 #include "bta/include/bta_av_api.h"
29 #include "stack/include/bt_hdr.h"
30 #include "types/raw_address.h"
31 
32 //
33 // Audio focus state for audio track.
34 //
35 // NOTE: The values must be same as:
36 //  - A2dpSinkStreamingStateMachine.STATE_FOCUS_LOST = 0
37 //  - A2dpSinkStreamingStateMachine.STATE_FOCUS_GRANTED = 1
38 //
39 typedef enum {
40   BTIF_A2DP_SINK_FOCUS_NOT_GRANTED = 0,
41   BTIF_A2DP_SINK_FOCUS_GRANTED = 1
42 } btif_a2dp_sink_focus_state_t;
43 
44 // Initialize the A2DP Sink module.
45 // This function should be called by the BTIF state machine prior to using the
46 // module.
47 bool btif_a2dp_sink_init(void);
48 
49 // Startup the A2DP Sink module.
50 // This function should be called by the BTIF state machine after
51 // btif_a2dp_sink_init() to prepare for receiving and processing audio
52 // streaming.
53 bool btif_a2dp_sink_startup(void);
54 
55 // Start the A2DP Sink session.
56 // This function should be called by the BTIF state machine after
57 // btif_a2dp_sink_startup() to start the streaming session for |peer_address|.
58 bool btif_a2dp_sink_start_session(const RawAddress& peer_address,
59                                   std::promise<void> peer_ready_promise);
60 
61 // Restart the A2DP Sink session.
62 // This function should be called by the BTIF state machine after
63 // btif_a2dp_sink_startup() to restart the streaming session.
64 // |old_peer_address| is the peer address of the old session. This address
65 // can be empty.
66 // |new_peer_address| is the peer address of the new session. This address
67 // cannot be empty.
68 bool btif_a2dp_sink_restart_session(const RawAddress& old_peer_address,
69                                     const RawAddress& new_peer_address,
70                                     std::promise<void> peer_ready_promise);
71 
72 // End the A2DP Sink session.
73 // This function should be called by the BTIF state machine to end the
74 // streaming session for |peer_address|.
75 bool btif_a2dp_sink_end_session(const RawAddress& peer_address);
76 
77 // Shutdown the A2DP Sink module.
78 // This function should be called by the BTIF state machine before
79 // btif_a2dp_sink_cleanup() to shutdown the processing of the audio streaming.
80 void btif_a2dp_sink_shutdown(void);
81 
82 // Cleanup the A2DP Sink module.
83 // This function should be called by the BTIF state machine during graceful
84 // cleanup.
85 void btif_a2dp_sink_cleanup(void);
86 
87 // Update the decoder for the A2DP Sink module.
88 // |p_codec_info| contains the new codec information.
89 void btif_a2dp_sink_update_decoder(const RawAddress& peer_address, const uint8_t* p_codec_info);
90 
91 // Process 'idle' request from the BTIF state machine during initialization.
92 void btif_a2dp_sink_on_idle(void);
93 
94 // Process 'stop' request from the BTIF state machine to stop A2DP streaming.
95 // |p_av_suspend| is the data associated with the request - see
96 // |tBTA_AV_SUSPEND|.
97 void btif_a2dp_sink_on_stopped(tBTA_AV_SUSPEND* p_av_suspend);
98 
99 // Process 'suspend' request from the BTIF state machine to suspend A2DP
100 // streaming.
101 // |p_av_suspend| is the data associated with the request - see
102 // |tBTA_AV_SUSPEND|.
103 void btif_a2dp_sink_on_suspended(tBTA_AV_SUSPEND* p_av_suspend);
104 
105 // Start the decoder for the A2DP Sink module.
106 bool btif_a2dp_sink_on_start(void);
107 
108 // Enable/disable discarding of received A2DP frames.
109 // If |enable| is true, the discarding is enabled, otherwise is disabled.
110 void btif_a2dp_sink_set_rx_flush(bool enable);
111 
112 // Enqueue a buffer to the A2DP Sink queue. If the queue has reached its
113 // maximum size |MAX_INPUT_A2DP_FRAME_QUEUE_SZ|, the oldest buffer is
114 // removed from the queue.
115 // |p_buf| is the buffer to enqueue.
116 // Returns the number of buffers in the Sink queue after the enqueing.
117 uint8_t btif_a2dp_sink_enqueue_buf(BT_HDR* p_buf);
118 
119 // Dump debug-related information for the A2DP Sink module.
120 // |fd| is the file descriptor to use for writing the ASCII formatted
121 // information.
122 void btif_a2dp_sink_debug_dump(int fd);
123 
124 // Create a request to set the audio focus state for the audio track.
125 // |state| is the new state value - see |btif_a2dp_sink_focus_state_t|
126 // for valid values.
127 void btif_a2dp_sink_set_focus_state_req(btif_a2dp_sink_focus_state_t state);
128 
129 // Set the audio track gain for the audio track.
130 // |gain| is the audio track gain value to use.
131 void btif_a2dp_sink_set_audio_track_gain(float gain);
132 
133 // Get audio track handle
134 void* btif_a2dp_sink_get_audio_track(void);
135 
136 namespace std {
137 template <>
138 struct formatter<btif_a2dp_sink_focus_state_t> : enum_formatter<btif_a2dp_sink_focus_state_t> {};
139 }  // namespace std
140 
141 #endif /* BTIF_A2DP_SINK_H */
142