• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 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     Status SetSpeed(float speed);
56 protected:
57     std::atomic<OHOS::Media::Pipeline::FilterState> state_;
58 private:
59     struct SubtitleInfo {
60         std::string text_;
61         int64_t pts_;
62         int64_t duration_;
63     };
64     void NotifyRender(SubtitleInfo &subtitleInfo);
65     void RenderLoop();
66     uint64_t CalcWaitTime(SubtitleInfo &subtitleInfo);
67     uint32_t ActionToDo(SubtitleInfo &subtitleInfo);
68     void GetTargetSubtitleIndex(int64_t currentTime);
69     Status PrepareInputBufferQueue();
70     int64_t getDurationUsPlayedAtSampleRate(uint32_t numFrames);
71     int64_t GetMediaTime();
72     std::shared_ptr<Pipeline::EventReceiver> playerEventReceiver_;
73     int32_t appUid_{0};
74     int32_t appPid_{0};
75     int64_t numFramesWritten_ {0};
76     int64_t lastReportedClockTime_ {HST_TIME_NONE};
77     int64_t latestBufferPts_ {HST_TIME_NONE};
78     int64_t latestBufferDuration_ {0};
79     const std::string INPUT_BUFFER_QUEUE_NAME = "SubtitleSinkInputBufferQueue";
80     std::shared_ptr<AVBufferQueue> inputBufferQueue_;
81     sptr<AVBufferQueueProducer> inputBufferQueueProducer_;
82     sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_;
83     bool isTransitent_ {false};
84     std::atomic<bool> isEos_{false};
85     std::unique_ptr<std::thread> readThread_ = nullptr;
86     std::mutex mutex_;
87     std::condition_variable updateCond_;
88     std::shared_ptr<AVBuffer> filledOutputBuffer_;
89     std::atomic<bool> isPaused_{false};
90     std::atomic<bool> isThreadExit_{false};
91     std::atomic<bool> shouldUpdate_{false};
92     float speed_ = 1.0;
93     enum SubtitleBufferState : uint32_t {
94         WAIT,
95         SHOW,
96         DROP,
97     };
98     std::deque<SubtitleInfo> subtitleInfoVec_;
99     uint32_t currentInfoIndex_ = 0;
100     std::atomic<bool> isFlush_ = false;
101     std::vector<std::shared_ptr<AVBuffer>> inputBufferVector_;
102 };
103 }
104 }
105 
106 #endif // HISTREAMER_AUDIO_SINK_H
107