Files
2026-08-24 10:03:24 +08:00

129 lines
3.2 KiB
C++

/*
* Copyright (C) 2014-2025 Amlogic, Inc. All rights reserved.
*
* All information contained herein is Amlogic confidential.
*
* This software is provided to you pursuant to Software License Agreement
* (SLA) with Amlogic Inc ("Amlogic"). This software may be used
* only in accordance with the terms of this agreement.
*
* Redistribution and use in source and binary forms, with or without
* modification is strictly prohibited without prior written permission from
* Amlogic.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_TAG "FileSource"
#include "FileSource.h"
#include <fcntl.h>
#include <unistd.h>
#include <utils/CallStack.h>
#include <string>
#include "IpcDataTypes.h"
#include "SubtitleLog.h"
FileSource::FileSource(int fd, int extFd) {
SUBTITLE_LOGI("mFd=%d, extFd=%d", fd, extFd);
mFd = fd;
if (mFd > 0) {
::lseek(mFd, 0, SEEK_SET);
}
// mExtraFd is used for SUB_IDX subtitle, which has one index file
// and one sub data file
if (extFd > 0) {
mExtraFd = extFd;
}
}
FileSource::~FileSource() {
SUBTITLE_LOGI("mFd=%d", mFd);
if (mFd > 0) {
::close(mFd);
mFd = -1;
}
if (mExtraFd > 0) {
::close(mExtraFd);
}
}
int FileSource::onData(const char* buffer, int len) {
(void)buffer;
(void)len;
return 0;
}
bool FileSource::start() {
SUBTITLE_LOGI_FUNC_IN();
return true;
}
bool FileSource::stop() {
SUBTITLE_LOGI("mFd=%d", mFd);
return true;
}
SubtitleIOType FileSource::type() {
return E_SUBTITLE_FILE;
}
bool FileSource::isFileAvailable() {
SUBTITLE_LOGI_FUNC_IN();
return (mDumpFd > 0);
}
size_t FileSource::lseek(int offSet, int whence) {
SUBTITLE_LOGI_FUNC_IN();
if (mFd > 0) {
return ::lseek(mFd, offSet, whence);
} else {
return 0;
}
}
size_t FileSource::availableDataSize() {
int len = 0;
if (mFd > 0) {
len = ::lseek(mFd, 0L, SEEK_END);
lseek(0, SEEK_SET);
}
return len;
}
size_t FileSource::read(void* buffer, size_t size) {
size_t ret = ::read(mFd, buffer, size);
if (ret < 0) {
SUBTITLE_LOGE("%m");
// Read one more time
if (errno == EINTR || errno == EAGAIN) {
ret = ::read(mFd, buffer, size);
}
}
if (ret != size) {
SUBTITLE_LOGI("requested size=%zu, but ret=%zu", size, ret);
}
return ret;
}
void FileSource::dump(int fd, const char* prefix) {
(void)prefix;
dprintf(fd, "\nFileSource:\n");
}