1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ST_AUDIO_SESSION_TIMER_H 17 #define ST_AUDIO_SESSION_TIMER_H 18 19 #include <chrono> 20 #include <memory> 21 #include <mutex> 22 #include <thread> 23 24 #include "audio_session_info.h" 25 26 namespace OHOS { 27 namespace AudioStandard { 28 class AudioSessionTimerCallback { 29 public: 30 virtual ~AudioSessionTimerCallback() = default; 31 32 virtual void OnAudioSessionTimeOut(const int32_t callerPid) = 0; 33 }; 34 35 enum class TimerState { 36 TIMER_INVALID = -1, 37 TIMER_NEW = 0, 38 TIMER_RUNNING = 1, 39 TIMER_STOPPED = 2, 40 }; 41 42 class AudioSessionTimer { 43 public: 44 AudioSessionTimer(); 45 virtual ~AudioSessionTimer(); 46 47 void StartTimer(const int32_t callerPid); 48 void StopTimer(const int32_t callerPid); 49 bool IsSessionTimerRunning(const int32_t callerPid); 50 int32_t SetAudioSessionTimerCallback(const std::shared_ptr<AudioSessionTimerCallback> sessionTimerCallback); 51 52 private: 53 void SendSessionTimeOutCallback(const int32_t callerPid); 54 void TimerLoopFunc(); 55 56 std::mutex sessionTimerMutex_; 57 58 TimerState state_ = TimerState::TIMER_INVALID; 59 std::unordered_map<int32_t, std::time_t> timerMap_; 60 std::shared_ptr<std::thread> timerThread_; 61 std::atomic<bool> isThreadRunning_ = false; 62 std::condition_variable timerCond_; 63 std::mutex timerLoopMutex_; 64 std::weak_ptr<AudioSessionTimerCallback> timerCallback_; 65 }; 66 } // namespace AudioStandard 67 } // namespace OHOS 68 69 #endif // ST_AUDIO_SESSION_SERVICE_H