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 PLAYER_IMPL_H 17 #define PLAYER_IMPL_H 18 19 #include <memory> 20 #include <vector> 21 #include <mutex> 22 #include <pthread.h> 23 #include "buffer_source.h" 24 #include "hi_demuxer.h" 25 #include "liteplayer.h" 26 #include "player_define.h" 27 #include "source.h" 28 #include "surface.h" 29 #include "player.h" 30 #include "player_interface.h" 31 32 using namespace std; 33 using OHOS::Media::PlayerControl; 34 namespace OHOS { 35 namespace Media { 36 typedef enum { 37 PAYERADP_EVT_PREPARED = 0, 38 PAYERADP_EVT_PLAY_COMPLETE = 1, 39 PAYERADP_EVT_REWIND_COMPLETE = 2, 40 PAYERADP_EVT_VIDEO_SOLUTION = 3, 41 PAYERADP_EVT_VIDEO_PLAYED = 4, 42 PAYERADP_EVT_PLAY_TIME = 5, 43 PAYERADP_EVT_ERROR = 6, 44 PAYERADP_EVT_INFO = 7, 45 } PlayAdapterEvt; 46 47 class AdapterStreamCallback : public StreamCallback { 48 public: 49 AdapterStreamCallback(std::shared_ptr<StreamSource> &stream, std::shared_ptr<BufferSource> &buffer); 50 virtual ~AdapterStreamCallback(); 51 uint8_t* GetBuffer(size_t index); 52 void QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags); 53 void SetParameters(const Format ¶ms); 54 int Init(void); 55 void DeInit(void); 56 private: 57 static void* IdleBufferProcess(void* arg); 58 59 std::weak_ptr<StreamSource> streamSource_; 60 std::shared_ptr<BufferSource> bufferSource_; 61 pthread_t streamProcess_; 62 pthread_mutex_t mutex_; 63 bool isRunning_; 64 }; 65 66 class PlayerControl; 67 class PlayerImpl : public PlayerInterface { 68 friend class BufferSource; 69 friend class StreamSource; 70 71 public: 72 PlayerImpl(); 73 ~PlayerImpl(); 74 int32_t SetSource(const Source &source) override; 75 int32_t Prepare() override; 76 int32_t Play() override; 77 bool IsPlaying() override; 78 int32_t Pause() override; 79 int32_t Stop() override; 80 int32_t Rewind(int64_t mSeconds, int32_t mode) override; 81 int32_t SetVolume(float leftVolume, float rightVolume) override; 82 int32_t SetSurface(Surface *surface) override; 83 bool IsSingleLooping() override; 84 int32_t GetPlayerState(int32_t &state) override; 85 int32_t GetCurrentPosition(int64_t &position) override; 86 int32_t GetDuration(int64_t &durationMs) override; 87 int32_t GetVideoWidth(int32_t &videoWidth) override; 88 int32_t GetVideoHeight(int32_t &videoHeight) override; 89 int32_t SetPlaybackSpeed(float speed) override; 90 int32_t GetPlaybackSpeed(float &speed) override; 91 int32_t SetAudioStreamType(int32_t type) override; 92 void GetAudioStreamType(int32_t &type) override; 93 int32_t Reset(void) override; 94 int32_t Release() override; 95 int32_t SetLoop(bool loop) override; 96 void SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb) override; 97 int32_t SetVideoScaleType(int32_t type); 98 int32_t Init(void) override; 99 int32_t DeInit(void) override; 100 int32_t SetParameter(const Format ¶ms) override; 101 private: 102 void NotifySeekComplete(PlayerImpl *curPlayer, int64_t seekToMs); 103 void NotifyPlaybackComplete(PlayerImpl *curPlayer); 104 int32_t SetUriSource(const Source &source); 105 int SetStreamSource(const Source &source); 106 int32_t RewindInner(int64_t mSeconds, PlayerSeekMode mode); 107 bool IsValidRewindMode(PlayerSeekMode mode); 108 void UpdateState(PlayerImpl *curPlayer, PlayerStatus state); 109 static void PlayerControlEventCb(void* pPlayer, PlayerControlEvent enEvent, const void* pData); 110 int GetPlayer(); 111 static int32_t GetReadableSize(const void *handle); 112 int32_t ReadDataPro(uint8_t *data, int32_t size, DataFlags &flags); 113 static int32_t ReadData(void *handle, uint8_t *data, int32_t size, int32_t timeOutMs, DataFlags *flags); 114 int CreatePlayerParamCheck(PlayerControlParam &createParam); 115 void GetDurationInner(int64_t &durationMs); 116 void ResetInner(void); 117 int32_t EnablePauseAfterPlay(bool isPauseAfterPlay); 118 int32_t SetMediaStream(void); 119 void ReportVideoSizeChange(void); 120 121 std::shared_ptr<PlayerControl> player_; 122 float speed_; 123 FormatFileInfo formatFileInfo_; 124 PlayerControlStreamAttr mediaAttr_; 125 PlayerStatus playerControlState_; 126 127 bool isSingleLoop_; 128 int64_t currentPosition_; 129 int64_t rewindPosition_; 130 Surface* surface_; 131 uint32_t currentState_; 132 PlayerSeekMode rewindMode_; 133 PlayerSeekMode currentRewindMode_; 134 int audioStreamType_; 135 std::mutex lock_; 136 std::mutex rewindLock_; 137 std::shared_ptr<PlayerCallback> callback_; 138 bool inited_; 139 bool released_; 140 bool isStreamSource_; 141 QueBuffer buffer_; 142 std::shared_ptr<BufferSource> bufferSource_; 143 std::shared_ptr<AdapterStreamCallback> streamCallback_; 144 bool pauseAfterPlay_; 145 bool extraRewind_; 146 }; 147 } // namespace Media 148 } // namespace OHOS 149 150 #endif // PLAYER_IMPL_H 151