1 /* 2 * Copyright 2020 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 SAMPLES_DEFAULT_ERROR_CALLBACK_H 18 #define SAMPLES_DEFAULT_ERROR_CALLBACK_H 19 20 #include <vector> 21 #include <oboe/AudioStreamCallback.h> 22 #include <logging_macros.h> 23 24 #include "IRestartable.h" 25 26 /** 27 * This is a callback object which will be called when a stream error occurs. 28 * 29 * It is constructed using an `IRestartable` which allows it to automatically restart the parent 30 * object if the stream is disconnected (for example, when headphones are attached). 31 * 32 * @param IRestartable - the object which should be restarted when the stream is disconnected 33 */ 34 class DefaultErrorCallback : public oboe::AudioStreamErrorCallback { 35 public: 36 DefaultErrorCallback(IRestartable & parent)37 DefaultErrorCallback(IRestartable &parent): mParent(parent) {} 38 virtual ~DefaultErrorCallback() = default; 39 onErrorAfterClose(oboe::AudioStream * oboeStream,oboe::Result error)40 virtual void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override { 41 // Restart the stream if the error is a disconnect, otherwise do nothing and log the error 42 // reason. 43 if (error == oboe::Result::ErrorDisconnected) { 44 LOGI("Restarting AudioStream"); 45 mParent.restart(); 46 } 47 LOGE("Error was %s", oboe::convertToText(error)); 48 } 49 50 private: 51 IRestartable &mParent; 52 53 }; 54 55 56 #endif //SAMPLES_DEFAULT_ERROR_CALLBACK_H 57