• 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         std::shared_ptr<AVBuffer> buffer_ {nullptr};
65 
SubtitleInfoSubtitleInfo66         SubtitleInfo(std::string text, int64_t pts, int64_t duration) : text_(text), pts_(pts), duration_(duration) {}
67 
SubtitleInfoSubtitleInfo68         SubtitleInfo(std::string text, int64_t pts, int64_t duration, std::shared_ptr<AVBuffer> buffer)
69             : text_(text), pts_(pts), duration_(duration), buffer_(buffer) {}
70 
SubtitleInfoSubtitleInfo71         SubtitleInfo(const SubtitleInfo& other)
72             : text_(other.text_), pts_(other.pts_), duration_(other.duration_), buffer_(other.buffer_) {}
73 
74         SubtitleInfo& operator=(const SubtitleInfo& other)
75         {
76             if (this != &other) {
77                 text_ = other.text_;
78                 pts_ = other.pts_;
79                 duration_ = other.duration_;
80                 buffer_ = other.buffer_;
81             }
82             return *this;
83         }
84     };
85     void NotifyRender(SubtitleInfo &subtitleInfo);
86     void RenderLoop();
87     uint64_t CalcWaitTime(SubtitleInfo &subtitleInfo);
88     uint32_t ActionToDo(SubtitleInfo &subtitleInfo);
89     void GetTargetSubtitleIndex(int64_t currentTime);
90     Status PrepareInputBufferQueue();
91     int64_t getDurationUsPlayedAtSampleRate(uint32_t numFrames);
92     int64_t GetMediaTime();
93     std::shared_ptr<Pipeline::EventReceiver> playerEventReceiver_;
94     int32_t appUid_{0};
95     int32_t appPid_{0};
96     int64_t numFramesWritten_ {0};
97     int64_t lastReportedClockTime_ {HST_TIME_NONE};
98     int64_t latestBufferPts_ {HST_TIME_NONE};
99     int64_t latestBufferDuration_ {0};
100     const std::string INPUT_BUFFER_QUEUE_NAME = "SubtitleSinkInputBufferQueue";
101     std::shared_ptr<AVBufferQueue> inputBufferQueue_;
102     sptr<AVBufferQueueProducer> inputBufferQueueProducer_;
103     sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_;
104     bool isTransitent_ {false};
105     std::atomic<bool> isEos_{false};
106     std::unique_ptr<std::thread> readThread_ = nullptr;
107     std::mutex mutex_;
108     std::condition_variable updateCond_;
109     std::shared_ptr<AVBuffer> filledOutputBuffer_;
110     std::atomic<bool> isPaused_{false};
111     std::atomic<bool> isThreadExit_{false};
112     std::atomic<bool> shouldUpdate_{false};
113     float speed_ = 1.0;
114     enum SubtitleBufferState : uint32_t {
115         WAIT,
116         SHOW,
117         DROP,
118     };
119     std::deque<SubtitleInfo> subtitleInfoVec_;
120     uint32_t currentInfoIndex_ = 0;
121     std::atomic<bool> isFlush_ = false;
122     std::vector<std::shared_ptr<AVBuffer>> inputBufferVector_;
123 };
124 }
125 }
126 
127 #endif // HISTREAMER_AUDIO_SINK_H
128