1/*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_FRAME_MERGE_H
17#define OHOS_SHARING_FRAME_MERGE_H
18
19#include <functional>
20#include <list>
21#include <memory>
22#include "frame.h"
23
24namespace OHOS {
25namespace Sharing {
26/**
27 * Merge some frames with the same timestamp
28 */
29class FrameMerger {
30public:
31    using onOutput =
32        std::function<void(uint32_t dts, uint32_t pts, const DataBuffer::Ptr &buffer, bool have_key_frame)>;
33    using Ptr = std::shared_ptr<FrameMerger>;
34
35    enum {
36        NONE = 0,
37        H264_PREFIX,
38        MP4_NAL_SIZE,
39    };
40
41public:
42    FrameMerger() = default;
43    ~FrameMerger() = default;
44
45    void Clear();
46    void SetType(int32_t type);
47    bool InputFrame(const Frame::Ptr &frame, DataBuffer::Ptr &buffer, const onOutput &cb);
48
49private:
50    bool WillFlush(const Frame::Ptr &frame) const;
51    void DoMerge(DataBuffer::Ptr &merged, const Frame::Ptr &frame) const;
52
53private:
54    bool haveDecodeAbleFrame_ = false;
55    int32_t type_;
56
57    std::list<Frame::Ptr> frameCache_;
58};
59} // namespace Sharing
60} // namespace OHOS
61#endif