1 /*
2 * Copyright (C) 2023 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 "common/media_data_source.h"
17 #include <fstream>
18 #include <iostream>
19 #include <sstream>
20 #include <vector>
21 #include "media_data_source_impl.h"
22 #include "media_errors.h"
23 #include "securec.h"
24 #include "foundation/osal/utils/util.h"
25
26 namespace OHOS {
27 namespace Media {
IMediaDataSourceImpl(std::string url,Plugin::Seekable seekable)28 IMediaDataSourceImpl::IMediaDataSourceImpl(std::string url, Plugin::Seekable seekable)
29 : url_(std::move(url)), readPos_(0), seekable_(seekable)
30 {
31 size_ = ReadDataFromFile();
32 }
33
34
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos)35 int32_t IMediaDataSourceImpl::ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos)
36 {
37 readPos_ = pos;
38 return GetDataFromSource(mem, length);
39 }
40
ReadAt(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)41 int32_t IMediaDataSourceImpl::ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
42 {
43 readPos_ = pos;
44 return GetDataFromSource(mem, length);
45 }
46
ReadDataFromFile()47 int IMediaDataSourceImpl::ReadDataFromFile()
48 {
49 std::string dataFullPath;
50 if (OHOS::Media::OSAL::ConvertFullPath(url_, dataFullPath) && !dataFullPath.empty()) {
51 url_ = dataFullPath;
52 }
53 std::fstream fs(url_);
54 if (!fs.is_open()) {
55 std::cout << "failed to open " << url_ << '\n';
56 return 0;
57 }
58 std::stringstream ss;
59 while (!fs.eof()) {
60 std::string s;
61 fs >> s;
62 ss << s;
63 }
64 std::string data = ss.str();
65 uint32_t formatData;
66 const char* split = ",";
67 char* p = strtok(const_cast<char *>(data.c_str()), split);
68 while (p != nullptr) {
69 if (sscanf_s(p, "%x", &formatData) != -1) {
70 data_.push_back(formatData);
71 p = strtok(nullptr, split);
72 }
73 }
74 return data_.size() * 4; // 4
75 }
76
GetDataFromSource(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length)77 uint32_t IMediaDataSourceImpl::GetDataFromSource(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length)
78 {
79 if (readPos_ >= size_) {
80 return SOURCE_ERROR_EOF;
81 }
82 if (memcpy_s(mem->GetBase(), mem->GetSize(), &data_[0] + readPos_ / sizeof(uint32_t), length) != EOK) {
83 return SOURCE_ERROR_IO;
84 }
85 readPos_ += length;
86 return length;
87 }
88
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)89 int32_t IMediaDataSourceImpl::ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
90 {
91 return GetDataFromSource(mem, length);
92 }
93
GetSize(int64_t & size)94 int32_t IMediaDataSourceImpl::GetSize(int64_t &size)
95 {
96 if (seekable_ == Plugin::Seekable::UNSEEKABLE) {
97 size = -1;
98 return MSERR_OK;
99 }
100 size = size_;
101 return MSERR_OK;
102 }
103 }
104 }
105