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 "playerfile_fuzzer.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
24 using namespace std;
25 using namespace OHOS;
26 using namespace OHOS::Media;
27
28 namespace OHOS {
29 namespace Media {
PlayerFileFuzzer()30 PlayerFileFuzzer::PlayerFileFuzzer()
31 {
32 }
33
~PlayerFileFuzzer()34 PlayerFileFuzzer::~PlayerFileFuzzer()
35 {
36 }
37
FuzzFile(const uint8_t * data,size_t size)38 bool PlayerFileFuzzer::FuzzFile(const uint8_t* data, size_t size)
39 {
40 player_ = OHOS::Media::PlayerFactory::CreatePlayer();
41 if (player_ == nullptr) {
42 return false;
43 }
44 std::shared_ptr<TestPlayerCallback> cb = std::make_shared<TestPlayerCallback>();
45 player_->SetPlayerCallback(cb);
46 const string path = "/data/test/media/fuzztest.mp4";
47 int32_t retWrite = WriteDataToFile(path, data, size);
48 if (retWrite != 0) {
49 return true;
50 }
51 int32_t retValue = SetFdSource(path);
52 if (retValue != 0) {
53 return true;
54 }
55 sptr<Surface> producerSurface = nullptr;
56 producerSurface = GetVideoSurface();
57 player_->SetVideoSurface(producerSurface);
58 player_->PrepareAsync();
59 sleep(3); // wait PrepareAsync success, sleep 3 s
60 player_->Play();
61 sleep(1);
62 player_->Pause();
63 player_->Seek(3000, SEEK_NEXT_SYNC); // seek 3000 ms
64 player_->SetVolume(0.5, 0.5); // leftVolume is 0.5, rightVolume is 0.5
65 int32_t time;
66 player_->GetCurrentTime(time);
67 std::vector<Format> videoTrack;
68 player_->GetVideoTrackInfo(videoTrack);
69 std::vector<Format> audioTrack;
70 player_->GetAudioTrackInfo(audioTrack);
71 player_->GetVideoWidth();
72 player_->GetVideoHeight();
73 int32_t duration = 0;
74 player_->GetDuration(duration);
75 player_->SetPlaybackSpeed(SPEED_FORWARD_2_00_X);
76 PlaybackRateMode mode;
77 player_->GetPlaybackSpeed(mode);
78 player_->SelectBitRate(0);
79 player_->Reset();
80 player_->Stop();
81 player_->Release();
82 return true;
83 }
84 }
85
WriteDataToFile(const string & path,const uint8_t * data,size_t size)86 int32_t WriteDataToFile(const string &path, const uint8_t* data, size_t size)
87 {
88 FILE *file = nullptr;
89 file = fopen(path.c_str(), "w+");
90 if (file == nullptr) {
91 return -1;
92 }
93 if (fwrite(data, 1, size, file) != size) {
94 (void)fclose(file);
95 return -1;
96 }
97 (void)fclose(file);
98 return 0;
99 }
100
FuzzPlayerFile(const uint8_t * data,size_t size)101 bool FuzzPlayerFile(const uint8_t* data, size_t size)
102 {
103 auto player = std::make_unique<PlayerFileFuzzer>();
104 if (player == nullptr) {
105 return true;
106 }
107 return player->FuzzFile(data, size);
108 }
109 }
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 /* Run your code on data */
115 FuzzPlayerFile(data, size);
116 return 0;
117 }
118