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 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #if USE(LIBWEBRTC) |
29 | |
30 | #include "LibWebRTCMacros.h" |
31 | |
32 | ALLOW_UNUSED_PARAMETERS_BEGIN |
33 | |
34 | #include <webrtc/modules/audio_device/include/audio_device.h> |
35 | #include <webrtc/rtc_base/messagehandler.h> |
36 | #include <webrtc/rtc_base/thread.h> |
37 | |
38 | ALLOW_UNUSED_PARAMETERS_END |
39 | |
40 | namespace WebCore { |
41 | |
42 | // LibWebRTCAudioModule is pulling streamed data to ensure audio data is passed to the audio track. |
43 | class LibWebRTCAudioModule final : public webrtc::AudioDeviceModule, private rtc::MessageHandler { |
44 | WTF_MAKE_FAST_ALLOCATED; |
45 | public: |
46 | LibWebRTCAudioModule(); |
47 | |
48 | private: |
49 | template<typename U> U shouldNotBeCalled(U value) const |
50 | { |
51 | ASSERT_NOT_REACHED(); |
52 | return value; |
53 | } |
54 | |
55 | void AddRef() const final { } |
56 | rtc::RefCountReleaseStatus Release() const final { return rtc::RefCountReleaseStatus::kOtherRefsRemained; } |
57 | void OnMessage(rtc::Message*); |
58 | |
59 | // webrtc::AudioDeviceModule API |
60 | int32_t StartPlayout() final; |
61 | int32_t StopPlayout() final; |
62 | int32_t RegisterAudioCallback(webrtc::AudioTransport*) final; |
63 | bool Playing() const final { return m_isPlaying; } |
64 | |
65 | int32_t ActiveAudioLayer(AudioLayer*) const final { return shouldNotBeCalled(-1); } |
66 | int32_t Init() final { return 0; } |
67 | int32_t Terminate() final { return 0; } |
68 | bool Initialized() const final { return true; } |
69 | int16_t PlayoutDevices() final { return 0; } |
70 | int16_t RecordingDevices() final { return 0; } |
71 | int32_t PlayoutDeviceName(uint16_t, char[webrtc::kAdmMaxDeviceNameSize], char[webrtc::kAdmMaxGuidSize]) final { return 0; } |
72 | int32_t RecordingDeviceName(uint16_t, char[webrtc::kAdmMaxDeviceNameSize], char[webrtc::kAdmMaxGuidSize]) final { return 0; } |
73 | int32_t SetPlayoutDevice(uint16_t) final { return 0; } |
74 | int32_t SetPlayoutDevice(WindowsDeviceType) final { return 0; } |
75 | int32_t SetRecordingDevice(uint16_t) final { return 0; } |
76 | int32_t SetRecordingDevice(WindowsDeviceType) final { return 0; } |
77 | int32_t PlayoutIsAvailable(bool*) final { return shouldNotBeCalled(-1); } |
78 | int32_t InitPlayout() final { return 0; } |
79 | bool PlayoutIsInitialized() const final { return true; } |
80 | int32_t RecordingIsAvailable(bool*) final { return shouldNotBeCalled(-1); } |
81 | int32_t InitRecording() final { return 0; } |
82 | bool RecordingIsInitialized() const final { return false; } |
83 | int32_t StartRecording() final { return 0; } |
84 | int32_t StopRecording() final { return 0; } |
85 | bool Recording() const final { return 0; } |
86 | int32_t InitSpeaker() final { return 0; } |
87 | bool SpeakerIsInitialized() const final { return false; } |
88 | int32_t InitMicrophone() final { return 0; } |
89 | bool MicrophoneIsInitialized() const final { return false; } |
90 | int32_t MicrophoneVolumeIsAvailable(bool*) final { return shouldNotBeCalled(-1); } |
91 | int32_t SpeakerVolumeIsAvailable(bool*) final { return shouldNotBeCalled(-1); } |
92 | int32_t SetSpeakerVolume(uint32_t) final { return shouldNotBeCalled(-1); } |
93 | int32_t SpeakerVolume(uint32_t*) const final { return shouldNotBeCalled(-1); } |
94 | int32_t MaxSpeakerVolume(uint32_t*) const final { return shouldNotBeCalled(-1); } |
95 | int32_t MinSpeakerVolume(uint32_t*) const final { return shouldNotBeCalled(-1); } |
96 | int32_t SetMicrophoneVolume(uint32_t) final { return shouldNotBeCalled(-1); } |
97 | int32_t MicrophoneVolume(uint32_t*) const final { return shouldNotBeCalled(-1); } |
98 | int32_t MaxMicrophoneVolume(uint32_t*) const final { return shouldNotBeCalled(-1); } |
99 | int32_t MinMicrophoneVolume(uint32_t*) const final { return shouldNotBeCalled(-1); } |
100 | int32_t SpeakerMuteIsAvailable(bool*) final { return shouldNotBeCalled(-1); } |
101 | int32_t SetSpeakerMute(bool) final { return shouldNotBeCalled(-1); } |
102 | int32_t SpeakerMute(bool*) const final { return shouldNotBeCalled(-1); } |
103 | int32_t MicrophoneMuteIsAvailable(bool*) final { return shouldNotBeCalled(-1); } |
104 | int32_t SetMicrophoneMute(bool) final { return shouldNotBeCalled(-1); } |
105 | int32_t MicrophoneMute(bool*) const final { return shouldNotBeCalled(-1); } |
106 | int32_t StereoPlayoutIsAvailable(bool* available) const final { *available = false; return 0; } |
107 | int32_t SetStereoPlayout(bool) final { return 0; } |
108 | int32_t StereoPlayout(bool*) const final { return shouldNotBeCalled(-1); } |
109 | int32_t StereoRecordingIsAvailable(bool* available) const final { *available = false; return 0; } |
110 | int32_t SetStereoRecording(bool) final { return 0; } |
111 | int32_t StereoRecording(bool*) const final { return shouldNotBeCalled(-1); } |
112 | int32_t PlayoutDelay(uint16_t* delay) const final { *delay = 0; return 0; } |
113 | bool BuiltInAECIsAvailable() const final { return false; } |
114 | bool BuiltInAGCIsAvailable() const final { return false; } |
115 | bool BuiltInNSIsAvailable() const final { return false; } |
116 | int32_t EnableBuiltInAEC(bool) final { return shouldNotBeCalled(-1); } |
117 | int32_t EnableBuiltInAGC(bool) final { return shouldNotBeCalled(-1); } |
118 | int32_t EnableBuiltInNS(bool) final { return shouldNotBeCalled(-1); } |
119 | |
120 | #if defined(WEBRTC_IOS) |
121 | int GetPlayoutAudioParameters(webrtc::AudioParameters*) const final { return shouldNotBeCalled(-1); } |
122 | int GetRecordAudioParameters(webrtc::AudioParameters*) const final { return shouldNotBeCalled(-1); } |
123 | #endif |
124 | |
125 | private: |
126 | void StartPlayoutOnAudioThread(); |
127 | |
128 | void PollFromSource(); |
129 | |
130 | std::unique_ptr<rtc::Thread> m_audioTaskRunner; |
131 | |
132 | bool m_isPlaying = false; |
133 | webrtc::AudioTransport* m_audioTransport = nullptr; |
134 | }; |
135 | |
136 | } // namespace WebCore |
137 | |
138 | #endif // USE(LIBWEBRTC) |
139 | |