1 /*
2  * Copyright (c) 2023 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 #ifndef OHOS_AV_TRANSPORT_UTILS_H
17 #define OHOS_AV_TRANSPORT_UTILS_H
18 
19 #include <fstream>
20 #include <string>
21 #include <sys/time.h>
22 #include <unistd.h>
23 
24 #include "av_trans_buffer.h"
25 #include "av_trans_types.h"
26 #include "cJSON.h"
27 
28 // follwing head files depends on histreamer
29 #include "event.h"
30 #include "plugin_event.h"
31 #include "plugin_buffer.h"
32 #include "plugin_source_tags.h"
33 
34 namespace OHOS {
35 namespace DistributedHardware {
36 using namespace OHOS::Media;
37 using namespace OHOS::Media::Plugin;
38 using AVBuffer = OHOS::Media::Plugin::Buffer;
39 const int64_t NS_ONE_US = 1000;
40 const int64_t NS_ONE_MS = 1000000;
41 const int64_t NS_ONE_S = 1000000000;
42 std::string TransName2PkgName(const std::string &ownerName);
43 MediaType TransName2MediaType(const std::string &ownerName);
44 
45 std::shared_ptr<AVBuffer> TransBuffer2HiSBuffer(const std::shared_ptr<AVTransBuffer> &transBuffer);
46 std::shared_ptr<AVTransBuffer> HiSBuffer2TransBuffer(const std::shared_ptr<AVBuffer> &hisBuffer);
47 void Convert2HiSBufferMeta(std::shared_ptr<AVTransBuffer> transBuffer, std::shared_ptr<AVBuffer> hisBuffer);
48 void Convert2TransBufferMeta(std::shared_ptr<AVBuffer> hisBuffer, std::shared_ptr<AVTransBuffer> transBuffer);
49 
50 std::string BuildChannelDescription(const std::string &ownerName, const std::string &peerDevId);
51 void ParseChannelDescription(const std::string &descJsonStr, std::string &ownerName, std::string &peerDevId);
52 
53 EventType CastEventType(Plugin::PluginEventType type, bool isAbnormal);
54 void DumpBufferToFile(std::string fileName, uint8_t *buffer, int32_t bufSize);
55 
56 bool IsUInt32(const cJSON *jsonObj, const std::string &key);
57 bool IsInt64(const cJSON *jsonObj, const std::string &key);
58 bool IsString(const cJSON *jsonObj, const std::string &key);
59 
60 int64_t GetCurrentTime();
61 
62 void GenerateAdtsHeader(unsigned char* adtsHeader, uint32_t packetLen, uint32_t profile, uint32_t sampleRate,
63     uint32_t channels);
64 
65 template<typename T, typename U>
ReinterpretCastPointer(const std::shared_ptr<U> & ptr)66 inline std::shared_ptr<T> ReinterpretCastPointer(const std::shared_ptr<U> &ptr) noexcept
67 {
68     return std::shared_ptr<T>(ptr, reinterpret_cast<T*>(ptr.get()));
69 }
70 } // namespace DistributedHardware
71 } // namespace OHOS
72 #endif // OHOS_AV_TRANSPORT_UTILS_H