1/*
2 * Copyright (c) 2022 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 "test_player.h"
17#include <iostream>
18#include "string_ex.h"
19#include "media_errors.h"
20#include "directory_ex.h"
21#include "ui/rs_surface_node.h"
22#include "window_option.h"
23
24using namespace std;
25using namespace OHOS;
26using namespace OHOS::Media;
27
28void TestPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
29{
30    cout << "Error received, errorCode: " << errorCode << "errorMsg: " << errorMsg << endl;
31}
32
33void TestPlayerCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
34{
35    (void)infoBody;
36    switch (type) {
37        case INFO_TYPE_SEEKDONE:
38            cout << "PlayerCallback: OnSeekDone currentPositon is " << extra << endl;
39            break;
40        case INFO_TYPE_SPEEDDONE:
41            cout << "PlayerCallback: SpeedDone " << endl;
42            break;
43        case INFO_TYPE_EOS:
44            cout << "PlayerCallback: OnEndOfStream isLooping is " << extra << endl;
45            break;
46        case INFO_TYPE_BUFFERING_UPDATE:
47            break;
48        case INFO_TYPE_STATE_CHANGE:
49            break;
50        case INFO_TYPE_POSITION_UPDATE:
51            cout << "OnPositionUpdated position is " << extra << endl;
52            break;
53        case INFO_TYPE_MESSAGE:
54            cout << "PlayerCallback: OnMessage is " << extra << endl;
55            break;
56        case INFO_TYPE_RESOLUTION_CHANGE:
57            break;
58        case INFO_TYPE_VOLUME_CHANGE:
59            cout << "PlayerCallback: volume changed" << endl;
60            break;
61        default:
62            break;
63    }
64}
65TestPlayer::TestPlayer()
66{
67}
68
69TestPlayer::~TestPlayer()
70{
71    if (previewWindow_ != nullptr) {
72        previewWindow_->Destroy();
73        previewWindow_ = nullptr;
74    }
75}
76sptr<Surface> TestPlayer::GetVideoSurface()
77{
78    sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
79    option->SetWindowRect({ 0, 0, 640, 480 });
80    option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING);
81    option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
82    previewWindow_ = Rosen::Window::Create("xcomponent_window_fuzztest", option);
83    if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) {
84        cout << "previewWindow_ is nullptr" << endl;
85        return nullptr;
86    }
87    previewWindow_->Show();
88    return previewWindow_->GetSurfaceNode()->GetSurface();
89}
90
91int32_t TestPlayer::SetFdSource(const string &path)
92{
93    int32_t fdValue = open(path.c_str(), O_RDONLY);
94    if (fdValue < 0) {
95        cout << "Open file failed" << endl;
96        (void)close(fdValue);
97        return -1;
98    }
99    int32_t offsetValue = 0;
100
101    struct stat64 buffer;
102    if (fstat64(fdValue, &buffer) != 0) {
103        cout << "Get file state failed" << endl;
104        (void)close(fdValue);
105        return -1;
106    }
107    int64_t lengthValue = static_cast<int64_t>(buffer.st_size);
108    int32_t retValue = player_->SetSource(fdValue, offsetValue, lengthValue);
109    if (retValue != 0) {
110        (void)close(fdValue);
111        return -1;
112    }
113    (void)close(fdValue);
114    return 0;
115}
116