• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 #define HST_LOG_TAG "DumpBuffer"
16 
17 #include "foundation/utils/dump_buffer.h"
18 #include <cstdio>
19 #include <cstring>
20 #ifdef _WIN32
21 #include <direct.h>
22 #else
23 #include <unistd.h>
24 #endif
25 #include <dirent.h>
26 #include "foundation/log.h"
27 #include "foundation/osal/filesystem/file_system.h"
28 #include "foundation/osal/utils/util.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Pipeline {
33 #ifdef _WIN32
34 #define DUMP_FILE_DIR ""
35 #endif
36 // Specify the dump file dir in BUILD.gn
37 #ifndef DUMP_FILE_DIR
38 #define DUMP_FILE_DIR "/data/local/tmp"
39 #endif
40 
41 std::vector<std::string> allDumpFiles = {
42     DEMUXER_INPUT_PEEK,
43     DEMUXER_INPUT_GET,
44     DEMUXER_OUTPUT,
45     DECODER_OUTPUT
46 };
47 std::map<std::string, FILE*> allDumpFileFds;
48 
GetDumpFileDir()49 std::string GetDumpFileDir()
50 {
51     return std::string(DUMP_FILE_DIR);
52 }
53 
DumpBufferToFile(const std::string & fileName,const std::shared_ptr<Plugin::Buffer> & buffer)54 void DumpBufferToFile(const std::string& fileName, const std::shared_ptr<Plugin::Buffer>& buffer)
55 {
56     FALSE_RETURN_MSG(allDumpFileFds[fileName] != nullptr, "fd is null");
57     size_t bufferSize = buffer->GetMemory()->GetSize();
58     FALSE_RETURN(bufferSize != 0);
59     (void)fwrite(reinterpret_cast<const char*>(buffer->GetMemory()->GetReadOnlyData()),
60                  1, bufferSize, allDumpFileFds[fileName]);
61 }
62 
PrepareDumpDir()63 void PrepareDumpDir()
64 {
65     MEDIA_LOG_I("Prepare dumpDir enter.");
66     for (auto iter : allDumpFiles) {
67         std::string filePath = GetDumpFileDir() + "/" + iter;
68         MEDIA_LOG_I("Prepare dumpDir: " PUBLIC_LOG_S, filePath.c_str());
69         std::string fullPath;
70         bool isFileExist = OSAL::ConvertFullPath(filePath, fullPath);
71         if (isFileExist) { // 文件存在
72             OSAL::FileSystem::ClearFileContent(fullPath);
73         }
74         allDumpFileFds[iter] = fopen(fullPath.c_str(), "ab+");
75         if (allDumpFileFds[iter] == nullptr) {
76             MEDIA_LOG_W("Open file(" PUBLIC_LOG_S ") failed(" PUBLIC_LOG_S ").", fullPath.c_str(), strerror(errno));
77         }
78     }
79 }
80 
EndDumpFile()81 void EndDumpFile()
82 {
83     MEDIA_LOG_I("End dump enter.");
84     for (auto iter : allDumpFiles) {
85         if (allDumpFileFds[iter]) {
86             fclose(allDumpFileFds[iter]);
87             allDumpFileFds[iter] = nullptr;
88         }
89     }
90 }
91 
DumpBufferToLog(const char * desc,const std::shared_ptr<Plugin::Buffer> & buffer,uint64_t offset,size_t dumpSize)92 void DumpBufferToLog(const char* desc, const std::shared_ptr<Plugin::Buffer>& buffer, uint64_t offset, size_t dumpSize)
93 {
94     FALSE_RETURN_MSG(buffer && (!buffer->IsEmpty()),  PUBLIC_LOG_S " Buffer(null or empty)", desc);
95     size_t bufferSize = buffer->GetMemory()->GetSize();
96     size_t realDumpSize = std::min(dumpSize, bufferSize);
97     realDumpSize = std::min(realDumpSize, static_cast<size_t>(DUMP_BUFFER2LOG_SIZE)); // max DUMP_BUFFER2LOG_SIZE bytes
98     char tmpStr[2 * DUMP_BUFFER2LOG_SIZE + 10] = {0}; // 字符串长度是打印的buffer长度的2倍 + 1 (字符串结束符)
99     char* dstPtr = tmpStr;
100     int len;
101     const uint8_t* p = buffer->GetMemory()->GetReadOnlyData();
102     for (size_t i = 0; i < realDumpSize; i++) {
103         len = snprintf_s(dstPtr, 3, 2, "%02x", p[i]); // max write 3 bytes, string len 2
104         FALSE_RETURN_MSG(len > 0 && len <= 2, "snprintf_s returned unexpected value " PUBLIC_LOG_D32, len); // max len 2
105         dstPtr += len;
106     }
107     MEDIA_LOG_I(PUBLIC_LOG_S " Buffer(offset " PUBLIC_LOG_D64 ", size " PUBLIC_LOG_ZU ", capacity "
108         PUBLIC_LOG_ZU ") : " PUBLIC_LOG_S, desc, offset, bufferSize, buffer->GetMemory()->GetCapacity(), tmpStr);
109 }
110 } // Pipeline
111 } // Media
112 } // OHOS