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