Commit 46fdc9d9 authored by Philippe Gorley's avatar Philippe Gorley Committed by Philippe Gorley

media_stream: add frame size

This will help once audio streaming is implemented, as the encoder will
need to know the number of samples per frame in the decoded file.

Change-Id: I6d6375b46d74eec6618c0f1e7ee1f07fd86b6e45
parent 9e3f2160
......@@ -161,7 +161,7 @@ AudioSender::update(Observable<std::shared_ptr<ring::AudioFrame>>* /*obs*/, cons
{
auto frame = framePtr->pointer();
auto ms = MediaStream("a:local", frame->format, rational<int>(1, frame->sample_rate),
frame->sample_rate, frame->channels);
frame->sample_rate, frame->channels, frame->nb_samples);
frame->pts = sent_samples;
ms.firstTimestamp = frame->pts;
sent_samples += frame->nb_samples;
......
......@@ -40,6 +40,7 @@ struct MediaStream {
rational<int> frameRate;
int sampleRate {0};
int nbChannels {0};
int frameSize {0};
MediaStream()
{}
......@@ -56,13 +57,14 @@ struct MediaStream {
, frameRate(fr)
{}
MediaStream(const std::string& streamName, int fmt, rational<int> tb, int sr, int channels)
MediaStream(const std::string& streamName, int fmt, rational<int> tb, int sr, int channels, int size)
: name(streamName)
, format(fmt)
, isVideo(false)
, timeBase(tb)
, sampleRate(sr)
, nbChannels(channels)
, frameSize(size)
{}
MediaStream(const std::string& streamName, AudioFormat fmt)
......@@ -72,6 +74,7 @@ struct MediaStream {
, timeBase(1, fmt.sample_rate)
, sampleRate(fmt.sample_rate)
, nbChannels(fmt.nb_channels)
, frameSize(fmt.sample_rate / 50) // standard frame size for our encoder is 20 ms
{}
MediaStream(const std::string& streamName, AVCodecContext* c)
......@@ -97,6 +100,7 @@ struct MediaStream {
isVideo = false;
sampleRate = c->sample_rate;
nbChannels = c->channels;
frameSize = c->frame_size;
break;
default:
break;
......@@ -127,6 +131,8 @@ struct MediaStream {
sampleRate = f->sample_rate;
nbChannels = f->channels;
timeBase = rational<int>(1, f->sample_rate);
if (!frameSize)
frameSize = f->nb_samples;
}
}
};
......@@ -142,7 +148,8 @@ inline std::ostream& operator<<(std::ostream& os, const MediaStream& ms)
os << (ms.name.empty() ? "(null)" : ms.name) << ": "
<< av_get_sample_fmt_name(static_cast<AVSampleFormat>(ms.format)) << " audio, "
<< ms.nbChannels << " channel(s), "
<< ms.sampleRate << " Hz (" << ms.timeBase << ")";
<< ms.sampleRate << " Hz (" << ms.timeBase << "), "
<< ms.frameSize << " samples per frame";
}
if (ms.firstTimestamp > 0)
os << ", start: " << ms.firstTimestamp;
......
......@@ -117,7 +117,7 @@ fillAudioFrameProps(AVFrame* frame, const MediaStream& ms)
{
frame->format = ms.format;
frame->channel_layout = av_get_default_channel_layout(ms.nbChannels);
frame->nb_samples = 960;
frame->nb_samples = ms.frameSize;
frame->sample_rate = ms.sampleRate;
frame->channels = ms.nbChannels;
CPPUNIT_ASSERT(frame->format > AV_SAMPLE_FMT_NONE);
......@@ -146,7 +146,7 @@ MediaFilterTest::testAudioFilter()
frame->channels = av_get_channel_layout_nb_channels(channelLayout);
// construct the filter parameters
auto params = MediaStream("in1", format, rational<int>(1, sampleRate), sampleRate, frame->channels);
auto params = MediaStream("in1", format, rational<int>(1, sampleRate), sampleRate, frame->channels, nbSamples);
// allocate and fill frame buffers
CPPUNIT_ASSERT(av_frame_get_buffer(frame, 0) >= 0);
......@@ -180,9 +180,9 @@ MediaFilterTest::testAudioMixing()
auto frame3 = af3.pointer();
std::vector<MediaStream> vec;
vec.emplace_back("a1", AV_SAMPLE_FMT_S16, rational<int>(1, 48000), 48000, 2);
vec.emplace_back("a2", AV_SAMPLE_FMT_S16, rational<int>(1, 48000), 48000, 2);
vec.emplace_back("a3", AV_SAMPLE_FMT_S16, rational<int>(1, 48000), 48000, 2);
vec.emplace_back("a1", AV_SAMPLE_FMT_S16, rational<int>(1, 48000), 48000, 2, 960);
vec.emplace_back("a2", AV_SAMPLE_FMT_S16, rational<int>(1, 48000), 48000, 2, 960);
vec.emplace_back("a3", AV_SAMPLE_FMT_S16, rational<int>(1, 48000), 48000, 2, 960);
CPPUNIT_ASSERT(filter_->initialize(filterSpec, vec) >= 0);
float t1 = 0, t2 = 0, t3 = 0;
......@@ -324,7 +324,7 @@ MediaFilterTest::testReinit()
frame->channels = 2;
// construct the filter parameters with different sample rate
auto params = MediaStream("in1", frame->format, rational<int>(1, 16000), 16000, frame->channels);
auto params = MediaStream("in1", frame->format, rational<int>(1, 16000), 16000, frame->channels, frame->nb_samples);
// allocate and fill frame buffers
CPPUNIT_ASSERT(av_frame_get_buffer(frame, 0) >= 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment