1*05767d91SRobert Wu /* 2*05767d91SRobert Wu * Copyright 2020 The Android Open Source Project 3*05767d91SRobert Wu * 4*05767d91SRobert Wu * Licensed under the Apache License, Version 2.0 (the "License"); 5*05767d91SRobert Wu * you may not use this file except in compliance with the License. 6*05767d91SRobert Wu * You may obtain a copy of the License at 7*05767d91SRobert Wu * 8*05767d91SRobert Wu * http://www.apache.org/licenses/LICENSE-2.0 9*05767d91SRobert Wu * 10*05767d91SRobert Wu * Unless required by applicable law or agreed to in writing, software 11*05767d91SRobert Wu * distributed under the License is distributed on an "AS IS" BASIS, 12*05767d91SRobert Wu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*05767d91SRobert Wu * See the License for the specific language governing permissions and 14*05767d91SRobert Wu * limitations under the License. 15*05767d91SRobert Wu */ 16*05767d91SRobert Wu 17*05767d91SRobert Wu #ifndef SAMPLES_DEFAULT_ERROR_CALLBACK_H 18*05767d91SRobert Wu #define SAMPLES_DEFAULT_ERROR_CALLBACK_H 19*05767d91SRobert Wu 20*05767d91SRobert Wu #include <vector> 21*05767d91SRobert Wu #include <oboe/AudioStreamCallback.h> 22*05767d91SRobert Wu #include <logging_macros.h> 23*05767d91SRobert Wu 24*05767d91SRobert Wu #include "IRestartable.h" 25*05767d91SRobert Wu 26*05767d91SRobert Wu /** 27*05767d91SRobert Wu * This is a callback object which will be called when a stream error occurs. 28*05767d91SRobert Wu * 29*05767d91SRobert Wu * It is constructed using an `IRestartable` which allows it to automatically restart the parent 30*05767d91SRobert Wu * object if the stream is disconnected (for example, when headphones are attached). 31*05767d91SRobert Wu * 32*05767d91SRobert Wu * @param IRestartable - the object which should be restarted when the stream is disconnected 33*05767d91SRobert Wu */ 34*05767d91SRobert Wu class DefaultErrorCallback : public oboe::AudioStreamErrorCallback { 35*05767d91SRobert Wu public: 36*05767d91SRobert Wu DefaultErrorCallback(IRestartable & parent)37*05767d91SRobert Wu DefaultErrorCallback(IRestartable &parent): mParent(parent) {} 38*05767d91SRobert Wu virtual ~DefaultErrorCallback() = default; 39*05767d91SRobert Wu onErrorAfterClose(oboe::AudioStream * oboeStream,oboe::Result error)40*05767d91SRobert Wu virtual void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override { 41*05767d91SRobert Wu // Restart the stream if the error is a disconnect, otherwise do nothing and log the error 42*05767d91SRobert Wu // reason. 43*05767d91SRobert Wu if (error == oboe::Result::ErrorDisconnected) { 44*05767d91SRobert Wu LOGI("Restarting AudioStream"); 45*05767d91SRobert Wu mParent.restart(); 46*05767d91SRobert Wu } 47*05767d91SRobert Wu LOGE("Error was %s", oboe::convertToText(error)); 48*05767d91SRobert Wu } 49*05767d91SRobert Wu 50*05767d91SRobert Wu private: 51*05767d91SRobert Wu IRestartable &mParent; 52*05767d91SRobert Wu 53*05767d91SRobert Wu }; 54*05767d91SRobert Wu 55*05767d91SRobert Wu 56*05767d91SRobert Wu #endif //SAMPLES_DEFAULT_ERROR_CALLBACK_H 57