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 #include "dump_buffer_manager.h"
17 #include "dump_buffer_define.h"
18 #include "log.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "HiStreamer" };
26 }
27
28 using namespace OHOS;
29 using namespace OHOS::Media;
30
DumpBufferNew(void)31 DumpBuffer *DumpBufferNew(void)
32 {
33 std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer();
34 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "new AVBuffer failed");
35 struct DumpBuffer *buf = new (std::nothrow) DumpBuffer(buffer);
36 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "failed to new DumpBuffer");
37 return buf;
38 }
39
DumpBufferCreate(int32_t capacity)40 DumpBuffer *DumpBufferCreate(int32_t capacity)
41 {
42 FALSE_RETURN_V_MSG_E(capacity > 0, nullptr, "capacity %{public}d is error!", capacity);
43 AVBufferConfig avBufferConfig;
44 avBufferConfig.size = capacity;
45 avBufferConfig.memoryType = MemoryType::SHARED_MEMORY;
46 avBufferConfig.memoryFlag = MemoryFlag::MEMORY_READ_WRITE;
47 std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(avBufferConfig);
48 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "create AVBuffer failed");
49 FALSE_RETURN_V_MSG_E(buffer->memory_ != nullptr, nullptr, "memory is nullptr");
50 FALSE_RETURN_V_MSG_E(buffer->memory_->GetAddr() != nullptr, nullptr, "memory's addr is nullptr");
51
52 struct DumpBuffer *buf = new (std::nothrow) DumpBuffer(buffer);
53 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "failed to new DumpBuffer");
54 return buf;
55 }
56
DumpBufferDestroy(struct DumpBuffer * buffer)57 void DumpBufferDestroy(struct DumpBuffer *buffer)
58 {
59 FALSE_RETURN_MSG(buffer != nullptr, "input buffer is nullptr!");
60 delete buffer;
61 return;
62 }
63
DumpBufferGetAddr(DumpBuffer * buffer)64 uint8_t *DumpBufferGetAddr(DumpBuffer *buffer)
65 {
66 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
67 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
68 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, nullptr, "buffer's memory is nullptr!");
69 return buffer->buffer_->memory_->GetAddr();
70 }
71
DumpBufferGetCapacity(DumpBuffer * buffer)72 int32_t DumpBufferGetCapacity(DumpBuffer *buffer)
73 {
74 FALSE_RETURN_V_MSG_E(buffer != nullptr, -1, "input buffer is nullptr!");
75 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, -1, "buffer is nullptr!");
76 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, -1, "buffer's memory is nullptr!");
77 return buffer->buffer_->memory_->GetCapacity();
78 }
79
DumpBufferGetSize(DumpBuffer * buffer)80 int32_t DumpBufferGetSize(DumpBuffer *buffer)
81 {
82 FALSE_RETURN_V_MSG_E(buffer != nullptr, -1, "input buffer is nullptr!");
83 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, -1, "buffer is nullptr!");
84 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, -1, "buffer's memory is nullptr!");
85 return buffer->buffer_->memory_->GetSize();
86 }
87
DumpBufferSetSize(DumpBuffer * buffer,int32_t size)88 bool DumpBufferSetSize(DumpBuffer *buffer, int32_t size)
89 {
90 FALSE_RETURN_V_MSG_E(buffer != nullptr, false, "input buffer is nullptr!");
91 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, false, "buffer is nullptr!");
92 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, false, "buffer's memory is nullptr!");
93 buffer->buffer_->memory_->SetSize(size);
94 return true;
95 }
96
DumpBufferWrite(DumpBuffer * buffer,const uint8_t * in,int32_t writeSize)97 int32_t DumpBufferWrite(DumpBuffer *buffer, const uint8_t *in, int32_t writeSize)
98 {
99 FALSE_RETURN_V_MSG_E(buffer != nullptr, -1, "input buffer is nullptr!");
100 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, -1, "buffer is nullptr!");
101 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, -1, "buffer's memory is nullptr!");
102 return buffer->buffer_->memory_->Write(in, writeSize, 0);
103 }
104
DumpBufferGetUniqueId(DumpBuffer * buffer)105 uint64_t DumpBufferGetUniqueId(DumpBuffer *buffer)
106 {
107 FALSE_RETURN_V_MSG_E(buffer != nullptr, 0, "input buffer is nullptr!");
108 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, 0, "buffer is nullptr!");
109 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, 0, "buffer's memory is nullptr!");
110 return buffer->buffer_->GetUniqueId();
111 }
112
DumpBufferReadFromParcel(DumpBuffer * buffer,void * parcel)113 bool DumpBufferReadFromParcel(DumpBuffer *buffer, void *parcel)
114 {
115 FALSE_RETURN_V_MSG_E(buffer != nullptr, false, "input buffer is nullptr!");
116 FALSE_RETURN_V_MSG_E(parcel != nullptr, false, "input parcel is nullptr!");
117 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, false, "buffer is nullptr!");
118 MessageParcel *parcelIn = static_cast<MessageParcel *>(parcel);
119 bool ret = buffer->buffer_->ReadFromMessageParcel(*parcelIn);
120 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, false, "buffer's memory is nullptr!");
121 return ret;
122 }
123
DumpBufferWriteToParcel(DumpBuffer * buffer,void * parcel)124 bool DumpBufferWriteToParcel(DumpBuffer *buffer, void *parcel)
125 {
126 FALSE_RETURN_V_MSG_E(buffer != nullptr, false, "input buffer is nullptr!");
127 FALSE_RETURN_V_MSG_E(parcel != nullptr, false, "input parcel is nullptr!");
128 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, false, "buffer is nullptr!");
129 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, false, "buffer's memory is nullptr!");
130 MessageParcel *parcelIn = static_cast<MessageParcel *>(parcel);
131 bool ret = buffer->buffer_->WriteToMessageParcel(*parcelIn);
132 return ret;
133 }
134
135 #ifdef __cplusplus
136 }
137 #endif