Skip to content
Snippets Groups Projects
Commit 46fdc9d9 authored by Philippe Gorley's avatar Philippe Gorley Committed by Philippe Gorley
Browse files

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