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     void SetInterruptState(bool isNeed);
43 private:
44     Status SetBufferFilledListener();
45     Status RemoveBufferFilledListener();
46     Status GetAllTrackInfo(uint32_t &videoTrackId, std::vector<uint32_t> &audioTrackIds);
47 
48     std::shared_ptr<Pipeline::DemuxerFilter> demuxer_;
49     Mutex targetArrivedLock_;
50     ConditionVariable targetArrivedCond_;
51     bool isAudioTargetArrived_{true};
52     bool isVideoTargetArrived_{true};
53 
54     int64_t seekTargetPts_{-1};
55     int64_t mediaStartPts_{0};
56     bool isInterrputNeeded_{false};
57     std::atomic<bool> isSeeking_{false};
58     std::map<uint32_t, sptr<AVBufferQueueProducer>> producerMap_;
59     std::map<uint32_t, sptr<IBrokerListener>> listenerMap_;
60 
61     static constexpr uint32_t WAIT_MAX_MS = 10000;
62     static constexpr uint32_t MS_TO_US = 1000;
63 };
64 
65 class AudioBufferFilledListener : public IRemoteStub<IBrokerListener> {
66 public:
67     AudioBufferFilledListener(std::shared_ptr<SeekAgent> seekAgent,
68         sptr<AVBufferQueueProducer> producer, int32_t trackId);
69     ~AudioBufferFilledListener() = default;
70 
71     void OnBufferFilled(std::shared_ptr<AVBuffer>& buffer);
72 
73 private:
74     std::weak_ptr<SeekAgent> seekAgent_;
75     sptr<AVBufferQueueProducer> producer_;
76     int32_t trackId_;
77 };
78 
79 class VideoBufferFilledListener : public IRemoteStub<IBrokerListener> {
80 public:
81     VideoBufferFilledListener(std::shared_ptr<SeekAgent> seekAgent,
82         sptr<AVBufferQueueProducer> producer, int32_t trackId);
83     ~VideoBufferFilledListener() = default;
84 
85     void OnBufferFilled(std::shared_ptr<AVBuffer>& buffer);
86 
87 private:
88     std::weak_ptr<SeekAgent> seekAgent_;
89     sptr<AVBufferQueueProducer> producer_;
90     int32_t trackId_;
91 };
92 } // namespace Media
93 } // namespace OHOS
94 #endif // SEEK_AGENT_H
95