18c77b71bSopenharmony_ci/*
28c77b71bSopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
38c77b71bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48c77b71bSopenharmony_ci * you may not use this file except in compliance with the License.
58c77b71bSopenharmony_ci * You may obtain a copy of the License at
68c77b71bSopenharmony_ci *
78c77b71bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88c77b71bSopenharmony_ci *
98c77b71bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108c77b71bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118c77b71bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128c77b71bSopenharmony_ci * See the License for the specific language governing permissions and
138c77b71bSopenharmony_ci * limitations under the License.
148c77b71bSopenharmony_ci */
158c77b71bSopenharmony_ci
168c77b71bSopenharmony_ci#ifndef PLAYER_IMPL_H
178c77b71bSopenharmony_ci#define PLAYER_IMPL_H
188c77b71bSopenharmony_ci
198c77b71bSopenharmony_ci#include <memory>
208c77b71bSopenharmony_ci#include <vector>
218c77b71bSopenharmony_ci#include <mutex>
228c77b71bSopenharmony_ci#include <pthread.h>
238c77b71bSopenharmony_ci#include "buffer_source.h"
248c77b71bSopenharmony_ci#include "hi_demuxer.h"
258c77b71bSopenharmony_ci#include "liteplayer.h"
268c77b71bSopenharmony_ci#include "player_define.h"
278c77b71bSopenharmony_ci#include "source.h"
288c77b71bSopenharmony_ci#include "surface.h"
298c77b71bSopenharmony_ci#include "player.h"
308c77b71bSopenharmony_ci#include "player_interface.h"
318c77b71bSopenharmony_ci
328c77b71bSopenharmony_ciusing namespace std;
338c77b71bSopenharmony_ciusing OHOS::Media::PlayerControl;
348c77b71bSopenharmony_cinamespace OHOS {
358c77b71bSopenharmony_cinamespace Media {
368c77b71bSopenharmony_citypedef enum {
378c77b71bSopenharmony_ci    PAYERADP_EVT_PREPARED = 0,
388c77b71bSopenharmony_ci    PAYERADP_EVT_PLAY_COMPLETE = 1,
398c77b71bSopenharmony_ci    PAYERADP_EVT_REWIND_COMPLETE = 2,
408c77b71bSopenharmony_ci    PAYERADP_EVT_VIDEO_SOLUTION = 3,
418c77b71bSopenharmony_ci    PAYERADP_EVT_VIDEO_PLAYED = 4,
428c77b71bSopenharmony_ci    PAYERADP_EVT_PLAY_TIME = 5,
438c77b71bSopenharmony_ci    PAYERADP_EVT_ERROR           = 6,
448c77b71bSopenharmony_ci    PAYERADP_EVT_INFO            = 7,
458c77b71bSopenharmony_ci} PlayAdapterEvt;
468c77b71bSopenharmony_ci
478c77b71bSopenharmony_ciclass AdapterStreamCallback : public StreamCallback {
488c77b71bSopenharmony_cipublic:
498c77b71bSopenharmony_ci    AdapterStreamCallback(std::shared_ptr<StreamSource> &stream, std::shared_ptr<BufferSource> &buffer);
508c77b71bSopenharmony_ci    virtual ~AdapterStreamCallback();
518c77b71bSopenharmony_ci    uint8_t* GetBuffer(size_t index);
528c77b71bSopenharmony_ci    void QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags);
538c77b71bSopenharmony_ci    void SetParameters(const Format &params);
548c77b71bSopenharmony_ci    int Init(void);
558c77b71bSopenharmony_ci    void DeInit(void);
568c77b71bSopenharmony_ciprivate:
578c77b71bSopenharmony_ci    static void* IdleBufferProcess(void* arg);
588c77b71bSopenharmony_ci
598c77b71bSopenharmony_ci    std::weak_ptr<StreamSource> streamSource_;
608c77b71bSopenharmony_ci    std::shared_ptr<BufferSource> bufferSource_;
618c77b71bSopenharmony_ci    pthread_t streamProcess_;
628c77b71bSopenharmony_ci    pthread_mutex_t mutex_;
638c77b71bSopenharmony_ci    bool isRunning_;
648c77b71bSopenharmony_ci};
658c77b71bSopenharmony_ci
668c77b71bSopenharmony_ciclass PlayerControl;
678c77b71bSopenharmony_ciclass PlayerImpl : public PlayerInterface {
688c77b71bSopenharmony_cifriend class BufferSource;
698c77b71bSopenharmony_cifriend class StreamSource;
708c77b71bSopenharmony_ci
718c77b71bSopenharmony_cipublic:
728c77b71bSopenharmony_ci    PlayerImpl();
738c77b71bSopenharmony_ci    ~PlayerImpl();
748c77b71bSopenharmony_ci    int32_t SetSource(const Source &source) override;
758c77b71bSopenharmony_ci    int32_t Prepare() override;
768c77b71bSopenharmony_ci    int32_t Play() override;
778c77b71bSopenharmony_ci    bool IsPlaying() override;
788c77b71bSopenharmony_ci    int32_t Pause() override;
798c77b71bSopenharmony_ci    int32_t Stop() override;
808c77b71bSopenharmony_ci    int32_t Rewind(int64_t mSeconds, int32_t mode) override;
818c77b71bSopenharmony_ci    int32_t SetVolume(float leftVolume, float rightVolume) override;
828c77b71bSopenharmony_ci    int32_t SetSurface(Surface *surface) override;
838c77b71bSopenharmony_ci    bool IsSingleLooping() override;
848c77b71bSopenharmony_ci    int32_t GetPlayerState(int32_t &state) override;
858c77b71bSopenharmony_ci    int32_t GetCurrentPosition(int64_t &position) override;
868c77b71bSopenharmony_ci    int32_t GetDuration(int64_t &durationMs) override;
878c77b71bSopenharmony_ci    int32_t GetVideoWidth(int32_t &videoWidth) override;
888c77b71bSopenharmony_ci    int32_t GetVideoHeight(int32_t &videoHeight) override;
898c77b71bSopenharmony_ci    int32_t SetPlaybackSpeed(float speed) override;
908c77b71bSopenharmony_ci    int32_t GetPlaybackSpeed(float &speed) override;
918c77b71bSopenharmony_ci    int32_t SetAudioStreamType(int32_t type) override;
928c77b71bSopenharmony_ci    void GetAudioStreamType(int32_t &type) override;
938c77b71bSopenharmony_ci    int32_t Reset(void) override;
948c77b71bSopenharmony_ci    int32_t Release() override;
958c77b71bSopenharmony_ci    int32_t SetLoop(bool loop) override;
968c77b71bSopenharmony_ci    void SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb) override;
978c77b71bSopenharmony_ci    int32_t SetVideoScaleType(int32_t type);
988c77b71bSopenharmony_ci    int32_t Init(void) override;
998c77b71bSopenharmony_ci    int32_t DeInit(void) override;
1008c77b71bSopenharmony_ci    int32_t SetParameter(const Format &params) override;
1018c77b71bSopenharmony_ciprivate:
1028c77b71bSopenharmony_ci    void NotifySeekComplete(PlayerImpl *curPlayer, int64_t seekToMs);
1038c77b71bSopenharmony_ci    void NotifyPlaybackComplete(PlayerImpl *curPlayer);
1048c77b71bSopenharmony_ci    int32_t SetUriSource(const Source &source);
1058c77b71bSopenharmony_ci    int SetStreamSource(const Source &source);
1068c77b71bSopenharmony_ci    int32_t RewindInner(int64_t mSeconds, PlayerSeekMode mode);
1078c77b71bSopenharmony_ci    bool IsValidRewindMode(PlayerSeekMode mode);
1088c77b71bSopenharmony_ci    void UpdateState(PlayerImpl *curPlayer, PlayerStatus state);
1098c77b71bSopenharmony_ci    static void PlayerControlEventCb(void* pPlayer, PlayerControlEvent enEvent, const void* pData);
1108c77b71bSopenharmony_ci    int GetPlayer();
1118c77b71bSopenharmony_ci    static int32_t GetReadableSize(const void *handle);
1128c77b71bSopenharmony_ci    int32_t ReadDataPro(uint8_t *data, int32_t size, DataFlags &flags);
1138c77b71bSopenharmony_ci    static int32_t ReadData(void *handle, uint8_t *data, int32_t size, int32_t timeOutMs, DataFlags *flags);
1148c77b71bSopenharmony_ci    int CreatePlayerParamCheck(PlayerControlParam &createParam);
1158c77b71bSopenharmony_ci    void GetDurationInner(int64_t &durationMs);
1168c77b71bSopenharmony_ci    void ResetInner(void);
1178c77b71bSopenharmony_ci    int32_t EnablePauseAfterPlay(bool isPauseAfterPlay);
1188c77b71bSopenharmony_ci    int32_t SetMediaStream(void);
1198c77b71bSopenharmony_ci    void ReportVideoSizeChange(void);
1208c77b71bSopenharmony_ci
1218c77b71bSopenharmony_ci    std::shared_ptr<PlayerControl> player_;
1228c77b71bSopenharmony_ci    float speed_;
1238c77b71bSopenharmony_ci    FormatFileInfo formatFileInfo_;
1248c77b71bSopenharmony_ci    PlayerControlStreamAttr mediaAttr_;
1258c77b71bSopenharmony_ci    PlayerStatus playerControlState_;
1268c77b71bSopenharmony_ci
1278c77b71bSopenharmony_ci    bool isSingleLoop_;
1288c77b71bSopenharmony_ci    int64_t currentPosition_;
1298c77b71bSopenharmony_ci    int64_t rewindPosition_;
1308c77b71bSopenharmony_ci    Surface* surface_;
1318c77b71bSopenharmony_ci    uint32_t currentState_;
1328c77b71bSopenharmony_ci    PlayerSeekMode rewindMode_;
1338c77b71bSopenharmony_ci    PlayerSeekMode currentRewindMode_;
1348c77b71bSopenharmony_ci    int audioStreamType_;
1358c77b71bSopenharmony_ci    std::mutex lock_;
1368c77b71bSopenharmony_ci    std::mutex rewindLock_;
1378c77b71bSopenharmony_ci    std::shared_ptr<PlayerCallback> callback_;
1388c77b71bSopenharmony_ci    bool inited_;
1398c77b71bSopenharmony_ci    bool released_;
1408c77b71bSopenharmony_ci    bool isStreamSource_;
1418c77b71bSopenharmony_ci    QueBuffer buffer_;
1428c77b71bSopenharmony_ci    std::shared_ptr<BufferSource> bufferSource_;
1438c77b71bSopenharmony_ci    std::shared_ptr<AdapterStreamCallback> streamCallback_;
1448c77b71bSopenharmony_ci    bool pauseAfterPlay_;
1458c77b71bSopenharmony_ci    bool extraRewind_;
1468c77b71bSopenharmony_ci};
1478c77b71bSopenharmony_ci}  // namespace Media
1488c77b71bSopenharmony_ci}  // namespace OHOS
1498c77b71bSopenharmony_ci
1508c77b71bSopenharmony_ci#endif  // PLAYER_IMPL_H
151