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

241 lines
8.2 KiB
C++

/*
* Copyright (C) 2014-2024 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 "SubtitleExternalDataSource"
#include "ExternalDataSource.h"
#include "IpcDataTypes.h"
#include "SubtitleLog.h"
namespace {
static inline unsigned int make32bitValue(const char *buffer) {
return buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24;
}
static inline std::shared_ptr<char> makeNewSpBuffer(const char *buffer, int size) {
char *copyBuffer = new char[size];
memcpy(copyBuffer, buffer, size);
return std::shared_ptr<char>(copyBuffer, [](char *buf) { delete[] buf; });
}
} // namespace
ExternalDataSource::ExternalDataSource() {
SUBTITLE_LOGI_FUNC_IN();
mDumpFd = -1;
mSegment = std::shared_ptr<BufferSegment>(new BufferSegment());
}
ExternalDataSource::~ExternalDataSource(){SUBTITLE_LOGI_FUNC_IN();}
SubtitleIOType ExternalDataSource::type() {
return E_SUBTITLE_FMQ;
}
bool ExternalDataSource::start() {
mState = E_SOURCE_STARTED;
return true;
}
bool ExternalDataSource::stop() {
mState = E_SOURCE_STOPPED;
mSegment->notifyExit();
return true;
}
size_t ExternalDataSource::availableDataSize() {
int size = 0;
if (mSegment) size += mSegment->availableDataSize();
if (mCurrentItem) size += mCurrentItem->getRemainSize();
return size;
}
size_t ExternalDataSource::read(void *buffer, size_t size) {
size_t read = 0;
// Current design of Parser Read, do not need add lock protection.
// because all the read, is in parser thread.
// We only need add lock here, is for protect access the mCurrentItem's
// member function multi-thread.
// Current Impl do not need lock, if required, then redesign the SegmentBuffer.
// std::unique_lock<std::mutex> autolock(mLock);
// in case of applied size more than 1024*2, such as dvb subtitle,
// and data process error for two reads.
// so add until read applied data then exit.
int retry = 3;
while (read != size && mState == E_SOURCE_STARTED && retry-- > 0) {
if (mCurrentItem && !mCurrentItem->isEmpty()) {
read += mCurrentItem->read_l(((char *)buffer + read), size - read);
if (read == size) break;
} else {
mCurrentItem = mSegment->pop();
}
}
if (isSubtitleDumpAvailable()) {
if (mDumpFd == -1) {
mDumpFd = ::open(DUMP_DATA_FILE_PATH, O_RDWR | O_CREAT, 0666);
SUBTITLE_LOGI("need dump Source: mDumpFd=%d %d", mDumpFd, errno);
}
if (mDumpFd > 0) {
size_t ret = write(mDumpFd, buffer, size);
if (ret != size) {
SUBTITLE_LOGE("wrote %zu/%zu", ret, size);
}
}
}
return read;
}
int ExternalDataSource::onData(const char *buffer, int len) {
if (!buffer) {
SUBTITLE_LOGE("Wrong buffer");
return -1;
}
if (len < 4) {
SUBTITLE_LOGE("Wrong data size, len=%d", len);
return -1;
}
auto type = make32bitValue(buffer);
switch (type) {
case eTypeSubtitleRenderTime: {
static int count = 0;
mRenderTimeUs =
((int64_t)make32bitValue(buffer + 8)) << 32 | make32bitValue(buffer + 4);
if (count++ % 100 == 0) {
// 100 times is around 4 seconds
SUBTITLE_LOGI("mRenderTimeUs: counter=%d, renderPts=%" PRId64, count,
mRenderTimeUs);
}
break;
}
case eTypeSubtitleTotal: {
mTotalSubtitle = make32bitValue(buffer + 4);
SUBTITLE_LOGI("eTypeSubtitleTotal: totalSubtitle=%d", mTotalSubtitle);
break;
}
case eTypeSubtitleStartPts: {
mStartPts = ((int64_t)make32bitValue(buffer + 8)) << 32 | make32bitValue(buffer + 4);
SUBTITLE_LOGI("eTypeSubtitleStartPts: startPts=%" PRId64, mStartPts);
break;
}
case eTypeSubtitleType: {
const auto oldType = mSubtitleType;
mSubtitleType = make32bitValue(buffer + 4);
if (oldType != mSubtitleType) {
SUBTITLE_LOGI("eTypeSubtitleType: subtitleType changed from %d to %d", oldType,
mSubtitleType);
}
break;
}
case eTypeSubtitleData: {
// Mute the log as so many
// SUBTITLE_LOGI("eTypeSubtitleData: len-4=%d", len-4);
if (mTotalSubtitle != -1 || mSubtitleType != -1) {
mSegment->push(makeNewSpBuffer(buffer + 4, len - 4), len - 4);
}
break;
}
case eTypeSubtitleLangString:
mLanguage = buffer + 4;
SUBTITLE_LOGI("eTypeSubtitleLangString: lang=%s", mLanguage.c_str());
break;
case eTypeSubtitleTypeString:
[[fallthrough]];
case eTypeSubtitleResetServ:
[[fallthrough]];
case eTypeSubtitleExitServ:
SUBTITLE_LOGI("unhandled_type: %s", buffer + 4);
break;
default: {
SUBTITLE_LOGE("unknown_type: type=%x, len=%d", type, len);
mSegment->push(makeNewSpBuffer(buffer, len), len);
break;
}
}
notifyInfoChange(type);
return 0;
}
void ExternalDataSource::dump(int fd, const char *prefix) {
dprintf(fd, "%s FastMessageQueueSource:\n", prefix);
{
std::unique_lock<std::mutex> autolock(mLock);
for (auto it = mInfoListeners.begin(); it != mInfoListeners.end(); it++) {
auto wk_listener = (*it);
if (auto lstn = wk_listener.lock())
dprintf(fd, "%s InfoListener: %p\n", prefix, lstn.get());
}
}
dprintf(fd, "%s state:%d\n\n", prefix, mState);
dprintf(fd, "%s Total Subtitle Tracks: %d\n", prefix, mTotalSubtitle);
dprintf(fd, "%s Current Subtitle type: %d\n", prefix, mSubtitleType);
dprintf(fd, "%s VideoStart PTS : %" PRId64 "\n", prefix, mStartPts);
dprintf(fd, "%s Current Video PTS : %" PRId64 "\n", prefix, mRenderTimeUs);
dprintf(fd, "\n%s Current Unconsumed Data Size: %zu\n", prefix,
mSegment->availableDataSize());
}
// Private
// ==========================
void ExternalDataSource::notifyInfoChange(int type) {
if (mState == E_SOURCE_STOPPED) {
SUBTITLE_LOGI("return as already stopped");
return;
}
std::unique_lock<std::mutex> autolock(mLock);
for (auto &it : mInfoListeners) {
auto lstn = it.lock();
if (!lstn) {
SUBTITLE_LOGE("something may be wrong");
continue;
}
switch (type) {
case eTypeSubtitleRenderTime:
lstn->onRenderTimeChanged(mRenderTimeUs);
break;
case eTypeSubtitleTotal:
lstn->onSubtitleChanged(mTotalSubtitle);
break;
case eTypeSubtitleStartPts:
lstn->onRenderStartTimestamp(mStartPts);
break;
case eTypeSubtitleType:
lstn->onTypeChanged(mSubtitleType);
break;
}
}
}