Skip to content
Snippets Groups Projects
file.c 3.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
    /*
     * Buffered file io for ffmpeg system
    
     * Copyright (c) 2001 Fabrice Bellard
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * This file is part of FFmpeg.
     *
     * FFmpeg is free software; you can redistribute it and/or
    
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
    
     * version 2.1 of the License, or (at your option) any later version.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * FFmpeg is distributed in the hope that it will be useful,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * You should have received a copy of the GNU Lesser General Public
    
     * License along with FFmpeg; if not, write to the Free Software
    
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     */
    
    
    #include "libavutil/avstring.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include "avformat.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <fcntl.h>
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <unistd.h>
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include <sys/time.h>
    
    #include <stdlib.h>
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    /* standard file protocol */
    
    static int file_open(URLContext *h, const char *filename, int flags)
    {
        int access;
        int fd;
    
    
        av_strstart(filename, "file:", &filename);
    
        if (flags & URL_RDWR) {
            access = O_CREAT | O_TRUNC | O_RDWR;
        } else if (flags & URL_WRONLY) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            access = O_CREAT | O_TRUNC | O_WRONLY;
        } else {
            access = O_RDONLY;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        access |= O_BINARY;
    #endif
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        fd = open(filename, access, 0666);
        if (fd < 0)
    
        h->priv_data = (void *) (intptr_t) fd;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
    }
    
    static int file_read(URLContext *h, unsigned char *buf, int size)
    {
    
        int fd = (intptr_t) h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return read(fd, buf, size);
    }
    
    static int file_write(URLContext *h, unsigned char *buf, int size)
    {
    
        int fd = (intptr_t) h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return write(fd, buf, size);
    }
    
    /* XXX: use llseek */
    
    static int64_t file_seek(URLContext *h, int64_t pos, int whence)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        int fd = (intptr_t) h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return lseek(fd, pos, whence);
    }
    
    static int file_close(URLContext *h)
    {
    
        int fd = (intptr_t) h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return close(fd);
    }
    
    
    static int file_get_handle(URLContext *h)
    {
    
        return (intptr_t) h->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    URLProtocol file_protocol = {
        "file",
        file_open,
        file_read,
        file_write,
        file_seek,
        file_close,
    
        .url_get_file_handle = file_get_handle,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    };
    
    /* pipe protocol */
    
    static int pipe_open(URLContext *h, const char *filename, int flags)
    {
        int fd;
    
        av_strstart(filename, "pipe:", &filename);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        fd = strtol(filename, &final, 10);
        if((filename == final) || *final ) {/* No digits found, or something like 10ab */
    
    Vincent Fourmond's avatar
    Vincent Fourmond committed
            if (flags & URL_WRONLY) {
                fd = 1;
            } else {
                fd = 0;
            }
    
        setmode(fd, O_BINARY);
    #endif
    
        h->priv_data = (void *) (intptr_t) fd;
    
        h->is_streamed = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
    }
    
    URLProtocol pipe_protocol = {
        "pipe",
        pipe_open,
    
        .url_get_file_handle = file_get_handle,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    };