1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#include <cstdio> 17fa7767c5Sopenharmony_ci#include <cstring> 18fa7767c5Sopenharmony_ci#include <sstream> 19fa7767c5Sopenharmony_ci#include <dirent.h> 20fa7767c5Sopenharmony_ci#include "common/log.h" 21fa7767c5Sopenharmony_ci#include "osal/utils/dump_buffer.h" 22fa7767c5Sopenharmony_ci#include "parameter.h" 23fa7767c5Sopenharmony_ci#include "osal/filesystem/file_system.h" 24fa7767c5Sopenharmony_ci 25fa7767c5Sopenharmony_cinamespace { 26fa7767c5Sopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "DumpBuffer" }; 27fa7767c5Sopenharmony_ciconstexpr size_t DUMP_DATA_UNIT = 1; // data unit is 1 byte 28fa7767c5Sopenharmony_ci} 29fa7767c5Sopenharmony_ci 30fa7767c5Sopenharmony_cinamespace OHOS { 31fa7767c5Sopenharmony_cinamespace Media { 32fa7767c5Sopenharmony_cistd::map<std::string, FILE*> allDumpFileFds; 33fa7767c5Sopenharmony_civoid DumpAVBufferToFile(const std::string& para, const std::string& fileName, const std::shared_ptr<AVBuffer>& buffer) 34fa7767c5Sopenharmony_ci{ 35fa7767c5Sopenharmony_ci MEDIA_LOG_D("dump avbuffer to %{public}s", fileName.c_str()); 36fa7767c5Sopenharmony_ci if (buffer == nullptr || buffer->memory_ == nullptr) { 37fa7767c5Sopenharmony_ci MEDIA_LOG_E("buffer or memory is nullptr."); 38fa7767c5Sopenharmony_ci return; 39fa7767c5Sopenharmony_ci } 40fa7767c5Sopenharmony_ci FALSE_RETURN_MSG((para == "w" || para == "a") && !fileName.empty(), "para or fileName is invalid."); 41fa7767c5Sopenharmony_ci size_t bufferSize = static_cast<size_t>(buffer->memory_->GetSize()); 42fa7767c5Sopenharmony_ci FALSE_RETURN((bufferSize != 0) && (buffer->memory_->GetAddr() != nullptr)); 43fa7767c5Sopenharmony_ci std::string mode = para + "b+"; 44fa7767c5Sopenharmony_ci std::string filePath = DUMP_FILE_DIR + fileName; 45fa7767c5Sopenharmony_ci if (filePath == "") { 46fa7767c5Sopenharmony_ci return; 47fa7767c5Sopenharmony_ci } 48fa7767c5Sopenharmony_ci FILE* dumpFile = std::fopen(filePath.c_str(), mode.c_str()); 49fa7767c5Sopenharmony_ci if (dumpFile == nullptr) { 50fa7767c5Sopenharmony_ci MEDIA_LOG_E("dump buffer to file failed."); 51fa7767c5Sopenharmony_ci return; 52fa7767c5Sopenharmony_ci } 53fa7767c5Sopenharmony_ci size_t ret = 54fa7767c5Sopenharmony_ci fwrite(reinterpret_cast<const char*>(buffer->memory_->GetAddr()), DUMP_DATA_UNIT, bufferSize, dumpFile); 55fa7767c5Sopenharmony_ci if (ret < 0) { 56fa7767c5Sopenharmony_ci MEDIA_LOG_I("dump is fail."); 57fa7767c5Sopenharmony_ci } 58fa7767c5Sopenharmony_ci std::fclose(dumpFile); 59fa7767c5Sopenharmony_ci} 60fa7767c5Sopenharmony_ci} // Media 61fa7767c5Sopenharmony_ci} // OHOS