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 | |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "base/threading/thread_checker.h" | |
15 #include "content/renderer/media/media_stream_audio_deliverer.h" | |
10 #include "content/renderer/media/media_stream_track.h" | 16 #include "content/renderer/media/media_stream_track.h" |
11 | 17 |
12 namespace webrtc { | |
13 class AudioTrackInterface; | |
14 } // namespace webrtc | |
15 | |
16 namespace content { | 18 namespace content { |
17 | 19 |
18 class MediaStreamAudioSink; | 20 class MediaStreamAudioSink; |
21 class MediaStreamAudioSource; | |
19 | 22 |
20 // TODO(miu): In a soon-upcoming set of refactoring changes, this class will | 23 // Provides the part of the audio pipeline delivering audio from a |
21 // take on the functionality of managing sink connections and the audio data | 24 // MediaStreamAudioSource to one or more MediaStreamAudioSinks. An instance of |
22 // flow between source and sinks. The WebRTC-specific elements will then be | 25 // this class is owned by blink::WebMediaStreamTrack, and clients should use |
23 // moved into a subclass. http://crbug.com/577874 | 26 // From() to gain access to a MediaStreamAudioTrack. |
24 class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { | 27 class CONTENT_EXPORT MediaStreamAudioTrack : public MediaStreamTrack { |
25 public: | 28 public: |
26 explicit MediaStreamAudioTrack(bool is_local_track); | 29 explicit MediaStreamAudioTrack(bool is_local_track); |
27 | 30 |
28 // Subclasses must ensure the track is stopped (i.e., Stop() has been called | 31 // Subclasses must ensure the track is stopped (i.e., Stop() has been called |
29 // at least once) before this destructor is invoked. | 32 // at least once) before this destructor is invoked. |
30 ~MediaStreamAudioTrack() override; | 33 ~MediaStreamAudioTrack() override; |
31 | 34 |
32 // Returns the MediaStreamAudioTrack instance owned by the given blink |track| | 35 // Returns the MediaStreamAudioTrack instance owned by the given blink |track| |
33 // or null. | 36 // or null. |
34 static MediaStreamAudioTrack* From(const blink::WebMediaStreamTrack& track); | 37 static MediaStreamAudioTrack* From(const blink::WebMediaStreamTrack& track); |
35 | 38 |
39 // Provides a weak reference to this MediaStreamAudioTrack which is | |
40 // invalidated when Stop() is called. The weak pointer may only be | |
41 // dereferenced on the main thread. | |
42 base::WeakPtr<MediaStreamAudioTrack> GetWeakPtr() { | |
43 return weak_factory_.GetWeakPtr(); | |
44 } | |
45 | |
36 // Add a sink to the track. This function will trigger a OnSetFormat() | 46 // Add a sink to the track. This function will trigger a OnSetFormat() |
37 // call on the |sink|. | 47 // call on the |sink| before the first chunk of audio is delivered. |
38 // Called on the main render thread. | 48 void AddSink(MediaStreamAudioSink* sink); |
39 virtual void AddSink(MediaStreamAudioSink* sink) = 0; | |
40 | 49 |
41 // Remove a sink from the track. | 50 // Remove a sink from the track. When this method returns, the sink's |
42 // Called on the main render thread. | 51 // OnSetFormat() and OnData() methods will not be called again on any thread. |
43 virtual void RemoveSink(MediaStreamAudioSink* sink) = 0; | 52 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 | 53 |
49 // Returns the output format of the capture source. May return an invalid | 54 // Returns the output format of the capture source. May return an invalid |
50 // AudioParameters if the format is not yet available. | 55 // AudioParameters if the format is not yet available. |
51 // Called on the main render thread. | 56 // Called on the main render thread. |
52 // TODO(tommi): This method appears to only be used by Pepper and in fact | 57 // 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 | 58 // 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. | 59 // to the complexity of all types of audio tracks+source implementations. |
55 virtual media::AudioParameters GetOutputFormat() const = 0; | 60 // http://crbug.com/577874 |
56 | 61 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 | 62 |
62 // Halts the flow of audio data from the source (and to the sinks), and then | 63 // Halts the flow of audio data from the source (and to the sinks), and then |
63 // invokes OnStop() to perform any additional actions. | 64 // invokes OnStop() to perform any additional actions. |
64 void Stop() final; | 65 void Stop() final; |
65 | 66 |
67 // MediaStreamTrack override. | |
68 void SetEnabled(bool enabled) override; | |
69 | |
70 // Returns a unique class identifier. Some subclasses override and use this | |
71 // method to provide safe down-casting to their type. | |
72 virtual void* GetClassIdentifier() const; | |
73 | |
66 protected: | 74 protected: |
67 // Called by Stop() after the source has halted the flow of audio data. | 75 // Called by Stop() after the source has halted the flow of audio data. |
68 virtual void OnStop(); | 76 virtual void OnStop(); |
69 | 77 |
70 private: | 78 private: |
79 friend class MediaStreamAudioSource; | |
80 friend class MediaStreamAudioDeliverer<MediaStreamAudioTrack>; | |
81 | |
82 // Called by MediaStreamAudioSource to notify this track that the flow of | |
83 // audio data has started from the source. |stop_callback| is run by Stop() | |
84 // when the source must halt the flow of audio data to this track. | |
85 void Start(const base::Closure& stop_callback); | |
perkj_chrome
2016/04/20 13:34:54
No risk that MediaStreamTrack::Stop() is called be
miu
2016/04/20 22:04:53
No risk. MediaStreamAudioSource::ConnectToTrack()
| |
86 | |
87 // Called by the MediaStreamAudioDeliverer to notify this track of an audio | |
88 // format change. In turn, all MediaStreamAudioSinks will be notified before | |
89 // the next chunk of audio is delivered to them. | |
90 void OnSetFormat(const media::AudioParameters& params); | |
91 | |
92 // Called by the MediaStreamAudioDeliverer to deliver audio data to this | |
93 // track, which in turn delivers the audio to one or more | |
94 // MediaStreamAudioSinks. While this track is disabled, silent audio will be | |
95 // delivered to the sinks instead of the content of |audio_bus|. | |
96 void OnData(const media::AudioBus& audio_bus, base::TimeTicks reference_time); | |
97 | |
98 private: | |
99 // In debug builds, check that all methods that could cause object graph | |
100 // or data flow changes are being called on the main thread. | |
101 base::ThreadChecker thread_checker_; | |
102 | |
71 // Callback provided to Start() which is run when the audio flow must halt. | 103 // Callback provided to Start() which is run when the audio flow must halt. |
72 base::Closure stop_callback_; | 104 base::Closure stop_callback_; |
73 | 105 |
106 // Manages sinks connected to this track and the audio format and data flow. | |
107 MediaStreamAudioDeliverer<MediaStreamAudioSink> deliverer_; | |
108 | |
109 // While false, silent audio is delivered to the sinks. | |
110 bool is_enabled_; | |
111 base::Lock enabled_lock_; | |
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 |