1 /* 2 * Copyright (C) 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 "player.h" 20 #include "nocopyable.h" 21 #include "osal/task/autolock.h" 22 #include "i_player_service.h" 23 #include "hitrace/tracechain.h" 24 #include "hiappevent_agent.h" 25 #include "common/fdsan_fd.h" 26 27 namespace OHOS { 28 namespace Media { 29 using namespace OHOS::HiviewDFX; 30 class PlayerImpl : public Player, public NoCopyable, public std::enable_shared_from_this<PlayerImpl> { 31 public: 32 PlayerImpl(); 33 ~PlayerImpl(); 34 35 int32_t SetSource(const std::string &url) override; 36 int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override; 37 int32_t Prepare() override; 38 int32_t SetSource(int32_t fd, int64_t offset, int64_t size) override; 39 int32_t Pause() override; 40 int32_t Play() override; 41 int32_t Reset() override; 42 int32_t SetRenderFirstFrame(bool display) override; 43 int32_t SetPlayRange(int64_t start, int64_t end) override; 44 int32_t SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode = SEEK_PREVIOUS_SYNC) override; 45 int32_t SetPlayRangeUsWithMode(int64_t start, int64_t end, PlayerSeekMode mode = SEEK_PREVIOUS_SYNC) override; 46 int32_t PrepareAsync() override; 47 int32_t AddSubSource(const std::string &url) override; 48 int32_t AddSubSource(int32_t fd, int64_t offset, int64_t size) override; 49 int32_t Stop() override; 50 int32_t Release() override; 51 int32_t ReleaseSync() override; 52 int32_t SetVolume(float leftVolume, float rightVolume) override; 53 int32_t SetVolumeMode(int32_t mode) override; 54 int32_t GetCurrentTime(int32_t ¤tTime) override; 55 int32_t GetPlaybackPosition(int32_t &playbackPosition) override; 56 int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override; 57 int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override; 58 int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) override; 59 int32_t GetPlaybackInfo(Format &playbackInfo) override; 60 int32_t GetVideoWidth() override; 61 int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack) override; 62 int32_t GetVideoHeight() override; 63 int32_t SetPlaybackSpeed(PlaybackRateMode mode) override; 64 int32_t SetPlaybackRate(float rate) override; 65 int32_t GetDuration(int32_t &duration) override; 66 int32_t GetApiVersion(int32_t &apiVersion) override; 67 int32_t GetPlaybackSpeed(PlaybackRateMode &mode) override; 68 int32_t SetLooping(bool loop) override; 69 #ifdef SUPPORT_VIDEO 70 int32_t SetVideoSurface(sptr<Surface> surface) override; 71 #endif 72 bool IsPlaying() override; 73 int32_t SetParameter(const Format ¶m) override; 74 bool IsLooping() override; 75 int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback) override; 76 int32_t SelectBitRate(uint32_t bitRate) override; 77 int32_t SelectTrack(int32_t index, PlayerSwitchMode mode) override; 78 int32_t DeselectTrack(int32_t index) override; 79 int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override; 80 int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, 81 bool svp) override; 82 int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy) override; 83 int32_t Init(const PlayerProducer producer = PlayerProducer::INNER); 84 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody); 85 int32_t SetPlaybackStrategy(AVPlayStrategy playbackStrategy) override; 86 int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted) override; 87 int32_t SetSuperResolution(bool enabled) override; 88 int32_t SetVideoWindowSize(int32_t width, int32_t height) override; 89 int32_t SetMaxAmplitudeCbStatus(bool status) override; 90 int32_t SetDeviceChangeCbStatus(bool status) override; 91 bool IsSeekContinuousSupported() override; 92 int32_t SetSeiMessageCbStatus(bool status, const std::vector<int32_t> &payloadTypes) override; 93 int32_t EnableReportMediaProgress(bool enable) override; 94 int32_t EnableReportAudioInterrupt(bool enable) override; 95 int32_t SetStartFrameRateOptEnabled(bool enabled) override; 96 void ReleaseClientListener() override; 97 HiviewDFX::HiTraceId GetTraceId(); 98 int32_t SetReopenFd(int32_t fd) override; 99 int32_t EnableCameraPostprocessing() override; 100 int32_t SetCameraPostprocessing(bool isOpen) override; 101 void TraceApiEvent(int errCode, const std::string& message, time_t startTime); 102 int32_t ForceLoadVideo(bool status) override; 103 private: 104 void ResetSeekVariables(); 105 void HandleSeekDoneInfo(PlayerOnInfoType type, int32_t extra); 106 int32_t SetSourceTask(int32_t fd, int64_t offset, int64_t size); 107 std::recursive_mutex recMutex_; 108 int32_t mCurrentPosition = INT32_MIN; 109 PlayerSeekMode mCurrentSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC; 110 int32_t mSeekPosition = INT32_MIN; 111 PlayerSeekMode mSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC; 112 std::atomic<bool> isSeeking_{false}; 113 int32_t prevTrackIndex_ = INT32_MIN; 114 std::shared_ptr<PlayerCallback> callback_; 115 std::unique_ptr<FdsanFd> fdsanFd_ = nullptr; 116 117 std::shared_ptr<IPlayerService> playerService_ = nullptr; 118 sptr<Surface> surface_ = nullptr; 119 HiviewDFX::HiTraceId traceId_; 120 std::mutex cbMutex_; 121 std::shared_ptr<HiAppEventAgent> hiAppEventAgent_ = nullptr; 122 int64_t prepareStartTimeMs_ = -1; 123 }; 124 125 class PlayerImplCallback : public PlayerCallback { 126 public: 127 PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb, std::shared_ptr<PlayerImpl> player); 128 ~PlayerImplCallback() = default; 129 130 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody); 131 void OnError(int32_t errorCode, const std::string &errorMsg); 132 private: 133 std::shared_ptr<PlayerCallback> playerCb_; 134 std::weak_ptr<PlayerImpl> player_; 135 std::mutex playerImplCbMutex_; 136 bool getApiVersionFlag_ = true; 137 }; 138 } // namespace Media 139 } // namespace OHOS 140 #endif // PLAYER_IMPL_H 141