Testplayer.cpp 12.4 KB
Newer Older
S
sharpshooter_t 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Testplayer.h"
#include "parameter.h"
B
bird_j 已提交
19
#include "media_errors.h"
S
sharpshooter_t 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

using namespace OHOS;
using namespace OHOS::Media;
using namespace PlayerNameSpace;

namespace PlayerNameSpace {
std::string GetUri()
{
    char path[256] = "/data/1.mp4";
    GetParameter("sys.media.test.stream.path", "/data/1.mp4", &path[0], 256);
    MEDIA_INFO_LOG("PATH : %s", path);
    return path;
}
}
TestPlayer::TestPlayer(PlayerSignal *test)
    : test_(test)
{
}
TestPlayer::~TestPlayer()
{
}
bool TestPlayer::CreatePlayer()
{
B
bird_j 已提交
43
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
44 45 46 47 48 49 50 51
    player = PlayerFactory::CreatePlayer();
    if (player == nullptr) {
        return false;
    }
    return true;
}
int32_t TestPlayer::SetSource(const std::string &uri)
{
B
bird_j 已提交
52
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
53 54 55 56 57
    return player->SetSource(uri);
}

int32_t TestPlayer::Play()
{
B
bird_j 已提交
58
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
    int32_t ret = player->Play();
    if (test_->mutexFlag_ == true && test_->state_ != PLAYER_STARTED) {
        std::unique_lock<std::mutex> lockPlay(test_->mutexPlay_);
        test_->condVarPlay_.wait_for(lockPlay, std::chrono::seconds(WAITSECOND));
        if (test_->state_ != PLAYER_STARTED) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::Prepare()
{
B
bird_j 已提交
72
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
    int32_t ret = player->Prepare();
    if (test_->mutexFlag_ == true && test_->state_ != PLAYER_PREPARED) {
        std::unique_lock<std::mutex> lockPrepare(test_->mutexPrepare_);
        test_->condVarPrepare_.wait_for(lockPrepare, std::chrono::seconds(WAITSECOND));
        if (test_->state_ != PLAYER_PREPARED) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::PrepareAsync()
{
B
bird_j 已提交
86
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
    int32_t ret = player->PrepareAsync();
    if (test_->mutexFlag_ == true && test_->state_ != PLAYER_PREPARED) {
        std::unique_lock<std::mutex> lockPrepare(test_->mutexPrepare_);
        test_->condVarPrepare_.wait_for(lockPrepare, std::chrono::seconds(WAITSECOND));
        if (test_->state_ != PLAYER_PREPARED) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::Pause()
{
B
bird_j 已提交
100
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
    int32_t ret = player->Pause();
    if (test_->mutexFlag_ == true && test_->state_ != PLAYER_PAUSED) {
        std::unique_lock<std::mutex> lockPause(test_->mutexPause_);
        test_->condVarPause_.wait_for(lockPause, std::chrono::seconds(WAITSECOND));
        if (test_->state_ != PLAYER_PAUSED) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::Stop()
{
B
bird_j 已提交
114
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127
    int32_t ret = player->Stop();
    if (test_->mutexFlag_ == true && test_->state_ != PLAYER_STOPPED) {
        std::unique_lock<std::mutex> lockStop(test_->mutexStop_);
        test_->condVarStop_.wait_for(lockStop, std::chrono::seconds(WAITSECOND));
        if (test_->state_ != PLAYER_STOPPED) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::Reset()
{
B
bird_j 已提交
128
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141
    int32_t ret = player->Reset();
    if (test_->mutexFlag_ == true && test_->state_ != PLAYER_IDLE) {
        std::unique_lock<std::mutex> lockReset(test_->mutexReset_);
        test_->condVarReset_.wait_for(lockReset, std::chrono::seconds(WAITSECOND));
        if (test_->state_ != PLAYER_IDLE) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::Release()
{
B
bird_j 已提交
142
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
143 144 145 146 147
    return player->Release();
}

int32_t TestPlayer::SetVolume(float leftVolume, float rightVolume)
{
B
bird_j 已提交
148
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
149 150 151 152 153
    return player->SetVolume(leftVolume, rightVolume);
}

int32_t TestPlayer::Seek(int32_t mseconds, PlayerSeekMode mode)
{
B
bird_j 已提交
154
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    test_->seekDoneFlag_ = false;
    test_->seekPositon_ = mseconds;
    int32_t ret = player->Seek(mseconds, mode);
    if (test_->mutexFlag_ == true && test_->seekDoneFlag_ == false) {
        std::unique_lock<std::mutex> lockSeek(test_->mutexSeek_);
        test_->condVarSeek_.wait_for(lockSeek, std::chrono::seconds(WAITSECOND));
        if (test_->seekDoneFlag_ != true) {
            return -1;
        }
    }
    return ret;
}

int32_t TestPlayer::GetCurrentTime(int32_t &currentTime)
{
B
bird_j 已提交
170
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
171 172 173 174 175
    return player->GetCurrentTime(currentTime);
}

int32_t TestPlayer::GetDuration(int32_t &duration)
{
B
bird_j 已提交
176
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
177 178 179 180 181
    return player->GetDuration(duration);
}

int32_t TestPlayer::SetPlaybackSpeed(PlaybackRateMode mode)
{
B
bird_j 已提交
182
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
183 184 185 186 187
    return player->SetPlaybackSpeed(mode);
}

int32_t TestPlayer::GetPlaybackSpeed(PlaybackRateMode &mode)
{
B
bird_j 已提交
188
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
189 190 191 192 193 194
    return player->GetPlaybackSpeed(mode);
}
sptr<Surface> TestPlayer::GetVideoSurface(WindowConfig sub_config)
{
    char surface[256] = "null";
    GetParameter("sys.media.test.surface", "null", &surface[0], 256);
B
bird_j 已提交
195 196 197 198
    sptr<Surface> videoSurface = nullptr;
    if (strcmp(surface, "null") == 0) {
        return videoSurface;
    }
S
sharpshooter_t 已提交
199 200 201 202 203 204 205 206 207 208 209 210
    mwindow = WindowManager::GetInstance()->CreateWindow(&g_config);
    if (mwindow  == nullptr) {
        MEDIA_ERROR_LOG("Create mwindow failed!!!");
        return nullptr;
    }
    if (strcmp(surface, "subwindow") == 0) {
        InitSubWindow(sub_config);
        videoSurface = window->GetSurface();
    } else if (strcmp(surface, "window") == 0) {
        videoSurface = mwindow->GetSurface();
        videoSurface->SetUserData(SURFACE_FORMAT, std::to_string(PIXEL_FMT_RGBA_8888));
        std::string format = videoSurface->GetUserData(SURFACE_FORMAT);
B
bird_j 已提交
211
        MEDIA_DEBUG_LOG("SetUserData SURFACE_FORMAT = %s", format.c_str());
S
sharpshooter_t 已提交
212 213 214 215 216
    }
    return videoSurface;
}
int32_t TestPlayer::SetVideoSurface(const sptr<Surface> surface)
{
B
bird_j 已提交
217
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    char parameter[256] = "null";
    GetParameter("sys.media.test.surface", "null", &parameter[0], 256);
    if (strcmp(parameter, "null") == 0) {
        MEDIA_INFO_LOG("sys.media.test.surface null");
        return 0;
    }
    return player->SetVideoSurface(surface);
}

bool TestPlayer::IsPlaying()
{
    return player->IsPlaying();
}

bool TestPlayer::IsLooping()
{
    return player->IsLooping();
}

int32_t TestPlayer::SetLooping(bool loop)
{
B
bird_j 已提交
239
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
240 241 242 243 244
    return player->SetLooping(loop);
}

int32_t TestPlayer::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
{
B
bird_j 已提交
245
    MEDIA_INFO_LOG("%s", __FUNCTION__);
S
sharpshooter_t 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    return player->SetPlayerCallback(callback);
}

void TestPlayer::InitSubWindow(WindowConfig sub_config)
{
    sptr<SurfaceBuffer> buffer;
    BufferRequestConfig requestConfig;
    int32_t releaseFence;
    mwindow->GetRequestConfig(requestConfig);
    (void)mwindow->GetSurface()->RequestBuffer(buffer, releaseFence, requestConfig);
    uint32_t buffSize = buffer->GetSize();
    void *bufferVirAddr = buffer->GetVirAddr();
    (void)memset_s(bufferVirAddr, buffSize, 0, buffSize);
    BufferFlushConfig flushConfig = {
        .damage = {
            .x = 0,
            .y = 0,
            .w = requestConfig.width,
            .h = requestConfig.height,
            },
        .timestamp = 0,
    };
    if (mwindow->GetSurface()->FlushBuffer(buffer, -1, flushConfig) != 0) {
        MEDIA_ERROR_LOG("FlushBuffer failed");
    }
    window = WindowManager::GetInstance()->CreateSubWindow(mwindow->GetWindowID(), &sub_config);
    ASSERT_NE(nullptr, window);
    if (window  == nullptr) {
        MEDIA_ERROR_LOG("Create window failed!!!");
        return;
    }
    return;
}

TestPlayerCallback::TestPlayerCallback(PlayerSignal *test)
    : test_(test)
{
}
TestPlayerCallback::~TestPlayerCallback()
{
}
void TestPlayerCallback::OnError(PlayerErrorType errorType, int32_t errorCode)
{
    errorNum++;
    errorType_ = errorType;
    errorCode_ = errorCode;
B
bird_j 已提交
292 293 294 295
    std::string errorTypeMsg = PlayerErrorTypeToString(errorType);
    std::string errorCodeMsg = MSErrorToString(static_cast<MediaServiceErrCode>(errorCode));
    MEDIA_ERROR_LOG("TestPlayerCallback: OnError errorType is %s, errorCode is %s",
        errorTypeMsg.c_str(), errorCodeMsg.c_str());
S
sharpshooter_t 已提交
296
}
B
bird_j 已提交
297
void TestPlayerCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
S
sharpshooter_t 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
{
    switch (type) {
        case INFO_TYPE_SEEKDONE:
            seekDoneFlag = true;
            test_->SetSeekResult(true);
            MEDIA_INFO_LOG("TestPlayerCallback: OnSeekDone currentPositon is %d", extra);
            if (abs(test_->seekPositon_ - extra) <= DELTA_TIME) {
                test_->condVarSeek_.notify_all();
            } else {
                test_->SetSeekResult(false);
            }
            break;
        case INFO_TYPE_EOS:
            MEDIA_INFO_LOG("TestPlayerCallback: OnEndOfStream isLooping is %d", extra);
            break;
        case INFO_TYPE_STATE_CHANGE:
            state_ = static_cast<PlayerStates>(extra);
            PrintState(state_);
            break;
        case INFO_TYPE_POSITION_UPDATE:
            postion_ = extra;
            break;
        case INFO_TYPE_MESSAGE:
            MEDIA_INFO_LOG("TestPlayerCallback: OnMessage type is %d", extra);
            break;
        default:
            break;
    }
}
int TestPlayerCallback::WaitForSeekDone(int32_t currentPositon)
{
    int64_t waitTime = 0;
    seekDoneFlag = false;
    while (seekDoneFlag != true && waitTime < WAITSECOND * 1000) {
        usleep(1000);
        waitTime += 1;
    }
    seekDoneFlag = false;
    if (waitTime >= WAITSECOND * 1000) {
        MEDIA_INFO_LOG("Failed to seek [%d]s ", currentPositon);
        return -1;
    }
    return 0;
}

int TestPlayerCallback::WaitForState(PlayerStates state)
{
    int64_t waitTime = 0;
    while (state_ != state && waitTime < WAITSECOND * 1000) {
        usleep(1000);
        waitTime += 1;
    }
    if (waitTime >= WAITSECOND * 1000) {
B
bird_j 已提交
351
        MEDIA_ERROR_LOG("Failed to wait for state[%d] down", state);
S
sharpshooter_t 已提交
352 353 354 355 356 357 358 359 360 361 362 363
        return -1;
    }
    return 0;
}
void TestPlayerCallback::PrintState(PlayerStates state)
{
    switch (state) {
        case PLAYER_STATE_ERROR:
            MEDIA_INFO_LOG("State: Error");
            break;
        case PLAYER_IDLE:
            MEDIA_INFO_LOG("State: IDLE");
B
bird_j 已提交
364
            test_->SetState(state);
S
sharpshooter_t 已提交
365 366 367 368 369 370 371 372 373 374
            test_->condVarReset_.notify_all();
            break;
        case PLAYER_INITIALIZED:
            MEDIA_INFO_LOG("State: Initialized");
            break;
        case PLAYER_PREPARING:
            MEDIA_INFO_LOG("State: Preparing");
            break;
        case PLAYER_PREPARED:
            MEDIA_INFO_LOG("State: Prepared");
B
bird_j 已提交
375
            test_->SetState(state);
S
sharpshooter_t 已提交
376 377 378 379
            test_->condVarPrepare_.notify_all();
            break;
        case PLAYER_STARTED:
            MEDIA_INFO_LOG("State: Started");
B
bird_j 已提交
380
            test_->SetState(state);
S
sharpshooter_t 已提交
381 382 383 384
            test_->condVarPlay_.notify_all();
            break;
        case PLAYER_PAUSED:
            MEDIA_INFO_LOG("State: Paused");
B
bird_j 已提交
385
            test_->SetState(state);
S
sharpshooter_t 已提交
386 387 388 389
            test_->condVarPause_.notify_all();
            break;
        case PLAYER_STOPPED:
            MEDIA_INFO_LOG("State: Stopped");
B
bird_j 已提交
390
            test_->SetState(state);
S
sharpshooter_t 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
            test_->condVarStop_.notify_all();
            break;
        case PLAYER_PLAYBACK_COMPLETE:
            MEDIA_INFO_LOG("State: Complete");
            break;
        default:
            MEDIA_INFO_LOG("Invalid state");
            break;
    }
}

void PlayerSignal::SetState(PlayerStates state)
{
    state_ = state;
}
void PlayerSignal::SetSeekResult(bool seekDoneFlag)
{
    seekDoneFlag_ = seekDoneFlag;
}