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 #define HST_LOG_TAG "DataSinkFile"
17 
18 #include "data_sink_file.h"
19 #include <fcntl.h>
20 #include "common/log.h"
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_MUXER, "HiStreamer" };
24 }
25 
26 namespace OHOS {
27 namespace Media {
DataSinkFile(FILE * file)28 DataSinkFile::DataSinkFile(FILE *file) : file_(file), pos_(0), end_(-1), isCanRead_(true)
29 {
30     end_ = fseek(file_, 0L, SEEK_END);
31     if (fseek(file_, 0L, SEEK_SET) < 0) {
32         MEDIA_LOG_E("failed to construct, error is %{public}s", strerror(errno));
33     }
34 }
35 
~DataSinkFile()36 DataSinkFile::~DataSinkFile()
37 {
38     if (file_ != nullptr) {
39     }
40 }
41 
Read(uint8_t * buf,int32_t bufSize)42 int32_t DataSinkFile::Read(uint8_t *buf, int32_t bufSize)
43 {
44     FALSE_RETURN_V_MSG_E(file_ != nullptr, -1, "failed to read, file is nullptr");
45     if (pos_ >= end_) {
46         return 0;
47     }
48 
49     FALSE_RETURN_V_MSG_E(fseek(file_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
50     int32_t size = static_cast<int32_t>(fread(buf, 1, bufSize, file_));
51     FALSE_RETURN_V_MSG_E(size >= 0, -1, "failed to read, %{public}s", strerror(errno));
52 
53     pos_ = pos_ + size;
54     return size;
55 }
56 
Write(const uint8_t * buf,int32_t bufSize)57 int32_t DataSinkFile::Write(const uint8_t *buf, int32_t bufSize)
58 {
59     FALSE_RETURN_V_MSG_E(file_ != nullptr, -1, "failed to read, file is nullptr");
60 
61     FALSE_RETURN_V_MSG_E(fseek(file_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
62     int32_t size = static_cast<int32_t>(fwrite(buf, 1, bufSize, file_));
63     FALSE_RETURN_V_MSG_E(size == bufSize, -1, "failed to write, %{public}s", strerror(errno));
64 
65     pos_ = pos_ + size;
66     end_ = pos_ > end_ ? pos_ : end_;
67     return size;
68 }
69 
Seek(int64_t offset,int whence)70 int64_t DataSinkFile::Seek(int64_t offset, int whence)
71 {
72     switch (whence) {
73         case SEEK_SET:
74             pos_ = offset;
75             break;
76         case SEEK_CUR:
77             pos_ = pos_ + offset;
78             break;
79         case SEEK_END:
80             pos_ = end_ + offset;
81             break;
82         default:
83             pos_ = offset;
84             break;
85     }
86     return pos_;
87 }
88 
GetCurrentPosition() const89 int64_t DataSinkFile::GetCurrentPosition() const
90 {
91     return pos_;
92 }
93 
CanRead()94 bool DataSinkFile::CanRead()
95 {
96     return isCanRead_;
97 }
98 } // namespace Media
99 } // namespace OHOS