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_WEBRTC_MEDIA_STREAM_REMOTE_AUDIO_TRACK_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_REMOTE_AUDIO_SOURCE_H_ |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_AUDIO_TRACK_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_REMOTE_AUDIO_SOURCE_H_ |
| 7 |
| 8 #include <memory> |
7 | 9 |
8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
9 #include "base/threading/thread_checker.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "content/renderer/media/media_stream_audio_source.h" |
10 #include "content/renderer/media/media_stream_audio_track.h" | 13 #include "content/renderer/media/media_stream_audio_track.h" |
11 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 14 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 15 |
| 16 namespace media { |
| 17 class AudioBus; |
| 18 } |
12 | 19 |
13 namespace content { | 20 namespace content { |
14 | 21 |
15 class MediaStreamRemoteAudioSource; | 22 // PeerConnectionRemoteAudioTrack is a WebRTC specific implementation of an |
| 23 // audio track whose data is sourced from a PeerConnection. |
| 24 class PeerConnectionRemoteAudioTrack final |
| 25 : NON_EXPORTED_BASE(public MediaStreamAudioTrack) { |
| 26 public: |
| 27 explicit PeerConnectionRemoteAudioTrack( |
| 28 scoped_refptr<webrtc::AudioTrackInterface> track_interface); |
| 29 ~PeerConnectionRemoteAudioTrack() final; |
16 | 30 |
17 // MediaStreamRemoteAudioTrack is a WebRTC specific implementation of an | 31 // If |track| is an instance of PeerConnectionRemoteAudioTrack, return a |
18 // audio track received from a PeerConnection. | 32 // type-casted pointer to it. Otherwise, return null. |
19 // TODO(tommi): Chrome shouldn't have to care about remote vs local so | 33 static PeerConnectionRemoteAudioTrack* From(MediaStreamAudioTrack* track); |
20 // we should have a single track implementation that delegates to the | |
21 // sources that do different things depending on the type of source. | |
22 class MediaStreamRemoteAudioTrack : public MediaStreamAudioTrack { | |
23 public: | |
24 explicit MediaStreamRemoteAudioTrack( | |
25 const blink::WebMediaStreamSource& source, bool enabled); | |
26 ~MediaStreamRemoteAudioTrack() override; | |
27 | 34 |
28 // MediaStreamTrack override. | 35 webrtc::AudioTrackInterface* track_interface() const { |
| 36 return track_interface_.get(); |
| 37 } |
| 38 |
| 39 // MediaStreamAudioTrack override. |
29 void SetEnabled(bool enabled) override; | 40 void SetEnabled(bool enabled) override; |
30 | 41 |
| 42 private: |
31 // MediaStreamAudioTrack overrides. | 43 // MediaStreamAudioTrack overrides. |
32 void AddSink(MediaStreamAudioSink* sink) override; | 44 void* GetClassIdentifier() const final; |
33 void RemoveSink(MediaStreamAudioSink* sink) override; | 45 void OnStop() final; |
34 media::AudioParameters GetOutputFormat() const override; | |
35 | 46 |
36 webrtc::AudioTrackInterface* GetAudioAdapter() override; | 47 const scoped_refptr<webrtc::AudioTrackInterface> track_interface_; |
| 48 |
| 49 // In debug builds, check that all methods that could cause object graph |
| 50 // or data flow changes are being called on the main thread. |
| 51 base::ThreadChecker thread_checker_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(PeerConnectionRemoteAudioTrack); |
| 54 }; |
| 55 |
| 56 // Represents the audio provided by the receiving end of a PeerConnection. |
| 57 class PeerConnectionRemoteAudioSource final |
| 58 : NON_EXPORTED_BASE(public MediaStreamAudioSource), |
| 59 NON_EXPORTED_BASE(protected webrtc::AudioTrackSinkInterface) { |
| 60 public: |
| 61 explicit PeerConnectionRemoteAudioSource( |
| 62 scoped_refptr<webrtc::AudioTrackInterface> track_interface); |
| 63 ~PeerConnectionRemoteAudioSource() final; |
| 64 |
| 65 protected: |
| 66 // MediaStreamAudioSource implementation. |
| 67 std::unique_ptr<MediaStreamAudioTrack> CreateMediaStreamAudioTrack( |
| 68 const std::string& id) final; |
| 69 bool EnsureSourceIsStarted() final; |
| 70 void EnsureSourceIsStopped() final; |
| 71 |
| 72 // webrtc::AudioTrackSinkInterface implementation. |
| 73 void OnData(const void* audio_data, int bits_per_sample, int sample_rate, |
| 74 size_t number_of_channels, size_t number_of_frames) final; |
37 | 75 |
38 private: | 76 private: |
39 // MediaStreamAudioTrack override. | 77 // Interface to the implementation that calls OnData(). |
40 void OnStop() final; | 78 const scoped_refptr<webrtc::AudioTrackInterface> track_interface_; |
41 | 79 |
42 MediaStreamRemoteAudioSource* source() const; | 80 // In debug builds, check that all methods that could cause object graph |
| 81 // or data flow changes are being called on the main thread. |
| 82 base::ThreadChecker thread_checker_; |
43 | 83 |
44 blink::WebMediaStreamSource source_; | 84 // True if |this| is receiving an audio flow as a sink of the remote |
45 bool enabled_; | 85 // PeerConnection via |track_interface_|. |
46 }; | 86 bool is_sink_of_peer_connection_; |
47 | 87 |
48 // Inheriting from ExtraData directly since MediaStreamAudioSource has | 88 // Buffer for converting from interleaved signed-integer PCM samples to the |
49 // too much unrelated bloat. | 89 // planar float format. Only used on the thread that calls OnData(). |
50 // TODO(tommi): MediaStreamAudioSource needs refactoring. | 90 std::unique_ptr<media::AudioBus> audio_bus_; |
51 // TODO(miu): On it! ;-) | |
52 class MediaStreamRemoteAudioSource | |
53 : public blink::WebMediaStreamSource::ExtraData { | |
54 public: | |
55 explicit MediaStreamRemoteAudioSource( | |
56 const scoped_refptr<webrtc::AudioTrackInterface>& track); | |
57 ~MediaStreamRemoteAudioSource() override; | |
58 | 91 |
59 // Controls whether or not the source is included in the main, mixed, audio | 92 // In debug builds, use a "try lock" to sanity-check that there are no |
60 // output from WebRTC as rendered by WebRtcAudioRenderer (media players). | 93 // concurrent calls to OnData(). See notes in OnData() implementation. |
61 void SetEnabledForMixing(bool enabled); | 94 #ifndef NDEBUG |
| 95 base::Lock single_audio_thread_guard_; |
| 96 #endif |
62 | 97 |
63 // Adds an audio sink for a track belonging to this source. | 98 DISALLOW_COPY_AND_ASSIGN(PeerConnectionRemoteAudioSource); |
64 // |enabled| is the enabled state of the track and can be updated via | |
65 // a call to SetSinksEnabled. | |
66 void AddSink(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track, | |
67 bool enabled); | |
68 | |
69 // Removes an audio sink for a track belonging to this source. | |
70 void RemoveSink(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track); | |
71 | |
72 // Turns audio callbacks on/off for all sinks belonging to a track. | |
73 void SetSinksEnabled(MediaStreamAudioTrack* track, bool enabled); | |
74 | |
75 // Removes all sinks belonging to a track. | |
76 void RemoveAll(MediaStreamAudioTrack* track); | |
77 | |
78 webrtc::AudioTrackInterface* GetAudioAdapter(); | |
79 | |
80 private: | |
81 class AudioSink; | |
82 std::unique_ptr<AudioSink> sink_; | |
83 const scoped_refptr<webrtc::AudioTrackInterface> track_; | |
84 base::ThreadChecker thread_checker_; | |
85 }; | 99 }; |
86 | 100 |
87 } // namespace content | 101 } // namespace content |
88 | 102 |
89 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_REMOTE_AUDIO_TRACK_H_ | 103 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_REMOTE_AUDIO_SOURCE_H_ |
OLD | NEW |