1  /*
2  * Copyright (c) 2024-2024 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 SEEK_AGENT_H
17 #define SEEK_AGENT_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include "common/status.h"
22 #include "avbuffer_queue_define.h"
23 #include "osal/task/condition_variable.h"
24 #include "osal/task/mutex.h"
25 #include "iremote_stub.h"
26 #include "demuxer_filter.h"
27 
28 namespace OHOS {
29 namespace Media {
30 class HiPlayerImpl;
31 
32 class SeekAgent : public std::enable_shared_from_this<SeekAgent> {
33 public:
34     explicit SeekAgent(std::shared_ptr<Pipeline::DemuxerFilter> demuxer, int64_t startPts = 0);
35     ~SeekAgent();
36 
37     Status Seek(int64_t seekPos, bool &timeout);
38     Status OnAudioBufferFilled(std::shared_ptr<AVBuffer>& buffer,
39         sptr<AVBufferQueueProducer> producer, int32_t trackId);
40     Status OnVideoBufferFilled(std::shared_ptr<AVBuffer>& buffer,
41         sptr<AVBufferQueueProducer> producer, int32_t trackId);
42     Status AlignAudioPosition(int64_t audioPosition);
43     void SetInterruptState(bool isNeed);
44 private:
45     Status SetBufferFilledListener();
46     Status RemoveBufferFilledListener();
47     Status GetAllTrackInfo(uint32_t &videoTrackId, std::vector<uint32_t> &audioTrackIds);
48     bool GetAudioTrackId(uint32_t &audioTrackId);
49 
50     std::shared_ptr<Pipeline::DemuxerFilter> demuxer_;
51     Mutex targetArrivedLock_;
52     ConditionVariable targetArrivedCond_;
53     bool isAudioTargetArrived_{true};
54     bool isVideoTargetArrived_{true};
55 
56     int64_t seekTargetPts_{-1};
57     int64_t mediaStartPts_{0};
58     bool isInterrputNeeded_{false};
59     std::atomic<bool> isSeeking_{false};
60     std::map<uint32_t, sptr<AVBufferQueueProducer>> producerMap_;
61     std::map<uint32_t, sptr<IBrokerListener>> listenerMap_;
62 
63     static constexpr uint32_t WAIT_MAX_MS = 4000;
64     static constexpr uint32_t MS_TO_US = 1000;
65 };
66 
67 class AudioBufferFilledListener : public IRemoteStub<IBrokerListener> {
68 public:
69     AudioBufferFilledListener(std::shared_ptr<SeekAgent> seekAgent,
70         sptr<AVBufferQueueProducer> producer, int32_t trackId);
71     ~AudioBufferFilledListener() = default;
72 
73     void OnBufferFilled(std::shared_ptr<AVBuffer>& buffer);
74 
75 private:
76     std::weak_ptr<SeekAgent> seekAgent_;
77     sptr<AVBufferQueueProducer> producer_;
78     int32_t trackId_;
79 };
80 
81 class VideoBufferFilledListener : public IRemoteStub<IBrokerListener> {
82 public:
83     VideoBufferFilledListener(std::shared_ptr<SeekAgent> seekAgent,
84         sptr<AVBufferQueueProducer> producer, int32_t trackId);
85     ~VideoBufferFilledListener() = default;
86 
87     void OnBufferFilled(std::shared_ptr<AVBuffer>& buffer);
88 
89 private:
90     std::weak_ptr<SeekAgent> seekAgent_;
91     sptr<AVBufferQueueProducer> producer_;
92     int32_t trackId_;
93 };
94 } // namespace Media
95 } // namespace OHOS
96 #endif // SEEK_AGENT_H
97