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