1/*
2 * Copyright (C) 2018 Metrological Group B.V.
3 * Copyright (C) 2018 Igalia S.L. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * aint with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#pragma once
22
23#if ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)
24
25#include "AudioStreamDescription.h"
26#include "GStreamerCommon.h"
27#include <gst/audio/audio.h>
28
29namespace WebCore {
30
31class GStreamerAudioStreamDescription final: public AudioStreamDescription {
32public:
33 GStreamerAudioStreamDescription(GstAudioInfo info)
34 : m_info(info)
35 , m_caps(adoptGRef(gst_audio_info_to_caps(&m_info)))
36 {
37 }
38
39 GStreamerAudioStreamDescription(GstAudioInfo *info)
40 : m_info(*info)
41 , m_caps(adoptGRef(gst_audio_info_to_caps(&m_info)))
42 {
43 }
44
45 GStreamerAudioStreamDescription()
46 {
47 gst_audio_info_init(&m_info);
48 }
49
50 WEBCORE_EXPORT ~GStreamerAudioStreamDescription() { };
51
52 const PlatformDescription& platformDescription() const
53 {
54 m_platformDescription = { PlatformDescription::GStreamerAudioStreamDescription, reinterpret_cast<const AudioStreamBasicDescription*>(&m_info) };
55
56 return m_platformDescription;
57 }
58
59 WEBCORE_EXPORT PCMFormat format() const final {
60 switch (GST_AUDIO_INFO_FORMAT(&m_info)) {
61 case GST_AUDIO_FORMAT_S16LE:
62 case GST_AUDIO_FORMAT_S16BE:
63 return Int16;
64 case GST_AUDIO_FORMAT_S32LE:
65 case GST_AUDIO_FORMAT_S32BE:
66 return Int32;
67 case GST_AUDIO_FORMAT_F32LE:
68 case GST_AUDIO_FORMAT_F32BE:
69 return Float32;
70 case GST_AUDIO_FORMAT_F64LE:
71 case GST_AUDIO_FORMAT_F64BE:
72 return Float64;
73 default:
74 break;
75 }
76 return None;
77 }
78
79 double sampleRate() const final { return GST_AUDIO_INFO_RATE(&m_info); }
80 bool isPCM() const final { return format() != None; }
81 bool isInterleaved() const final { return GST_AUDIO_INFO_LAYOUT(&m_info) == GST_AUDIO_LAYOUT_INTERLEAVED; }
82 bool isSignedInteger() const final { return GST_AUDIO_INFO_IS_INTEGER(&m_info); }
83 bool isNativeEndian() const final { return GST_AUDIO_INFO_ENDIANNESS(&m_info) == G_BYTE_ORDER; }
84 bool isFloat() const final { return GST_AUDIO_INFO_IS_FLOAT(&m_info); }
85 int bytesPerFrame() { return GST_AUDIO_INFO_BPF(&m_info); }
86
87 uint32_t numberOfInterleavedChannels() const final { return isInterleaved() ? GST_AUDIO_INFO_CHANNELS(&m_info) : TRUE; }
88 uint32_t numberOfChannelStreams() const final { return GST_AUDIO_INFO_CHANNELS(&m_info); }
89 uint32_t numberOfChannels() const final { return GST_AUDIO_INFO_CHANNELS(&m_info); }
90 uint32_t sampleWordSize() const final { return GST_AUDIO_INFO_BPS(&m_info); }
91
92 bool operator==(const GStreamerAudioStreamDescription& other) { return gst_audio_info_is_equal(&m_info, &other.m_info); }
93 bool operator!=(const GStreamerAudioStreamDescription& other) { return !operator == (other); }
94
95 GstCaps* caps() { return m_caps.get(); }
96 GstAudioInfo* getInfo() { return &m_info; }
97
98private:
99 GstAudioInfo m_info;
100 GRefPtr<GstCaps> m_caps;
101 mutable PlatformDescription m_platformDescription;
102};
103
104} // WebCore
105
106#endif // ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)
107