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