[WebRTC] Add support for incoming and outgoing libwebrtc audio tracks
[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 "RealtimeMediaSourceSettings.h"
37 #include "WebAudioSourceProviderAVFObjC.h"
38
39 #include "CoreMediaSoftLink.h"
40
41 namespace WebCore {
42
43 Ref<RealtimeIncomingAudioSource> RealtimeIncomingAudioSource::create(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
44 {
45     return adoptRef(*new RealtimeIncomingAudioSource(WTFMove(audioTrack), WTFMove(audioTrackId)));
46 }
47
48 RealtimeIncomingAudioSource::RealtimeIncomingAudioSource(rtc::scoped_refptr<webrtc::AudioTrackInterface>&& audioTrack, String&& audioTrackId)
49     : RealtimeMediaSource(WTFMove(audioTrackId), RealtimeMediaSource::Type::Audio, String())
50     , m_audioTrack(WTFMove(audioTrack))
51 {
52 }
53
54 void RealtimeIncomingAudioSource::OnData(const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames)
55 {
56     // FIXME: Implement this.
57     UNUSED_PARAM(audioData);
58     UNUSED_PARAM(bitsPerSample);
59     UNUSED_PARAM(sampleRate);
60     UNUSED_PARAM(numberOfChannels);
61     UNUSED_PARAM(numberOfFrames);
62 }
63
64 void RealtimeIncomingAudioSource::startProducingData()
65 {
66     if (m_isProducingData)
67         return;
68
69     m_isProducingData = true;
70     if (m_audioTrack)
71         m_audioTrack->AddSink(this);
72 }
73
74 void RealtimeIncomingAudioSource::stopProducingData()
75 {
76     if (m_isProducingData)
77         return;
78
79     m_isProducingData = false;
80     if (m_audioTrack)
81         m_audioTrack->RemoveSink(this);
82 }
83
84
85 RefPtr<RealtimeMediaSourceCapabilities> RealtimeIncomingAudioSource::capabilities() const
86 {
87     return m_capabilities;
88 }
89
90 const RealtimeMediaSourceSettings& RealtimeIncomingAudioSource::settings() const
91 {
92     return m_currentSettings;
93 }
94
95 RealtimeMediaSourceSupportedConstraints& RealtimeIncomingAudioSource::supportedConstraints()
96 {
97     return m_supportedConstraints;
98 }
99
100 void RealtimeIncomingAudioSource::addObserver(AudioSourceObserverObjC& observer)
101 {
102     m_audioSourceObservers.append(observer);
103     if (m_formatDescription) {
104         const auto* description = CMAudioFormatDescriptionGetStreamBasicDescription(m_formatDescription.get());
105         observer.prepare(description);
106     }
107 }
108
109 void RealtimeIncomingAudioSource::removeObserver(AudioSourceObserverObjC& observer)
110 {
111     m_audioSourceObservers.removeFirstMatching([&observer](const auto& registeredObserver) {
112         return &observer == &registeredObserver.get();
113     });
114 }
115
116 void RealtimeIncomingAudioSource::start()
117 {
118     startProducingData();
119 }
120
121 AudioSourceProvider* RealtimeIncomingAudioSource::audioSourceProvider()
122 {
123     if (!m_audioSourceProvider)
124         m_audioSourceProvider = WebAudioSourceProviderAVFObjC::create(*this);
125
126     return m_audioSourceProvider.get();
127 }
128
129 } // namespace WebCore
130
131 #endif // USE(LIBWEBRTC)