1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* AV_codec.h
*
* Audio and video codec intitialisation, encoding/decoding and playback
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*----------------------------------------------------------------------------------*/
#ifndef _AVCODEC_H_
#define _AVCODEC_H_
#include <stdio.h>
#include <math.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavutil/opt.h>
#include <pthread.h>
#include <AL/al.h>
#include <AL/alc.h>
#include "../toxrtp/toxrtp.h"
#include "../toxcore/tox.h"
#include <SDL.h>
#include <opus/opus.h>
/* ffmpeg VP8 codec ID */
#define VIDEO_CODEC AV_CODEC_ID_VP8
/* ffmpeg Opus codec ID */
#define AUDIO_CODEC AV_CODEC_ID_OPUS
/* default video bitrate in bytes/s */
#define VIDEO_BITRATE 10*1000
/* default audio bitrate in bytes/s */
#define AUDIO_BITRATE 64000
/* audio frame duration in miliseconds */
#define AUDIO_FRAME_DURATION 20
/* audio sample rate recommended to be 48kHz for Opus */
#define AUDIO_SAMPLE_RATE 48000
/* the amount of samples in one audio frame */
#define AUDIO_FRAME_SIZE AUDIO_SAMPLE_RATE*AUDIO_FRAME_DURATION/1000
/* the quit event for SDL */
#define FF_QUIT_EVENT (SDL_USEREVENT + 2)
#ifdef __linux__
#define VIDEO_DRIVER "video4linux2"
#define DEFAULT_WEBCAM "/dev/video0"
#endif
#ifdef WIN32
#define VIDEO_DRIVER "vfwcap"
#define DEFAULT_WEBCAM "0"
#endif
SDL_Surface *screen;
typedef struct {
SDL_Overlay *bmp;
int width, height;
} VideoPicture;
typedef struct {
uint8_t send_audio;
uint8_t receive_audio;
uint8_t send_video;
uint8_t receive_video;
uint8_t support_send_audio;
uint8_t support_send_video;
uint8_t support_receive_audio;
uint8_t support_receive_video;
/* video encoding */
AVInputFormat *video_input_format;
AVFormatContext *video_format_ctx;
uint8_t video_stream;
AVCodecContext *webcam_decoder_ctx;
AVCodec *webcam_decoder;
AVCodecContext *video_encoder_ctx;
AVCodec *video_encoder;
/* video decoding */
AVCodecContext *video_decoder_ctx;
AVCodec *video_decoder;
/* audio encoding */
ALCdevice *audio_capture_device;
OpusEncoder *audio_encoder;
int audio_bitrate;
/* audio decoding */
OpusDecoder *audio_decoder;
uint8_t req_video_refresh;
/* context for converting image format to something SDL can use*/
struct SwsContext *sws_SDL_r_ctx;
/* context for converting webcam image format to something the video encoder can use */
struct SwsContext *sws_ctx;
/* rendered video picture, ready for display */
VideoPicture video_picture;
rtp_session_t *_rtp_video;
rtp_session_t *_rtp_audio;
int socket;
Networking_Core *_networking;
pthread_t encode_audio_thread;
pthread_t encode_video_thread;
pthread_t decode_audio_thread;
pthread_t decode_video_thread;
pthread_mutex_t rtp_msg_mutex_lock;
pthread_mutex_t avcodec_mutex_lock;
uint8_t quit;
SDL_Event SDL_event;
msi_session_t *_msi;
uint32_t _frame_rate;
uint16_t _send_port, _recv_port;
int _tox_sock;
//pthread_id _medialoop_id;
} codec_state;
int display_received_frame(codec_state *cs, AVFrame *r_video_frame);
int init_receive_audio(codec_state *cs);
int init_decoder(codec_state *cs);
int init_send_video(codec_state *cs);
int init_send_audio(codec_state *cs);
int init_encoder(codec_state *cs);
int video_encoder_refresh(codec_state *cs, int bps);
void *encode_video_thread(void *arg);
void *encode_audio_thread(void *arg);
int video_decoder_refresh(codec_state *cs, int width, int height);
int handle_rtp_video_packet(codec_state *cs, rtp_msg_t *r_msg);
void *decode_video_thread(void *arg);
void *decode_audio_thread(void *arg);
#endif
|