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/scheduler/public/frame_scheduler.h"
37 #include "third_party/blink/renderer/platform/wtf/forward.h"
38 
39 namespace blink {
40 
41 class AudioSourceProvider;
42 class ImageCapture;
43 class MediaTrackCapabilities;
44 class MediaTrackConstraints;
45 class MediaStream;
46 class MediaTrackSettings;
47 class ScriptPromiseResolver;
48 class ScriptState;
49 
50 class MODULES_EXPORT MediaStreamTrack
51     : public EventTargetWithInlineData,
52       public ActiveScriptWrappable<MediaStreamTrack>,
53       public MediaStreamSource::Observer {
54   DEFINE_WRAPPERTYPEINFO();
55 
56  public:
57   MediaStreamTrack(ExecutionContext*, MediaStreamComponent*);
58   MediaStreamTrack(ExecutionContext*,
59                    MediaStreamComponent*,
60                    base::OnceClosure callback);
61   MediaStreamTrack(ExecutionContext*,
62                    MediaStreamComponent*,
63                    MediaStreamSource::ReadyState,
64                    base::OnceClosure callback);
65   ~MediaStreamTrack() override;
66 
67   String kind() const;
68   String id() const;
69   String label() const;
70 
71   bool enabled() const;
72   void setEnabled(bool);
73 
74   bool muted() const;
75 
76   String ContentHint() const;
77   void SetContentHint(const String&);
78 
79   String readyState() const;
80 
81   void stopTrack(ExecutionContext*);
82   virtual MediaStreamTrack* clone(ScriptState*);
83 
84   // This function is called when constrains have been successfully applied.
85   // Called from UserMediaRequest when it succeeds. It is not IDL-exposed.
86   void SetConstraints(const MediaConstraints&);
87 
88   MediaTrackCapabilities* getCapabilities() const;
89   MediaTrackConstraints* getConstraints() const;
90   MediaTrackSettings* getSettings() const;
91   ScriptPromise applyConstraints(ScriptState*, const MediaTrackConstraints*);
92 
DEFINE_ATTRIBUTE_EVENT_LISTENER(mute,kMute)93   DEFINE_ATTRIBUTE_EVENT_LISTENER(mute, kMute)
94   DEFINE_ATTRIBUTE_EVENT_LISTENER(unmute, kUnmute)
95   DEFINE_ATTRIBUTE_EVENT_LISTENER(ended, kEnded)
96 
97   MediaStreamComponent* Component() { return component_; }
98   bool Ended() const;
99 
100   void RegisterMediaStream(MediaStream*);
101   void UnregisterMediaStream(MediaStream*);
102 
103   // EventTarget
104   const AtomicString& InterfaceName() const override;
105   ExecutionContext* GetExecutionContext() const override;
106 
107   // ScriptWrappable
108   bool HasPendingActivity() const final;
109 
110   std::unique_ptr<AudioSourceProvider> CreateWebAudioSource(
111       int context_sample_rate);
112 
GetImageCapture()113   ImageCapture* GetImageCapture() { return image_capture_; }
114 
115   void Trace(Visitor*) const override;
116 
117  private:
118   friend class CanvasCaptureMediaStreamTrack;
119 
120   // MediaStreamSourceObserver
121   void SourceChangedState() override;
122 
123   void PropagateTrackEnded();
124   void applyConstraintsImageCapture(ScriptPromiseResolver*,
125                                     const MediaTrackConstraints*);
126 
127   std::string GetTrackLogString() const;
128 
129   // Ensures that |feature_handle_for_scheduler_| is initialized.
130   void EnsureFeatureHandleForScheduler();
131 
132   // This handle notifies the scheduler about a live media stream track
133   // associated with a frame. The handle should be destroyed when the track
134   // is stopped.
135   FrameScheduler::SchedulingAffectingFeatureHandle
136       feature_handle_for_scheduler_;
137 
138   MediaStreamSource::ReadyState ready_state_;
139   HeapHashSet<Member<MediaStream>> registered_media_streams_;
140   bool is_iterating_registered_media_streams_ = false;
141   Member<MediaStreamComponent> component_;
142   Member<ImageCapture> image_capture_;
143   WeakMember<ExecutionContext> execution_context_;
144 };
145 
146 typedef HeapVector<Member<MediaStreamTrack>> MediaStreamTrackVector;
147 
148 }  // namespace blink
149 
150 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_H_
151