OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ |
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ |
7 | 7 |
8 #include <memory> | |
9 | |
10 #include "base/atomicops.h" | |
8 #include "base/callback.h" | 11 #include "base/callback.h" |
9 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "base/threading/thread_checker.h" | |
16 #include "content/renderer/media/media_stream_audio_deliverer.h" | |
10 #include "content/renderer/media/media_stream_track.h" | 17 #include "content/renderer/media/media_stream_track.h" |
11 | 18 |
12 namespace webrtc { | |
13 class AudioTrackInterface; | |
14 } // namespace webrtc | |
15 | |
16 namespace content { | 19 namespace content { |
17 | 20 |
18 class MediaStreamAudioSink; | 21 class MediaStreamAudioSink; |
22 class MediaStreamAudioSource; | |
19 | 23 |
20 // TODO(miu): In a soon-upcoming set of refactoring changes, this class will | 24 // Provides the part of the audio pipeline delivering audio from a |
21 // take on the functionality of managing sink connections and the audio data | 25 // MediaStreamAudioSource to one or more MediaStreamAudioSinks. An instance of |
22 // flow between source and sinks. The WebRTC-specific elements will then be | 26 // this class is owned by blink::WebMediaStreamTrack, and clients should use |
23 // moved into a subclass. http://crbug.com/577874 | 27 // From() to gain access to a MediaStreamAudioTrack. |
24 class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { | 28 class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { |
25 public: | 29 public: |
26 explicit MediaStreamAudioTrack(bool is_local_track); | 30 explicit MediaStreamAudioTrack(bool is_local_track); |
27 | 31 |
28 // Subclasses must ensure the track is stopped (i.e., Stop() has been called | 32 // Subclasses must ensure the track is stopped (i.e., Stop() has been called |
29 // at least once) before this destructor is invoked. | 33 // at least once) before this destructor is invoked. |
30 ~MediaStreamAudioTrack() override; | 34 ~MediaStreamAudioTrack() override; |
31 | 35 |
32 // Returns the MediaStreamAudioTrack instance owned by the given blink |track| | 36 // Returns the MediaStreamAudioTrack instance owned by the given blink |track| |
33 // or null. | 37 // or null. |
34 static MediaStreamAudioTrack* From(const blink::WebMediaStreamTrack& track); | 38 static MediaStreamAudioTrack* From(const blink::WebMediaStreamTrack& track); |
35 | 39 |
40 // Provides a weak reference to this MediaStreamAudioTrack which is | |
41 // invalidated when Stop() is called. The weak pointer may only be | |
42 // dereferenced on the main thread. | |
43 base::WeakPtr<MediaStreamAudioTrack> GetWeakPtr() { | |
44 return weak_factory_.GetWeakPtr(); | |
45 } | |
46 | |
36 // Add a sink to the track. This function will trigger a OnSetFormat() | 47 // Add a sink to the track. This function will trigger a OnSetFormat() |
37 // call on the |sink|. | 48 // call on the |sink| before the first chunk of audio is delivered. |
38 // Called on the main render thread. | 49 void AddSink(MediaStreamAudioSink* sink); |
39 virtual void AddSink(MediaStreamAudioSink* sink) = 0; | |
40 | 50 |
41 // Remove a sink from the track. | 51 // Remove a sink from the track. When this method returns, the sink's |
42 // Called on the main render thread. | 52 // OnSetFormat() and OnData() methods will not be called again on any thread. |
43 virtual void RemoveSink(MediaStreamAudioSink* sink) = 0; | 53 void RemoveSink(MediaStreamAudioSink* sink); |
44 | |
45 // TODO(tommi, xians): Remove this method. | |
46 // TODO(miu): See class-level TODO comment. ;-) | |
47 virtual webrtc::AudioTrackInterface* GetAudioAdapter(); | |
48 | 54 |
49 // Returns the output format of the capture source. May return an invalid | 55 // Returns the output format of the capture source. May return an invalid |
50 // AudioParameters if the format is not yet available. | 56 // AudioParameters if the format is not yet available. |
51 // Called on the main render thread. | 57 // Called on the main render thread. |
52 // TODO(tommi): This method appears to only be used by Pepper and in fact | 58 // TODO(tommi): This method appears to only be used by Pepper and in fact |
53 // does not appear to be necessary there. We should remove it since it adds | 59 // does not appear to be necessary there. We should remove it since it adds |
54 // to the complexity of all types of audio tracks+source implementations. | 60 // to the complexity of all types of audio tracks+source implementations. |
55 virtual media::AudioParameters GetOutputFormat() const = 0; | 61 // http://crbug.com/577874 |
56 | 62 media::AudioParameters GetOutputFormat() const; |
57 // Called to notify this track that the flow of audio data has started from | |
58 // the source. |stop_callback| is run by Stop() when the source must halt the | |
59 // flow of audio data to this track. | |
60 void Start(const base::Closure& stop_callback); | |
61 | 63 |
62 // Halts the flow of audio data from the source (and to the sinks), and then | 64 // Halts the flow of audio data from the source (and to the sinks), and then |
63 // invokes OnStop() to perform any additional actions. | 65 // invokes OnStop() to perform any additional actions. |
64 void Stop() final; | 66 void Stop() final; |
65 | 67 |
68 // MediaStreamTrack override. | |
69 void SetEnabled(bool enabled) override; | |
70 | |
71 // Returns a unique class identifier. Some subclasses override and use this | |
72 // method to provide safe down-casting to their type. | |
73 virtual void* GetClassIdentifier() const; | |
74 | |
66 protected: | 75 protected: |
67 // Called by Stop() after the source has halted the flow of audio data. | 76 // Called by Stop() after the source has halted the flow of audio data. |
68 virtual void OnStop(); | 77 virtual void OnStop(); |
o1ka
2016/05/04 08:49:23
Nice that we don't have it any more!
| |
69 | 78 |
70 private: | 79 private: |
80 friend class MediaStreamAudioSource; | |
81 friend class MediaStreamAudioDeliverer<MediaStreamAudioTrack>; | |
82 | |
83 // Called by MediaStreamAudioSource to notify this track that the flow of | |
84 // audio data has started from the source. |stop_callback| is run by Stop() | |
85 // when the source must halt the flow of audio data to this track. | |
86 void Start(const base::Closure& stop_callback); | |
87 | |
88 // Called by the MediaStreamAudioDeliverer to notify this track of an audio | |
89 // format change. In turn, all MediaStreamAudioSinks will be notified before | |
90 // the next chunk of audio is delivered to them. | |
91 void OnSetFormat(const media::AudioParameters& params); | |
92 | |
93 // Called by the MediaStreamAudioDeliverer to deliver audio data to this | |
94 // track, which in turn delivers the audio to one or more | |
95 // MediaStreamAudioSinks. While this track is disabled, silent audio will be | |
96 // delivered to the sinks instead of the content of |audio_bus|. | |
97 void OnData(const media::AudioBus& audio_bus, base::TimeTicks reference_time); | |
98 | |
99 private: | |
100 // In debug builds, check that all methods that could cause object graph | |
101 // or data flow changes are being called on the main thread. | |
102 base::ThreadChecker thread_checker_; | |
103 | |
71 // Callback provided to Start() which is run when the audio flow must halt. | 104 // Callback provided to Start() which is run when the audio flow must halt. |
72 base::Closure stop_callback_; | 105 base::Closure stop_callback_; |
73 | 106 |
107 // Manages sinks connected to this track and the audio format and data flow. | |
108 MediaStreamAudioDeliverer<MediaStreamAudioSink> deliverer_; | |
109 | |
110 // While false (0), silent audio is delivered to the sinks. | |
111 base::subtle::AtomicWord is_enabled_; | |
o1ka
2016/04/29 09:53:46
Atomic32 maybe? (AtomicWord size varies depending
miu
2016/05/04 02:47:53
Done.
| |
112 | |
113 // Buffer used to deliver silent audio data while this track is disabled. | |
114 std::unique_ptr<media::AudioBus> silent_bus_; | |
115 | |
116 // Provides weak pointers that are valid until Stop() is called. | |
117 base::WeakPtrFactory<MediaStreamAudioTrack> weak_factory_; | |
118 | |
74 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioTrack); | 119 DISALLOW_COPY_AND_ASSIGN(MediaStreamAudioTrack); |
75 }; | 120 }; |
76 | 121 |
77 } // namespace content | 122 } // namespace content |
78 | 123 |
79 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ | 124 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_TRACK_H_ |
OLD | NEW |