• 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 #include "data_sink_fd.h"
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include "avcodec_log.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "DataSinkFd"};
23 }
24 
25 namespace OHOS {
26 namespace MediaAVCodec {
27 namespace Plugin {
DataSinkFd(int32_t fd)28 DataSinkFd::DataSinkFd(int32_t fd) : fd_(dup(fd)), pos_(0), end_(-1)
29 {
30     AVCODEC_LOGD("dup fd is %{public}d", fd_);
31     end_ = lseek(fd_, 0, SEEK_END);
32     if (lseek(fd_, 0, SEEK_SET) < 0) {
33         AVCODEC_LOGE("failed to construct, fd is %{public}d, error is %{public}s", fd_, strerror(errno));
34     }
35 }
36 
~DataSinkFd()37 DataSinkFd::~DataSinkFd()
38 {
39     if (fd_ > 0) {
40         AVCODEC_LOGD("close fd is %{public}d", fd_);
41         close(fd_);
42         fd_ = -1;
43     }
44 }
45 
Read(uint8_t * buf,int32_t bufSize)46 int32_t DataSinkFd::Read(uint8_t *buf, int32_t bufSize)
47 {
48     CHECK_AND_RETURN_RET_LOG(fd_ > 0, -1, "failed to read, fd is  %{public}d", fd_);
49     if (pos_ >= end_) {
50         return 0;
51     }
52     CHECK_AND_RETURN_RET_LOG(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
53     int32_t size = read(fd_, buf, bufSize);
54     CHECK_AND_RETURN_RET_LOG(size >= 0, -1, "failed to read, %{public}s", strerror(errno));
55     pos_ = pos_ + size;
56     return size;
57 }
58 
Write(uint8_t * buf,int32_t bufSize)59 int32_t DataSinkFd::Write(uint8_t *buf, int32_t bufSize)
60 {
61     CHECK_AND_RETURN_RET_LOG(fd_ > 0, -1, "failed to write, fd is  %{public}d", fd_);
62     CHECK_AND_RETURN_RET_LOG(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
63     int32_t size = write(fd_, buf, bufSize);
64     CHECK_AND_RETURN_RET_LOG(size == bufSize, -1, "failed to write, %{public}s", strerror(errno));
65     pos_ = pos_ + size;
66     end_ = pos_ > end_ ? pos_ : end_;
67     return size;
68 }
69 
Seek(int64_t offset,int whence)70 int64_t DataSinkFd::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 
GetPos() const89 int64_t DataSinkFd::GetPos() const
90 {
91     return pos_;
92 }
93 } // namespace Plugin
94 } // namespace MediaAVCodec
95 } // namespace OHOS