1/*
2 * Copyright (c) 2023-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 MEDIA_PIPELINE_VIDEO_SINK_H
17#define MEDIA_PIPELINE_VIDEO_SINK_H
18
19#include "osal/task/task.h"
20#include "sink/media_synchronous_sink.h"
21#include "buffer/avbuffer.h"
22#include "common/status.h"
23#include "meta/video_types.h"
24#include "filter/filter.h"
25
26namespace OHOS {
27namespace Media {
28namespace Pipeline {
29class VideoSink : public MediaSynchronousSink {
30public:
31    VideoSink();
32    ~VideoSink();
33    int64_t DoSyncWrite(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer) override; // true and render
34    void ResetSyncInfo() override;
35    Status GetLatency(uint64_t& nanoSec);
36    int64_t CheckBufferLatenessMayWait(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
37    void SetSyncCenter(std::shared_ptr<MediaSyncManager> syncCenter);
38    void SetEventReceiver(const std::shared_ptr<EventReceiver> &receiver);
39    void SetFirstPts(int64_t pts);
40    void SetSeekFlag();
41    void SetLastPts(int64_t lastPts);
42    Status SetParameter(const std::shared_ptr<Meta>& meta);
43    void UpdateTimeAnchorActually(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
44private:
45    float GetSpeed(float speed);
46    void UpdateTimeAnchorIfNeeded(int64_t nowCt, int64_t waitTime,
47        const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
48    std::atomic<bool> needUpdateTimeAnchor_ {true};
49    int64_t refreshTime_ {0};
50    bool isFirstFrame_ {true};
51    uint32_t frameRate_ {0};
52    int64_t firstFramePts_ {0};
53    int64_t firstFrameNowct_ {0};
54    int64_t lastTimeStamp_ {HST_TIME_NONE};
55    int64_t lastBufferTime_ {HST_TIME_NONE};
56    int64_t deltaTimeAccu_ {0};
57    VideoScaleType videoScaleType_ {VideoScaleType::VIDEO_SCALE_TYPE_FIT};
58
59    void CalcFrameRate();
60    std::shared_ptr<OHOS::Media::Task> frameRateTask_ {nullptr};
61    std::atomic<uint64_t> renderFrameCnt_ {0};
62    std::atomic<uint64_t> discardFrameCnt_ {0};
63    std::shared_ptr<EventReceiver> eventReceiver_ {nullptr};
64    int64_t firstPts_ {HST_TIME_NONE};
65    int64_t fixDelay_ {0};
66    bool seekFlag_{false};
67    std::atomic<bool> lastFrameDropped_ {false};
68    int64_t lastPts_ = -1;
69    int64_t lastClockTime_ = -1;
70    std::atomic<bool> isRenderStarted_{false};
71};
72} // namespace Pipeline
73} // namespace Media
74} // namespace OHOS
75
76#endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H