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_demo.h"
17 #include <iostream>
18 #include "media_data_source_demo_noseek.h"
19 #include "media_data_source_demo_seekable.h"
20 #include "string_ex.h"
21 #include "media_errors.h"
22 #include "directory_ex.h"
23 #include "transaction/rs_transaction.h"
24 #include "ui/rs_surface_node.h"
25 #include "window_option.h"
26
27 using namespace OHOS;
28 using namespace OHOS::Media;
29 using namespace MediaDemo;
30 using namespace std;
31 namespace {
32 const std::string SURFACE_STRIDE_ALIGNMENT = "SURFACE_STRIDE_ALIGNMENT";
33 const std::string SURFACE_FORMAT_KEY = "SURFACE_FORMAT";
34 constexpr float EPSINON = 0.0001;
35 constexpr float SPEED_0_75_X = 0.75;
36 constexpr float SPEED_1_00_X = 1.00;
37 constexpr float SPEED_1_25_X = 1.25;
38 constexpr float SPEED_1_75_X = 1.75;
39 constexpr float SPEED_2_00_X = 2.00;
40 constexpr uint32_t PERCENT = 1;
41 constexpr uint32_t TIME = 2;
42 }
43
44 // PlayerCallback override
OnError(int32_t errorCode,const std::string & errorMsg)45 void PlayerCallbackDemo::OnError(int32_t errorCode, const std::string &errorMsg)
46 {
47 cout << "Error received, errorCode: " << errorCode << "errorMsg: " << errorMsg << endl;
48 }
49
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)50 void PlayerCallbackDemo::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
51 {
52 (void)infoBody;
53 switch (type) {
54 case INFO_TYPE_SEEKDONE:
55 cout << "PlayerCallback: OnSeekDone currentPositon is " << extra << endl;
56 break;
57 case INFO_TYPE_SPEEDDONE:
58 cout << "PlayerCallback: SpeedDone " << endl;
59 break;
60 case INFO_TYPE_BITRATEDONE:
61 cout << "PlayerCallback: BitRateDone " << endl;
62 break;
63 case INFO_TYPE_EOS:
64 cout << "PlayerCallback: OnEndOfStream isLooping is " << extra << endl;
65 break;
66 case INFO_TYPE_BUFFERING_UPDATE:
67 PrintBufferingUpdate(infoBody);
68 break;
69 case INFO_TYPE_BITRATE_COLLECT:
70 PrintBitRate(infoBody);
71 break;
72 case INFO_TYPE_STATE_CHANGE:
73 state_ = static_cast<PlayerStates>(extra);
74 PrintState(state_);
75 break;
76 case INFO_TYPE_POSITION_UPDATE:
77 if (updateCount_ == POSITION_UPDATE_INTERVAL) {
78 cout << "OnPositionUpdated position is " << extra << endl;
79 updateCount_ = 0;
80 }
81 updateCount_++;
82 break;
83 case INFO_TYPE_MESSAGE:
84 cout << "PlayerCallback: OnMessage is " << extra << endl;
85 break;
86 case INFO_TYPE_RESOLUTION_CHANGE:
87 PrintResolution(infoBody);
88 break;
89 case INFO_TYPE_VOLUME_CHANGE:
90 cout << "PlayerCallback: volume changed" << endl;
91 break;
92 default:
93 break;
94 }
95 }
96
SetBufferingOut(int32_t bufferingOut)97 void PlayerCallbackDemo::SetBufferingOut(int32_t bufferingOut)
98 {
99 bufferingOut_ = bufferingOut;
100 }
101
PrintState(PlayerStates state) const102 void PlayerCallbackDemo::PrintState(PlayerStates state) const
103 {
104 if (STATE_MAP.count(state) != 0) {
105 cout << "State:" << (STATE_MAP.at(state)).c_str() << endl;
106 } else {
107 cout << "Invalid state" << endl;
108 }
109 }
110
PrintResolution(const Format & infoBody) const111 void PlayerCallbackDemo::PrintResolution(const Format &infoBody) const
112 {
113 int32_t width = 0;
114 int32_t height = 0;
115 (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_WIDTH), width);
116 (void)infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_HEIGHT), height);
117 cout << "PlayerCallback: OnResolution changed width " << width << " height " << height << endl;
118 }
119
PrintBufferingUpdate(const Format & infoBody) const120 void PlayerCallbackDemo::PrintBufferingUpdate(const Format &infoBody) const
121 {
122 int32_t value = 0;
123 if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_START))) {
124 infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_START), value);
125 cout << "PlayerCallback: OnMessage is buffering start" << endl;
126 } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_END))) {
127 infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_END), value);
128 cout << "PlayerCallback: OnMessage is buffering end" << endl;
129 } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BUFFERING_PERCENT))) {
130 infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_PERCENT), value);
131 if ((static_cast<uint32_t>(bufferingOut_) & PERCENT) == PERCENT) {
132 cout << "OnBufferingPercent update is " << value << "%" << endl;
133 }
134 } else if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_CACHED_DURATION))) {
135 infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_CACHED_DURATION), value);
136 if ((static_cast<uint32_t>(bufferingOut_) & TIME) == TIME) {
137 cout << "OnCachedDuration update is " << value << "ms" << endl;
138 }
139 }
140 }
141
PrintBitRate(const Format & infoBody) const142 void PlayerCallbackDemo::PrintBitRate(const Format &infoBody) const
143 {
144 uint8_t *addr = nullptr;
145 size_t size = 0;
146 uint32_t bitrate = 0;
147 if (infoBody.ContainKey(std::string(PlayerKeys::PLAYER_BITRATE))) {
148 infoBody.GetBuffer(std::string(PlayerKeys::PLAYER_BITRATE), &addr, size);
149 if (addr == nullptr) {
150 return;
151 }
152
153 while (size > 0) {
154 if ((size - sizeof(uint32_t)) < 0) {
155 break;
156 }
157
158 bitrate = *(static_cast<uint32_t *>(static_cast<void *>(addr)));
159 cout << "hls bitrate : " << bitrate << endl;
160
161 addr += sizeof(uint32_t);
162 size -= sizeof(uint32_t);
163 }
164 }
165 }
166
PlayerDemo()167 PlayerDemo::PlayerDemo()
168 {
169 }
170
~PlayerDemo()171 PlayerDemo::~PlayerDemo()
172 {
173 if (previewWindow_ != nullptr) {
174 previewWindow_->Destroy();
175 previewWindow_ = nullptr;
176 }
177 }
178
GetSubWindowSurface()179 sptr<Surface> PlayerDemo::GetSubWindowSurface()
180 {
181 if (SetSurfaceSize() != 0) {
182 cout << "SetSurface Size fail" << endl;
183 return nullptr;
184 }
185
186 sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
187 option->SetWindowRect({ 0, 0, width_, height_ });
188 option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING);
189 option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
190 previewWindow_ = Rosen::Window::Create("xcomponent_window", option);
191 if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) {
192 cout << "previewWindow_ is nullptr" << endl;
193 return nullptr;
194 }
195
196 previewWindow_->Show();
197 auto surfaceNode = previewWindow_->GetSurfaceNode();
198 surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE);
199 Rosen::RSTransaction::FlushImplicitTransaction();
200 return surfaceNode->GetSurface();
201 }
202
GetVideoSurface()203 sptr<Surface> PlayerDemo::GetVideoSurface()
204 {
205 cout << "Please enter the number of mode(default no window):" << endl;
206 cout << "0:no window" << endl;
207 cout << "2:sub window" << endl;
208 string mode;
209 (void)getline(cin, mode);
210 sptr<Surface> producerSurface = nullptr;
211 if (mode == "0" || mode == "") {
212 return nullptr;
213 } else if (mode == "2") {
214 producerSurface = GetSubWindowSurface();
215 }
216
217 if (producerSurface == nullptr) {
218 cout << "producerSurface is nullptr" << endl;
219 return nullptr;
220 }
221 (void)producerSurface->SetUserData(SURFACE_FORMAT_KEY, std::to_string(static_cast<int>(PIXEL_FMT_RGBA_8888)));
222 cout << "GetVideoSurface ok" << endl;
223 return producerSurface;
224 }
225
Seek(const std::string & cmd)226 void PlayerDemo::Seek(const std::string &cmd)
227 {
228 int32_t time = -1;
229 if (!StrToInt(cmd, time) || time < 0) {
230 cout << "You need to configure the seek time parameter" << endl;
231 return;
232 }
233
234 cout << "Please enter the seek mode(seek previous sync):" << endl;
235 cout << "0:seek previous sync" << endl;
236 cout << "1:seek next sync" << endl;
237 cout << "2:seek closest sync" << endl;
238 cout << "3:seek closest" << endl;
239 string mode;
240 PlayerSeekMode seekMode = SEEK_PREVIOUS_SYNC;
241 (void)getline(cin, mode);
242 if (mode == "1") {
243 seekMode = SEEK_NEXT_SYNC;
244 } else if (mode == "2") {
245 seekMode = SEEK_CLOSEST_SYNC;
246 } else if (mode == "3") {
247 seekMode = SEEK_CLOSEST;
248 }
249
250 if (player_->Seek(time, seekMode) != 0) {
251 cout << "Operation Failed" << endl;
252 } else {
253 cout << "Operation OK" << endl;
254 }
255 }
256
ChangeModeToSpeed(const PlaybackRateMode & mode,double & rate) const257 int32_t PlayerDemo::ChangeModeToSpeed(const PlaybackRateMode &mode, double &rate) const
258 {
259 if (mode == SPEED_FORWARD_0_75_X) {
260 rate = SPEED_0_75_X;
261 } else if (mode == SPEED_FORWARD_1_00_X) {
262 rate = SPEED_1_00_X;
263 } else if (mode == SPEED_FORWARD_1_25_X) {
264 rate = SPEED_1_25_X;
265 } else if (mode == SPEED_FORWARD_1_75_X) {
266 rate = SPEED_1_75_X;
267 } else if (mode == SPEED_FORWARD_2_00_X) {
268 rate = SPEED_2_00_X;
269 } else {
270 return -1;
271 }
272 return 0;
273 }
274
ChangeSpeedToMode(const double & rate,PlaybackRateMode & mode) const275 int32_t PlayerDemo::ChangeSpeedToMode(const double &rate, PlaybackRateMode &mode) const
276 {
277 if (abs(rate - SPEED_0_75_X) < EPSINON) {
278 mode = SPEED_FORWARD_0_75_X;
279 } else if (abs(rate - SPEED_1_00_X) < EPSINON) {
280 mode = SPEED_FORWARD_1_00_X;
281 } else if (abs(rate - SPEED_1_25_X) < EPSINON) {
282 mode = SPEED_FORWARD_1_25_X;
283 } else if (abs(rate - SPEED_1_75_X) < EPSINON) {
284 mode = SPEED_FORWARD_1_75_X;
285 } else if (abs(rate - SPEED_2_00_X) < EPSINON) {
286 mode = SPEED_FORWARD_2_00_X;
287 } else {
288 return -1;
289 }
290 return 0;
291 }
292
GetVideoTrackInfo()293 int32_t PlayerDemo::GetVideoTrackInfo()
294 {
295 std::vector<Format> videoTrack;
296 int32_t ret = player_->GetVideoTrackInfo(videoTrack);
297 if (ret == 0) {
298 cout << "Video Track cnt: " << videoTrack.size() << endl;
299 std::string mime = "";
300 int32_t bitrate = -1;
301 int32_t width = -1;
302 int32_t height = -1;
303 int32_t framerate = -1;
304 int32_t type = -1;
305 int32_t index = -1;
306 for (auto iter = videoTrack.begin(); iter != videoTrack.end(); iter++) {
307 iter->GetStringValue(std::string(PlayerKeys::PLAYER_MIME), mime);
308 iter->GetIntValue(std::string(PlayerKeys::PLAYER_BITRATE), bitrate);
309 iter->GetIntValue(std::string(PlayerKeys::PLAYER_WIDTH), width);
310 iter->GetIntValue(std::string(PlayerKeys::PLAYER_HEIGHT), height);
311 iter->GetIntValue(std::string(PlayerKeys::PLAYER_FRAMERATE), framerate);
312 iter->GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_TYPE), type);
313 iter->GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), index);
314 cout << "mime: " << mime.c_str() << ", bitrate: " << bitrate << ", width: " << width << ", height: " <<
315 height << ", framerate: " << framerate << ", type: " << type << ", index: " << index << endl;
316 }
317 }
318 return 0;
319 }
GetAudioTrackInfo()320 int32_t PlayerDemo::GetAudioTrackInfo()
321 {
322 std::vector<Format> audioTrack;
323 int32_t ret = player_->GetAudioTrackInfo(audioTrack);
324 if (ret == 0) {
325 cout << "Audio Track cnt: " << audioTrack.size() << endl;
326 std::string mime = "";
327 int32_t bitrate = -1;
328 int32_t sampleRate = -1;
329 int32_t channels = -1;
330 int32_t type = -1;
331 int32_t index = -1;
332 std::string language = "";
333 for (auto iter = audioTrack.begin(); iter != audioTrack.end(); iter++) {
334 iter->GetStringValue(std::string(PlayerKeys::PLAYER_MIME), mime);
335 iter->GetIntValue(std::string(PlayerKeys::PLAYER_BITRATE), bitrate);
336 iter->GetIntValue(std::string(PlayerKeys::PLAYER_SAMPLE_RATE), sampleRate);
337 iter->GetIntValue(std::string(PlayerKeys::PLAYER_CHANNELS), channels);
338 iter->GetStringValue(std::string(PlayerKeys::PLAYER_LANGUGAE), language);
339 iter->GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_TYPE), type);
340 iter->GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), index);
341 cout << "mime: " << mime.c_str() << ", bitrate: " << bitrate << ", samplerate: " << sampleRate <<
342 ", channels: " << channels << ", language: " << language.c_str() << ", type: " << type <<
343 ", index: " << index << endl;
344 }
345 }
346 return 0;
347 }
GetTrackInfo()348 int32_t PlayerDemo::GetTrackInfo()
349 {
350 GetVideoTrackInfo();
351 GetAudioTrackInfo();
352 return 0;
353 }
354
GetPlaybackSpeed() const355 int32_t PlayerDemo::GetPlaybackSpeed() const
356 {
357 PlaybackRateMode mode;
358 double rate;
359 (void)player_->GetPlaybackSpeed(mode);
360 if (ChangeModeToSpeed(mode, rate) != 0) {
361 cout << "Change mode " << mode << " to speed failed" << endl;
362 } else {
363 cout << "Speed:" << rate << endl;
364 }
365 return 0;
366 }
367
SetPlaybackSpeed(const std::string & cmd) const368 void PlayerDemo::SetPlaybackSpeed(const std::string &cmd) const
369 {
370 PlaybackRateMode mode;
371 if (!cmd.empty()) {
372 double rate = std::stod(cmd.c_str());
373 if (ChangeSpeedToMode(rate, mode) != 0) {
374 cout << "Change speed " << rate << " to mode failed" << endl;
375 return;
376 }
377
378 if (player_->SetPlaybackSpeed(mode) != 0) {
379 cout << "Operation Failed" << endl;
380 } else {
381 cout << "Operation OK" << endl;
382 }
383 }
384 }
385
SelectBitRate(const std::string & cmd) const386 void PlayerDemo::SelectBitRate(const std::string &cmd) const
387 {
388 int32_t bitRate = 0;
389 if (!StrToInt(cmd, bitRate)) {
390 cout << "You need to configure the loop parameter" << endl;
391 return;
392 }
393
394 if (player_->SelectBitRate(bitRate) != 0) {
395 cout << "Operation Failed" << endl;
396 } else {
397 cout << "Operation OK" << endl;
398 }
399 }
400
SetVideoScaleType(const std::string & cmd) const401 void PlayerDemo::SetVideoScaleType(const std::string &cmd) const
402 {
403 int32_t videoScaleType = 0;
404 if (!StrToInt(cmd, videoScaleType)) {
405 cout << "video scale type input invaild" << endl;
406 return;
407 }
408 Format format;
409 format.PutIntValue(PlayerKeys::VIDEO_SCALE_TYPE, videoScaleType);
410 if (player_->SetParameter(format) != 0) {
411 cout << "Operation Failed" << endl;
412 } else {
413 cout << "Operation OK" << endl;
414 }
415 }
SetLoop(const std::string & cmd)416 void PlayerDemo::SetLoop(const std::string &cmd)
417 {
418 int32_t loopEn = -1;
419 if (!StrToInt(cmd, loopEn)) {
420 cout << "You need to configure the loop parameter" << endl;
421 return;
422 }
423 if (player_->SetLooping(static_cast<bool>(loopEn)) != 0) {
424 cout << "Operation Failed" << endl;
425 } else {
426 cout << "Operation OK" << endl;
427 }
428 }
429
GetPlaying()430 int32_t PlayerDemo::GetPlaying()
431 {
432 bool isPlay = player_->IsPlaying();
433 cout << "Playing:" << isPlay << endl;
434 return 0;
435 }
436
GetLooping()437 int32_t PlayerDemo::GetLooping()
438 {
439 bool isLoop = player_->IsLooping();
440 cout << "Looping:" << isLoop << endl;
441 return 0;
442 }
443
GetCurrentTime()444 void PlayerDemo::GetCurrentTime()
445 {
446 int32_t time = -1;
447 (void)player_->GetCurrentTime(time);
448 cout << "GetCurrentTime:" << time << endl;
449 }
450
DoCmd(const std::string & cmd)451 void PlayerDemo::DoCmd(const std::string &cmd)
452 {
453 if (cmd.find("source ") != std::string::npos) {
454 (void)SelectSource(cmd.substr(cmd.find("source ") + std::string("source ").length()));
455 } else if (cmd.find("seek ") != std::string::npos) {
456 Seek(cmd.substr(cmd.find("seek ") + std::string("seek ").length()));
457 } else if (cmd.find("volume ") != std::string::npos) {
458 std::string volume = cmd.substr(cmd.find("volume ") + std::string("volume ").length());
459 if (!volume.empty()) {
460 (void)player_->SetVolume(std::stof(volume.c_str()), std::stof(volume.c_str()));
461 }
462 } else if (cmd.find("duration") != std::string::npos) {
463 int32_t duration = -1;
464 (void)player_->GetDuration(duration);
465 cout << "GetDuration:" << duration << endl;
466 } else if (cmd.find("time") != std::string::npos) {
467 GetCurrentTime();
468 } else if (cmd.find("loop ") != std::string::npos) {
469 SetLoop(cmd.substr(cmd.find("loop ") + std::string("loop ").length()));
470 } else if (cmd.find("speed ") != std::string::npos) {
471 SetPlaybackSpeed(cmd.substr(cmd.find("speed ") + std::string("speed ").length()));
472 } else if (cmd.find("trackinfo") != std::string::npos) {
473 GetTrackInfo();
474 } else if (cmd.find("videosize") != std::string::npos) {
475 cout << "video width: " << player_->GetVideoWidth() << ", height: " << player_->GetVideoHeight();
476 } else if (cmd.find("bitrate ") != std::string::npos) {
477 SelectBitRate(cmd.substr(cmd.find("bitrate ") + std::string("bitrate ").length()));
478 } else if (cmd.find("videoscaletype ") != std::string::npos) {
479 SetVideoScaleType(cmd.substr(cmd.find("videoscaletype ") + std::string("videoscaletype ").length()));
480 }
481 }
482
DoNext()483 void PlayerDemo::DoNext()
484 {
485 std::string cmd;
486 while (std::getline(std::cin, cmd)) {
487 auto iter = playerTable_.find(cmd);
488 if (iter != playerTable_.end()) {
489 auto func = iter->second;
490 if (func() != 0) {
491 cout << "Operation error" << endl;
492 }
493 if (cmd.find("stop") != std::string::npos && dataSrc_ != nullptr) {
494 dataSrc_->Reset();
495 }
496 continue;
497 } else if (cmd.find("quit") != std::string::npos || cmd == "q") {
498 break;
499 } else {
500 DoCmd(cmd);
501 continue;
502 }
503 }
504 }
505
RegisterTable()506 void PlayerDemo::RegisterTable()
507 {
508 (void)playerTable_.emplace("prepare", std::bind(&Player::Prepare, player_));
509 (void)playerTable_.emplace("prepareasync", std::bind(&Player::PrepareAsync, player_));
510 (void)playerTable_.emplace("", std::bind(&Player::Play, player_)); // ENTER -> play
511 (void)playerTable_.emplace("play", std::bind(&Player::Play, player_));
512 (void)playerTable_.emplace("pause", std::bind(&Player::Pause, player_));
513 (void)playerTable_.emplace("stop", std::bind(&Player::Stop, player_));
514 (void)playerTable_.emplace("reset", std::bind(&Player::Reset, player_));
515 (void)playerTable_.emplace("release", std::bind(&Player::Release, player_));
516 (void)playerTable_.emplace("isplaying", std::bind(&PlayerDemo::GetPlaying, this));
517 (void)playerTable_.emplace("isloop", std::bind(&PlayerDemo::GetLooping, this));
518 (void)playerTable_.emplace("speed", std::bind(&PlayerDemo::GetPlaybackSpeed, this));
519 }
520
SetDataSrc(const string & path,bool seekable)521 int32_t PlayerDemo::SetDataSrc(const string &path, bool seekable)
522 {
523 cout << "Please enter the size of buffer:" << endl;
524 cout << "0:default" << endl;
525 string sizeStr;
526 (void)getline(cin, sizeStr);
527 int32_t size = -1;
528 if (!StrToInt(sizeStr, size) || size < 0) {
529 cout << "default size" << endl;
530 } else {
531 cout << "buffer size:" << size << endl;
532 }
533 if (seekable) {
534 dataSrc_ = MediaDataSourceDemoSeekable::Create(path, size);
535 } else {
536 dataSrc_ = MediaDataSourceDemoNoSeek::Create(path, size);
537 }
538 return player_->SetSource(dataSrc_);
539 }
540
SetFdSource(const string & path)541 int32_t PlayerDemo::SetFdSource(const string &path)
542 {
543 int32_t fd = open(path.c_str(), O_RDONLY);
544 if (fd < 0) {
545 cout << "Open file failed" << endl;
546 return -1;
547 }
548 int32_t offset = 0;
549
550 struct stat64 buffer;
551 if (fstat64(fd, &buffer) != 0) {
552 cout << "Get file state failed" << endl;
553 return -1;
554 }
555 int64_t length = static_cast<int64_t>(buffer.st_size);
556 cout << "fd = " << fd << ", offset = " << offset << ", length = " << length << endl;
557
558 int32_t ret = player_->SetSource(fd, offset, length);
559 (void)close(fd);
560 return ret;
561 }
562
SelectSource(const string & pathOuter)563 int32_t PlayerDemo::SelectSource(const string &pathOuter)
564 {
565 string path;
566 int32_t ret = -1;
567 if (pathOuter == "") {
568 cout << "Please enter the video/audio path: " << endl;
569 (void)getline(cin, path);
570 } else {
571 path = pathOuter;
572 }
573
574 cout << "Path is " << path << endl;
575 cout << "Please enter the number of source mode(default LOCAL):" << endl;
576 cout << "0:local file source" << endl;
577 cout << "1:stream file source with no seek" << endl;
578 cout << "2:stream file source with seekable" << endl;
579 cout << "3:file descriptor source" << endl;
580 string srcMode;
581 (void)getline(cin, srcMode);
582 if (srcMode == "" || srcMode == "0") {
583 cout << "source mode is LOCAL" << endl;
584 ret = player_->SetSource(path);
585 } else if (srcMode == "1") {
586 cout << "source mode is stream NO seek" << endl;
587 ret = SetDataSrc(path, false);
588 } else if (srcMode == "2") {
589 cout << "source mode is stream seekable" << endl;
590 ret = SetDataSrc(path, true);
591 } else if (srcMode == "3") {
592 cout << "source mode is FD" << endl;
593 ret = SetFdSource(path);
594 } else {
595 cout << "unknown mode" << endl;
596 }
597 return ret;
598 }
599
SetVideoScaleType()600 void PlayerDemo::SetVideoScaleType()
601 {
602 cout << "Please select video scale type(default 0):" << endl;
603 cout << "0:VIDEO_SCALE_TYPE_FIT" << endl;
604 cout << "1:VIDEO_SCALE_TYPE_FIT_CROP" << endl;
605 string type;
606 (void)getline(cin, type);
607 SetVideoScaleType(type);
608 }
SelectBufferingOut()609 int32_t PlayerDemo::SelectBufferingOut()
610 {
611 cout << "Please enter the number of mode(no buffering info):" << endl;
612 cout << "0:no buffering info" << endl;
613 cout << "1:percent" << endl;
614 cout << "2:time" << endl;
615 cout << "3:percent and time" << endl;
616 string mode;
617 (void)getline(cin, mode);
618 if (mode == "1") {
619 return PERCENT;
620 } else if (mode == "2") {
621 return TIME;
622 } else if (mode == "3") {
623 return (PERCENT | TIME);
624 } else {
625 return 0;
626 }
627 }
628
SelectRendererMode()629 int32_t PlayerDemo::SelectRendererMode()
630 {
631 cout << "Please select renderer mode (default no renderer)" << endl;
632 cout << "0:no renderer" << endl;
633 cout << "1:please set more details" << endl;
634 string mode;
635 (void)getline(cin, mode);
636 if (mode == "1") {
637 return SetRendererInfo();
638 } else {
639 return 0;
640 }
641 }
642
SetRendererInfo()643 int32_t PlayerDemo::SetRendererInfo()
644 {
645 cout << "Please enter contentType(int)" << endl;
646 int32_t contentType;
647 cin >> contentType;
648 cout << "Please enter streamUsage(int)" << endl;
649 int32_t streamUsage;
650 cin >> streamUsage;
651 cout << "Please enter rendererFlags(int)" << endl;
652 int32_t rendererFlags;
653 cin >> rendererFlags;
654 Format format;
655 (void)format.PutIntValue(PlayerKeys::CONTENT_TYPE, contentType);
656 (void)format.PutIntValue(PlayerKeys::STREAM_USAGE, streamUsage);
657 (void)format.PutIntValue(PlayerKeys::RENDERER_FLAG, rendererFlags);
658 return player_->SetParameter(format);
659 }
660
661
SetSurfaceSize()662 int32_t PlayerDemo::SetSurfaceSize()
663 {
664 int32_t ret = 0;
665 cout << "Please enter surface size(width x height):" << endl;
666 cout << "0:1920 x 1080" << endl;
667 cout << "1:640 x 360" << endl;
668 string mode;
669 (void)getline(cin, mode);
670 if (mode == "" || mode == "0") {
671 width_ = 1920; // 1920 for width
672 height_ = 1080; // 1080 for height
673 } else if (mode == "1") {
674 width_ = 640; // 640 for width
675 height_ = 360; // 360 for height
676 } else {
677 ret = -1;
678 }
679 return ret;
680 }
681
RunCase(const string & path)682 void PlayerDemo::RunCase(const string &path)
683 {
684 player_ = OHOS::Media::PlayerFactory::CreatePlayer();
685 if (player_ == nullptr) {
686 cout << "player_ is null" << endl;
687 return;
688 }
689 RegisterTable();
690 std::shared_ptr<PlayerCallbackDemo> cb = std::make_shared<PlayerCallbackDemo>();
691 cb->SetBufferingOut(SelectBufferingOut());
692
693 int32_t ret = player_->SetPlayerCallback(cb);
694 if (ret != 0) {
695 cout << "SetPlayerCallback fail" << endl;
696 }
697 if (SelectSource(path) != 0) {
698 cout << "SetSource fail" << endl;
699 return;
700 }
701 sptr<Surface> producerSurface = nullptr;
702 producerSurface = GetVideoSurface();
703 if (producerSurface != nullptr) {
704 ret = player_->SetVideoSurface(producerSurface);
705 if (ret != 0) {
706 cout << "SetVideoSurface fail" << endl;
707 }
708 }
709 SetVideoScaleType();
710 if (SelectRendererMode() != 0) {
711 cout << "set renderer info fail" << endl;
712 }
713 ret = player_->PrepareAsync();
714 if (ret != 0) {
715 cout << "PrepareAsync fail" << endl;
716 return;
717 }
718 cout << "Enter your step:" << endl;
719 DoNext();
720 }
721