• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "player_impl.h"
17 #include "i_media_service.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PlayerImpl"};
23 }
24 
25 namespace OHOS {
26 namespace Media {
CreatePlayer()27 std::shared_ptr<Player> PlayerFactory::CreatePlayer()
28 {
29     MEDIA_LOGD("PlayerImpl: CreatePlayer in");
30     std::shared_ptr<PlayerImpl> impl = std::make_shared<PlayerImpl>();
31     CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "failed to new PlayerImpl");
32 
33     int32_t ret = impl->Init();
34     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to init PlayerImpl");
35 
36     return impl;
37 }
38 
Init()39 int32_t PlayerImpl::Init()
40 {
41     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Init in", FAKE_POINTER(this));
42     playerService_ = MediaServiceFactory::GetInstance().CreatePlayerService();
43     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_UNKNOWN, "failed to create player service");
44     return MSERR_OK;
45 }
46 
PlayerImpl()47 PlayerImpl::PlayerImpl()
48 {
49     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
50 }
51 
~PlayerImpl()52 PlayerImpl::~PlayerImpl()
53 {
54     if (playerService_ != nullptr) {
55         (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
56         playerService_ = nullptr;
57     }
58     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
59 }
60 
SetSource(const std::shared_ptr<IMediaDataSource> & dataSrc)61 int32_t PlayerImpl::SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)
62 {
63     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(dataSrc)", FAKE_POINTER(this));
64     CHECK_AND_RETURN_RET_LOG(dataSrc != nullptr, MSERR_INVALID_VAL, "failed to create data source");
65     return playerService_->SetSource(dataSrc);
66 }
67 
SetSource(const std::string & url)68 int32_t PlayerImpl::SetSource(const std::string &url)
69 {
70     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(url): %{public}s", FAKE_POINTER(this), url.c_str());
71     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
72     CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
73     return playerService_->SetSource(url);
74 }
75 
SetSource(int32_t fd,int64_t offset,int64_t size)76 int32_t PlayerImpl::SetSource(int32_t fd, int64_t offset, int64_t size)
77 {
78     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(fd)", FAKE_POINTER(this));
79     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
80     return playerService_->SetSource(fd, offset, size);
81 }
82 
Play()83 int32_t PlayerImpl::Play()
84 {
85     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Play in", FAKE_POINTER(this));
86     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
87     return playerService_->Play();
88 }
89 
Prepare()90 int32_t PlayerImpl::Prepare()
91 {
92     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
93     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
94     return playerService_->Prepare();
95 }
96 
PrepareAsync()97 int32_t PlayerImpl::PrepareAsync()
98 {
99     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " PrepareAsync in", FAKE_POINTER(this));
100     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
101     return playerService_->PrepareAsync();
102 }
103 
Pause()104 int32_t PlayerImpl::Pause()
105 {
106     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
107     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
108     return playerService_->Pause();
109 }
110 
Stop()111 int32_t PlayerImpl::Stop()
112 {
113     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Stop in", FAKE_POINTER(this));
114     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
115     return playerService_->Stop();
116 }
117 
Reset()118 int32_t PlayerImpl::Reset()
119 {
120     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Reset in", FAKE_POINTER(this));
121     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
122     return playerService_->Reset();
123 }
124 
Release()125 int32_t PlayerImpl::Release()
126 {
127     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
128     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
129     (void)playerService_->Release();
130     (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
131     playerService_ = nullptr;
132     return MSERR_OK;
133 }
134 
ReleaseSync()135 int32_t PlayerImpl::ReleaseSync()
136 {
137     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " ReleaseSync in", FAKE_POINTER(this));
138     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
139     (void)playerService_->ReleaseSync();
140     (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
141     playerService_ = nullptr;
142     return MSERR_OK;
143 }
144 
SetVolume(float leftVolume,float rightVolume)145 int32_t PlayerImpl::SetVolume(float leftVolume, float rightVolume)
146 {
147     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVolume(%{public}f, %{public}f) in",
148         FAKE_POINTER(this), leftVolume, rightVolume);
149     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
150     return playerService_->SetVolume(leftVolume, rightVolume);
151 }
152 
Seek(int32_t mSeconds,PlayerSeekMode mode)153 int32_t PlayerImpl::Seek(int32_t mSeconds, PlayerSeekMode mode)
154 {
155     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Seek in, seek to %{public}d ms, mode is %{public}d",
156         FAKE_POINTER(this), mSeconds, mode);
157     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
158     return playerService_->Seek(mSeconds, mode);
159 }
160 
GetCurrentTime(int32_t & currentTime)161 int32_t PlayerImpl::GetCurrentTime(int32_t &currentTime)
162 {
163     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTime in", FAKE_POINTER(this));
164     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
165     return playerService_->GetCurrentTime(currentTime);
166 }
167 
GetVideoTrackInfo(std::vector<Format> & videoTrack)168 int32_t PlayerImpl::GetVideoTrackInfo(std::vector<Format> &videoTrack)
169 {
170     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoTrackInfo in", FAKE_POINTER(this));
171     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
172     return playerService_->GetVideoTrackInfo(videoTrack);
173 }
174 
GetAudioTrackInfo(std::vector<Format> & audioTrack)175 int32_t PlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
176 {
177     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetAudioTrackInfo in", FAKE_POINTER(this));
178     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
179     return playerService_->GetAudioTrackInfo(audioTrack);
180 }
181 
GetVideoWidth()182 int32_t PlayerImpl::GetVideoWidth()
183 {
184     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoWidth in", FAKE_POINTER(this));
185     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
186     return playerService_->GetVideoWidth();
187 }
188 
GetVideoHeight()189 int32_t PlayerImpl::GetVideoHeight()
190 {
191     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoHeight in", FAKE_POINTER(this));
192     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
193     return playerService_->GetVideoHeight();
194 }
195 
SetPlaybackSpeed(PlaybackRateMode mode)196 int32_t PlayerImpl::SetPlaybackSpeed(PlaybackRateMode mode)
197 {
198     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlaybackSpeed in, mode is %{public}d", FAKE_POINTER(this), mode);
199     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
200     return playerService_->SetPlaybackSpeed(mode);
201 }
202 
GetPlaybackSpeed(PlaybackRateMode & mode)203 int32_t PlayerImpl::GetPlaybackSpeed(PlaybackRateMode &mode)
204 {
205     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackSpeed in", FAKE_POINTER(this));
206     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
207     return playerService_->GetPlaybackSpeed(mode);
208 }
209 
SelectBitRate(uint32_t bitRate)210 int32_t PlayerImpl::SelectBitRate(uint32_t bitRate)
211 {
212     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectBitRate(%{public}d) in", FAKE_POINTER(this), bitRate);
213     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
214     return playerService_->SelectBitRate(bitRate);
215 }
216 
GetDuration(int32_t & duration)217 int32_t PlayerImpl::GetDuration(int32_t &duration)
218 {
219     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetDuration in", FAKE_POINTER(this));
220     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
221     return playerService_->GetDuration(duration);
222 }
223 
224 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)225 int32_t PlayerImpl::SetVideoSurface(sptr<Surface> surface)
226 {
227     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVideoSurface in", FAKE_POINTER(this));
228     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
229     CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_INVALID_VAL, "surface is nullptr");
230     surface_ = surface;
231     return playerService_->SetVideoSurface(surface);
232 }
233 #endif
234 
IsPlaying()235 bool PlayerImpl::IsPlaying()
236 {
237     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsPlaying in", FAKE_POINTER(this));
238     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
239 
240     return playerService_->IsPlaying();
241 }
242 
IsLooping()243 bool PlayerImpl::IsLooping()
244 {
245     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsLooping in", FAKE_POINTER(this));
246     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
247 
248     return playerService_->IsLooping();
249 }
250 
SetLooping(bool loop)251 int32_t PlayerImpl::SetLooping(bool loop)
252 {
253     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetLooping in, loop %{public}d", FAKE_POINTER(this), loop);
254     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
255     return playerService_->SetLooping(loop);
256 }
257 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)258 int32_t PlayerImpl::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
259 {
260     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayerCallback in", FAKE_POINTER(this));
261     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
262     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "callback is nullptr");
263     return playerService_->SetPlayerCallback(callback);
264 }
265 
SetParameter(const Format & param)266 int32_t PlayerImpl::SetParameter(const Format &param)
267 {
268     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetParameter in", FAKE_POINTER(this));
269     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
270     return playerService_->SetParameter(param);
271 }
272 } // namespace Media
273 } // namespace OHOS
274