FILE *av_fopen_utf8(const char *path, const char *mode) { int fd; int access; const char *m = mode; switch (*m++) { case 'r': access = O_RDONLY; break; case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break; case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break; default : errno = EINVAL; return NULL; } while (*m) { if (*m == '+') { access &= ~(O_RDONLY | O_WRONLY); access |= O_RDWR; } else if (*m == 'b') { #ifdef O_BINARY access |= O_BINARY; #endif } else if (*m) { errno = EINVAL; return NULL; } m++; } fd = avpriv_open(path, access, 0666); if (fd == -1) return NULL; return fdopen(fd, mode); }
static int file_open(URLContext *h, const char *filename, int flags) { FileContext *c = h->priv_data; int access; int fd; av_strstart(filename, "file:", &filename); if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) { access = O_CREAT | O_RDWR; if (c->trunc) access |= O_TRUNC; } else if (flags & AVIO_FLAG_WRITE) { access = O_CREAT | O_WRONLY; if (c->trunc) access |= O_TRUNC; } else { access = O_RDONLY; } #ifdef O_BINARY access |= O_BINARY; #endif fd = avpriv_open(filename, access, 0666); if (fd == -1) return AVERROR(errno); c->fd = fd; return 0; }
/* Wrapper to work around the lack of mkstemp() on mingw. * Also, tries to create file in /tmp first, if possible. * *prefix can be a character constant; *filename will be allocated internally. * @return file descriptor of opened file (or -1 on error) * and opened file name in **filename. */ int ff_tempfile(const char *prefix, char **filename) { int fd=-1; #if !HAVE_MKSTEMP *filename = tempnam(".", prefix); #else size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */ *filename = av_malloc(len); #endif /* -----common section-----*/ if (*filename == NULL) { av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n"); return -1; } #if !HAVE_MKSTEMP fd = avpriv_open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444); #else snprintf(*filename, len, "/tmp/%sXXXXXX", prefix); fd = mkstemp(*filename); if (fd < 0) { snprintf(*filename, len, "./%sXXXXXX", prefix); fd = mkstemp(*filename); } #endif /* -----common section-----*/ if (fd < 0) { av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename); return -1; } return fd; /* success */ }
static int read_random(uint32_t *dst, const char *file) { #if HAVE_UNISTD_H int fd = avpriv_open(file, O_RDONLY); int err = -1; if (fd == -1) return -1; err = read(fd, dst, sizeof(*dst)); close(fd); return err; #else return -1; #endif }
static int file_open(URLContext *h, const char *filename, int flags) { FileContext *c = h->priv_data; int access; int fd; struct stat st; av_strstart(filename, "file:", &filename); if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) { access = O_CREAT | O_RDWR; if (c->trunc) access |= O_TRUNC; } else if (flags & AVIO_FLAG_WRITE) { access = O_CREAT | O_WRONLY; if (c->trunc) access |= O_TRUNC; } else { access = O_RDONLY | O_SEQUENTIAL; } #ifdef O_BINARY access |= O_BINARY; #endif fd = avpriv_open(filename, access, 0666); if (fd == -1) return AVERROR(errno); c->fd = fd; h->is_streamed = !(flags & AVIO_FLAG_AVOID_FSTAT) && !fstat(fd, &st) && S_ISFIFO(st.st_mode); /* Buffer writes more than the default 32k to improve throughput especially * with networked file systems */ if (!h->is_streamed && flags & AVIO_FLAG_WRITE) h->min_packet_size = h->max_packet_size = 262144; return 0; }
static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device) { AudioData *s = s1->priv_data; int audio_fd; int tmp, err; char *flip = getenv("AUDIO_FLIP_LEFT"); if (is_output) audio_fd = avpriv_open(audio_device, O_WRONLY); else audio_fd = avpriv_open(audio_device, O_RDONLY); if (audio_fd < 0) { av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno)); return AVERROR(EIO); } if (flip && *flip == '1') { s->flip_left = 1; } /* non blocking mode */ if (!is_output) { if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) { av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno)); } } s->frame_size = AUDIO_BLOCK_SIZE; /* select format : favour native format */ err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp); #if HAVE_BIGENDIAN if (tmp & AFMT_S16_BE) { tmp = AFMT_S16_BE; } else if (tmp & AFMT_S16_LE) { tmp = AFMT_S16_LE; } else { tmp = 0; } #else if (tmp & AFMT_S16_LE) { tmp = AFMT_S16_LE; } else if (tmp & AFMT_S16_BE) { tmp = AFMT_S16_BE; } else { tmp = 0; } #endif switch(tmp) { case AFMT_S16_LE: s->codec_id = AV_CODEC_ID_PCM_S16LE; break; case AFMT_S16_BE: s->codec_id = AV_CODEC_ID_PCM_S16BE; break; default: av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n"); close(audio_fd); return AVERROR(EIO); } err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp); if (err < 0) { av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno)); goto fail; } tmp = (s->channels == 2); err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp); if (err < 0) { av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno)); goto fail; } tmp = s->sample_rate; err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp); if (err < 0) { av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno)); goto fail; } s->sample_rate = tmp; /* store real sample rate */ s->fd = audio_fd; return 0; fail: close(audio_fd); return AVERROR(EIO); }
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device) { OSSAudioData *s = s1->priv_data; int audio_fd; int tmp, err; char *flip = getenv("AUDIO_FLIP_LEFT"); if (is_output) audio_fd = avpriv_open(audio_device, O_WRONLY); else audio_fd = avpriv_open(audio_device, O_RDONLY); if (audio_fd < 0) { av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno)); return AVERROR(EIO); } if (flip && *flip == '1') { s->flip_left = 1; } /* non blocking mode */ if (!is_output) { if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) { av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno)); } } s->frame_size = OSS_AUDIO_BLOCK_SIZE; #define CHECK_IOCTL_ERROR(event) \ if (err < 0) { \ av_log(s1, AV_LOG_ERROR, #event ": %s\n", strerror(errno)); \ goto fail; \ } /* select format : favour native format * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to * fail anyway. `err =` kept to eliminate compiler warning. */ err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp); #if HAVE_BIGENDIAN if (tmp & AFMT_S16_BE) { tmp = AFMT_S16_BE; } else if (tmp & AFMT_S16_LE) { tmp = AFMT_S16_LE; } else { tmp = 0; } #else if (tmp & AFMT_S16_LE) { tmp = AFMT_S16_LE; } else if (tmp & AFMT_S16_BE) { tmp = AFMT_S16_BE; } else { tmp = 0; } #endif switch(tmp) { case AFMT_S16_LE: s->codec_id = AV_CODEC_ID_PCM_S16LE; break; case AFMT_S16_BE: s->codec_id = AV_CODEC_ID_PCM_S16BE; break; default: av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n"); close(audio_fd); return AVERROR(EIO); } err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp); CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS) tmp = (s->channels == 2); err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp); CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO) tmp = s->sample_rate; err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp); CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED) s->sample_rate = tmp; /* store real sample rate */ s->fd = audio_fd; return 0; fail: close(audio_fd); return AVERROR(EIO); #undef CHECK_IOCTL_ERROR }