• 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 #ifndef FILE_SOURCE_H
16 #define FILE_SOURCE_H
17 
18 #include <memory>
19 #include <thread>
20 #include <functional>
21 #include <atomic>
22 #include <fstream>
23 #include <string>
24 
25 namespace OHOS {
26 namespace IntellVoiceEngine {
27 using OnFileBufferCb = std::function<void(uint8_t *buffer, uint32_t size)>;
28 using OnFileEndCb = std::function<void(bool isError)>;
29 
30 struct FileSourceListener {
FileSourceListenerFileSourceListener31     FileSourceListener(OnFileBufferCb fileBufferCb, OnFileEndCb fileEndCb)
32         : fileBufferCb_(fileBufferCb), fileEndCb_(fileEndCb) {}
33     OnFileBufferCb fileBufferCb_;
34     OnFileEndCb fileEndCb_;
35 };
36 
37 class FileSource {
38 public:
39     FileSource(uint32_t minBufferSize, uint32_t bufferCnt, const std::string &filePath,
40         std::unique_ptr<FileSourceListener> listener);
41     ~FileSource();
42     bool Start();
43     void Stop();
44 
45 private:
46     void ReadThread();
47     bool Read();
48 
49 private:
50     uint32_t minBufferSize_ = 0;
51     uint32_t bufferCnt_ = 0;
52     std::string filePath_;
53     std::unique_ptr<FileSourceListener> listener_ = nullptr;
54     std::atomic<bool> isReading_ = false;
55     std::thread readThread_;
56     std::unique_ptr<std::ifstream> fileIn_ = nullptr;
57     std::shared_ptr<uint8_t> buffer_ = nullptr;
58 };
59 }
60 }
61 #endif