diff --git a/av_transport/common/include/av_sync_utils.h b/av_transport/common/include/av_sync_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..2b9663120b1d3152105ff9aec5b6182ffc8d468e --- /dev/null +++ b/av_transport/common/include/av_sync_utils.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_SHARED_MEMORY_H +#define OHOS_AV_TRANSPORT_SHARED_MEMORY_H + +#include +#include +#include "nocopyable.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr uint8_t INVALID_VALUE_FALG = 0; +constexpr uint32_t MAX_CLOCK_UNIT_COUNT = 50; +constexpr int32_t DEFAULT_INVALID_FRAME_NUM = -1; + +struct AVTransSharedMemory { + int32_t fd; + int32_t size; + std::string name; +}; + +struct AVSyncClockUnit { + uint32_t index; + uint32_t frameNum; + int64_t timestamp; +}; + +/** + * @brief create shared memory space for av sync. + * @param name name for the shared memory. + * @return shared memory struct, include fd, size and name. + */ +AVTransSharedMemory CreateAVTransSharedMemory(const std::string &name); + +/** + * @brief close shared memory space. + * @param memory shared memory. + */ +void CloseAVTransSharedMemory(const AVTransSharedMemory &memory) noexcept; + +/** + * @brief write the clock unit into the shared memory space. + * @param memory shared memory + * @param clockUnit the clock unit + * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. + */ +int32_t WriteClockUnitToMemory(const AVTransSharedMemory &memory, AVSyncClockUnit &clockUnit); + +/** + * @brief read clock unit from the shared memory space. + * @param memory shared memory + * @param clockUnit the clock unit + * @return Returns DH_AVT_SUCCESS(0) if successful, otherwise returns other error code. + */ +int32_t ReadClockUnitFromMemory(const AVTransSharedMemory &memory, AVSyncClockUnit &clockUnit); + +bool IsInValidSharedMemory(const AVTransSharedMemory &memory); +bool IsInValidClockUnit(const AVSyncClockUnit &clockUnit); +bool IsInValidUnitIndex(const AVSyncClockUnit &clockUnit); + +uint32_t U8ToU32(const uint8_t *ptr); +uint64_t U8ToU64(const uint8_t *ptr); +void U32ToU8(uint8_t *ptr, uint32_t value); +void U64ToU8(uint8_t *ptr, uint64_t value); +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_SHARED_MEMORY_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_buffer.h b/av_transport/common/include/av_trans_buffer.h new file mode 100644 index 0000000000000000000000000000000000000000..bc61a485ccc88d562c058c38ead95b2e5b2edf64 --- /dev/null +++ b/av_transport/common/include/av_trans_buffer.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_BUFFER_H +#define OHOS_AV_TRANSPORT_BUFFER_H + +#include +#include +#include + +#include "av_trans_types.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr size_t INVALID_POSITION = -1; + +enum struct MetaType : uint32_t { + AUDIO, + VIDEO, +}; + +/** +* @brief BufferData description. Only manager the basic memory information. +*/ +class BufferData { +public: + explicit BufferData(size_t capacity); + BufferData(size_t capacity, std::shared_ptr bufData); + virtual ~BufferData() = default; + + size_t GetSize(); + size_t GetCapacity(); + uint8_t *GetAddress() const; + size_t Write(const uint8_t* in, size_t writeSize, size_t position = INVALID_POSITION); + size_t Read(uint8_t* out, size_t readSize, size_t position = INVALID_POSITION); + void Reset(); + void SetSize(size_t size); + +private: + size_t capacity_; + size_t size_; + std::shared_ptr address_; +}; + +/** + * @brief BufferMeta for buffer. Base class that describes various metadata. + */ +class BufferMeta { +public: + explicit BufferMeta(MetaType type); + virtual ~BufferMeta() = default; + + MetaType GetMetaType() const; + bool GetMetaItem(AVTransTag tag, std::string &value); + void SetMetaItem(AVTransTag tag, const std::string &value); + +private: + MetaType type_; + std::map tagMap_; +}; + +/** +* @brief AVTransBuffer base class. +* Contains the data storage and metadata information of the buffer (buffer description information). +*/ +class AVTransBuffer { +public: + explicit AVTransBuffer(MetaType type = MetaType::VIDEO); + ~AVTransBuffer() = default; + + std::shared_ptr GetBufferData(uint32_t index = 0); + std::shared_ptr CreateBufferData(size_t capacity); + std::shared_ptr WrapBufferData(const uint8_t* data, size_t capacity, size_t size); + std::shared_ptr GetBufferMeta(); + void UpdateBufferMeta(std::shared_ptr bufferMeta); + uint32_t GetDataCount(); + void Reset(); + bool IsEmpty(); + +private: + std::vector> data_ {}; + std::shared_ptr meta_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_BUFFER_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_constants.h b/av_transport/common/include/av_trans_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..f308f4742af9f1d3ea14c347295f8c69ef3717b4 --- /dev/null +++ b/av_transport/common/include/av_trans_constants.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_CONSTANTS_H +#define OHOS_AV_TRANSPORT_CONSTANTS_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +const int32_t LOG_MAX_LEN = 4096; +const int32_t LINK_TYPE_MAX = 4; +const int32_t BASE_ENGINE_ID = 1000; +const int32_t DEVICE_ID_LENGTH = 65; +const int32_t INVALID_ENGINE_ID = -1; +const int32_t INVALID_SESSION_ID = -1; +const int32_t SESSION_WAIT_SECONDS = 5; +const int32_t INTERCERT_STRING_LENGTH = 20; + +const uint32_t MAX_DEVICE_ID_LEN = 100; +const uint32_t MAX_SESSION_NAME_LEN = 100; +const uint32_t MAX_MESSAGES_LEN = 40 * 1024 * 1024; +const uint32_t DSOFTBUS_INPUT_MAX_RECV_EXT_LEN = 104857600; +const uint32_t DSOFTBUS_INPUT_MAX_RECV_DATA_LEN = 104857600; + +const std::string EMPTY_STRING = ""; +const std::string DH_LOG_TITLE_TAG = "DH_AV_TRANS"; +const std::string SENDER_CONTROL_SESSION_NAME_SUFFIX = "sender.avtrans.control"; +const std::string RECEIVER_CONTROL_SESSION_NAME_SUFFIX = "receiver.avtrans.control"; +const std::string SENDER_DATA_SESSION_NAME_SUFFIX = "sender.avtrans.data"; +const std::string RECEIVER_DATA_SESSION_NAME_SUFFIX = "receiver.avtrans.data"; +const std::string DH_FWK_OWNER_NAME = "ohos.dhardware"; +const std::string AV_SYNC_SENDER_CONTROL_SESSION_NAME = "ohos.dhardware.av.sync.sender.control"; +const std::string AV_SYNC_RECEIVER_CONTROL_SESSION_NAME = "ohos.dhardware.av.sync.receiver.control"; + +const std::string AVINPUT_NAME = "builtin.avtransport.avinput"; +const std::string AVOUTPUT_NAME = "builtin.avtransport.avoutput"; +const std::string AENCODER_NAME = "builtin.recorder.audioencoder"; +const std::string VENCODER_NAME = "builtin.recorder.videoencoder"; +const std::string ADECODER_NAME = "builtin.player.audiodecoder"; +const std::string VDECODER_NAME = "builtin.player.videodecoder"; + +const std::string AVT_DATA_META_TYPE = "avtrans_data_meta_type"; +const std::string AVT_DATA_PARAM = "avtrans_data_param"; +const std::string AVT_PARAM_BUFFERMETA_DATATYPE = "avtrans_param_buffermeta_datatype"; +const std::string AV_TRANS_SPECIAL_DEVICE_ID = "av_trans_special_device_id"; + +const std::string KEY_MY_DEV_ID = "myDevId"; +const std::string KEY_SCENE_TYPE = "sceneType"; +const std::string KEY_PEER_DEV_ID = "peerDevId"; +const std::string KEY_SESSION_ID = "sessionId"; +const std::string KEY_SESSION_NAME = "sessionName"; +const std::string KEY_AV_SYNC_FLAG = "avSyncFlag"; +const std::string KEY_START_FRAME_NUM = "startFrameNum"; +const std::string KEY_GROUP_INFO_ARRAY = "groupInfoArray"; +const std::string KEY_SHARED_MEM_FD = "sharedMemoryFd"; +const std::string KEY_SHARED_MEM_SIZE = "sharedMemorySize"; +const std::string KEY_SHARED_MEM_NAME = "sharedMemoryName"; + +const uint8_t DATA_WAIT_SECONDS = 1; +const size_t DATA_QUEUE_MAX_SIZE = 1000; +constexpr const char *SEND_CHANNEL_EVENT = "SendChannelEvent"; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_CONSTANTS_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_errno.h b/av_transport/common/include/av_trans_errno.h new file mode 100644 index 0000000000000000000000000000000000000000..dfa1babe448802e4b176aee43cee32ecee0fb499 --- /dev/null +++ b/av_transport/common/include/av_trans_errno.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_ERRNO_H +#define OHOS_AV_TRANSPORT_ERRNO_H + +#include + +namespace OHOS { +namespace DistributedHardware { + /* + * The av transport module define errno, range: [-80000; -89999] + * Here's external errno, range: [-80000; -80199] + */ + constexpr int32_t DH_AVT_SUCCESS = 0; + constexpr int32_t ERR_DH_AVT_INVALID_PARAM = -80001; + constexpr int32_t ERR_DH_AVT_INIT_FAILED = -80002; + constexpr int32_t ERR_DH_AVT_START_FAILED = -80003; + constexpr int32_t ERR_DH_AVT_STOP_FAILED = -80004; + constexpr int32_t ERR_DH_AVT_RELEASE_FAILED = -80005; + constexpr int32_t ERR_DH_AVT_SETUP_FAILED = -80006; + constexpr int32_t ERR_DH_AVT_PUSH_DATA_FAILED = -80007; + constexpr int32_t ERR_DH_AVT_PULL_DATA_FAILED = -80008; + constexpr int32_t ERR_DH_AVT_SEND_MESSAGE_FAILED = -80009; + constexpr int32_t ERR_DH_AVT_CREATE_CHANNEL_FAILED = -80010; + constexpr int32_t ERR_DH_AVT_UNIMPLEMENTED = -80011; + constexpr int32_t ERR_DH_AVT_COMMON_FAILED = -80012; + + /* Here's internal errno, range: [-81000; -89999] */ + constexpr int32_t ERR_DH_AVT_INVALID_PARAM_VALUE = -81000; + constexpr int32_t ERR_DH_AVT_INVALID_PARAM_TYPE = -81001; + constexpr int32_t ERR_DH_AVT_INVALID_OPERATION = -81002; + constexpr int32_t ERR_DH_AVT_UNSUPPORTED_FORMAT = -81003; + constexpr int32_t ERR_DH_AVT_NOT_EXISTED = -81004; + constexpr int32_t ERR_DH_AVT_TIMED_OUT = -81005; + constexpr int32_t ERR_DH_AVT_NO_MEMORY = -81006; + constexpr int32_t ERR_DH_AVT_INVALID_STATE = -81007; + constexpr int32_t ERR_DH_AVT_PERMISSION_DENIED = -81008; + constexpr int32_t ERR_DH_AVT_NO_NOTIFY = -81009; + constexpr int32_t ERR_DH_AVT_NULL_POINTER = -81010; + constexpr int32_t ERR_DH_AVT_CHANNEL_ERROR = -81011; + constexpr int32_t ERR_DH_AVT_SEND_DATA_FAILED = -81012; + constexpr int32_t ERR_DH_AVT_PREPARE_FAILED = -81013; + constexpr int32_t ERR_DH_AVT_SET_PARAM_FAILED = -81014; + constexpr int32_t ERR_DH_AVT_OUTPUT_DATA_FAILED = -80015; + constexpr int32_t ERR_DH_AVT_TIME_SYNC_FAILED = -80016; + constexpr int32_t ERR_DH_AVT_SERVICE_REMOTE_IS_NULL = -80017; + constexpr int32_t ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL = -80018; + constexpr int32_t ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL = -80019; + constexpr int32_t ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL = -80019; + constexpr int32_t ERR_DH_AVT_CTRL_CENTER_INIT_FAIL = -80020; + constexpr int32_t ERR_DH_AVT_REGISTER_CALLBACK_FAIL = -80021; + constexpr int32_t ERR_DH_AVT_CTRL_CENTER_KIT_NULL = -80022; + constexpr int32_t ERR_DH_AVT_SHARED_MEMORY_FAILED = -80023; + constexpr int32_t ERR_DH_AVT_MASTER_NOT_READY = -80024; + constexpr int32_t ERR_DH_AVT_CHANNEL_ALREADY_OPENED = -80025; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_ERRNO_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_log.h b/av_transport/common/include/av_trans_log.h new file mode 100644 index 0000000000000000000000000000000000000000..634b30c1afa18fe69fbeb2947cfccd84bec13753 --- /dev/null +++ b/av_transport/common/include/av_trans_log.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_LOG_H +#define OHOS_AV_TRANSPORT_LOG_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +typedef enum { + DH_LOG_DEBUG, + DH_LOG_INFO, + DH_LOG_WARN, + DH_LOG_ERROR, +} DHLogLevel; + +void DHLog(DHLogLevel logLevel, const char *fmt, ...); + +#define DHLOGD(fmt, ...) DHLog(DH_LOG_DEBUG, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +#define DHLOGI(fmt, ...) DHLog(DH_LOG_INFO, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +#define DHLOGW(fmt, ...) DHLog(DH_LOG_WARN, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +#define DHLOGE(fmt, ...) DHLog(DH_LOG_ERROR, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +std::string GetAnonyString(const std::string &value); +std::string GetAnonyInt32(const int32_t value); + +#ifndef TRUE_RETURN +#define TRUE_RETURN(exec, fmt, args...) \ + do { \ + bool retCode = (exec); \ + if (retCode) { \ + DHLOGE(fmt, ##args); \ + return; \ + } \ + } while (0) +#endif + +#ifndef TRUE_RETURN_V +#define TRUE_RETURN_V(exec, ret) \ + do { \ + bool retCode = (exec); \ + if (retCode) { \ + DHLOGE("TRUE_RETURN_V " #exec); \ + return ret; \ + } \ + } while (0) +#endif + +#ifndef TRUE_RETURN_V_MSG_E +#define TRUE_RETURN_V_MSG_E(exec, ret, fmt, args...) \ + do { \ + bool retCode = (exec); \ + if (retCode) { \ + DHLOGE(fmt, ##args); \ + return ret; \ + } \ + } while (0) +#endif + +#ifndef TRUE_LOG_MSG +#define TRUE_LOG_MSG(exec, fmt, args...) \ + do { \ + bool retCode = (exec); \ + if (retCode) { \ + DHLOGE(fmt, ##args); \ + } \ + } while (0) +#endif +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_LOG_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_message.h b/av_transport/common/include/av_trans_message.h new file mode 100644 index 0000000000000000000000000000000000000000..d3be62734d7bec30591b395e0d2f47379e9a2773 --- /dev/null +++ b/av_transport/common/include/av_trans_message.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_MESSAGE_H +#define OHOS_AV_TRANSPORT_MESSAGE_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class AVTransMessage { +public: + AVTransMessage(); + AVTransMessage(uint32_t type, std::string content, std::string dstDevId); + ~AVTransMessage(); + + std::string MarshalMessage(); + bool UnmarshalMessage(const std::string &jsonStr); + +public: + uint32_t type_; + std::string content_; + std::string dstDevId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_MESSAGE_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_meta.h b/av_transport/common/include/av_trans_meta.h new file mode 100644 index 0000000000000000000000000000000000000000..0addcdceeb7f964e51b3096e4d68dd5a2ba398bd --- /dev/null +++ b/av_transport/common/include/av_trans_meta.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_EXTEND_META_H +#define OHOS_AV_TRANSPORT_EXTEND_META_H + +#include "av_trans_types.h" + +// follwing head files depends on histreamer +#include "plugin_buffer.h" + +namespace OHOS { +namespace DistributedHardware { +using namespace OHOS::Media; +using namespace OHOS::Media::Plugin; + +/** + * @brief av transport audio buffer metadata. + * Buffer metadata describing how data is laid out inside the buffer + */ +class AVTransAudioBufferMeta : public OHOS::Media::Plugin::BufferMeta { +public: + AVTransAudioBufferMeta() : OHOS::Media::Plugin::BufferMeta(BufferMetaType::AUDIO) {} + ~AVTransAudioBufferMeta() override = default; + + std::shared_ptr Clone() override; + + std::string MarshalAudioMeta(); + bool UnmarshalAudioMeta(const std::string &jsonStr); + +public: + int64_t pts_ {0}; + + int64_t cts_ {0}; + + uint32_t frameNum_ {0}; + + uint32_t channels_ {0}; + + uint32_t sampleRate_ {0}; + + BufferDataType dataType_ {BufferDataType::AUDIO}; + + AudioSampleFormat format_ {AudioSampleFormat::S8}; + +private: + friend class Buffer; +}; + +/** + * @brief av transport video buffer metadata. + * Extra buffer metadata describing video properties. + */ +class AVTransVideoBufferMeta : public OHOS::Media::Plugin::BufferMeta { +public: + AVTransVideoBufferMeta() : OHOS::Media::Plugin::BufferMeta(BufferMetaType::VIDEO) {} + ~AVTransVideoBufferMeta() override = default; + + std::shared_ptr Clone() override; + + std::string MarshalVideoMeta(); + bool UnmarshalVideoMeta(const std::string &jsonStr); + + int64_t pts_ {0}; + + int64_t cts_ {0}; + + uint32_t width_ {0}; + + uint32_t height_ {0}; + + uint32_t frameNum_ {0}; + + VideoPixelFormat format_ {VideoPixelFormat::UNKNOWN}; + + BufferDataType dataType_ {BufferDataType::VIDEO_STREAM}; + +private: + friend class Buffer; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_EXTEND_META_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_types.h b/av_transport/common/include/av_trans_types.h new file mode 100644 index 0000000000000000000000000000000000000000..a0a07bdc324a2325c6edf4d9c4faf71791766475 --- /dev/null +++ b/av_transport/common/include/av_trans_types.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_TYPES_H +#define OHOS_AV_TRANSPORT_TYPES_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +const std::string OWNER_NAME_D_MIC = "ohos.dhardware.dmic"; +const std::string OWNER_NAME_D_CAMERA = "ohos.dhardware.dcamera"; +const std::string OWNER_NAME_D_SCREEN = "ohos.dhardware.dscreen"; +const std::string OWNER_NAME_D_SPEAKER = "ohos.dhardware.dspeaker"; + +const std::string SCENE_TYPE_D_MIC = "dmic_stream"; +const std::string SCENE_TYPE_D_SCREEN = "dscreen_stream"; +const std::string SCENE_TYPE_D_SPEAKER = "dspeaker_stream"; +const std::string SCENE_TYPE_D_CAMERA_STR = "dcamera_stream"; +const std::string SCENE_TYPE_D_CAMERA_PIC = "dcamera_picture"; + +const std::string MINE_VIDEO_RAW = "video/raw"; +const std::string MINE_VIDEO_H264 = "video/avc"; +const std::string MINE_VIDEO_H265 = "video/hevc"; + +const std::string VIDEO_FORMAT_NV12 = "nv12"; +const std::string VIDEO_FORMAT_NV21 = "nv21"; +const std::string VIDEO_FORMAT_JEPG = "jpeg"; +const std::string VIDEO_FORMAT_YUVI420 = "yuvi420"; +const std::string VIDEO_FORMAT_RGBA8888 = "rgba8888"; + +enum struct TransRole : uint32_t { + AV_SENDER = 0, + AV_RECEIVER = 1, + UNKNOWN = 2 +}; + +enum struct AvSyncFlag : uint32_t { + MASTER = 0, + SLAVE = 1, + UNKNOWN = 2 +}; + +enum struct TransStrategy : uint32_t { + LOW_LATANCY_STRATEGY, + LOW_JITTER_STRATEGY +}; + +struct ChannelAttribute { + TransStrategy strategy; +}; + +enum struct BufferDataType : uint32_t { + AUDIO = 0, + VIDEO_STREAM, + PICTURE, + UNKNOW, +}; + +enum struct StateId : uint32_t { + IDLE = 0, + INITIALIZED = 1, + CH_CREATING = 2, + CH_CREATED = 3, + STARTED = 4, + PLAYING = 5, + STOPPED = 6, + BUTT, +}; + +enum struct TagSection : uint8_t { + REGULAR = 1, + D_AUDIO = 2, + D_VIDEO = 3, + MAX_SECTION = 64 +}; + +enum struct AVTransTag : uint32_t { + INVALID = 0, + SECTION_REGULAR_START = static_cast(TagSection::REGULAR) << 16U, + SECTION_D_AUDIO_START = static_cast(TagSection::D_AUDIO) << 16U, + SECTION_D_VIDEO_START = static_cast(TagSection::D_VIDEO) << 16U, + + /* -------------------- regular tag -------------------- */ + FRAME_NUMBER = SECTION_REGULAR_START + 1, + BUFFER_DATA_TYPE, + PRE_TIMESTAMP, + CUR_TIMESTAMP, + ENGINE_READY, + AV_SYNC_GROUP_INFO, + TIME_SYNC_RESULT, + SHARED_MEMORY_FD, + + /* -------------------- d_audio tag -------------------- */ + AUDIO_CHANNELS = SECTION_D_AUDIO_START + 1, + AUDIO_SAMPLE_RATE, + AUDIO_SAMPLE_FORMAT, + + /* -------------------- d_video tag -------------------- */ + VIDEO_WIDTH = SECTION_D_VIDEO_START + 1, + VIDEO_HEIGHT, + VIDEO_CODEC_TYPE, + VIDEO_PIXEL_FORMAT, + VIDEO_FRAME_RATE, + VIDEO_BIT_RATE, +}; + +enum struct EventType : uint32_t { + EVENT_CHANNEL_OPENED = 0, + EVENT_CHANNEL_OPEN_FAIL = 1, + EVENT_CHANNEL_CLOSED = 2, + EVENT_START_SUCCESS = 3, + EVENT_START_FAIL = 4, + EVENT_STOP_SUCCESS = 5, + EVENT_STOP_FAIL = 6, + EVENT_ENGINE_ERROR = 7, + EVENT_REMOTE_ERROR = 8, + EVENT_DATA_RECEIVED = 9, + EVENT_TIME_SYNC_RESULT = 10, + EVENT_NOTIFY_STREAM_INFO = 11, +}; + +struct AVTransEvent { + EventType type; + std::string content; + std::string peerDevId; +}; + +struct AVStreamInfo { + std::string sceneType; + std::string peerDevId; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_TYPES_H \ No newline at end of file diff --git a/av_transport/common/include/av_trans_utils.h b/av_transport/common/include/av_trans_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..bf68d5b3b128fd56a98fecfa64296926e3413f24 --- /dev/null +++ b/av_transport/common/include/av_trans_utils.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_UTILS_H +#define OHOS_AV_TRANSPORT_UTILS_H + +#include +#include +#include +#include + +#include "av_trans_buffer.h" +#include "av_trans_types.h" +#include "nlohmann/json.hpp" + +// follwing head files depends on histreamer +#include "event.h" +#include "plugin_event.h" +#include "plugin_buffer.h" +#include "plugin_source_tags.h" + +namespace OHOS { +namespace DistributedHardware { +using namespace OHOS::Media; +using namespace OHOS::Media::Plugin; +using AVBuffer = OHOS::Media::Plugin::Buffer; +constexpr int32_t MS_ONE_SECOND = 1000; + +SrcInputType TransName2InputType(const std::string &ownerName); +MediaType TransName2MediaType(const std::string &ownerName); + +std::shared_ptr TransBuffer2HiSBuffer(const std::shared_ptr &transBuffer); +std::shared_ptr HiSBuffer2TransBuffer(const std::shared_ptr &hisBuffer); +void Convert2HiSBufferMeta(std::shared_ptr transBuffer, std::shared_ptr hisBuffer); +void Convert2TransBufferMeta(std::shared_ptr hisBuffer, std::shared_ptr transBuffer); + +std::string BuildChannelDescription(const std::string &ownerName, const std::string &peerDevId); +void ParseChannelDescription(const std::string &descJsonStr, std::string &ownerName, std::string &peerDevId); + +EventType CastEventType(Plugin::PluginEventType type); +void DumpBufferToFile(std::string fileName, uint8_t *buffer, int32_t bufSize); + +bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key); +bool IsInt64(const nlohmann::json &jsonObj, const std::string &key); +bool IsString(const nlohmann::json &jsonObj, const std::string &key); + +int64_t GetCurrentTime(); + +template +inline std::shared_ptr ReinterpretCastPointer(const std::shared_ptr &ptr) noexcept +{ + return std::shared_ptr(ptr, reinterpret_cast(ptr.get())); +} +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_UTILS_H \ No newline at end of file diff --git a/av_transport/common/include/single_instance.h b/av_transport/common/include/single_instance.h new file mode 100644 index 0000000000000000000000000000000000000000..fe68569efad8598d57c9837707c6426ff195f91c --- /dev/null +++ b/av_transport/common/include/single_instance.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AV_TRANSPORT_SINGLE_INSTANCE_H +#define OHOS_AV_TRANSPORT_SINGLE_INSTANCE_H + +namespace OHOS { +namespace DistributedHardware { +#define REMOVE_NO_USE_CONSTRUCTOR(className) \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + +#define DECLARE_SINGLE_INSTANCE_BASE(className) \ +public: \ + static className & GetInstance(); \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + +#define DECLARE_SINGLE_INSTANCE(className) \ + DECLARE_SINGLE_INSTANCE_BASE(className) \ +private: \ + className() = default; \ + virtual ~className() = default; \ + +#define IMPLEMENT_SINGLE_INSTANCE(className) \ +className & className::GetInstance() \ +{ \ + static className instance; \ + return instance; \ +} +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_AV_TRANSPORT_SINGLE_INSTANCE_H diff --git a/av_transport/common/include/softbus_channel_adapter.h b/av_transport/common/include/softbus_channel_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..fffcea47eb923d03cf490a47e35236ad598f00a3 --- /dev/null +++ b/av_transport/common/include/softbus_channel_adapter.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SOFTBUS_CHANNEL_ADAPTER +#define OHOS_SOFTBUS_CHANNEL_ADAPTER + +#include +#include +#include + +#include "av_trans_types.h" +#include "session.h" +#include "single_instance.h" +#include "softbus_bus_center.h" +#include "softbus_common.h" + +namespace OHOS { +namespace DistributedHardware { +class ISoftbusChannelListener { +public: + virtual ~ISoftbusChannelListener() = default; + virtual void OnChannelEvent(const AVTransEvent &event) = 0; + virtual void OnStreamReceived(const StreamData *data, const StreamData *ext) = 0; +}; + +class SoftbusChannelAdapter { + DECLARE_SINGLE_INSTANCE_BASE(SoftbusChannelAdapter); +public: + int32_t CreateChannelServer(const std::string &ownerName, const std::string &sessName); + int32_t RemoveChannelServer(const std::string &ownerName, const std::string &sessName); + + int32_t OpenSoftbusChannel(const std::string &mySessName, const std::string &peerSessName, + const std::string &peerDevId); + int32_t CloseSoftbusChannel(const std::string &mySessName, const std::string &peerDevId); + + int32_t SendBytesData(const std::string &sessName, const std::string &peerDevId, const std::string &data); + int32_t SendStreamData(const std::string &sessName, const std::string &peerDevId, const StreamData *data, + const StreamData *ext); + + int32_t RegisterChannelListener(const std::string &sessName, const std::string &peerDevId, + ISoftbusChannelListener *listener); + int32_t UnRegisterChannelListener(const std::string &sessName, const std::string &peerDevId); + + int32_t StartDeviceTimeSync(const std::string &ownerName, const std::string &sessName, + const std::string &peerDevId); + int32_t StopDeviceTimeSync(const std::string &ownerName, const std::string &sessName, + const std::string &peerDevId); + + void SendChannelEvent(ISoftbusChannelListener *listener, const AVTransEvent &event); + + int32_t OnSoftbusChannelOpened(int32_t sessionId, int32_t result); + void OnSoftbusChannelClosed(int32_t sessionId); + void OnSoftbusBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); + void OnSoftbusTimeSyncResult(const TimeSyncResultInfo *info, int32_t result); + void OnSoftbusStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *frameInfo); + +private: + SoftbusChannelAdapter(); + ~SoftbusChannelAdapter(); + + std::string GetSessionNameById(int32_t sessionId); + int32_t GetSessIdBySessName(const std::string &sessName, const std::string &peerDevId); + +private: + std::mutex name2IdMtx_; + std::mutex timeSyncMtx_; + std::mutex idMapMutex_; + std::mutex listenerMtx_; + + ISessionListener sessListener_; + std::set sessionNameSet_; + std::set timeSyncSessNames_; + std::map devId2SessIdMap_; + std::map listenerMap_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_SOFTBUS_CHANNEL_ADAPTER \ No newline at end of file