1 /* 2 * Copyright (c) 2024 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 FRAMEWORKS_BOOTANIMATION_INCLUDE_BOOT_PLAYER_H 17 #define FRAMEWORKS_BOOTANIMATION_INCLUDE_BOOT_PLAYER_H 18 19 #ifdef PLAYER_FRAMEWORK_ENABLE 20 #include "player.h" 21 #endif 22 #include "transaction/rs_interfaces.h" 23 #include "util.h" 24 #include <system_ability_definition.h> 25 #include <iservice_registry.h> 26 27 namespace OHOS { 28 static const int64_t MAX_WAIT_MEDIA_CREATE_TIME = 5000; // 5S 29 #ifdef PLAYER_FRAMEWORK_ENABLE 30 static const int CONTENT_TYPE_UNKNOWN = 0; 31 static const int STREAM_USAGE_ENFORCED_TONE = 15; 32 #endif 33 34 class BootPlayer { 35 public: ~BootPlayer()36 virtual ~BootPlayer() {}; 37 Play()38 virtual void Play() {}; 39 GetResPath(const std::string & type)40 std::string GetResPath(const std::string& type) 41 { 42 if (IsFileExisted(resPath_)) { 43 return FILE_PREFIX + resPath_; 44 } 45 return type == TYPE_VIDEO ? BOOT_VIDEO_PATH : BOOT_SOUND_PATH; 46 } 47 48 #ifdef PLAYER_FRAMEWORK_ENABLE SetCustomizedVolume(const int volume)49 bool SetCustomizedVolume(const int volume) 50 { 51 if (mediaPlayer_ == nullptr) { 52 LOGE("mediaPlayer is nullptr."); 53 return false; 54 } 55 float customizedVolume = (float)volume/MAX_VOLUME; 56 LOGE("customizedVolume: %{public}d -> %{public}f", volume, customizedVolume); 57 int ret = mediaPlayer_->SetVolume(customizedVolume, customizedVolume); 58 if (ret != 0) { 59 LOGE("PlayVideo SetVolume fail, errorCode:%{public}d", ret); 60 return false; 61 } 62 return true; 63 } 64 buildMediaFormat()65 Media::Format buildMediaFormat() 66 { 67 Media::Format format; 68 format.PutIntValue(Media::PlayerKeys::CONTENT_TYPE, CONTENT_TYPE_UNKNOWN); 69 format.PutIntValue(Media::PlayerKeys::STREAM_USAGE, STREAM_USAGE_ENFORCED_TONE); 70 format.PutIntValue(Media::PlayerKeys::RENDERER_FLAG, 0); 71 return format; 72 } 73 CheckAndCreateMedia()74 void CheckAndCreateMedia() 75 { 76 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 77 if (samgr == nullptr) { 78 LOGE("samgr is nullptr"); 79 return; 80 } 81 int64_t startTime = GetSystemCurrentTime(); 82 int64_t endTime = startTime; 83 while ((endTime - startTime) < MAX_WAIT_MEDIA_CREATE_TIME 84 && samgr->CheckSystemAbility(OHOS::PLAYER_DISTRIBUTED_SERVICE_ID) == nullptr) { 85 usleep(SLEEP_TIME_US_10); 86 endTime = GetSystemCurrentTime(); 87 } 88 if (samgr->CheckSystemAbility(OHOS::PLAYER_DISTRIBUTED_SERVICE_ID) == nullptr) { 89 LOGE("CheckMediaSA fail"); 90 return; 91 } 92 LOGI("CheckMediaSA success"); 93 94 startTime = GetSystemCurrentTime(); 95 endTime = startTime; 96 while ((endTime - startTime) < MAX_WAIT_MEDIA_CREATE_TIME 97 && (mediaPlayer_ = Media::PlayerFactory::CreatePlayer()) == nullptr) { 98 usleep(SLEEP_TIME_US); 99 endTime = GetSystemCurrentTime(); 100 LOGI("mediaPlayer is nullptr, try create again"); 101 } 102 } 103 #endif 104 105 Rosen::ScreenId screenId_; 106 std::string resPath_; 107 bool isSoundEnabled_ = false; 108 std::shared_ptr<Media::Player> mediaPlayer_; 109 }; 110 } // namespace OHOS 111 112 #endif // FRAMEWORKS_BOOTANIMATION_INCLUDE_BOOT_PLAYER_H 113