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 #include <mutex>
16 #include "avcodec_log.h"
17 #include "codec_service_stub.h"
18 #define PRINT_HILOG
19 #include "unittest_log.h"
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_TEST, "CodecServiceStubMock"};
22 std::mutex g_mutex;
23 std::weak_ptr<OHOS::MediaAVCodec::CodecServiceStubMock> g_mockObject;
24 } // namespace
25
26 namespace OHOS {
27 namespace MediaAVCodec {
RegisterMock(std::shared_ptr<CodecServiceStubMock> & mock)28 void CodecServiceStub::RegisterMock(std::shared_ptr<CodecServiceStubMock> &mock)
29 {
30 std::lock_guard<std::mutex> lock(g_mutex);
31 UNITTEST_INFO_LOG("CodecServiceStub:0x%" PRIXPTR, FAKE_POINTER(mock.get()));
32 g_mockObject = mock;
33 }
34
Create(int32_t instanceId)35 sptr<CodecServiceStub> CodecServiceStub::Create(int32_t instanceId)
36 {
37 (void)instanceId;
38 std::lock_guard<std::mutex> lock(g_mutex);
39 UNITTEST_INFO_LOG("");
40 auto mock = g_mockObject.lock();
41 UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, nullptr, "mock object is nullptr");
42 return mock->Create();
43 }
44
CodecServiceStub()45 CodecServiceStub::CodecServiceStub()
46 {
47 UNITTEST_INFO_LOG("");
48 }
49
~CodecServiceStub()50 CodecServiceStub::~CodecServiceStub()
51 {
52 std::lock_guard<std::mutex> lock(g_mutex);
53 UNITTEST_INFO_LOG("");
54 auto mock = g_mockObject.lock();
55 UNITTEST_CHECK_AND_RETURN_LOG(mock != nullptr, "mock object is nullptr");
56 mock->CodecServiceStubDtor();
57 }
58
Init(AVCodecType type,bool isMimeType,const std::string & name)59 int32_t CodecServiceStub::Init(AVCodecType type, bool isMimeType, const std::string &name)
60 {
61 std::lock_guard<std::mutex> lock(g_mutex);
62 UNITTEST_INFO_LOG("type:%d, isMimeType:%d, name:%s", static_cast<int32_t>(type), static_cast<int32_t>(isMimeType),
63 name.c_str());
64 auto mock = g_mockObject.lock();
65 UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
66 return mock->Init(type, isMimeType, name);
67 }
68
DestroyStub()69 int32_t CodecServiceStub::DestroyStub()
70 {
71 std::lock_guard<std::mutex> lock(g_mutex);
72 auto mock = g_mockObject.lock();
73 UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
74 return mock->DestroyStub();
75 }
76
Dump(int32_t fd,const std::vector<std::u16string> & args)77 int32_t CodecServiceStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
78 {
79 std::lock_guard<std::mutex> lock(g_mutex);
80 UNITTEST_INFO_LOG("fd:%d", fd);
81 auto mock = g_mockObject.lock();
82 UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
83 return mock->Dump(fd, args);
84 }
85
NotifyMemoryRecycle()86 void CodecServiceStub::NotifyMemoryRecycle()
87 {
88 std::lock_guard<std::mutex> lock(g_mutex);
89 UNITTEST_INFO_LOG("");
90 auto mock = g_mockObject.lock();
91 UNITTEST_CHECK_AND_RETURN_LOG(mock != nullptr, "mock object is nullptr");
92 mock->NotifyMemoryRecycle();
93 }
94
NotifyMemoryWriteBack()95 void CodecServiceStub::NotifyMemoryWriteBack()
96 {
97 std::lock_guard<std::mutex> lock(g_mutex);
98 UNITTEST_INFO_LOG("");
99 auto mock = g_mockObject.lock();
100 UNITTEST_CHECK_AND_RETURN_LOG(mock != nullptr, "mock object is nullptr");
101 mock->NotifyMemoryWriteBack();
102 }
103
104 } // namespace MediaAVCodec
105 } // namespace OHOS
106