blob: d764f53eb70f7f4fbd1e3c09e6fa4ff0caa85a5a [file] [log] [blame]
agsantosc9181b42020-11-26 12:03:04 -05001/*
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantosc9181b42020-11-26 12:03:04 -05003 *
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;
Aline Gondim Santosd251ea62023-03-02 09:40:56 -0300116 nbChannels = c->ch_layout.nb_channels;
agsantosc9181b42020-11-26 12:03:04 -0500117 frameSize = c->frame_size;
118 break;
119 default:
120 break;
121 }
122 }
123 }
124
agsantosc9181b42020-11-26 12:03:04 -0500125 bool isValid() const
126 {
127 if (format < 0)
128 return false;
129 if (isVideo)
130 return width > 0 && height > 0;
131 else
132 return sampleRate > 0 && nbChannels > 0;
133 }
134
135 void update(AVFrame* f)
136 {
137 // update all info possible (AVFrame has no fps or bitrate data)
138 format = f->format;
139 if (isVideo) {
140 width = f->width;
141 height = f->height;
142 } else {
143 sampleRate = f->sample_rate;
Aline Gondim Santosd251ea62023-03-02 09:40:56 -0300144 nbChannels = f->ch_layout.nb_channels;
agsantosc9181b42020-11-26 12:03:04 -0500145 timeBase = rational<int>(1, f->sample_rate);
146 if (!frameSize)
147 frameSize = f->nb_samples;
148 }
149 }
150};
151
152inline std::ostream&
153operator<<(std::ostream& os, const MediaStream& ms)
154{
155 if (ms.isVideo) {
156 auto formatName = av_get_pix_fmt_name(static_cast<AVPixelFormat>(ms.format));
157 os << (ms.name.empty() ? "(null)" : ms.name) << ": "
158 << (formatName ? formatName : "(unknown format)") << " video, " << ms.width << "x"
159 << ms.height << ", " << ms.frameRate << " fps (" << ms.timeBase << ")";
160 if (ms.bitrate > 0)
161 os << ", " << ms.bitrate << " kb/s";
162 } else {
163 os << (ms.name.empty() ? "(null)" : ms.name) << ": "
164 << av_get_sample_fmt_name(static_cast<AVSampleFormat>(ms.format)) << " audio, "
165 << ms.nbChannels << " channel(s), " << ms.sampleRate << " Hz (" << ms.timeBase << "), "
166 << ms.frameSize << " samples per frame";
167 }
168 if (ms.firstTimestamp > 0)
169 os << ", start: " << ms.firstTimestamp;
170 return os;
171}
172}; // namespace jami
173
174#endif // MEDIASTREAM_H