• 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 
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_I("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_I("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, fd is %{public}d",
57         strerror(errno), fd_);
58     int32_t size = read(fd_, buf, bufSize);
59     FALSE_RETURN_V_MSG_E(size >= 0, -1, "failed to read, %{public}s", strerror(errno));
60     pos_ = pos_ + size;
61     return size;
62 }
63 
Write(const uint8_t * buf,int32_t bufSize)64 int32_t DataSinkFd::Write(const uint8_t *buf, int32_t bufSize)
65 {
66     FALSE_RETURN_V_MSG_E(fd_ > 0, -1, "failed to write, fd is %{public}d", fd_);
67     FALSE_RETURN_V_MSG_E(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s, fd is %{public}d",
68         strerror(errno), fd_);
69     int32_t size = write(fd_, buf, bufSize);
70     FALSE_RETURN_V_MSG_E(size == bufSize, -1, "failed to write, %{public}s", strerror(errno));
71     pos_ = pos_ + size;
72     end_ = pos_ > end_ ? pos_ : end_;
73     return size;
74 }
75 
Seek(int64_t offset,int whence)76 int64_t DataSinkFd::Seek(int64_t offset, int whence)
77 {
78     switch (whence) {
79         case SEEK_SET:
80             pos_ = offset;
81             break;
82         case SEEK_CUR:
83             pos_ = pos_ + offset;
84             break;
85         case SEEK_END:
86             pos_ = end_ + offset;
87             break;
88         default:
89             pos_ = offset;
90             break;
91     }
92     return pos_;
93 }
94 
GetCurrentPosition() const95 int64_t DataSinkFd::GetCurrentPosition() const
96 {
97     return pos_;
98 }
99 
CanRead()100 bool DataSinkFd::CanRead()
101 {
102     return isCanRead_;
103 }
104 } // namespace Media
105 } // namespace OHOS