1 /*
2  * Copyright (c) 2020-2021 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 PLAYERCONTROL_H
17 #define PLAYERCONTROL_H
18 
19 #include <string>
20 #include "hi_liteplayer.h"
21 #include "liteplayer_state_machine.h"
22 #include "player_define.h"
23 #include "player_source.h"
24 #include "player_sink_manager.h"
25 #include "hi_demuxer.h"
26 #include "decoder.h"
27 #include "surface.h"
28 
29 namespace OHOS {
30 namespace Media {
31 class PlayerSource;
32 class SinkManager;
33 class Decoder;
34 
35 struct PalayControlEventItem{
36     EventCbType event;
37 };
38 
39 class PlayerControl : public PlayerControlHandle {
40 public:
41     friend class PlayerControlState;
42     friend class PlayerControlSMObserver;
43 
44     PlayerControl();
45     ~PlayerControl() override;
46 
47     int32_t Init(const PlayerControlParam &createParam);  // playercontrol create
48 
49     int32_t Deinit(void);  // playercontrol destroy
50 
51     PlayerStatus GetState(void);
52 
53     int32_t RegCallback(PlayerCtrlCallbackParam &eventCallback);
54 
55     int32_t SetDataSource(const std::string filepath);
56     int32_t SetDataSource(const int32_t fd);
57     int32_t SetDataSource(BufferStream &stream);
58 
59     int32_t Prepare(void);
60 
61     int32_t Play(void);
62 
63     int32_t SetVolume(VolumeAttr &volumeAttr);
64 
65     int32_t Stop(void);
66 
67     int32_t Pause(void);
68 
69     int32_t Seek(int64_t timeInMs);
70 
71     int32_t GetFileInfo(FormatFileInfo &formatInfo);
72 
73     int32_t SetMedia(PlayerControlStreamAttr &mediaAttr);
74 
75     int32_t SetSurface(Surface *surface);
76 
77     int32_t TPlay(TplayAttr tplayAttr);
78 
79     int32_t Invoke(PlayerInvoke invokeId, void *param);
80 
81     int32_t OnPlayControlEvent(void *priv, const EventCbType event);
82 
83     int32_t OnSwitchTPlay2Play(void);
84 
85     void OnVideoEndOfStream(void);
86     void StateChangeCallback(PlayerStatus state) override;
87     int32_t SetAudioStreamType(int32_t type);
88 
89 protected:
90     int32_t DoRegCallback(PlayerCtrlCallbackParam &observer) override;
91 
92     int32_t DoSetDataSource(const std::string filepath) override;
93     int32_t DoSetDataSource(const int32_t fd) override;
94     int32_t DoSetDataSource(BufferStream &stream)  override;
95     int32_t DoPrepare(void) override;
96 
97     int32_t DoPlay(void) override;
98 
99     int32_t DoSetVolume(VolumeAttr &volumeAttr) override;
100 
101     int32_t ReadPacketAndPushToDecoder(void) override;
102 
103     int32_t DoStop(void) override;
104 
105     int32_t DoSeek(int64_t timeInMs) override;
106 
107     int32_t DoPause(void) override;
108 
109     int32_t DoGetFileInfo(FormatFileInfo &fileInfo) override;
110 
111     int32_t DoSetMedia(PlayerControlStreamAttr &mediaAttr) override;
112 
113     int32_t DoTPlay(TplayAttr& trickPlayAttr) override;
114 
115     int32_t DoInvoke(InvokeParameter& invokeParam) override;
116     int32_t DoSetAudioStreamType(int32_t type) override;
117     void ClearCachePacket(void);
118 
119     void NotifyError(PlayerControlError playerError) override;
120 
121     void EventCallback(PlayerControlEvent event, const void* data);
122 
123 private:
124     int32_t InitAttr(const PlayerControlParam &createParam);
125     bool IsPlayEos(void);
126     int32_t CheckMediaType(FormatFileInfo &fmtFileInfo);
127     int32_t SyncPrepare(void);
128     int32_t AyncSeek(int64_t seekTime);
129     int32_t TPlayAfterFrameRead(FormatFrame& packet);
130     int32_t TPlayBeforeFrameRead(void);
131     int32_t TPlayResetBuffer(void);
132     int32_t TPlayGetSeekOffset(float speed, TplayDirect direction);
133     int32_t IsRepeatTplayReq(TplayAttr &tplayAttr, bool &isRepeat);
134     PlayerTplayMode TPlayGetPlayMode(void);
135     int32_t TPlayCheckContinueLost(void);
136     bool TPlayIsFileReadEnd(void);
137     int32_t ReadPacket(void);
138     int32_t ReadTplayData(void);
139     int32_t SeekInTplayMode(int64_t seekTimeInMs, FormatSeekMode seekFlag);
140     void UpdateProgressNotify(void);
141     void DealPlayEnd(void);
142     int32_t ReadFrameFromSource(FormatFrame &fmtFrame);
143     int32_t GetVideoResolution(int32_t streamIdx, StreamResolution &resolution);
144     void DestroyDecoder(void);
145     void StopSinkAndDecoder(void);
146     void GetPlayElementEventCallBack(PlayEventCallback &callback);
147     int32_t PauseResume(void);
148     int32_t TPlayResume(void);
149     int32_t DecoderAndSinkReset(void);
150     int32_t GetStreamInfo(PlayerStreamInfo &streamInfo);
151     int32_t SinkStart(void);
152     int32_t DecoderStart(void);
153     int32_t VideoDecoderStart(void);
154     int32_t AudioDecoderStart(void);
155     int32_t SetDecoderAndStreamAttr(void);
156     int32_t CheckMediaInfo(void);
157     int32_t AddAudioSink(void);
158     int32_t AddVideoSink();
159     int32_t SetVDecAttr(void);
160     int32_t SetADecAttr(void);
161     int32_t SetStreamAttr();
162     void ReleaseADecoderOutputFrame(void);
163     void ReleaseVDecoderOutputFrame(void);
164     void RenderAudioFrame(void);
165     void RenderVideoFrame(void);
166     static void *DataSchProcess(void *priv);
167     void ReortRenderPosition(void);
168     void PushPacketToADecoder(void);
169     void PushPacketToVDecoder(void);
170     int32_t DoSeekIfNeed(void);
171     void FlushDecoder(void);
172     int32_t EnablePauseAfterPlay(bool pauseAfterPlay);
173     void EventProcess(EventCbType event);
174     void EventQueueProcess(void);
175 
176 private:
177     PlayerControlStateMachine *stateMachine_;
178     PlayerControlSMObserver *observer_;
179     bool isInited_;
180     FormatFileInfo fmtFileInfo_;
181     PlayerCtrlCallbackParam eventCallback_; /* the regeisted call back function */
182     bool isNeedPause_; /* need to pause after first frame render */
183     bool pauseMode_;
184     bool isPlayEnd_;
185     bool isVidPlayEos_;
186     bool isAudPlayEos_;
187     PlayerControlParam playerParam_;
188     FormatFrame formatPacket_;
189     bool cachedPacket_;
190     int64_t lastRendPos_;
191     uint64_t lastNotifyTime_;
192     /* TPlay */
193     PlayerTplayMode tplayMode_;
194     TplayAttr tplayAttr_;
195     uint32_t lastReadPktStrmIdx_;
196     int64_t lastReadPktPts_;
197     int64_t lastSendPktPts_;
198     int32_t curSeekOffset_;
199     bool isVideoStarted_;
200     bool isAudioStarted_;
201     bool isVidContinueLost_;
202     bool strmReadEnd_;
203     bool isPlayErr_;
204     bool isTplayLastFrame_;
205     bool isTplayStartRead_;
206     int64_t lastSendVdecPts_;
207     int64_t lastSendAdecPts_;
208     int64_t currentPosition_;
209     PlayControlStreamAttr streamAttr_;
210     PlayControlVdecAttr vdecAttr_;
211     PlayControlAdecAttr adecAttr_;
212     bool paused_;
213     bool schThreadExit_;
214     bool loop_;
215     bool hasRenderAudioEos_;
216     bool hasRenderVideoEos_;
217     uint32_t renderSleepTime_;
218     float leftVolume_;
219     float rightVolume_;
220     pthread_mutex_t schMutex_;
221     pthread_cond_t schCond_;
222     pthread_t schProcess_;
223     int64_t seekToTimeMs_;
224     bool firstAudioFrameAfterSeek_;
225     bool firstVideoFrameAfterSeek_;
226     SourceType sourceType_;
227     int32_t fd_;
228     std::string filePath_;
229     BufferStream stream_;
230     std::shared_ptr<PlayerSource> playerSource_;
231     std::shared_ptr<SinkManager> sinkManager_;
232     std::shared_ptr<Decoder> audioDecoder_;
233     std::shared_ptr<Decoder> videoDecoder_;
234     Surface *surface_;
235     std::vector<PalayControlEventItem> eventQueue;
236     int32_t audioStreamType_;
237     int64_t seekTabel_[0x2] = {-1, -1};
238 private:
239     PlayerControl(const PlayerControl &);
240     PlayerControl &operator=(const PlayerControl &);
241 };
242 }
243 }
244 #endif  // LITEPLAYER_H__
245