libmedia \
libutils
+audio_client_sources := \
+ aidl/android/brillo/brilloaudioservice/IAudioServiceCallback.aidl \
+ aidl/android/brillo/brilloaudioservice/IBrilloAudioService.aidl \
+ audio_service_callback.cpp \
+ brillo_audio_client.cpp \
+ brillo_audio_client_helpers.cpp \
+ brillo_audio_device_info.cpp \
+ brillo_audio_device_info_internal.cpp \
+ brillo_audio_manager.cpp
+
audio_service_sources := \
aidl/android/brillo/brilloaudioservice/IAudioServiceCallback.aidl \
aidl/android/brillo/brilloaudioservice/IBrilloAudioService.aidl \
LOCAL_INIT_RC := brilloaudioserv.rc
include $(BUILD_EXECUTABLE)
-# Unit tests for audio device handler.
+# Audio client library.
+# =============================================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbrilloaudio
+LOCAL_SRC_FILES := \
+ $(audio_client_sources)
+LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl
+LOCAL_SHARED_LIBRARIES := $(audio_service_shared_libraries)
+LOCAL_CFLAGS := -Wall -std=c++14
+include $(BUILD_SHARED_LIBRARY)
+
+# Unit tests for the Brillo audio service.
# =============================================================================
include $(CLEAR_VARS)
LOCAL_MODULE := brilloaudioservice_test
LOCAL_CFLAGS := -Werror -Wall
LOCAL_CFLAGS += -Wno-sign-compare
include $(BUILD_NATIVE_TEST)
+
+# Unit tests for the Brillo audio client.
+# =============================================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := brilloaudioclient_test
+LOCAL_SRC_FILES := \
+ $(audio_client_sources) \
+ test/audio_service_callback_test.cpp \
+ test/brillo_audio_device_info_internal_test.cpp
+LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl
+LOCAL_C_INCLUDES := external/gtest/include
+LOCAL_SHARED_LIBRARIES := \
+ $(audio_service_shared_libraries) \
+ libbinderwrapper_test_support
+LOCAL_STATIC_LIBRARIES := \
+ libBionicGtestMain \
+ libchrome_test_helpers \
+ libgmock
+LOCAL_CFLAGS := -Wno-sign-compare -Wall
+include $(BUILD_NATIVE_TEST)
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Implementation of audio_service_callback.
+
+#include "audio_service_callback.h"
+
+#include <base/bind.h>
+#include <base/logging.h>
+
+#include "brillo_audio_device_info_def.h"
+
+using android::binder::Status;
+
+namespace brillo {
+
+AudioServiceCallback::AudioServiceCallback(const BAudioCallback* callback,
+ void* user_data) {
+ connected_callback_ = base::Bind(callback->OnAudioDeviceAdded);
+ disconnected_callback_ = base::Bind(callback->OnAudioDeviceRemoved);
+ user_data_ = user_data;
+}
+
+Status AudioServiceCallback::OnAudioDevicesConnected(
+ const std::vector<int>& devices) {
+ for (auto device : devices) {
+ BAudioDeviceInfo device_info;
+ device_info.internal_ = std::unique_ptr<BAudioDeviceInfoInternal>(
+ BAudioDeviceInfoInternal::CreateFromAudioDevicesT(device));
+ connected_callback_.Run(&device_info, user_data_);
+ }
+ return Status::ok();
+}
+
+Status AudioServiceCallback::OnAudioDevicesDisconnected(
+ const std::vector<int>& devices) {
+ for (auto device : devices) {
+ BAudioDeviceInfo device_info;
+ device_info.internal_ = std::unique_ptr<BAudioDeviceInfoInternal>(
+ BAudioDeviceInfoInternal::CreateFromAudioDevicesT(device));
+ disconnected_callback_.Run(&device_info, user_data_);
+ }
+ return Status::ok();
+}
+
+bool AudioServiceCallback::Equals(AudioServiceCallback* callback) {
+ if (callback->connected_callback_.Equals(connected_callback_) &&
+ callback->disconnected_callback_.Equals(disconnected_callback_) &&
+ callback->user_data_ == user_data_)
+ return true;
+ return false;
+}
+
+} // namespace brillo
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Callback object to be passed to brilloaudioservice.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_AUDIO_SERVICE_CALLBACK_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_AUDIO_SERVICE_CALLBACK_H_
+
+#include <vector>
+
+#include <base/callback.h>
+#include <binder/Status.h>
+
+#include "android/brillo/brilloaudioservice/BnAudioServiceCallback.h"
+#include "include/brillo_audio_manager.h"
+
+using android::binder::Status;
+using android::brillo::brilloaudioservice::BnAudioServiceCallback;
+
+namespace brillo {
+
+class AudioServiceCallback : public BnAudioServiceCallback {
+ public:
+ // Constructor for AudioServiceCallback.
+ //
+ // |callback| is an object of type BAudioCallback.
+ // |user_data| is an object to be passed to the callbacks.
+ AudioServiceCallback(const BAudioCallback* callback, void* user_data);
+
+ // Callback function triggered when a device is connected.
+ //
+ // |devices| is a vector of audio_devices_t.
+ Status OnAudioDevicesConnected(const std::vector<int>& devices);
+
+ // Callback function triggered with a device is disconnected.
+ //
+ // |devices| is a vector of audio_devices_t.
+ Status OnAudioDevicesDisconnected(const std::vector<int>& devices);
+
+ // Method to compare two AudioServiceCallback objects.
+ //
+ // |callback| is a pointer to a AudioServiceCallback object to be compared
+ // with this.
+ //
+ // Returns true if |callback| equals this.
+ bool Equals(AudioServiceCallback* callback);
+
+ private:
+ // Callback when devices are connected.
+ base::Callback<void(const BAudioDeviceInfo*, void*)> connected_callback_;
+ // Callback when devices are disconnected.
+ base::Callback<void(const BAudioDeviceInfo*, void*)> disconnected_callback_;
+ // User data passed to the callbacks.
+ void* user_data_;
+};
+
+} // namespace brillo
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_AUDIO_SERVICE_CALLBACK_H_
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Implementation of brillo_audio_client.h
+
+#include "brillo_audio_client.h"
+
+#include <base/logging.h>
+#include <binder/Status.h>
+#include <binderwrapper/binder_wrapper.h>
+
+#include "brillo_audio_device_info_def.h"
+#include "brillo_audio_device_info_internal.h"
+
+using android::binder::Status;
+
+namespace brillo {
+
+static const char kBrilloAudioServiceName[] =
+ "android.brillo.brilloaudioservice.BrilloAudioService";
+static const char kAudioPolicyServiceName[] = "media.audio_policy";
+
+std::shared_ptr<BrilloAudioClient> BrilloAudioClient::instance_ = nullptr;
+
+int BrilloAudioClient::callback_id_counter_ = 1;
+
+std::weak_ptr<BrilloAudioClient> BrilloAudioClient::GetClientInstance() {
+ if (!instance_) {
+ instance_ = std::shared_ptr<BrilloAudioClient>(new BrilloAudioClient());
+ if (!instance_->Initialize()) {
+ LOG(ERROR) << "Could not Initialize the brillo audio client.";
+ instance_.reset();
+ return instance_;
+ }
+ }
+ return instance_;
+}
+
+android::sp<android::IBinder> BrilloAudioClient::ConnectToService(
+ std::string service_name, const base::Closure& callback) {
+ android::BinderWrapper* binder_wrapper =
+ android::BinderWrapper::GetOrCreateInstance();
+ auto service = binder_wrapper->GetService(service_name);
+ if (!service.get()) {
+ return service;
+ }
+ binder_wrapper->RegisterForDeathNotifications(service, callback);
+ return service;
+}
+
+void BrilloAudioClient::OnBASDisconnect() {
+ LOG(WARNING) << "The brillo audio service died! Please reset the "
+ << "BAudioManager.";
+ instance_.reset();
+}
+
+void BrilloAudioClient::OnAPSDisconnect() {
+ LOG(ERROR) << "The audio policy service died! Please reset the "
+ << "BAudioManager.";
+ instance_.reset();
+}
+
+bool BrilloAudioClient::Initialize() {
+ auto service = ConnectToService(
+ kBrilloAudioServiceName, base::Bind(&BrilloAudioClient::OnBASDisconnect,
+ weak_ptr_factory_.GetWeakPtr()));
+ if (!service.get()) {
+ LOG(ERROR) << "Could not connect to brillo audio service.";
+ return false;
+ }
+ brillo_audio_service_ = android::interface_cast<IBrilloAudioService>(service);
+ service = ConnectToService(kAudioPolicyServiceName,
+ base::Bind(&BrilloAudioClient::OnAPSDisconnect,
+ weak_ptr_factory_.GetWeakPtr()));
+ if (!service.get()) {
+ LOG(ERROR) << "Could not connect to audio policy service.";
+ return false;
+ }
+ aps_ = android::interface_cast<android::IAudioPolicyService>(service);
+ return true;
+}
+
+int BrilloAudioClient::GetDevices(int flag, std::vector<int>& devices) {
+ if (!brillo_audio_service_.get()) {
+ OnBASDisconnect();
+ return ECONNABORTED;
+ }
+ auto status = brillo_audio_service_->GetDevices(flag, &devices);
+ if (!status.isOk()) {
+ return status.exceptionCode();
+ }
+ return 0;
+}
+
+int BrilloAudioClient::SetDevice(audio_policy_force_use_t usage,
+ audio_policy_forced_cfg_t config) {
+ if (!aps_.get()) {
+ OnAPSDisconnect();
+ return ECONNABORTED;
+ }
+ return aps_->setForceUse(usage, config);
+}
+
+int BrilloAudioClient::RegisterAudioCallback(AudioServiceCallback* callback,
+ int* callback_id) {
+ if (!brillo_audio_service_.get()) {
+ OnBASDisconnect();
+ return ECONNABORTED;
+ }
+ if (!brillo_audio_service_->RegisterServiceCallback(callback).isOk()) {
+ *callback_id = 0;
+ return ECONNABORTED;
+ }
+ for (auto& entry : callback_map_) {
+ if (entry.second->Equals(callback)) {
+ LOG(ERROR) << "Callback has already been registered.";
+ return EINVAL;
+ }
+ }
+ *callback_id = callback_id_counter_++;
+ callback_map_.emplace(
+ *callback_id, std::unique_ptr<AudioServiceCallback>(callback));
+ return 0;
+}
+
+int BrilloAudioClient::UnregisterAudioCallback(int callback_id) {
+ if (!brillo_audio_service_.get()) {
+ OnBASDisconnect();
+ return ECONNABORTED;
+ }
+ auto callback_elem = callback_map_.find(callback_id);
+ if (callback_elem == callback_map_.end()) {
+ // If we were passed an invalid callback_id, do nothing.
+ LOG(ERROR) << "Unregister called with invalid callback ID.";
+ return EINVAL;
+ }
+ brillo_audio_service_->UnregisterServiceCallback(callback_elem->second.get());
+ callback_map_.erase(callback_elem);
+ return 0;
+}
+
+} // namespace brillo
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Client for the brilloaudioservice.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_H_
+
+#include <memory>
+#include <vector>
+
+#include <base/bind.h>
+#include <base/memory/weak_ptr.h>
+#include <gtest/gtest_prod.h>
+#include <media/IAudioPolicyService.h>
+
+#include "android/brillo/brilloaudioservice/IBrilloAudioService.h"
+#include "audio_service_callback.h"
+
+using android::brillo::brilloaudioservice::IBrilloAudioService;
+
+namespace brillo {
+
+class BrilloAudioClient {
+ public:
+
+ // Get or create a pointer to the client instance.
+ //
+ // Returns a weak_ptr to a BrilloAudioClient object.
+ static std::weak_ptr<BrilloAudioClient> GetClientInstance();
+
+ // Query brillo audio service to get list of connected audio devices.
+ //
+ // |flag| is an int which is either GET_DEVICES_INPUTS or GET_DEVICES_OUTPUTS.
+ // |devices| is a reference to a vector of audio_devices_t.
+ //
+ // Returns 0 on success and errno on failure.
+ int GetDevices(int flag, std::vector<int>& devices);
+
+ // Register a callback object with the service.
+ //
+ // |callback| is a pointer to a callback object to be register with the
+ // brillo audio service.
+ // |callback_id| is a pointer to an int that represents a callback id token on
+ // success and 0 on failure.
+ //
+ // Returns 0 on success and errno on failure.
+ int RegisterAudioCallback(AudioServiceCallback* callback, int* callback_id);
+
+ // Unregister a callback object with the service.
+ //
+ // |callback_id| is an int referring to the callback object.
+ //
+ // Returns 0 on success and errno on failure.
+ int UnregisterAudioCallback(int callback_id);
+
+ // Set a device to be the default. This does not communicate with the brillo
+ // audio service but instead communicates directly with the audio policy
+ // service.
+ //
+ // Please see system/audio_policy.h for details on these arguments.
+ //
+ // Returns 0 on success and errno on failure.
+ int SetDevice(audio_policy_force_use_t usage,
+ audio_policy_forced_cfg_t config);
+
+ private:
+ BrilloAudioClient() = default;
+
+ // Initialize the BrilloAudioClient object and connects to the brillo audio
+ // service and the audio policy service. It also registers for death
+ // notifications.
+ bool Initialize();
+
+ // Callback to be triggered when the brillo audio service dies. It attempts to
+ // reconnect to the service.
+ void OnBASDisconnect();
+
+ // Callback to be triggered when the audio policy service dies. It attempts to
+ // reconnect to the service.
+ void OnAPSDisconnect();
+
+ // Helper method to connect to a service and register a callback to receive
+ // death notifications.
+ //
+ // |service_name| is a string representing the name of the service.
+ // |callback| is a base::Closure which will be called if the service dies.
+ android::sp<android::IBinder> ConnectToService(std::string service_name,
+ const base::Closure& callback);
+
+ // Pointer to the BrilloAudioClient object.
+ static std::shared_ptr<BrilloAudioClient> instance_;
+
+ // Used to generate weak_ptr to BrilloAudioClient for use in base::Bind.
+ base::WeakPtrFactory<BrilloAudioClient> weak_ptr_factory_{this};
+ // Pointer to the audio policy service.
+ android::sp<android::IAudioPolicyService> aps_;
+ // Pointer to the brillo audio service.
+ android::sp<IBrilloAudioService> brillo_audio_service_;
+ // Counter for callback IDs.
+ static int callback_id_counter_;
+ // Map of callback ids to callback objects.
+ std::map<int, std::unique_ptr<AudioServiceCallback> > callback_map_;
+
+ DISALLOW_COPY_AND_ASSIGN(BrilloAudioClient);
+};
+
+} // namespace brillo
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_H_
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "brillo_audio_client_helpers.h"
+
+namespace brillo {
+
+audio_policy_force_use_t BrilloAudioClientHelpers::GetForceUse(
+ BAudioUsage usage) {
+ if (usage == kUsageMedia)
+ return AUDIO_POLICY_FORCE_FOR_MEDIA;
+ else
+ return AUDIO_POLICY_FORCE_FOR_SYSTEM;
+}
+
+} // namespace brillo
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Helpers for the brillo audio client.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_HELPERS_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_HELPERS_H_
+
+#include <gtest/gtest_prod.h>
+#include <system/audio.h>
+#include <system/audio_policy.h>
+
+#include "include/brillo_audio_manager.h"
+
+namespace brillo {
+
+class BrilloAudioClientHelpers {
+ public:
+ static audio_policy_force_use_t GetForceUse(BAudioUsage usage);
+};
+
+} // namespace brillo
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_HELPERS_H_
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Implementation of brillo_audio_device_info.h.
+
+#include "include/brillo_audio_device_info.h"
+
+#include "brillo_audio_device_info_def.h"
+#include "brillo_audio_device_info_internal.h"
+
+using brillo::BAudioDeviceInfoInternal;
+
+BAudioDeviceInfo* BAudioDeviceInfo_new(int device) {
+ BAudioDeviceInfo* audio_device_info = new BAudioDeviceInfo;
+ audio_device_info->internal_ =
+ std::make_unique<BAudioDeviceInfoInternal>(device);
+ return audio_device_info;
+}
+
+int BAudioDeviceInfo_getType(BAudioDeviceInfo* device) {
+ return device->internal_->GetDeviceId();
+}
+
+void BAudioDeviceInfo_delete(BAudioDeviceInfo* device) {
+ delete device;
+}
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Definition of BAudioDeviceInfo.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_DEF_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_DEF_H_
+
+
+#include <memory>
+
+#include "brillo_audio_device_info_internal.h"
+#include "include/brillo_audio_device_info.h"
+
+using brillo::BAudioDeviceInfoInternal;
+
+struct BAudioDeviceInfo {
+ std::unique_ptr<BAudioDeviceInfoInternal> internal_;
+};
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_DEF_H_
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Internal helpers for BAudioDeviceInfo.
+
+#include "brillo_audio_device_info_internal.h"
+
+#include <base/logging.h>
+
+#include "brillo_audio_device_info_def.h"
+
+namespace brillo {
+
+BAudioDeviceInfoInternal::BAudioDeviceInfoInternal(int device_id) {
+ device_id_ = device_id;
+}
+
+int BAudioDeviceInfoInternal::GetDeviceId() {
+ return device_id_;
+}
+
+audio_policy_forced_cfg_t BAudioDeviceInfoInternal::GetConfig() {
+ switch (device_id_) {
+ case TYPE_BUILTIN_SPEAKER:
+ return AUDIO_POLICY_FORCE_SPEAKER;
+ case TYPE_WIRED_HEADSET:
+ return AUDIO_POLICY_FORCE_HEADPHONES;
+ case TYPE_WIRED_HEADSET_MIC:
+ return AUDIO_POLICY_FORCE_HEADPHONES;
+ case TYPE_WIRED_HEADPHONES:
+ return AUDIO_POLICY_FORCE_HEADPHONES;
+ case TYPE_BUILTIN_MIC:
+ return AUDIO_POLICY_FORCE_NONE;
+ default:
+ return AUDIO_POLICY_FORCE_NONE;
+ }
+}
+
+BAudioDeviceInfoInternal* BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
+ unsigned int device) {
+ int device_id = TYPE_UNKNOWN;
+ switch (device) {
+ case AUDIO_DEVICE_OUT_WIRED_HEADSET:
+ device_id = TYPE_WIRED_HEADSET;
+ break;
+ case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
+ device_id = TYPE_WIRED_HEADPHONES;
+ break;
+ case AUDIO_DEVICE_IN_WIRED_HEADSET:
+ device_id = TYPE_WIRED_HEADSET_MIC;
+ break;
+ }
+ if (device_id == TYPE_UNKNOWN) {
+ LOG(ERROR) << "Unsupported device.";
+ return nullptr;
+ }
+ return new BAudioDeviceInfoInternal(device_id);
+}
+
+} // namespace brillo
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Internal class to represent BAudioDeviceInfo.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_INTERNAL_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_INTERNAL_H_
+
+#include <vector>
+
+#include <gtest/gtest_prod.h>
+#include <hardware/audio_policy.h>
+
+#include "include/brillo_audio_device_info.h"
+
+namespace brillo {
+
+class BAudioDeviceInfoInternal {
+ public:
+ // Constructor for BAudioDeviceInfoInternal.
+ //
+ // |device_id| is an integer representing an audio device type as defined in
+ // brillo_audio_device_info.h.
+ BAudioDeviceInfoInternal(int device_id);
+
+ // Get audio policy config.
+ //
+ // Returns an audio_policy_forced_cfg_t.
+ audio_policy_forced_cfg_t GetConfig();
+
+ // Create a BAudioDeviceInfoInternal object from a audio_devices_t device
+ // type.
+ //
+ // |devices_t| is an audio device of type audio_devices_t which is represented
+ // using an int.
+ //
+ // Returns a pointer to a BAudioDeviceInfoInternal that has been created.
+ static BAudioDeviceInfoInternal* CreateFromAudioDevicesT(unsigned int device);
+
+ // Get the device id.
+ //
+ // Returns an int which is the device_id.
+ int GetDeviceId();
+
+ private:
+ FRIEND_TEST(BrilloAudioDeviceInfoInternalTest, InWiredHeadset);
+ FRIEND_TEST(BrilloAudioDeviceInfoInternalTest, OutWiredHeadset);
+ FRIEND_TEST(BrilloAudioDeviceInfoInternalTest, OutWiredHeadphone);
+
+ // An int representing the underlying audio device. The int is one of the
+ // constants defined in brillo_audio_device_info.h.
+ int device_id_;
+};
+
+} // namespace brillo
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_INTERNAL_H_
--- /dev/null
+ // Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Implementation of brillo_audio_manager.h.
+
+#include "include/brillo_audio_manager.h"
+
+#include <memory>
+#include <stdlib.h>
+
+#include "audio_service_callback.h"
+#include "brillo_audio_client.h"
+#include "brillo_audio_client_helpers.h"
+#include "brillo_audio_device_info_def.h"
+#include "brillo_audio_device_info_internal.h"
+
+using brillo::AudioServiceCallback;
+using brillo::BrilloAudioClient;
+using brillo::BrilloAudioClientHelpers;
+
+struct BAudioManager {
+ std::weak_ptr<BrilloAudioClient> client_;
+};
+
+BAudioManager* BAudioManager_new() {
+ auto client = BrilloAudioClient::GetClientInstance();
+ if (!client.lock())
+ return nullptr;
+ BAudioManager* bam = new BAudioManager;
+ bam->client_ = client;
+ return bam;
+}
+
+int BAudioManager_getDevices(
+ const BAudioManager* brillo_audio_manager, int flag,
+ BAudioDeviceInfo* device_array[], unsigned int size,
+ unsigned int* num_devices) {
+ if (!brillo_audio_manager || !num_devices)
+ return EINVAL;
+ auto client = brillo_audio_manager->client_.lock();
+ if (!client) {
+ *num_devices = 0;
+ return ECONNABORTED;
+ }
+ std::vector<int> devices;
+ auto rc = client->GetDevices(flag, devices);
+ if (rc) {
+ *num_devices = 0;
+ return rc;
+ }
+ unsigned int num_elems = (devices.size() < size) ? devices.size() : size;
+ for (size_t i = 0; i < num_elems; i++) {
+ device_array[i] = new BAudioDeviceInfo;
+ device_array[i]->internal_ = std::unique_ptr<BAudioDeviceInfoInternal>(
+ BAudioDeviceInfoInternal::CreateFromAudioDevicesT(devices[i]));
+ }
+ *num_devices = devices.size();
+ return 0;
+}
+
+int BAudioManager_setInputDevice(const BAudioManager* brillo_audio_manager,
+ const BAudioDeviceInfo* device) {
+ if (!brillo_audio_manager || !device)
+ return EINVAL;
+ auto client = brillo_audio_manager->client_.lock();
+ if (!client) {
+ return ECONNABORTED;
+ }
+ return client->SetDevice(AUDIO_POLICY_FORCE_FOR_RECORD,
+ device->internal_->GetConfig());
+}
+
+int BAudioManager_setOutputDevice(
+ const BAudioManager* brillo_audio_manager, const BAudioDeviceInfo* device,
+ BAudioUsage usage) {
+ if (!brillo_audio_manager || !device)
+ return EINVAL;
+ auto client = brillo_audio_manager->client_.lock();
+ if (!client)
+ return ECONNABORTED;
+ return client->SetDevice(BrilloAudioClientHelpers::GetForceUse(usage),
+ device->internal_->GetConfig());
+}
+
+int BAudioManager_registerAudioDeviceCallback(
+ const BAudioManager* brillo_audio_manager, const BAudioCallback* callback,
+ void* user_data, int* callback_id) {
+ if (!brillo_audio_manager || !callback || !callback_id)
+ return EINVAL;
+ // This copies the BAudioCallback into AudioServiceCallback so the
+ // BAudioCallback can be safely deleted.
+ auto client = brillo_audio_manager->client_.lock();
+ if (!client) {
+ *callback_id = 0;
+ return ECONNABORTED;
+ }
+ return client->RegisterAudioCallback(
+ new AudioServiceCallback(callback, user_data), callback_id);
+}
+
+int BAudioManager_unregisterAudioDeviceCallback(
+ const BAudioManager* brillo_audio_manager, int callback_id) {
+ if (!brillo_audio_manager)
+ return EINVAL;
+ auto client = brillo_audio_manager->client_.lock();
+ if (!client)
+ return ECONNABORTED;
+ return client->UnregisterAudioCallback(callback_id);
+}
+
+int BAudioManager_delete(BAudioManager* brillo_audio_manager) {
+ if (!brillo_audio_manager)
+ return EINVAL;
+ delete brillo_audio_manager;
+ return 0;
+}
--- /dev/null
+// copyright 2016 the android open source project
+//
+// licensed under the apache license, version 2.0 (the "license");
+// you may not use this file except in compliance with the license.
+// you may obtain a copy of the license at
+//
+// http://www.apache.org/licenses/license-2.0
+//
+// unless required by applicable law or agreed to in writing, software
+// distributed under the license is distributed on an "as is" basis,
+// without warranties or conditions of any kind, either express or implied.
+// see the license for the specific language governing permissions and
+// limitations under the license.
+//
+
+// Type to represent audio devices in a brillo system.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_H_
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+struct BAudioDeviceInfo;
+
+typedef struct BAudioDeviceInfo BAudioDeviceInfo;
+
+// A device type associated with an unknown or uninitialized device.
+static const int TYPE_UNKNOWN = 0;
+
+// A device type describing the speaker system (i.e. a mono speaker or stereo
+// speakers) built in a device.
+static const int TYPE_BUILTIN_SPEAKER = 1;
+
+// A device type describing a headset, which is the combination of a headphones
+// and microphone. This type represents just the transducer in the headset.
+static const int TYPE_WIRED_HEADSET = 2;
+
+// A device type describing a headset, which is the combination of a headphones
+// and microphone. This type represents the microphone in the headset.
+static const int TYPE_WIRED_HEADSET_MIC = 3;
+
+// A device type describing a pair of wired headphones.
+static const int TYPE_WIRED_HEADPHONES = 4;
+
+// A device type describing the microphone(s) built in a device.
+static const int TYPE_BUILTIN_MIC = 5;
+
+// Create a BAudioDeviceInfo based on a type described above.
+//
+// Arg:
+// device: An int representing an audio type as defined above.
+//
+// Returns a pointer to a BAudioDeviceInfo object.
+BAudioDeviceInfo* BAudioDeviceInfo_new(int device);
+
+// Get the type of the device.
+//
+// Arg:
+// device: A pointer to a BAudioDeviceInfo object to be freed.
+//
+// Returns an int representing the type of the device.
+int BAudioDeviceInfo_getType(BAudioDeviceInfo* device);
+
+// Free a BAudioDeviceInfo.
+//
+// Arg:
+// device: A pointer to a BAudioDeviceInfo object to be freed.
+void BAudioDeviceInfo_delete(BAudioDeviceInfo* device);
+
+__END_DECLS
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_DEVICE_INFO_H_
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Class to manage audio devices in Brillo.
+
+#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_MANAGER_H_
+#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_MANAGER_H_
+
+#include <sys/cdefs.h>
+
+#include "brillo_audio_device_info.h"
+
+__BEGIN_DECLS
+
+struct BAudioManager;
+
+typedef struct BAudioManager BAudioManager;
+
+// Get a pointer to a BAudioManager. This object will refer to the same
+// underlying client object no matter how many times it is called.
+//
+// Returns a pointer to a BAudioManager. Returns NULL on failure.
+BAudioManager* BAudioManager_new();
+
+// Returns the list of input/output devices connected to the system.
+//
+// Arg:
+// brillo_audio_manager: A pointer to a BAudioManager.
+// flag: Either GET_DEVICES_INPUTS or GET_DEVICES_OUTPUTS.
+// device_array: An array of BAudioDeviceInfo pointers. The caller has to
+// allocate this array.
+// size: The size of device_array.
+// num_devices: A pointer to an unsigned int which will represent the number
+// of audio devices connected to the device.
+//
+// Returns 0 on success and errno on failure.
+int BAudioManager_getDevices(
+ const BAudioManager* brillo_audio_manager, int flag,
+ BAudioDeviceInfo* device_array[], unsigned int size,
+ unsigned int* num_devices);
+
+// Select the input device to be used for recording.
+//
+// Arg:
+// brillo_audio_manager: A pointer to a BAudioManager.
+// device: Device to set as the input device. Note that the device has to be
+// an input device.
+//
+// Returns 0 on success and errno on failure.
+int BAudioManager_setInputDevice(const BAudioManager* brillo_audio_manager,
+ const BAudioDeviceInfo* device);
+
+// Usage types.
+enum BAudioUsage {
+ kUsageAlarm,
+ kUsageMedia,
+ kUsageNotifications,
+ kUsageSystem
+};
+
+// Select the output device to be used for playback.
+//
+// Arg:
+// brillo_audio_manager: A pointer to a BAudioManager.
+// device: Device to set as the output device. Note that the device has to
+// be an output device.
+// usage: A BAudioUsage type representing a usage to route to |device|.
+//
+// Returns 0 on success and errno on failure.
+int BAudioManager_setOutputDevice(
+ const BAudioManager* brillo_audio_manager, const BAudioDeviceInfo* device,
+ BAudioUsage usage);
+
+// Object used for callbacks.
+struct BAudioCallback {
+ // Function to be called when an audio device is added. If multiple audio
+ // devices are added, then this function will be called multiple times. The
+ // user is not responsible for freeing added_device.
+ void (*OnAudioDeviceAdded)(const BAudioDeviceInfo* added_device,
+ void* user_data);
+
+ // Function to be called when an audio device is removed. If multiple audio
+ // devices are removed, then this function will be called multiple times. The
+ // user is not responsible for freeing removed_device.
+ void (*OnAudioDeviceRemoved)(const BAudioDeviceInfo* removed_device,
+ void* user_data);
+};
+
+typedef struct BAudioCallback BAudioCallback;
+
+// Registers a callback object that lets clients know when audio devices have
+// been added/removed from the system.
+//
+// Arg:
+// brillo_audio_manager: A pointer to a BAudioManager.
+// callback: An object of type BAudioCallback. The BAudioManager
+// maintains ownership of this object.
+// user_data : A pointer to user data. This is not used by BAudioManager and
+// is passed as an arg to callbacks.
+// callback_id: A pointer to an int. The int represents a token that can be
+// used to de-register this callback. Contains 0 on failure.
+//
+// Returns 0 on success and errno on failure.
+int BAudioManager_registerAudioDeviceCallback(
+ const BAudioManager* brillo_audio_manager, const BAudioCallback* callback,
+ void* user_data, int* callback_id);
+
+// Unregisters a callback object.
+//
+// Arg:
+// brillo_audio_manager: A pointer to a BAudioManager.
+// callback_id: A token correspoding to the callback object.
+//
+// Returns 0 on success and errno on failure.
+int BAudioManager_unregisterAudioDeviceCallback(
+ const BAudioManager* brillo_audio_manager, int callback_id);
+
+// Free a Brillo audio manager object.
+//
+// Arg:
+// brillo_audio_manager: A pointer to a BAudioManager to be freed.
+//
+// Returns 0 on success and errno on failure.
+int BAudioManager_delete(BAudioManager* brillo_audio_manager);
+
+__END_DECLS
+
+#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_MANAGER_H_
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Tests for the audio service callback object.
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <hardware/audio.h>
+
+#include "audio_service_callback.h"
+
+namespace brillo {
+
+class AudioServiceCallbackTest : public testing::Test {
+ public:
+ void SetUp() override {
+ connected_call_count_ = 0;
+ disconnected_call_count_ = 0;
+ callback_.OnAudioDeviceAdded = OnDeviceConnectedMock;
+ callback_.OnAudioDeviceRemoved = OnDeviceDisconnectedMock;
+ user_data_ = static_cast<void*>(this);
+ }
+
+ static void OnDeviceConnectedMock(const BAudioDeviceInfo*, void* user_data) {
+ static_cast<AudioServiceCallbackTest*>(user_data)->connected_call_count_++;
+ }
+
+ static void OnDeviceDisconnectedMock(const BAudioDeviceInfo*, void* user_data) {
+ static_cast<AudioServiceCallbackTest*>(
+ user_data)->disconnected_call_count_++;
+ }
+
+ BAudioCallback callback_;
+ void* user_data_;
+ int connected_call_count_;
+ int disconnected_call_count_;
+};
+
+TEST_F(AudioServiceCallbackTest, CallbackCallCount) {
+ std::vector<int> devices = {AUDIO_DEVICE_OUT_WIRED_HEADSET,
+ AUDIO_DEVICE_OUT_WIRED_HEADPHONE};
+ AudioServiceCallback service_callback(&callback_, user_data_);
+ service_callback.OnAudioDevicesConnected(devices);
+ EXPECT_EQ(connected_call_count_, devices.size());
+ service_callback.OnAudioDevicesDisconnected(devices);
+ EXPECT_EQ(disconnected_call_count_, devices.size());
+}
+
+} // namespace brillo
--- /dev/null
+// Copyright 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Tests for the BrilloAudioDeviceInfoInternal test.
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <hardware/audio.h>
+
+#include "brillo_audio_device_info_internal.h"
+
+namespace brillo {
+
+TEST(BrilloAudioDeviceInfoInternalTest, OutWiredHeadset) {
+ BAudioDeviceInfoInternal* badi =
+ BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
+ AUDIO_DEVICE_OUT_WIRED_HEADSET);
+ EXPECT_EQ(badi->device_id_, TYPE_WIRED_HEADSET);
+ EXPECT_EQ(badi->GetConfig(), AUDIO_POLICY_FORCE_HEADPHONES);
+}
+
+TEST(BrilloAudioDeviceInfoInternalTest, OutWiredHeadphone) {
+ BAudioDeviceInfoInternal* badi =
+ BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
+ AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
+ EXPECT_EQ(badi->device_id_, TYPE_WIRED_HEADPHONES);
+ EXPECT_EQ(badi->GetConfig(), AUDIO_POLICY_FORCE_HEADPHONES);
+}
+
+TEST(BrilloAudioDeviceInfoInternalTest, InWiredHeadset) {
+ BAudioDeviceInfoInternal* badi =
+ BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
+ AUDIO_DEVICE_IN_WIRED_HEADSET);
+ EXPECT_EQ(badi->device_id_, TYPE_WIRED_HEADSET_MIC);
+ EXPECT_EQ(badi->GetConfig(), AUDIO_POLICY_FORCE_HEADPHONES);
+}
+
+} // namespace brillo