1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  * Copyright (C) 2011 Ericsson AB. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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 in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_H_
27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_H_
28 
29 #include <memory>
30 #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
31 #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
32 #include "third_party/blink/renderer/modules/event_target_modules.h"
33 #include "third_party/blink/renderer/modules/modules_export.h"
34 #include "third_party/blink/renderer/platform/mediastream/media_stream_descriptor.h"
35 #include "third_party/blink/renderer/platform/mediastream/media_stream_source.h"
36 #include "third_party/blink/renderer/platform/wtf/forward.h"
37 
38 namespace blink {
39 
40 class AudioSourceProvider;
41 class ImageCapture;
42 class MediaTrackCapabilities;
43 class MediaTrackConstraints;
44 class MediaStream;
45 class MediaTrackSettings;
46 class ScriptPromiseResolver;
47 class ScriptState;
48 
49 class MODULES_EXPORT MediaStreamTrack
50     : public EventTargetWithInlineData,
51       public ActiveScriptWrappable<MediaStreamTrack>,
52       public MediaStreamSource::Observer {
53   USING_GARBAGE_COLLECTED_MIXIN(MediaStreamTrack);
54   DEFINE_WRAPPERTYPEINFO();
55 
56  public:
57   MediaStreamTrack(ExecutionContext*, MediaStreamComponent*);
58   MediaStreamTrack(ExecutionContext*,
59                    MediaStreamComponent*,
60                    MediaStreamSource::ReadyState);
61   ~MediaStreamTrack() override;
62 
63   String kind() const;
64   String id() const;
65   String label() const;
66 
67   bool enabled() const;
68   void setEnabled(bool);
69 
70   bool muted() const;
71 
72   String ContentHint() const;
73   void SetContentHint(const String&);
74 
75   String readyState() const;
76 
77   void stopTrack(ExecutionContext*);
78   virtual MediaStreamTrack* clone(ScriptState*);
79 
80   // This function is called when constrains have been successfully applied.
81   // Called from UserMediaRequest when it succeeds. It is not IDL-exposed.
82   void SetConstraints(const MediaConstraints&);
83 
84   MediaTrackCapabilities* getCapabilities() const;
85   MediaTrackConstraints* getConstraints() const;
86   MediaTrackSettings* getSettings() const;
87   ScriptPromise applyConstraints(ScriptState*, const MediaTrackConstraints*);
88 
DEFINE_ATTRIBUTE_EVENT_LISTENER(mute,kMute)89   DEFINE_ATTRIBUTE_EVENT_LISTENER(mute, kMute)
90   DEFINE_ATTRIBUTE_EVENT_LISTENER(unmute, kUnmute)
91   DEFINE_ATTRIBUTE_EVENT_LISTENER(ended, kEnded)
92 
93   MediaStreamComponent* Component() { return component_; }
94   bool Ended() const;
95 
96   void RegisterMediaStream(MediaStream*);
97   void UnregisterMediaStream(MediaStream*);
98 
99   // EventTarget
100   const AtomicString& InterfaceName() const override;
101   ExecutionContext* GetExecutionContext() const override;
102 
103   // ScriptWrappable
104   bool HasPendingActivity() const final;
105 
106   std::unique_ptr<AudioSourceProvider> CreateWebAudioSource(
107       int context_sample_rate);
108 
109   void Trace(Visitor*) override;
110 
111  private:
112   friend class CanvasCaptureMediaStreamTrack;
113 
114   // MediaStreamSourceObserver
115   void SourceChangedState() override;
116 
117   void PropagateTrackEnded();
118   void applyConstraintsImageCapture(ScriptPromiseResolver*,
119                                     const MediaTrackConstraints*);
120 
121   std::string GetTrackLogString() const;
122 
123   MediaStreamSource::ReadyState ready_state_;
124   HeapHashSet<Member<MediaStream>> registered_media_streams_;
125   bool is_iterating_registered_media_streams_ = false;
126   Member<MediaStreamComponent> component_;
127   Member<ImageCapture> image_capture_;
128   WeakMember<ExecutionContext> execution_context_;
129 };
130 
131 typedef HeapVector<Member<MediaStreamTrack>> MediaStreamTrackVector;
132 
133 }  // namespace blink
134 
135 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_H_
136