1/* 2 * Copyright (C) 2011 Julien BLACHE <jb@jblache.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19#ifdef HAVE_CONFIG_H 20# include <config.h> 21#endif 22 23#include <stdlib.h> 24 25#include <libavformat/avformat.h> 26 27#include "evbuffer/evbuffer.h" 28#include "logger.h" 29#include "avio_evbuffer.h" 30 31/* 32 * libav AVIO interface for evbuffers 33 */ 34 35#define BUFFER_SIZE 4096 36 37struct avio_evbuffer { 38 struct evbuffer *evbuf; 39 uint8_t *buffer; 40}; 41 42 43static int 44avio_evbuffer_write(void *opaque, uint8_t *buf, int size) 45{ 46 struct avio_evbuffer *ae; 47 int ret; 48 49 ae = (struct avio_evbuffer *)opaque; 50 51 ret = evbuffer_add(ae->evbuf, buf, size); 52 53 return (ret == 0) ? size : -1; 54} 55 56AVIOContext * 57avio_evbuffer_open(struct evbuffer *evbuf) 58{ 59 struct avio_evbuffer *ae; 60 AVIOContext *s; 61 62 ae = (struct avio_evbuffer *)malloc(sizeof(struct avio_evbuffer)); 63 if (!ae) 64 { 65 DPRINTF(E_LOG, L_FFMPEG, "Out of memory for avio_evbuffer\n"); 66 67 return NULL; 68 } 69 70 ae->buffer = av_mallocz(BUFFER_SIZE); 71 if (!ae->buffer) 72 { 73 DPRINTF(E_LOG, L_FFMPEG, "Out of memory for avio buffer\n"); 74 75 free(ae); 76 return NULL; 77 } 78 79 ae->evbuf = evbuf; 80 81 s = avio_alloc_context(ae->buffer, BUFFER_SIZE, 1, ae, NULL, avio_evbuffer_write, NULL); 82 if (!s) 83 { 84 DPRINTF(E_LOG, L_FFMPEG, "Could not allocate AVIOContext\n"); 85 86 av_free(ae->buffer); 87 free(ae); 88 return NULL; 89 } 90 91 s->seekable = 0; 92 93 return s; 94} 95 96void 97avio_evbuffer_close(AVIOContext *s) 98{ 99 struct avio_evbuffer *ae; 100 101 ae = (struct avio_evbuffer *)s->opaque; 102 103 avio_flush(s); 104 105 av_free(ae->buffer); 106 free(ae); 107 108 av_free(s); 109} 110