1/*
2 * Copyright (C) 2017 Igalia Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29
30#if USE(LIBWEBRTC) && USE(GSTREAMER)
31#include "RealtimeIncomingAudioSourceLibWebRTC.h"
32
33#include "LibWebRTCAudioFormat.h"
34#include "gstreamer/GStreamerAudioData.h"
35#include "gstreamer/GStreamerAudioStreamDescription.h"
36
37namespace WebCore {
38
39Ref<RealtimeIncomingAudioSource> RealtimeIncomingAudioSource::create(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
40{
41 auto source = RealtimeIncomingAudioSourceLibWebRTC::create(WTFMove(audioTrack), WTFMove(audioTrackId));
42 source->start();
43 return source;
44}
45
46Ref<RealtimeIncomingAudioSourceLibWebRTC> RealtimeIncomingAudioSourceLibWebRTC::create(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
47{
48 return adoptRef(*new RealtimeIncomingAudioSourceLibWebRTC(WTFMove(audioTrack), WTFMove(audioTrackId)));
49}
50
51RealtimeIncomingAudioSourceLibWebRTC::RealtimeIncomingAudioSourceLibWebRTC(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
52 : RealtimeIncomingAudioSource(WTFMove(audioTrack), WTFMove(audioTrackId))
53{
54}
55
56void RealtimeIncomingAudioSourceLibWebRTC::OnData(const void* audioData, int, int sampleRate, size_t numberOfChannels, size_t numberOfFrames)
57{
58 GstAudioInfo info;
59 GstAudioFormat format = gst_audio_format_build_integer(
60 LibWebRTCAudioFormat::isSigned,
61 LibWebRTCAudioFormat::isBigEndian ? G_BIG_ENDIAN : G_LITTLE_ENDIAN,
62 LibWebRTCAudioFormat::sampleSize,
63 LibWebRTCAudioFormat::sampleSize);
64
65 gst_audio_info_set_format(&info, format, sampleRate, numberOfChannels, NULL);
66
67 auto bufferSize = GST_AUDIO_INFO_BPF(&info) * numberOfFrames;
68 gpointer bufferData = g_malloc(bufferSize);
69 if (muted())
70 gst_audio_format_fill_silence(info.finfo, bufferData, bufferSize);
71 else
72 memcpy(bufferData, audioData, bufferSize);
73
74 auto buffer = adoptGRef(gst_buffer_new_wrapped(bufferData, bufferSize));
75 GRefPtr<GstCaps> caps = adoptGRef(gst_audio_info_to_caps(&info));
76 auto sample = adoptGRef(gst_sample_new(buffer.get(), caps.get(), nullptr, nullptr));
77 auto data(std::unique_ptr<GStreamerAudioData>(new GStreamerAudioData(WTFMove(sample), info)));
78
79 auto mediaTime = MediaTime((m_numberOfFrames * G_USEC_PER_SEC) / sampleRate, G_USEC_PER_SEC);
80 audioSamplesAvailable(mediaTime, *data.get(), GStreamerAudioStreamDescription(info), numberOfFrames);
81
82 m_numberOfFrames += numberOfFrames;
83}
84}
85
86#endif // USE(LIBWEBRTC)
87