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 
25 namespace OHOS {
26 namespace Media {
27 using namespace OHOS::HiviewDFX;
28 class PlayerImpl : public Player, public NoCopyable, public std::enable_shared_from_this<PlayerImpl> {
29 public:
30     PlayerImpl();
31     ~PlayerImpl();
32 
33     int32_t SetSource(const std::string &url) override;
34     int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override;
35     int32_t Prepare() override;
36     int32_t SetSource(int32_t fd, int64_t offset, int64_t size) override;
37     int32_t Pause() override;
38     int32_t Play() override;
39     int32_t Reset() override;
40     int32_t SetRenderFirstFrame(bool display) override;
41     int32_t SetPlayRange(int64_t start, int64_t end) override;
42     int32_t SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode = SEEK_PREVIOUS_SYNC) override;
43     int32_t PrepareAsync() override;
44     int32_t AddSubSource(const std::string &url) override;
45     int32_t AddSubSource(int32_t fd, int64_t offset, int64_t size) override;
46     int32_t Stop() override;
47     int32_t Release() override;
48     int32_t ReleaseSync() override;
49     int32_t SetVolume(float leftVolume, float rightVolume) override;
50     int32_t GetCurrentTime(int32_t &currentTime) override;
51     int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) override;
52     int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) override;
53     int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) override;
54     int32_t GetPlaybackInfo(Format &playbackInfo) override;
55     int32_t GetVideoWidth() override;
56     int32_t GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack) override;
57     int32_t GetVideoHeight() override;
58     int32_t SetPlaybackSpeed(PlaybackRateMode mode) override;
59     int32_t GetDuration(int32_t &duration) override;
60     int32_t GetApiVersion(int32_t &apiVersion) override;
61     int32_t GetPlaybackSpeed(PlaybackRateMode &mode) override;
62     int32_t SetLooping(bool loop) override;
63 #ifdef SUPPORT_VIDEO
64     int32_t SetVideoSurface(sptr<Surface> surface) override;
65 #endif
66     bool IsPlaying() override;
67     int32_t SetParameter(const Format &param) override;
68     bool IsLooping() override;
69     int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback) override;
70     int32_t SelectBitRate(uint32_t bitRate) override;
71     int32_t SelectTrack(int32_t index, PlayerSwitchMode mode) override;
72     int32_t DeselectTrack(int32_t index) override;
73     int32_t GetCurrentTrack(int32_t trackType, int32_t &index) override;
74     int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy,
75         bool svp) override;
76     int32_t SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy) override;
77     int32_t Init();
78     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody);
79     int32_t SetPlaybackStrategy(AVPlayStrategy playbackStrategy) override;
80     int32_t SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted) override;
81     int32_t SetMaxAmplitudeCbStatus(bool status) override;
82     int32_t SetDeviceChangeCbStatus(bool status) override;
83     bool IsSeekContinuousSupported() override;
84 private:
85     void ResetSeekVariables();
86     void HandleSeekDoneInfo(PlayerOnInfoType type, int32_t extra);
87     std::recursive_mutex recMutex_;
88     int32_t mCurrentPosition = INT32_MIN;
89     PlayerSeekMode mCurrentSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
90     int32_t mSeekPosition = INT32_MIN;
91     PlayerSeekMode mSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
92     std::atomic<bool> isSeeking_{false};
93     int32_t prevTrackIndex_ = INT32_MIN;
94     std::shared_ptr<PlayerCallback> callback_;
95 
96     std::shared_ptr<IPlayerService> playerService_ = nullptr;
97     sptr<Surface> surface_ = nullptr;
98     HiviewDFX::HiTraceId traceId_;
99     std::mutex cbMutex_;
100 };
101 
102 class PlayerImplCallback : public PlayerCallback {
103 public:
104     PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb, std::shared_ptr<PlayerImpl> player);
105     ~PlayerImplCallback() = default;
106 
107     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody);
108     void OnError(int32_t errorCode, const std::string &errorMsg);
109 private:
110     std::shared_ptr<PlayerCallback> playerCb_;
111     std::weak_ptr<PlayerImpl> player_;
112     std::mutex playerImplCbMutex_;
113     bool getApiVersionFlag_ = true;
114 };
115 } // namespace Media
116 } // namespace OHOS
117 #endif // PLAYER_IMPL_H
118