ASSERTION FAILED: numberOfChannels == 2 in WebCore::RealtimeIncomingAudioSource:...
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / RealtimeIncomingAudioSource.cpp
1 /*
2  * Copyright (C) 2017 Apple 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  * 3. Neither the name of Google Inc. nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "RealtimeIncomingAudioSource.h"
33
34 #if USE(LIBWEBRTC)
35
36 #include "AudioStreamDescription.h"
37 #include "CAAudioStreamDescription.h"
38 #include "LibWebRTCAudioFormat.h"
39 #include "MediaTimeAVFoundation.h"
40 #include "WebAudioBufferList.h"
41 #include "WebAudioSourceProviderAVFObjC.h"
42
43 #include "CoreMediaSoftLink.h"
44
45 namespace WebCore {
46
47 Ref<RealtimeIncomingAudioSource> RealtimeIncomingAudioSource::create(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
48 {
49     auto source = adoptRef(*new RealtimeIncomingAudioSource(WTFMove(audioTrack), WTFMove(audioTrackId)));
50     source->startProducingData();
51     return source;
52 }
53
54 RealtimeIncomingAudioSource::RealtimeIncomingAudioSource(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
55     : RealtimeMediaSource(WTFMove(audioTrackId), RealtimeMediaSource::Type::Audio, String())
56     , m_audioTrack(WTFMove(audioTrack))
57 {
58 }
59
60 RealtimeIncomingAudioSource::~RealtimeIncomingAudioSource()
61 {
62     if (m_audioSourceProvider) {
63         m_audioSourceProvider->unprepare();
64         m_audioSourceProvider = nullptr;
65     }
66 }
67
68
69 static inline AudioStreamBasicDescription streamDescription(size_t sampleRate, size_t channelCount)
70 {
71     AudioStreamBasicDescription streamFormat;
72     FillOutASBDForLPCM(streamFormat, sampleRate, channelCount, LibWebRTCAudioFormat::sampleSize, LibWebRTCAudioFormat::sampleSize, LibWebRTCAudioFormat::isFloat, LibWebRTCAudioFormat::isBigEndian, LibWebRTCAudioFormat::isNonInterleaved);
73     return streamFormat;
74 }
75
76 void RealtimeIncomingAudioSource::OnData(const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames)
77 {
78     // We may receive OnData calls with empty sound data (mono, samples equal to zero and sampleRate equal to 16000) when starting the call.
79     // FIXME: For the moment we skip them, we should find a better solution at libwebrtc level to not be called until getting some real data.
80     if (sampleRate == 16000 && numberOfChannels == 1)
81         return;
82
83     ASSERT(bitsPerSample == 16);
84     ASSERT(numberOfChannels == 1 || numberOfChannels == 2);
85     ASSERT(sampleRate == 48000);
86
87     CMTime startTime = CMTimeMake(m_numberOfFrames, sampleRate);
88     auto mediaTime = toMediaTime(startTime);
89     m_numberOfFrames += numberOfFrames;
90
91     AudioStreamBasicDescription newDescription = streamDescription(sampleRate, numberOfChannels);
92     if (newDescription != m_streamFormat) {
93         m_streamFormat = newDescription;
94         if (m_audioSourceProvider)
95             m_audioSourceProvider->prepare(&m_streamFormat);
96     }
97
98     WebAudioBufferList audioBufferList { CAAudioStreamDescription(m_streamFormat), WTF::safeCast<uint32_t>(numberOfFrames) };
99     audioBufferList.buffer(0)->mDataByteSize = numberOfChannels * numberOfFrames * bitsPerSample / 8;
100     audioBufferList.buffer(0)->mNumberChannels = numberOfChannels;
101     // FIXME: We should not need to do the extra memory allocation and copy.
102     // Instead, we should be able to directly pass audioData pointer.
103     memcpy(audioBufferList.buffer(0)->mData, audioData, audioBufferList.buffer(0)->mDataByteSize);
104
105     audioSamplesAvailable(mediaTime, audioBufferList, CAAudioStreamDescription(m_streamFormat), numberOfFrames);
106 }
107
108 void RealtimeIncomingAudioSource::startProducingData()
109 {
110     if (m_isProducingData)
111         return;
112
113     m_isProducingData = true;
114     if (m_audioTrack)
115         m_audioTrack->AddSink(this);
116 }
117
118 void RealtimeIncomingAudioSource::stopProducingData()
119 {
120     if (m_isProducingData)
121         return;
122
123     m_isProducingData = false;
124     if (m_audioTrack)
125         m_audioTrack->RemoveSink(this);
126 }
127
128
129 RefPtr<RealtimeMediaSourceCapabilities> RealtimeIncomingAudioSource::capabilities() const
130 {
131     return m_capabilities;
132 }
133
134 const RealtimeMediaSourceSettings& RealtimeIncomingAudioSource::settings() const
135 {
136     return m_currentSettings;
137 }
138
139 RealtimeMediaSourceSupportedConstraints& RealtimeIncomingAudioSource::supportedConstraints()
140 {
141     return m_supportedConstraints;
142 }
143
144 AudioSourceProvider* RealtimeIncomingAudioSource::audioSourceProvider()
145 {
146     if (!m_audioSourceProvider) {
147         m_audioSourceProvider = WebAudioSourceProviderAVFObjC::create(*this);
148         if (m_numberOfFrames)
149             m_audioSourceProvider->prepare(&m_streamFormat);
150     }
151
152     return m_audioSourceProvider.get();
153 }
154
155 } // namespace WebCore
156
157 #endif // USE(LIBWEBRTC)