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