Android の aptX aptX-HD LDAC AACサポートについて

2017年にリリースされる新しいバージョンのAndroidではOSの標準機能として、A2DPのaptX、aptX-HD、LDAC、AACがサポートされるみたいです。

そう考える根拠としては、2017年１月ごろにAndroidのBluetoothプロトコルスタックにA2DPのaptX、aptX-HD、LDAC、AACサポートするコードが追加されました。

でも、実際の端末に搭載されるかどうかは、端末ベンダ毎にCODECのライセンス契約が必要になるんじゃないのかなぁ。

2017-01になって、ソニーのLDAC encoderのソースコード([libldac](https://android.googlesource.com/platform/external/libldac/)) がAOSPに追加されました。なので、LDACが次のAndroidに標準搭載される可能性があるのかもしれません。特許はどうするるんだろ。

# AndroidのBluetoothプロトコルスタックへのコードの変更点

## aptX and aptX-HD
* [Integration of the aptX and aptX-HD codecs for A2DP source](https://android.googlesource.com/platform/system/bt/+/3a3ec66a1bb7f5c99b17239021d6d184a3abd4ee)
* [Renamed the aptX and aptX-HD shared libraries](https://android.googlesource.com/platform/system/bt/+/d43a901c579ef08a1b5d7cfe18f759aad0a14f1a)
* [Integration of the aptX and aptX-HD codecs for A2DP source](https://android.googlesource.com/platform/hardware/libhardware/+/b2543a2f8eedb9f4cab176d0b75c27c363f4e0de)

```
        "a2dp/a2dp_vendor_aptx.cc",
        "a2dp/a2dp_vendor_aptx_hd.cc",
        "a2dp/a2dp_vendor_aptx_encoder.cc",
        "a2dp/a2dp_vendor_aptx_hd_encoder.cc",
```

## LDAC
* [Integration of the LDAC codec for A2DP source](https://android.googlesource.com/platform/system/bt/+/8e419b13ecb1e33aeb785d0221bacc5744e51113) 
* [Integrated A2DP LDAC support with the LDAC encoder library](https://android.googlesource.com/platform/system/bt/+/d556b752bd9f4219aa5b02c12ab1507b9f6e186b)
* [Integration of the LDAC codec for A2DP source](https://android.googlesource.com/platform/hardware/libhardware/+/be1bfcea291218cb20fb1123752932da0db9e57d)

```
        "a2dp/a2dp_vendor_ldac.cc",
        "a2dp/a2dp_vendor_ldac_encoder.cc",
```

## AAC
* [Integration of the AAC codec for A2DP source](https://android.googlesource.com/platform/system/bt/+/d5f4960b425ac84cc7a9fd699f39c06869ce2666)
* [Integration of the AAC codec for A2DP source](https://android.googlesource.com/platform/hardware/libhardware/+/f9b17fed4b40039681b9034f53500797fa7d51fa)

```
        "a2dp/a2dp_aac.cc",
        "a2dp/a2dp_aac_encoder.cc",
```

## configure A2DP source CODEC
* [Add a mechanism for configuring the A2DP Source codecs](https://android.googlesource.com/platform/system/bt/+/5ce0116fbf5b530d5b93f11312096eade1021c1e)

このパッチのコメントがまとまっているので引用しときます。

```
Add a mechanism for configuring the A2DP Source codecs

* Codec config internal abstraction:
 - Add new classes A2dpCodecConfig and A2dpCodecs that (will)
   encapsulate all codec-related APIs
 - Add unit tests for the above two classes
 - Add method A2dpCodecConfig.buildCodecConfig(), and use it when
   creating the codec configuration instead of A2DP_InitSource2SinkCodec().
   The new method can build the codec config by taking into account
   optional user codec-related configuration preferences.
 - Use the A2DP codec config API from the hardware/libhardware bt_av.h API
 - Replace enum tA2DP_CODEC_SEP_INDEX with btav_a2dp_codec_index_t
   from the bt_av.h API
 - Move codec-specific functions from stack/include/a2dp_api.h
   and stack/a2dp/a2dp_api.cc to stack/include/a2dp_codec_api.h
   and stack/a2dp/a2dp_codec_config.cc
 - Create a new base class A2dpCodecConfig() to hold some of the
   codec-related state, and implement the corresponding A2dpCodecConfigSbc
   and A2dpCodecConfigSbcSink derived classes.
 - Move A2DP spec-related constants from stack/include/a2dp_api.h
   to stack/include/a2dp_constants.h
 - Move A2DP-related error codes from stack/include/a2dp_api.h
   to stack/include/a2dp_error_codes.h
 - Move A2DP SBC spec-related constants from stack/include/a2dp_sbc.h to
   stack/include/a2dp_sbc_constants.h

* Implement the backend mechanism for handling user (re)configuration of
  A2DP Source codecs as requested via the JNI API calls.
  Also, any codec changes are reported back via JNI API callbacks.
  The current audio parameter selection (sample rate, bits per
  sample, channel mode - mono/stereo) is as follows:
  - If the user selected parameters are acceptable (based on
    local codec capability and the remote Sink capability),
    those parameters are used.
  - Else if the Audio HAL's requested parameters are acceptable,
    those are used.
  - Else if the default settings are acceptable, those are used.
  - Else use the best match among the local and the remote device's
    capabilities.

* Update the mechanism for handling OTA configuration requests from the
  remote Sink device.
  - The OTA prefered codec configuration is ignored if the current
  codec configuration contains explicit user configuration, or if the
  codec configuration for the same codec contains explicit user
  configuration.

* Refactor the Audio HAL <-> Bluetooth audio parameter negotiation
  mechanism:
  The new mechanism gives some flexibility to the Media Framework to
  choose the appropriate audio format (sample rate, bits per sample,
  and channel mode - mono/stereo), and at the same time allows
  the Bluetooth stack to double-check / overwrite the choice.
 - out_get_parameters() on the Audio HAL side asks the Bluetooth stack
   for all currently supported formats (for the current codec),
   and returns them to the Media Framework: sample rate, bits per sample,
   and channel mode (mono/stereo).
 - The first time adev_open_output_stream() is called on the Audio HAL,
   it asks the Bluetooth stack about the audio format currently selected
   by the Bluetooth stack (based on codec negotiation with the Sink device,
   and User Configuration).
 - The second time adev_open_output_stream() is called on the Audio HAL,
   its "config" will eventually contain the audio format selected
   internally by the Media Framework. That audio format is sent to the
   Bluetooth stack.
   If that format is acceptable to the Bluetooth stack, the Bluetooth
   stack will reconfigure itself internally, and will respond back with
   those values. Otherwise, it will respond back with the values that
   should be used instead.

* Misc other fixes and refactoring:
 - Fix the BTA handling of A2DP codec reconfiguration
 - Fix a bug in the implementation of A2DP_BitsSet(), and add the
   approriate unit test. Also, fix the code that was using this function
   incorrectly.
 - The SBC encoder is compiled as a separate library
 - Replace leftover usage of "false" with "FALSE" for macros, and
   vice-versa for variable values.
```

## ライブラリ

* libaptX_encoder.so
* libaptXHD_encoder.so
* libldacBT_enc.so
* libFraunhoferAAC.so

ldacライブラリの使い方について「[LDACエンコーダを使ってみた](http://qiita.com/eggman/items/fc6ba6844c71a1df305a)」に書きました。



# 追記

AACも追加されたので記載した。 (2017-01-28)