file.c 3.02 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Buffered file io for ffmpeg system
3
 * Copyright (c) 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21 22

#include "libavutil/avstring.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
23
#include "avformat.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
24
#include <fcntl.h>
25
#if HAVE_SETMODE
26 27
#include <io.h>
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
28
#include <unistd.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
29
#include <sys/time.h>
30
#include <stdlib.h>
31
#include "os_support.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
32 33 34 35 36 37 38 39 40


/* standard file protocol */

static int file_open(URLContext *h, const char *filename, int flags)
{
    int access;
    int fd;

41
    av_strstart(filename, "file:", &filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
42

43 44 45
    if (flags & URL_RDWR) {
        access = O_CREAT | O_TRUNC | O_RDWR;
    } else if (flags & URL_WRONLY) {
Fabrice Bellard's avatar
Fabrice Bellard committed
46 47 48 49
        access = O_CREAT | O_TRUNC | O_WRONLY;
    } else {
        access = O_RDONLY;
    }
50
#ifdef O_BINARY
Fabrice Bellard's avatar
Fabrice Bellard committed
51 52
    access |= O_BINARY;
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
53 54
    fd = open(filename, access, 0666);
    if (fd < 0)
55
        return AVERROR(ENOENT);
56
    h->priv_data = (void *) (intptr_t) fd;
Fabrice Bellard's avatar
Fabrice Bellard committed
57 58 59 60 61
    return 0;
}

static int file_read(URLContext *h, unsigned char *buf, int size)
{
62
    int fd = (intptr_t) h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
63 64 65 66 67
    return read(fd, buf, size);
}

static int file_write(URLContext *h, unsigned char *buf, int size)
{
68
    int fd = (intptr_t) h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
69 70 71 72
    return write(fd, buf, size);
}

/* XXX: use llseek */
73
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
74
{
75
    int fd = (intptr_t) h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
76 77 78 79 80
    return lseek(fd, pos, whence);
}

static int file_close(URLContext *h)
{
81
    int fd = (intptr_t) h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
82 83 84
    return close(fd);
}

85 86
static int file_get_handle(URLContext *h)
{
87
    return (intptr_t) h->priv_data;
88 89
}

Fabrice Bellard's avatar
Fabrice Bellard committed
90 91 92 93 94 95 96
URLProtocol file_protocol = {
    "file",
    file_open,
    file_read,
    file_write,
    file_seek,
    file_close,
97
    .url_get_file_handle = file_get_handle,
Fabrice Bellard's avatar
Fabrice Bellard committed
98 99 100 101 102 103 104
};

/* pipe protocol */

static int pipe_open(URLContext *h, const char *filename, int flags)
{
    int fd;
105
    char *final;
106
    av_strstart(filename, "pipe:", &filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
107

108 109
    fd = strtol(filename, &final, 10);
    if((filename == final) || *final ) {/* No digits found, or something like 10ab */
110 111 112 113 114
        if (flags & URL_WRONLY) {
            fd = 1;
        } else {
            fd = 0;
        }
115
    }
116
#if HAVE_SETMODE
117 118
    setmode(fd, O_BINARY);
#endif
119
    h->priv_data = (void *) (intptr_t) fd;
120
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
121 122 123 124 125 126
    return 0;
}

URLProtocol pipe_protocol = {
    "pipe",
    pipe_open,
127 128
    file_read,
    file_write,
129
    .url_get_file_handle = file_get_handle,
Fabrice Bellard's avatar
Fabrice Bellard committed
130
};