• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2025 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 HISTREAMER_SUBTITLE_SINK_H
17 #define HISTREAMER_SUBTITLE_SINK_H
18 #include <mutex>
19 #include <deque>
20 #include "common/status.h"
21 #include "meta/meta.h"
22 #include "sink/media_synchronous_sink.h"
23 #include "media_sync_manager.h"
24 #include "buffer/avbuffer_queue.h"
25 #include "buffer/avbuffer_queue_define.h"
26 #include "filter/filter.h"
27 
28 namespace OHOS {
29 namespace Media {
30 using namespace OHOS::Media::Plugins;
31 class SubtitleSink : public std::enable_shared_from_this<SubtitleSink>, public Pipeline::MediaSynchronousSink {
32 public:
33     SubtitleSink();
34     ~SubtitleSink();
35     Status Init(std::shared_ptr<Meta>& meta, const std::shared_ptr<Pipeline::EventReceiver>& receiver);
36     sptr<AVBufferQueueProducer> GetBufferQueueProducer();
37     sptr<AVBufferQueueConsumer> GetBufferQueueConsumer();
38     Status SetParameter(const std::shared_ptr<Meta>& meta);
39     Status GetParameter(std::shared_ptr<Meta>& meta);
40     Status Prepare();
41     Status Start();
42     Status Stop();
43     Status Pause();
44     Status Resume();
45     Status Flush();
46     Status Release();
47     void DrainOutputBuffer(bool flushed);
48     void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver>& receiver);
49     Status GetLatency(uint64_t& nanoSec);
50     void SetSyncCenter(std::shared_ptr<Pipeline::MediaSyncManager> syncCenter);
51     int64_t DoSyncWrite(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer) override;
52     void ResetSyncInfo() override;
53     Status SetIsTransitent(bool isTransitent);
54     void NotifySeek();
55     virtual void OnInterrupted(bool isInterruptNeeded) override;
56     Status SetSpeed(float speed);
57 protected:
58     std::atomic<OHOS::Media::Pipeline::FilterState> state_;
59 private:
60     struct SubtitleInfo {
61         std::string text_;
62         int64_t pts_;
63         int64_t duration_;
64     };
65     void NotifyRender(SubtitleInfo &subtitleInfo);
66     void RenderLoop();
67     uint64_t CalcWaitTime(SubtitleInfo &subtitleInfo);
68     uint32_t ActionToDo(SubtitleInfo &subtitleInfo);
69     void GetTargetSubtitleIndex(int64_t currentTime);
70     Status PrepareInputBufferQueue();
71     int64_t getDurationUsPlayedAtSampleRate(uint32_t numFrames);
72     int64_t GetMediaTime();
73     std::shared_ptr<Pipeline::EventReceiver> playerEventReceiver_;
74     int32_t appUid_{0};
75     int32_t appPid_{0};
76     int64_t numFramesWritten_ {0};
77     int64_t lastReportedClockTime_ {HST_TIME_NONE};
78     int64_t latestBufferPts_ {HST_TIME_NONE};
79     int64_t latestBufferDuration_ {0};
80     const std::string INPUT_BUFFER_QUEUE_NAME = "SubtitleSinkInputBufferQueue";
81     std::shared_ptr<AVBufferQueue> inputBufferQueue_;
82     sptr<AVBufferQueueProducer> inputBufferQueueProducer_;
83     sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_;
84     bool isTransitent_ {false};
85     std::atomic<bool> isEos_{false};
86     std::unique_ptr<std::thread> readThread_ = nullptr;
87     std::mutex mutex_;
88     std::condition_variable updateCond_;
89     std::shared_ptr<AVBuffer> filledOutputBuffer_;
90     std::atomic<bool> isPaused_{false};
91     std::atomic<bool> isThreadExit_{false};
92     std::atomic<bool> shouldUpdate_{false};
93     float speed_ = 1.0;
94     enum SubtitleBufferState : uint32_t {
95         WAIT,
96         SHOW,
97         DROP,
98     };
99     std::deque<SubtitleInfo> subtitleInfoVec_;
100     uint32_t currentInfoIndex_ = 0;
101     std::atomic<bool> isFlush_ = false;
102     std::vector<std::shared_ptr<AVBuffer>> inputBufferVector_;
103 };
104 }
105 }
106 
107 #endif // HISTREAMER_AUDIO_SINK_H
108