1 /*
2  * Copyright (C) 2012, Google 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'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_MEDIA_STREAM_AUDIO_SOURCE_NODE_H_
27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_MEDIA_STREAM_AUDIO_SOURCE_NODE_H_
28 
29 #include <memory>
30 #include "base/memory/scoped_refptr.h"
31 #include "third_party/blink/renderer/modules/mediastream/media_stream.h"
32 #include "third_party/blink/renderer/modules/webaudio/audio_node.h"
33 #include "third_party/blink/renderer/platform/audio/audio_source_provider.h"
34 #include "third_party/blink/renderer/platform/audio/audio_source_provider_client.h"
35 #include "third_party/blink/renderer/platform/wtf/threading.h"
36 
37 namespace blink {
38 
39 class AudioContext;
40 class MediaStreamAudioSourceOptions;
41 
42 class MediaStreamAudioSourceHandler final : public AudioHandler {
43  public:
44   static scoped_refptr<MediaStreamAudioSourceHandler> Create(
45       AudioNode&,
46       std::unique_ptr<AudioSourceProvider>);
47   ~MediaStreamAudioSourceHandler() override;
48 
49   // AudioHandler
50   void Process(uint32_t frames_to_process) override;
51 
52   // AudioNode
TailTime()53   double TailTime() const override { return 0; }
LatencyTime()54   double LatencyTime() const override { return 0; }
55 
56   // A helper for AudioSourceProviderClient implementation of
57   // MediaStreamAudioSourceNode.
58   void SetFormat(uint32_t number_of_channels, float sample_rate);
59 
RequiresTailProcessing()60   bool RequiresTailProcessing() const final { return false; }
61 
62  private:
63   MediaStreamAudioSourceHandler(AudioNode&,
64                                 std::unique_ptr<AudioSourceProvider>);
65 
66   // As an audio source, we will never propagate silence.
PropagatesSilence()67   bool PropagatesSilence() const override { return false; }
68 
GetAudioSourceProvider()69   AudioSourceProvider* GetAudioSourceProvider() const {
70     return audio_source_provider_.get();
71   }
72 
73   std::unique_ptr<AudioSourceProvider> audio_source_provider_;
74 
75   Mutex process_lock_;
76 
77   unsigned source_number_of_channels_;
78 };
79 
80 class MediaStreamAudioSourceNode final
81     : public AudioNode,
82       public AudioSourceProviderClient,
83       public ActiveScriptWrappable<MediaStreamAudioSourceNode> {
84   DEFINE_WRAPPERTYPEINFO();
85   USING_GARBAGE_COLLECTED_MIXIN(MediaStreamAudioSourceNode);
86 
87  public:
88   static MediaStreamAudioSourceNode* Create(AudioContext&,
89                                             MediaStream&,
90                                             ExceptionState&);
91   static MediaStreamAudioSourceNode*
92   Create(AudioContext*, const MediaStreamAudioSourceOptions*, ExceptionState&);
93 
94   MediaStreamAudioSourceNode(AudioContext&,
95                              MediaStream&,
96                              MediaStreamTrack*,
97                              std::unique_ptr<AudioSourceProvider>);
98 
99   void Trace(Visitor*) override;
100 
101   MediaStream* getMediaStream() const;
102 
103   // AudioSourceProviderClient functions:
104   void SetFormat(uint32_t number_of_channels, float sample_rate) override;
105 
106   bool HasPendingActivity() const final;
107 
108   // InspectorHelperMixin
109   void ReportDidCreate() final;
110   void ReportWillBeDestroyed() final;
111 
112  private:
113   MediaStreamAudioSourceHandler& GetMediaStreamAudioSourceHandler() const;
114 
115   Member<MediaStreamTrack> audio_track_;
116   Member<MediaStream> media_stream_;
117 };
118 
119 }  // namespace blink
120 
121 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_MEDIA_STREAM_AUDIO_SOURCE_NODE_H_
122