1 /*
2  * Copyright (c) 2022-2022 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 #define HST_LOG_TAG "MediaStatStub"
17 
18 #include "media_stat_stub.h"
19 
20 #include <algorithm>
21 #include "foundation/log.h"
22 #include "foundation/osal/thread/scoped_lock.h"
23 
24 namespace OHOS {
25 namespace Media {
Reset()26 void MediaStatStub::Reset()
27 {
28     OSAL::ScopedLock lock(mediaStatMutex_);
29     mediaStats_.clear();
30 }
31 
Append(const std::string & reporter)32 void MediaStatStub::Append(const std::string& reporter)
33 {
34     OSAL::ScopedLock lock(mediaStatMutex_);
35     for (auto& stat : mediaStats_) {
36         if (stat.reporter == reporter) {
37             return;
38         }
39     }
40     mediaStats_.emplace_back(reporter);
41 }
42 
ReceiveEvent(const Event & event)43 void MediaStatStub::ReceiveEvent(const Event& event)
44 {
45     switch (event.type) {
46         case EventType::EVENT_COMPLETE: {
47             OSAL::ScopedLock lock(mediaStatMutex_);
48             for (auto& stat : mediaStats_) {
49                 if (stat.reporter == event.srcFilter) {
50                     stat.completeEventReceived = true;
51                     break;
52                 }
53             }
54             break;
55         }
56         default:
57             MEDIA_LOG_W("MediaStats::ReceiveEvent receive unexpected event " PUBLIC_LOG_D32,
58                         static_cast<int>(event.type));
59             break;
60     }
61 }
62 
IsEventCompleteAllReceived()63 bool MediaStatStub::IsEventCompleteAllReceived()
64 {
65     OSAL::ScopedLock lock(mediaStatMutex_);
66     return std::all_of(mediaStats_.begin(), mediaStats_.end(), [](const MediaStat& stat) {
67         return stat.completeEventReceived.load();
68     });
69 }
70 
ResetEventCompleteAllReceived()71 void MediaStatStub::ResetEventCompleteAllReceived()
72 {
73     OSAL::ScopedLock lock(mediaStatMutex_);
74     for (auto& mediaStat : mediaStats_) {
75         mediaStat.completeEventReceived = false;
76     }
77 }
78 } // Media
79 } // OHOS