Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8c8ddea authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "MediaMuxer: add wrapper for MediaMuxer constructor"

parents daa3ff8b 022b52f6
Loading
Loading
Loading
Loading
+7 −3
Original line number Original line Diff line number Diff line
@@ -78,10 +78,14 @@ static int muxing(
    int fd = open(outputFileName, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
    int fd = open(outputFileName, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);


    if (fd < 0) {
    if (fd < 0) {
        ALOGE("couldn't open file");
        ALOGE("couldn't open output file %s", outputFileName);
        return fd;
        return 1;
    }
    sp<MediaMuxer> muxer = MediaMuxer::create(fd, container);
    if (muxer == nullptr) {
        fprintf(stderr, "unable to instantiate muxer for format %d\n", container);
        return 1;
    }
    }
    sp<MediaMuxer> muxer = new MediaMuxer(fd, container);
    close(fd);
    close(fd);


    size_t trackCount = extractor->countTracks();
    size_t trackCount = extractor->countTracks();
+5 −1
Original line number Original line Diff line number Diff line
@@ -308,7 +308,11 @@ status_t MediaAppender::start() {
        ALOGE("MediaAppender::start() is called in invalid state %d", mState);
        ALOGE("MediaAppender::start() is called in invalid state %d", mState);
        return INVALID_OPERATION;
        return INVALID_OPERATION;
    }
    }
    mMuxer = new (std::nothrow) MediaMuxer(mFd, mFormat);
    mMuxer = MediaMuxer::create(mFd, mFormat);
    if (mMuxer == nullptr) {
        ALOGE("MediaMuxer::create failed");
        return INVALID_OPERATION;
    }
    for (const auto& n : mFmtIndexMap) {
    for (const auto& n : mFmtIndexMap) {
        ssize_t muxIndex = mMuxer->addTrack(n.second);
        ssize_t muxIndex = mMuxer->addTrack(n.second);
        if (muxIndex < 0) {
        if (muxIndex < 0) {
+24 −0
Original line number Original line Diff line number Diff line
@@ -46,6 +46,30 @@ static bool isMp4Format(MediaMuxer::OutputFormat format) {
           format == MediaMuxer::OUTPUT_FORMAT_HEIF;
           format == MediaMuxer::OUTPUT_FORMAT_HEIF;
}
}


MediaMuxer* MediaMuxer::create(int fd, OutputFormat format) {
    bool isInputValid = true;
    if (isMp4Format(format)) {
        isInputValid = MPEG4Writer::isFdOpenModeValid(fd);
    } else if (format == OUTPUT_FORMAT_WEBM) {
        isInputValid = WebmWriter::isFdOpenModeValid(fd);
    } else if (format == OUTPUT_FORMAT_OGG) {
        isInputValid = OggWriter::isFdOpenModeValid(fd);
    } else {
        ALOGE("MediaMuxer does not support output format %d", format);
        return nullptr;
    }
    if (!isInputValid) {
        ALOGE("File descriptor is not suitable for format %d", format);
        return nullptr;
    }

    MediaMuxer *muxer = new (std::nothrow) MediaMuxer(fd, (MediaMuxer::OutputFormat)format);
    if (muxer == nullptr) {
        ALOGE("Failed to create writer object");
    }
    return muxer;
}

MediaMuxer::MediaMuxer(int fd, OutputFormat format)
MediaMuxer::MediaMuxer(int fd, OutputFormat format)
    : mFormat(format),
    : mFormat(format),
      mState(UNINITIALIZED) {
      mState(UNINITIALIZED) {
+8 −0
Original line number Original line Diff line number Diff line
@@ -48,6 +48,14 @@ struct NuMediaExtractor;
// deleting the output file after stop.
// deleting the output file after stop.
struct MediaMuxer : public MediaMuxerBase {
struct MediaMuxer : public MediaMuxerBase {
public:
public:
    /**
     * Creates the muxer for a given output format.
     * @param fd : file descriptor of the output file.
     * @param format : output format of the muxer. e.g.: webm/mp4/ogg
     * @return writer's object or nullptr if error.
     */
    static MediaMuxer* create(int fd, OutputFormat format);

    // Construct the muxer with the file descriptor. Note that the MediaMuxer
    // Construct the muxer with the file descriptor. Note that the MediaMuxer
    // will close this file at stop().
    // will close this file at stop().
    MediaMuxer(int fd, OutputFormat format);
    MediaMuxer(int fd, OutputFormat format);
+23 −0
Original line number Original line Diff line number Diff line
@@ -31,6 +31,29 @@ struct MediaWriter : public RefBase {
          mMaxFileDurationLimitUs(0) {
          mMaxFileDurationLimitUs(0) {
    }
    }


    // Returns true if the file descriptor is opened using a mode
    // which meets minimum writer/muxer requirements.
    static bool isFdOpenModeValid(int fd) {
        // check for invalid file descriptor.
        int flags = fcntl(fd, F_GETFL);
        if (flags == -1) {
            ALOGE("Invalid File Status Flags and/or mode : %d", flags);
            return false;
        }
        // fd must be in read-write mode or write-only mode.
        if ((flags & (O_RDWR | O_WRONLY)) == 0) {
            ALOGE("File must be writable");
            return false;
        }
        // Verify fd is seekable
        off64_t off = lseek64(fd, 0, SEEK_SET);
        if (off < 0) {
            ALOGE("File descriptor is not seekable");
            return false;
        }
        return true;
    }

    virtual status_t addSource(const sp<MediaSource> &source) = 0;
    virtual status_t addSource(const sp<MediaSource> &source) = 0;
    virtual bool reachedEOS() = 0;
    virtual bool reachedEOS() = 0;
    virtual status_t start(MetaData *params = NULL) = 0;
    virtual status_t start(MetaData *params = NULL) = 0;
Loading