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 "media_data_source_test_seekable.h"
17 #include <iostream>
18 #include "media_errors.h"
19 #include "directory_ex.h"
20 #include "media_log.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MediaDataSourceTestSeekable"};
24 }
25
26 namespace OHOS {
27 namespace Media {
Create(const std::string & uri,int32_t size)28 std::shared_ptr<MediaDataSourceTest> MediaDataSourceTestSeekable::Create(const std::string &uri, int32_t size)
29 {
30 std::string realPath;
31 if (!PathToRealPath(uri, realPath)) {
32 std::cout << "Path is unaccessable: " << uri << std::endl;
33 return nullptr;
34 }
35 std::shared_ptr<MediaDataSourceTestSeekable> dataSrc =
36 std::make_shared<MediaDataSourceTestSeekable>(realPath, size);
37 if (dataSrc->Init() != MSERR_OK) {
38 std::cout << "init source failed" << std::endl;
39 return nullptr;
40 }
41 return dataSrc;
42 }
43
MediaDataSourceTestSeekable(const std::string & uri,int32_t size)44 MediaDataSourceTestSeekable::MediaDataSourceTestSeekable(const std::string &uri, int32_t size)
45 : uri_(uri),
46 fixedSize_(size)
47 {
48 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
49 }
50
~MediaDataSourceTestSeekable()51 MediaDataSourceTestSeekable::~MediaDataSourceTestSeekable()
52 {
53 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
54 if (fd_ != nullptr) {
55 (void)fclose(fd_);
56 fd_ = nullptr;
57 }
58 }
59
Init()60 int32_t MediaDataSourceTestSeekable::Init()
61 {
62 fd_ = fopen(uri_.c_str(), "rb+");
63 if (fd_ == nullptr) {
64 std::cout<<"open file failed"<<std::endl;
65 return MSERR_INVALID_VAL;
66 }
67 return MSERR_OK;
68 }
69
Reset()70 void MediaDataSourceTestSeekable::Reset()
71 {
72 std::cout<< "reset data source" << std::endl;
73 pos_ = 0;
74 (void)fseek(fd_, 0, SEEK_SET);
75 }
76
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos)77 int32_t MediaDataSourceTestSeekable::ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos)
78 {
79 MEDIA_LOGD("ReadAt in, pos is %{public}" PRIu64 "", pos);
80 CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_INVALID_VAL, "Mem is nullptr");
81 if (pos != pos_) {
82 (void)fseek(fd_, static_cast<long>(pos), SEEK_SET);
83 pos_ = pos;
84 }
85 size_t readRet = 0;
86 int32_t realLen = static_cast<int32_t>(length);
87 if (pos_ >= fixedSize_) {
88 MEDIA_LOGI("Is eos");
89 return SOURCE_ERROR_EOF;
90 }
91 if (mem->GetBase() == nullptr) {
92 MEDIA_LOGI("Is null mem");
93 return SOURCE_ERROR_IO;
94 }
95 readRet = fread(mem->GetBase(), static_cast<size_t>(length), 1, fd_);
96 if (ferror(fd_)) {
97 MEDIA_LOGI("Failed to call fread");
98 return SOURCE_ERROR_IO;
99 }
100 if (readRet == 0) {
101 realLen = static_cast<int32_t>(fixedSize_ - pos_);
102 }
103 MEDIA_LOGD("length %{public}u realLen %{public}d", length, realLen);
104 pos_ += realLen;
105 return realLen;
106 }
107
ReadAt(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)108 int32_t MediaDataSourceTestSeekable::ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
109 {
110 (void)pos;
111 (void)length;
112 (void)mem;
113 return MSERR_OK;
114 }
115
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)116 int32_t MediaDataSourceTestSeekable::ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
117 {
118 (void)length;
119 (void)mem;
120 return MSERR_OK;
121 }
122
GetSize(int64_t & size)123 int32_t MediaDataSourceTestSeekable::GetSize(int64_t &size)
124 {
125 size = fixedSize_;
126 return MSERR_OK;
127 }
128 } // namespace Media
129 } // namespace OHOS
130