From 496fbbd618c9d938e5cfa159a627dde38c9db7bf Mon Sep 17 00:00:00 2001
From: philippegorley <philippe.gorley@savoirfairelinux.com>
Date: Thu, 9 Aug 2018 17:45:50 -0400
Subject: [PATCH] media: build MediaStream from AudioFormat

Keeps track of sample format in AudioFormat, because MediaStream needs a
format.

Change-Id: I3ee3fe05f3d5d98706a20132876685e37ffdd966
Reviewed-by: Hugo Lefeuvre <hugo.lefeuvre@savoirfairelinux.com>
---
 src/media/audio/audiobuffer.h | 18 +++++++++++++++---
 src/media/media_stream.h      | 10 ++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/media/audio/audiobuffer.h b/src/media/audio/audiobuffer.h
index aea0205f52..b0ceb12f87 100644
--- a/src/media/audio/audiobuffer.h
+++ b/src/media/audio/audiobuffer.h
@@ -30,6 +30,10 @@
 #include <string>
 #include <cstddef> // for size_t
 
+extern "C" {
+#include <libavutil/samplefmt.h>
+}
+
 #include "ring_types.h"
 
 #include <ciso646> // fix windows compiler bug
@@ -44,11 +48,18 @@ namespace ring {
 struct AudioFormat {
     unsigned sample_rate;
     unsigned nb_channels;
+    AVSampleFormat sampleFormat;
 
-    constexpr AudioFormat(unsigned sr, unsigned c) : sample_rate(sr), nb_channels(c) {}
+    constexpr AudioFormat(unsigned sr, unsigned c)
+        : sample_rate(sr)
+        , nb_channels(c)
+        , sampleFormat(AV_SAMPLE_FMT_S16)
+    {}
 
     inline bool operator == (const AudioFormat &b) const {
-        return ( (b.sample_rate == sample_rate) && (b.nb_channels == nb_channels) );
+        return ( (b.sample_rate == sample_rate) &&
+                 (b.nb_channels == nb_channels) &&
+                 (b.sampleFormat == sampleFormat) );
     }
 
     inline bool operator != (const AudioFormat &b) const {
@@ -57,7 +68,8 @@ struct AudioFormat {
 
     inline std::string toString() const {
         std::stringstream ss;
-        ss << "{" << nb_channels << " channels, " << sample_rate << "Hz}";
+        ss << "{" << av_get_sample_fmt_name(sampleFormat) << ", "
+            << nb_channels << " channels, " << sample_rate << "Hz}";
         return ss.str();
     }
 
diff --git a/src/media/media_stream.h b/src/media/media_stream.h
index 1475d8882b..d531869228 100644
--- a/src/media/media_stream.h
+++ b/src/media/media_stream.h
@@ -21,6 +21,7 @@
 #pragma once
 
 #include "config.h"
+#include "audio/audiobuffer.h"
 #include "libav_deps.h"
 #include "rational.h"
 
@@ -65,6 +66,15 @@ struct MediaStream {
         , nbChannels(channels)
     {}
 
+    MediaStream(std::string name, AudioFormat fmt)
+        : name(name)
+        , format(fmt.sampleFormat)
+        , isVideo(false)
+        , timeBase(1, fmt.sample_rate)
+        , sampleRate(fmt.sample_rate)
+        , nbChannels(fmt.nb_channels)
+    {}
+
     MediaStream(std::string name, AVCodecContext* c)
         : MediaStream(name, c, 0)
     {
-- 
GitLab