Revert "Remove IDataSource dependency from DataSource."

This reverts commit 514674257ef6954a7479ac93a26a07061f54462d.

The original change Iaba6d9be is no longer needed since we
separated out DataSourceBase from DataSource. Now, DataSource
may have a method relying on a class from libmedia, IDataSource.

Test: checked the correct wrapping path with 'dumpsys media.extractor'
Bug: 72869975

Change-Id: Ib0d5ca863fa70a4a96f97b3377c209e1f288f443
diff --git a/media/libmediaplayerservice/nuplayer/GenericSource.cpp b/media/libmediaplayerservice/nuplayer/GenericSource.cpp
index 511f46f..0cf5b9d 100644
--- a/media/libmediaplayerservice/nuplayer/GenericSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/GenericSource.cpp
@@ -27,6 +27,7 @@
 #include <media/MediaBufferHolder.h>
 #include <media/MediaExtractor.h>
 #include <media/MediaSource.h>
+#include <media/IMediaExtractorService.h>
 #include <media/IMediaHTTPService.h>
 #include <media/stagefright/foundation/ABuffer.h>
 #include <media/stagefright/foundation/ADebug.h>
@@ -161,30 +162,15 @@
 
 status_t NuPlayer::GenericSource::initFromDataSource() {
     sp<IMediaExtractor> extractor;
-    CHECK(mDataSource != NULL || mFd != -1);
+    CHECK(mDataSource != NULL);
     sp<DataSource> dataSource = mDataSource;
-    const int fd = mFd;
-    const int64_t offset = mOffset;
-    const int64_t length = mLength;
 
     mLock.unlock();
     // This might take long time if data source is not reliable.
-    if (dataSource != nullptr) {
-        extractor = MediaExtractorFactory::Create(dataSource, NULL /* mime */);
-    } else {
-        extractor = MediaExtractorFactory::CreateFromFd(
-                fd, offset, length, NULL /* mime */, &dataSource);
-    }
-
-    if (dataSource == nullptr) {
-        ALOGE("initFromDataSource, failed to create data source!");
-        mLock.lock();
-        return UNKNOWN_ERROR;
-    }
+    extractor = MediaExtractorFactory::Create(dataSource, NULL);
 
     if (extractor == NULL) {
         ALOGE("initFromDataSource, cannot create extractor!");
-        mLock.lock();
         return UNKNOWN_ERROR;
     }
 
@@ -193,13 +179,10 @@
     size_t numtracks = extractor->countTracks();
     if (numtracks == 0) {
         ALOGE("initFromDataSource, source has no track!");
-        mLock.lock();
         return UNKNOWN_ERROR;
     }
 
     mLock.lock();
-    mFd = -1;
-    mDataSource = dataSource;
     mFileMeta = fileMeta;
     if (mFileMeta != NULL) {
         int64_t duration;
@@ -403,15 +386,51 @@
             if (!mDisconnected) {
                 mDataSource = dataSource;
             }
+        } else {
+            if (property_get_bool("media.stagefright.extractremote", true) &&
+                    !FileSource::requiresDrm(mFd, mOffset, mLength, nullptr /* mime */)) {
+                sp<IBinder> binder =
+                        defaultServiceManager()->getService(String16("media.extractor"));
+                if (binder != nullptr) {
+                    ALOGD("FileSource remote");
+                    sp<IMediaExtractorService> mediaExService(
+                            interface_cast<IMediaExtractorService>(binder));
+                    sp<IDataSource> source =
+                            mediaExService->makeIDataSource(mFd, mOffset, mLength);
+                    ALOGV("IDataSource(FileSource): %p %d %lld %lld",
+                            source.get(), mFd, (long long)mOffset, (long long)mLength);
+                    if (source.get() != nullptr) {
+                        mDataSource = CreateDataSourceFromIDataSource(source);
+                        if (mDataSource != nullptr) {
+                            // Close the local file descriptor as it is not needed anymore.
+                            close(mFd);
+                            mFd = -1;
+                        }
+                    } else {
+                        ALOGW("extractor service cannot make data source");
+                    }
+                } else {
+                    ALOGW("extractor service not running");
+                }
+            }
+            if (mDataSource == nullptr) {
+                ALOGD("FileSource local");
+                mDataSource = new FileSource(mFd, mOffset, mLength);
+            }
+            // TODO: close should always be done on mFd, see the lines following
+            // CreateDataSourceFromIDataSource above,
+            // and the FileSource constructor should dup the mFd argument as needed.
+            mFd = -1;
         }
-        if (mFd == -1 && mDataSource == NULL) {
+
+        if (mDataSource == NULL) {
             ALOGE("Failed to create data source!");
             notifyPreparedAndCleanup(UNKNOWN_ERROR);
             return;
         }
     }
 
-    if (mDataSource != nullptr && mDataSource->flags() & DataSource::kIsCachingDataSource) {
+    if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
         mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
     }