]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - output.c
baa91832987178a4548cf484e518f95e63f09586
[frescor/streamer.git] / output.c
1 /*
2  *  Copyright (c) 2008 Luca Abeni
3  *
4  *  This is free software; see GPL.txt
5  */
6 #include <libavformat/avformat.h>
7 #include "output.h"
8
9
10 AVFormatContext *open_output_stream(const char *dst, int port, enum CodecType codec_type)
11 {
12     AVFormatContext *s;
13     static AVOutputFormat *rtp_format;
14     AVStream *st;
15
16     s = av_alloc_format_context();
17     rtp_format = guess_format("rtp", NULL, NULL);
18     if (!rtp_format) {
19         fprintf(stderr, "Unable for find the RTP format for output\n");
20
21         return NULL;
22     }
23
24     s->oformat = rtp_format;
25     st = av_new_stream(s, 0);
26     if (!st) {
27         fprintf(stderr, "Cannot allocate stream\n");
28
29         return NULL;
30     }
31
32     snprintf(s->filename, sizeof(s->filename), "rtp://%s:%d", dst, port);
33         
34     /* open the UDP sockets for RTP and RTCP */
35     if (url_fopen(&s->pb, s->filename, URL_WRONLY) < 0) {
36         fprintf(stderr, "Cannot open '%s'\n", s->filename);
37         /* FIXME: Free the stream! */
38
39         return NULL;
40     }
41     avcodec_get_context_defaults2(s->streams[0]->codec, codec_type);
42     s->streams[0]->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename, NULL, codec_type);
43     s->streams[0]->codec->codec_id = CODEC_ID_MPEG4;
44     s->streams[0]->codec->pix_fmt  = PIX_FMT_YUV420P;
45     s->streams[0]->codec->mb_decision = 2;
46     s->streams[0]->codec->me_cmp = 2;
47     //s->streams[0]->codec->me_sub_cmp = 6;
48     s->streams[0]->codec->trellis = 1;
49
50     return s;
51 }
52
53 int close_output_stream(AVFormatContext *s)
54 {
55   url_fclose(s->pb);
56   return 0;
57 }
58
59 int pkt_send(AVFormatContext *ctx, AVPacket *pkt)
60 {
61   static int inited;            /* FIXME: HACK! */
62
63
64   if (!inited) {
65     int res;
66
67 fprintf(stderr,"initing!\n");
68     res = av_write_header(ctx);
69     if (res < 0) {
70       fprintf(stderr, "Cannot open the out RTP stream!\n");
71
72       return res;
73     }
74     inited = 1;
75   }
76
77   return av_write_frame(ctx, pkt);
78 }
79