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