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 "DataSinkFd"
17
18 #include "data_sink_fd.h"
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include "common/log.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_MUXER, "HiStreamer" };
25 }
26
27 namespace OHOS {
28 namespace Media {
DataSinkFd(int32_t fd)29 DataSinkFd::DataSinkFd(int32_t fd) : fd_(dup(fd)), pos_(0), end_(-1)
30 {
31 MEDIA_LOG_D("dup fd is %{public}d", fd_);
32 end_ = lseek(fd_, 0, SEEK_END);
33 if (lseek(fd_, 0, SEEK_SET) < 0) {
34 MEDIA_LOG_E("failed to construct, fd is %{public}d, error is %{public}s", fd_, strerror(errno));
35 }
36 uint32_t fdPermission = static_cast<uint32_t>(fcntl(fd_, F_GETFL, 0));
37 isCanRead_ = (fdPermission & O_RDWR) == O_RDWR;
38 }
39
~DataSinkFd()40 DataSinkFd::~DataSinkFd()
41 {
42 if (fd_ > 0) {
43 MEDIA_LOG_D("close fd is %{public}d", fd_);
44 MEDIA_LOG_I("current pos is: " PUBLIC_LOG_U64 ", file end at: " PUBLIC_LOG_U64, pos_, end_);
45 close(fd_);
46 fd_ = -1;
47 }
48 }
49
Read(uint8_t * buf,int32_t bufSize)50 int32_t DataSinkFd::Read(uint8_t *buf, int32_t bufSize)
51 {
52 FALSE_RETURN_V_MSG_E(fd_ > 0, -1, "failed to read, fd is %{public}d", fd_);
53 if (pos_ >= end_) {
54 return 0;
55 }
56 FALSE_RETURN_V_MSG_E(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
57 int32_t size = read(fd_, buf, bufSize);
58 FALSE_RETURN_V_MSG_E(size >= 0, -1, "failed to read, %{public}s", strerror(errno));
59 pos_ = pos_ + size;
60 return size;
61 }
62
Write(const uint8_t * buf,int32_t bufSize)63 int32_t DataSinkFd::Write(const uint8_t *buf, int32_t bufSize)
64 {
65 FALSE_RETURN_V_MSG_E(fd_ > 0, -1, "failed to write, fd is %{public}d", fd_);
66 FALSE_RETURN_V_MSG_E(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
67 int32_t size = write(fd_, buf, bufSize);
68 FALSE_RETURN_V_MSG_E(size == bufSize, -1, "failed to write, %{public}s", strerror(errno));
69 pos_ = pos_ + size;
70 end_ = pos_ > end_ ? pos_ : end_;
71 return size;
72 }
73
Seek(int64_t offset,int whence)74 int64_t DataSinkFd::Seek(int64_t offset, int whence)
75 {
76 switch (whence) {
77 case SEEK_SET:
78 pos_ = offset;
79 break;
80 case SEEK_CUR:
81 pos_ = pos_ + offset;
82 break;
83 case SEEK_END:
84 pos_ = end_ + offset;
85 break;
86 default:
87 pos_ = offset;
88 break;
89 }
90 return pos_;
91 }
92
GetCurrentPosition() const93 int64_t DataSinkFd::GetCurrentPosition() const
94 {
95 return pos_;
96 }
97
CanRead()98 bool DataSinkFd::CanRead()
99 {
100 return isCanRead_;
101 }
102 } // namespace Media
103 } // namespace OHOS