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(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)77 int32_t MediaDataSourceTestSeekable::ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
78 {
79 CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_INVALID_VAL, "Mem is nullptr");
80 if (pos != pos_) {
81 (void)fseek(fd_, static_cast<long>(pos), SEEK_SET);
82 pos_ = pos;
83 }
84 if (fixedSize_ > 0) {
85 length = static_cast<uint32_t>(fixedSize_);
86 }
87 size_t readRet = 0;
88 CHECK_AND_RETURN_RET_LOG(mem->GetSize() > 0, SOURCE_ERROR_IO, "AVSHMEM length should large than 0");
89 length = std::min(length, static_cast<uint32_t>(mem->GetSize()));
90 int32_t realLen = static_cast<int32_t>(length);
91 if (pos_ >= size_) {
92 MEDIA_LOGI("Is eos");
93 return SOURCE_ERROR_EOF;
94 }
95 if (mem->GetBase() == nullptr) {
96 MEDIA_LOGI("Is null mem");
97 return SOURCE_ERROR_IO;
98 }
99 readRet = fread(mem->GetBase(), static_cast<size_t>(length), 1, fd_);
100 if (ferror(fd_)) {
101 MEDIA_LOGI("Failed to call fread");
102 return SOURCE_ERROR_IO;
103 }
104 if (readRet == 0) {
105 realLen = static_cast<int32_t>(size_ - pos_);
106 }
107 MEDIA_LOGD("length %{public}u realLen %{public}d", length, realLen);
108 return realLen;
109 }
110
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)111 int32_t MediaDataSourceTestSeekable::ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
112 {
113 (void)length;
114 (void)mem;
115 return 0;
116 }
117
GetSize(int64_t & size)118 int32_t MediaDataSourceTestSeekable::GetSize(int64_t &size)
119 {
120 (void)fseek(fd_, 0, SEEK_END);
121 size = static_cast<int64_t>(ftell(fd_));
122 (void)fseek(fd_, 0, SEEK_SET);
123 size_ = size;
124 return MSERR_OK;
125 }
126 } // namespace Media
127 } // namespace OHOS
128