• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "file_source.h"
16 
17 #include "securec.h"
18 #include "intell_voice_log.h"
19 #include "memory_guard.h"
20 
21 using namespace OHOS::IntellVoiceUtils;
22 #define LOG_TAG "FileSource"
23 
24 namespace OHOS {
25 namespace IntellVoiceEngine {
FileSource(uint32_t minBufferSize,uint32_t bufferCnt,const std::string & filePath,std::unique_ptr<FileSourceListener> listener)26 FileSource::FileSource(uint32_t minBufferSize, uint32_t bufferCnt, const std::string &filePath,
27     std::unique_ptr<FileSourceListener> listener)
28     : minBufferSize_(minBufferSize), bufferCnt_(bufferCnt), filePath_(filePath), listener_(std::move(listener))
29 {
30 }
31 
~FileSource()32 FileSource::~FileSource()
33 {
34     if (isReading_.load()) {
35         isReading_.store(false);
36         readThread_.join();
37     }
38 }
39 
Start()40 bool FileSource::Start()
41 {
42     INTELL_VOICE_LOG_INFO("enter");
43     if (listener_ == nullptr) {
44         INTELL_VOICE_LOG_ERROR("listener_ is nullptr");
45         return false;
46     }
47 
48     if (minBufferSize_ == 0) {
49         INTELL_VOICE_LOG_ERROR("minBufferSize_ is invalid");
50         return false;
51     }
52 
53     buffer_ = std::shared_ptr<uint8_t>(new uint8_t[minBufferSize_], [](uint8_t *p) { delete[] p; });
54     if (buffer_ == nullptr) {
55         INTELL_VOICE_LOG_ERROR("malloc buffer failed");
56         return false;
57     }
58 
59     fileIn_ = std::make_unique<std::ifstream>(filePath_, std::ios::binary);
60     if (fileIn_ == nullptr) {
61         INTELL_VOICE_LOG_ERROR("open input file failed");
62         return false;
63     }
64 
65     fileIn_->seekg(0, fileIn_->end);
66     uint32_t size = static_cast<uint32_t>(fileIn_->tellg());
67     if (size < minBufferSize_ * bufferCnt_) {
68         INTELL_VOICE_LOG_ERROR("file size:%{public}u is smaller than required", size);
69         fileIn_->close();
70         fileIn_ = nullptr;
71         return false;
72     }
73 
74     isReading_.store(true);
75     std::thread t1(std::bind(&FileSource::ReadThread, this));
76     readThread_ = std::move(t1);
77     return true;
78 }
79 
ReadThread()80 void FileSource::ReadThread()
81 {
82     uint32_t readCnt = 0;
83     bool isError = true;
84     if (fileIn_ == nullptr) {
85         INTELL_VOICE_LOG_ERROR("fileIn_ is nullptr");
86         return;
87     }
88 
89     fileIn_->seekg(0, fileIn_->beg);
90 
91     while (isReading_.load()) {
92         if (readCnt >= bufferCnt_) {
93             INTELL_VOICE_LOG_INFO("finish reading data");
94             isError = false;
95             break;
96         }
97 
98         if (!(fileIn_->read(reinterpret_cast<char *>(buffer_.get()), minBufferSize_))) {
99             INTELL_VOICE_LOG_ERROR("failed to read file");
100             break;
101         }
102 
103         if (listener_ != nullptr) {
104             listener_->fileBufferCb_(buffer_.get(), minBufferSize_);
105         }
106 
107         ++readCnt;
108     }
109 
110     if (listener_ != nullptr) {
111         listener_->fileEndCb_(isError);
112     }
113 }
114 
Stop()115 void FileSource::Stop()
116 {
117     INTELL_VOICE_LOG_INFO("enter");
118     MemoryGuard memoryGuard;
119     if (isReading_.load()) {
120         isReading_.store(false);
121         readThread_.join();
122     }
123 
124     if (fileIn_ != nullptr) {
125         fileIn_->close();
126         fileIn_ = nullptr;
127     }
128 
129     buffer_ = nullptr;
130     listener_ = nullptr;
131 }
132 }
133 }