1 /*
2 * Copyright (c) 2021-2021 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 #define HST_LOG_TAG "FileFdSinkPlugin"
16
17 #include "file_fd_sink_plugin.h"
18 #ifdef WIN32
19 #include <fcntl.h>
20 #else
21 #include <sys/types.h>
22 #endif
23 #include <unistd.h>
24 #include "foundation/log.h"
25
26 namespace OHOS {
27 namespace Media {
28 namespace Plugin {
29 namespace FileSink {
FileFdSinkPluginCreator(const std::string & name)30 std::shared_ptr<OutputSinkPlugin> FileFdSinkPluginCreator(const std::string& name)
31 {
32 return std::make_shared<FileFdSinkPlugin>(name);
33 }
34
FileFdSinkRegister(const std::shared_ptr<Register> & reg)35 Status FileFdSinkRegister(const std::shared_ptr<Register>& reg)
36 {
37 OutputSinkPluginDef definition;
38 definition.protocolType = ProtocolType::FD;
39 definition.name = "file_fd_sink";
40 definition.description = "file fd sink";
41 definition.rank = 100; // 100
42 definition.protocolType = ProtocolType::FD;
43 definition.creator = FileFdSinkPluginCreator;
44 return reg->AddPlugin(definition);
45 }
46
__anon767bea9d0102null47 PLUGIN_DEFINITION(FileFdSink, LicenseType::APACHE_V2, FileFdSinkRegister, [] {});
48
FileFdSinkPlugin(std::string name)49 FileFdSinkPlugin::FileFdSinkPlugin(std::string name)
50 : OutputSinkPlugin(std::move(name)), fd_(-1), seekable_(Seekable::SEEKABLE)
51 {
52 }
53
~FileFdSinkPlugin()54 FileFdSinkPlugin::~FileFdSinkPlugin()
55 {
56 CloseFd();
57 }
58
SetSink(const MediaSink & sink)59 Status FileFdSinkPlugin::SetSink(const MediaSink& sink)
60 {
61 FALSE_RETURN_V((sink.GetProtocolType() == ProtocolType::FD && sink.GetFd() != -1), Status::ERROR_INVALID_DATA);
62 fd_ = sink.GetFd();
63 return Status::OK;
64 }
65
GetSeekable()66 Seekable FileFdSinkPlugin::GetSeekable()
67 {
68 return seekable_;
69 }
70
SeekTo(uint64_t offset)71 Status FileFdSinkPlugin::SeekTo(uint64_t offset)
72 {
73 FALSE_RETURN_V_MSG_E(fd_ != -1, Status::ERROR_WRONG_STATE, "no valid fd.");
74 int64_t ret = lseek(fd_, offset, SEEK_SET);
75 if (ret != -1) {
76 MEDIA_LOG_I("now seek to " PUBLIC_LOG_D64, ret);
77 return Status::OK;
78 }
79 MEDIA_LOG_E("seek to " PUBLIC_LOG_U64 " failed due to " PUBLIC_LOG_S, offset, strerror(errno));
80 return Status::ERROR_UNKNOWN;
81 }
82
Write(const std::shared_ptr<Buffer> & buffer)83 Status FileFdSinkPlugin::Write(const std::shared_ptr<Buffer>& buffer)
84 {
85 MEDIA_LOG_DD("Write into fd");
86 if (buffer == nullptr || buffer->IsEmpty()) {
87 return Status::OK;
88 }
89 auto bufferData = buffer->GetMemory();
90 write(fd_, bufferData->GetReadOnlyData(), bufferData->GetSize());
91 return Status::OK;
92 }
93
Flush()94 Status FileFdSinkPlugin::Flush()
95 {
96 MEDIA_LOG_D("Flush");
97 return Status::OK;
98 }
99
Reset()100 Status FileFdSinkPlugin::Reset()
101 {
102 MEDIA_LOG_D("Reset");
103 ftruncate(fd_, 0);
104 lseek(fd_, 0, SEEK_SET);
105 return Status::OK;
106 }
107
CloseFd()108 void FileFdSinkPlugin::CloseFd()
109 {
110 if (fd_ != -1) {
111 MEDIA_LOG_D("close fd");
112 close(fd_);
113 fd_ = -1;
114 }
115 }
116
Stop()117 Status FileFdSinkPlugin::Stop()
118 {
119 CloseFd();
120 return Status::OK;
121 }
122 } // namespace FileSink
123 } // namespace Plugin
124 } // namespace Media
125 } // namespace OHOS