bt_av.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (C) 2012 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. #ifndef ANDROID_INCLUDE_BT_AV_H
  17. #define ANDROID_INCLUDE_BT_AV_H
  18. #include <vector>
  19. #include <hardware/bluetooth.h>
  20. __BEGIN_DECLS
  21. /* Bluetooth AV connection states */
  22. typedef enum {
  23. BTAV_CONNECTION_STATE_DISCONNECTED = 0,
  24. BTAV_CONNECTION_STATE_CONNECTING,
  25. BTAV_CONNECTION_STATE_CONNECTED,
  26. BTAV_CONNECTION_STATE_DISCONNECTING
  27. } btav_connection_state_t;
  28. /* Bluetooth AV datapath states */
  29. typedef enum {
  30. BTAV_AUDIO_STATE_REMOTE_SUSPEND = 0,
  31. BTAV_AUDIO_STATE_STOPPED,
  32. BTAV_AUDIO_STATE_STARTED,
  33. } btav_audio_state_t;
  34. /*
  35. * Enum values for each A2DP supported codec.
  36. * There should be a separate entry for each A2DP codec that is supported
  37. * for encoding (SRC), and for decoding purpose (SINK).
  38. */
  39. typedef enum {
  40. BTAV_A2DP_CODEC_INDEX_SOURCE_MIN = 0,
  41. // Add an entry for each source codec here.
  42. // NOTE: The values should be same as those listed in the following file:
  43. // BluetoothCodecConfig.java
  44. BTAV_A2DP_CODEC_INDEX_SOURCE_SBC = 0,
  45. BTAV_A2DP_CODEC_INDEX_SOURCE_AAC,
  46. BTAV_A2DP_CODEC_INDEX_SOURCE_APTX,
  47. BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD,
  48. BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC,
  49. BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
  50. BTAV_A2DP_CODEC_INDEX_SINK_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
  51. // Add an entry for each sink codec here
  52. BTAV_A2DP_CODEC_INDEX_SINK_SBC = BTAV_A2DP_CODEC_INDEX_SINK_MIN,
  53. BTAV_A2DP_CODEC_INDEX_SINK_AAC,
  54. BTAV_A2DP_CODEC_INDEX_SINK_LDAC,
  55. BTAV_A2DP_CODEC_INDEX_SINK_MAX,
  56. BTAV_A2DP_CODEC_INDEX_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN,
  57. BTAV_A2DP_CODEC_INDEX_MAX = BTAV_A2DP_CODEC_INDEX_SINK_MAX
  58. } btav_a2dp_codec_index_t;
  59. typedef enum {
  60. // Disable the codec.
  61. // NOTE: This value can be used only during initialization when
  62. // function btav_source_interface_t::init() is called.
  63. BTAV_A2DP_CODEC_PRIORITY_DISABLED = -1,
  64. // Reset the codec priority to its default value.
  65. BTAV_A2DP_CODEC_PRIORITY_DEFAULT = 0,
  66. // Highest codec priority.
  67. BTAV_A2DP_CODEC_PRIORITY_HIGHEST = 1000 * 1000
  68. } btav_a2dp_codec_priority_t;
  69. typedef enum {
  70. BTAV_A2DP_CODEC_SAMPLE_RATE_NONE = 0x0,
  71. BTAV_A2DP_CODEC_SAMPLE_RATE_44100 = 0x1 << 0,
  72. BTAV_A2DP_CODEC_SAMPLE_RATE_48000 = 0x1 << 1,
  73. BTAV_A2DP_CODEC_SAMPLE_RATE_88200 = 0x1 << 2,
  74. BTAV_A2DP_CODEC_SAMPLE_RATE_96000 = 0x1 << 3,
  75. BTAV_A2DP_CODEC_SAMPLE_RATE_176400 = 0x1 << 4,
  76. BTAV_A2DP_CODEC_SAMPLE_RATE_192000 = 0x1 << 5,
  77. BTAV_A2DP_CODEC_SAMPLE_RATE_16000 = 0x1 << 6,
  78. BTAV_A2DP_CODEC_SAMPLE_RATE_24000 = 0x1 << 7
  79. } btav_a2dp_codec_sample_rate_t;
  80. typedef enum {
  81. BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE = 0x0,
  82. BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 = 0x1 << 0,
  83. BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 = 0x1 << 1,
  84. BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32 = 0x1 << 2
  85. } btav_a2dp_codec_bits_per_sample_t;
  86. typedef enum {
  87. BTAV_A2DP_CODEC_CHANNEL_MODE_NONE = 0x0,
  88. BTAV_A2DP_CODEC_CHANNEL_MODE_MONO = 0x1 << 0,
  89. BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO = 0x1 << 1
  90. } btav_a2dp_codec_channel_mode_t;
  91. /*
  92. * Structure for representing codec capability or configuration.
  93. * It is used for configuring A2DP codec preference, and for reporting back
  94. * current configuration or codec capability.
  95. * For codec capability, fields "sample_rate", "bits_per_sample" and
  96. * "channel_mode" can contain bit-masks with all supported features.
  97. */
  98. typedef struct {
  99. btav_a2dp_codec_index_t codec_type;
  100. btav_a2dp_codec_priority_t
  101. codec_priority; // Codec selection priority
  102. // relative to other codecs: larger value
  103. // means higher priority. If 0, reset to
  104. // default.
  105. btav_a2dp_codec_sample_rate_t sample_rate;
  106. btav_a2dp_codec_bits_per_sample_t bits_per_sample;
  107. btav_a2dp_codec_channel_mode_t channel_mode;
  108. int64_t codec_specific_1; // Codec-specific value 1
  109. int64_t codec_specific_2; // Codec-specific value 2
  110. int64_t codec_specific_3; // Codec-specific value 3
  111. int64_t codec_specific_4; // Codec-specific value 4
  112. std::string ToString() const {
  113. std::string codec_name_str;
  114. switch (codec_type) {
  115. case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
  116. codec_name_str = "SBC";
  117. break;
  118. case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
  119. codec_name_str = "AAC";
  120. break;
  121. case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
  122. codec_name_str = "aptX";
  123. break;
  124. case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
  125. codec_name_str = "aptX HD";
  126. break;
  127. case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
  128. codec_name_str = "LDAC";
  129. break;
  130. case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
  131. codec_name_str = "SBC (Sink)";
  132. break;
  133. case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
  134. codec_name_str = "AAC (Sink)";
  135. break;
  136. case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
  137. codec_name_str = "LDAC (Sink)";
  138. break;
  139. case BTAV_A2DP_CODEC_INDEX_MAX:
  140. codec_name_str = "Unknown(CODEC_INDEX_MAX)";
  141. break;
  142. }
  143. std::string sample_rate_str;
  144. AppendCapability(sample_rate_str,
  145. (sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE), "NONE");
  146. AppendCapability(sample_rate_str,
  147. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100),
  148. "44100");
  149. AppendCapability(sample_rate_str,
  150. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000),
  151. "48000");
  152. AppendCapability(sample_rate_str,
  153. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200),
  154. "88200");
  155. AppendCapability(sample_rate_str,
  156. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000),
  157. "96000");
  158. AppendCapability(sample_rate_str,
  159. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400),
  160. "176400");
  161. AppendCapability(sample_rate_str,
  162. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000),
  163. "192000");
  164. AppendCapability(sample_rate_str,
  165. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_16000),
  166. "16000");
  167. AppendCapability(sample_rate_str,
  168. (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_24000),
  169. "24000");
  170. std::string bits_per_sample_str;
  171. AppendCapability(bits_per_sample_str,
  172. (bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE),
  173. "NONE");
  174. AppendCapability(bits_per_sample_str,
  175. (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16),
  176. "16");
  177. AppendCapability(bits_per_sample_str,
  178. (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24),
  179. "24");
  180. AppendCapability(bits_per_sample_str,
  181. (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32),
  182. "32");
  183. std::string channel_mode_str;
  184. AppendCapability(channel_mode_str,
  185. (channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE),
  186. "NONE");
  187. AppendCapability(channel_mode_str,
  188. (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO),
  189. "MONO");
  190. AppendCapability(channel_mode_str,
  191. (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO),
  192. "STEREO");
  193. return "codec: " + codec_name_str +
  194. " priority: " + std::to_string(codec_priority) +
  195. " sample_rate: " + sample_rate_str +
  196. " bits_per_sample: " + bits_per_sample_str +
  197. " channel_mode: " + channel_mode_str +
  198. " codec_specific_1: " + std::to_string(codec_specific_1) +
  199. " codec_specific_2: " + std::to_string(codec_specific_2) +
  200. " codec_specific_3: " + std::to_string(codec_specific_3) +
  201. " codec_specific_4: " + std::to_string(codec_specific_4);
  202. }
  203. private:
  204. static std::string AppendCapability(std::string& result, bool append,
  205. const std::string& name) {
  206. if (!append) return result;
  207. if (!result.empty()) result += "|";
  208. result += name;
  209. return result;
  210. }
  211. } btav_a2dp_codec_config_t;
  212. /** Callback for connection state change.
  213. * state will have one of the values from btav_connection_state_t
  214. */
  215. typedef void (*btav_connection_state_callback)(const RawAddress& bd_addr,
  216. btav_connection_state_t state);
  217. /** Callback for audiopath state change.
  218. * state will have one of the values from btav_audio_state_t
  219. */
  220. typedef void (*btav_audio_state_callback)(const RawAddress& bd_addr,
  221. btav_audio_state_t state);
  222. /** Callback for audio configuration change.
  223. * Used only for the A2DP Source interface.
  224. */
  225. typedef void (*btav_audio_source_config_callback)(
  226. const RawAddress& bd_addr, btav_a2dp_codec_config_t codec_config,
  227. std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
  228. std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities);
  229. /** Callback for audio configuration change.
  230. * Used only for the A2DP Sink interface.
  231. * sample_rate: sample rate in Hz
  232. * channel_count: number of channels (1 for mono, 2 for stereo)
  233. */
  234. typedef void (*btav_audio_sink_config_callback)(const RawAddress& bd_addr,
  235. uint32_t sample_rate,
  236. uint8_t channel_count);
  237. /** BT-AV A2DP Source callback structure. */
  238. typedef struct {
  239. /** set to sizeof(btav_source_callbacks_t) */
  240. size_t size;
  241. btav_connection_state_callback connection_state_cb;
  242. btav_audio_state_callback audio_state_cb;
  243. btav_audio_source_config_callback audio_config_cb;
  244. } btav_source_callbacks_t;
  245. /** BT-AV A2DP Sink callback structure. */
  246. typedef struct {
  247. /** set to sizeof(btav_sink_callbacks_t) */
  248. size_t size;
  249. btav_connection_state_callback connection_state_cb;
  250. btav_audio_state_callback audio_state_cb;
  251. btav_audio_sink_config_callback audio_config_cb;
  252. } btav_sink_callbacks_t;
  253. /**
  254. * NOTE:
  255. *
  256. * 1. AVRCP 1.0 shall be supported initially. AVRCP passthrough commands
  257. * shall be handled internally via uinput
  258. *
  259. * 2. A2DP data path shall be handled via a socket pipe between the AudioFlinger
  260. * android_audio_hw library and the Bluetooth stack.
  261. *
  262. */
  263. /** Represents the standard BT-AV A2DP Source interface.
  264. */
  265. typedef struct {
  266. /** set to sizeof(btav_source_interface_t) */
  267. size_t size;
  268. /**
  269. * Register the BtAv callbacks.
  270. */
  271. bt_status_t (*init)(btav_source_callbacks_t* callbacks,
  272. int max_connected_audio_devices,
  273. std::vector<btav_a2dp_codec_config_t> codec_priorities);
  274. /** connect to headset */
  275. bt_status_t (*connect)(const RawAddress& bd_addr);
  276. /** dis-connect from headset */
  277. bt_status_t (*disconnect)(const RawAddress& bd_addr);
  278. /** sets the connected device silence state */
  279. bt_status_t (*set_silence_device)(const RawAddress& bd_addr, bool silence);
  280. /** sets the connected device as active */
  281. bt_status_t (*set_active_device)(const RawAddress& bd_addr);
  282. /** configure the codecs settings preferences */
  283. bt_status_t (*config_codec)(
  284. const RawAddress& bd_addr,
  285. std::vector<btav_a2dp_codec_config_t> codec_preferences);
  286. /** Closes the interface. */
  287. void (*cleanup)(void);
  288. } btav_source_interface_t;
  289. /** Represents the standard BT-AV A2DP Sink interface.
  290. */
  291. typedef struct {
  292. /** set to sizeof(btav_sink_interface_t) */
  293. size_t size;
  294. /**
  295. * Register the BtAv callbacks
  296. */
  297. bt_status_t (*init)(btav_sink_callbacks_t* callbacks);
  298. /** connect to headset */
  299. bt_status_t (*connect)(const RawAddress& bd_addr);
  300. /** dis-connect from headset */
  301. bt_status_t (*disconnect)(const RawAddress& bd_addr);
  302. /** Closes the interface. */
  303. void (*cleanup)(void);
  304. /** Sends Audio Focus State. */
  305. void (*set_audio_focus_state)(int focus_state);
  306. /** Sets the audio track gain. */
  307. void (*set_audio_track_gain)(float gain);
  308. } btav_sink_interface_t;
  309. __END_DECLS
  310. #endif /* ANDROID_INCLUDE_BT_AV_H */