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