media: Refactor FileInputDataSource logic
sync with
@@ -50,11 +50,7 @@ FileInputDataSource::~FileInputDataSource()
bool FileInputDataSource::open()
{
- if (mFp) {
- meddbg("FileInputDataSource::open : file is already open\n");
- return false;
- }
-
+ if (!mFp) {
setAudioType(utils::getAudioTypeFromPath(mDataPath));
switch (getAudioType()) {
@@ -74,24 +70,44 @@ bool FileInputDataSource::open()
}
mFp = fopen(mDataPath.c_str(), "rb");
- medvdbg("FileInputDataSource : %s : %p\n", mDataPath.c_str(), mFp);
+ if (mFp) {
+ medvdbg("file open success\n");
+ return true;
+ } else {
+ meddbg("file open failed error : %d\n", errno);
+ return false;
+ }
+ }
- return isPrepare();
+ /** return true if mFp is not null, because it means it using now */
+ return true;
}
bool FileInputDataSource::close()
{
- if (mFp && fclose(mFp) != EOF) {
+ if (mFp) {
+ int ret = fclose(mFp);
+ if (ret == OK) {
mFp = nullptr;
+ medvdbg("close success!!\n");
return true;
+ } else {
+ meddbg("close failed ret : %d error : %d\n", ret, errno);
+ return false;
+ }
}
+ meddbg("close failed, mFp is nullptr!!\n");
return false;
}
bool FileInputDataSource::isPrepare()
{
- return (mFp != nullptr);
+ if (mFp == nullptr) {
+ meddbg("mFp is null\n");
+ return false;
+ }
+ return true;
}
size_t FileInputDataSource::read(unsigned char *buf, size_t size)
@@ -99,6 +115,12 @@ size_t FileInputDataSource::read(unsigned char *buf, size_t size)
size_t ret;
size_t readSize = size;
std::shared_ptr<Decoder> decoder = getDecoder();
+
+ if (!buf) {
+ meddbg("buf is nullptr, hence return 0\n");
+ return 0;
+ }
+
if (decoder) {
size_t frames = 0;
/* TODO Currently it just return frames which decoded only ONCE if available */
