blob: 405a6e46200736ec81431a82e06ce75ed06820b6 [file] [log] [blame]
agsantosc9181b42020-11-26 12:03:04 -05001/*
2 * Copyright (C) 2020 Savoir-faire Linux Inc.
3 *
4 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#ifndef MEDIASTREAM_H
22#define MEDIASTREAM_H
23
24#include <media/libav_deps.h>
25#include <rational.h>
26
27#include "audioFormat.h"
28
29#include <string>
30
31namespace jami {
32
33struct MediaStream
34{
35 std::string name {};
36 int format {-1};
37 bool isVideo {false};
38 rational<int> timeBase;
39 int64_t firstTimestamp {0};
40 int width {0};
41 int height {0};
42 int bitrate;
43 rational<int> frameRate;
44 int sampleRate {0};
45 int nbChannels {0};
46 int frameSize {0};
47
48 MediaStream() {}
49
50 MediaStream(const std::string& streamName,
51 int fmt,
52 rational<int> tb,
53 int w,
54 int h,
55 int br,
56 rational<int> fr)
57 : name(streamName)
58 , format(fmt)
59 , isVideo(true)
60 , timeBase(tb)
61 , width(w)
62 , height(h)
63 , bitrate(br)
64 , frameRate(fr)
65 {}
66
67 MediaStream(
68 const std::string& streamName, int fmt, rational<int> tb, int sr, int channels, int size)
69 : name(streamName)
70 , format(fmt)
71 , isVideo(false)
72 , timeBase(tb)
73 , sampleRate(sr)
74 , nbChannels(channels)
75 , frameSize(size)
76 {}
77
78 MediaStream(const std::string& streamName, AudioFormat fmt)
79 : MediaStream(streamName, fmt, 0)
80 {}
81
82 MediaStream(const std::string& streamName, AudioFormat fmt, int64_t startTimestamp)
83 : name(streamName)
84 , format(fmt.sampleFormat)
85 , isVideo(false)
86 , timeBase(1, fmt.sample_rate)
87 , firstTimestamp(startTimestamp)
88 , sampleRate(fmt.sample_rate)
89 , nbChannels(fmt.nb_channels)
90 , frameSize(fmt.sample_rate / 50) // standard frame size for our encoder is 20 ms
91 {}
92
93 MediaStream(const std::string& streamName, AVCodecContext* c)
94 : MediaStream(streamName, c, 0)
95 {}
96
97 MediaStream(const std::string& streamName, AVCodecContext* c, int64_t startTimestamp)
98 : name(streamName)
99 , firstTimestamp(startTimestamp)
100 {
101 if (c) {
102 timeBase = c->time_base;
103 switch (c->codec_type) {
104 case AVMEDIA_TYPE_VIDEO:
105 format = c->pix_fmt;
106 isVideo = true;
107 width = c->width;
108 height = c->height;
109 bitrate = c->bit_rate;
110 frameRate = c->framerate;
111 break;
112 case AVMEDIA_TYPE_AUDIO:
113 format = c->sample_fmt;
114 isVideo = false;
115 sampleRate = c->sample_rate;
116 nbChannels = c->channels;
117 frameSize = c->frame_size;
118 break;
119 default:
120 break;
121 }
122 }
123 }
124
125 MediaStream(const MediaStream& other) = default;
126
127 bool isValid() const
128 {
129 if (format < 0)
130 return false;
131 if (isVideo)
132 return width > 0 && height > 0;
133 else
134 return sampleRate > 0 && nbChannels > 0;
135 }
136
137 void update(AVFrame* f)
138 {
139 // update all info possible (AVFrame has no fps or bitrate data)
140 format = f->format;
141 if (isVideo) {
142 width = f->width;
143 height = f->height;
144 } else {
145 sampleRate = f->sample_rate;
146 nbChannels = f->channels;
147 timeBase = rational<int>(1, f->sample_rate);
148 if (!frameSize)
149 frameSize = f->nb_samples;
150 }
151 }
152};
153
154inline std::ostream&
155operator<<(std::ostream& os, const MediaStream& ms)
156{
157 if (ms.isVideo) {
158 auto formatName = av_get_pix_fmt_name(static_cast<AVPixelFormat>(ms.format));
159 os << (ms.name.empty() ? "(null)" : ms.name) << ": "
160 << (formatName ? formatName : "(unknown format)") << " video, " << ms.width << "x"
161 << ms.height << ", " << ms.frameRate << " fps (" << ms.timeBase << ")";
162 if (ms.bitrate > 0)
163 os << ", " << ms.bitrate << " kb/s";
164 } else {
165 os << (ms.name.empty() ? "(null)" : ms.name) << ": "
166 << av_get_sample_fmt_name(static_cast<AVSampleFormat>(ms.format)) << " audio, "
167 << ms.nbChannels << " channel(s), " << ms.sampleRate << " Hz (" << ms.timeBase << "), "
168 << ms.frameSize << " samples per frame";
169 }
170 if (ms.firstTimestamp > 0)
171 os << ", start: " << ms.firstTimestamp;
172 return os;
173}
174}; // namespace jami
175
176#endif // MEDIASTREAM_H