OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_REMOTE_AUDIO_SOURCE_H_ | |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_REMOTE_AUDIO_SOURCE_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "content/renderer/media/media_stream_audio_source.h" | |
13 #include "content/renderer/media/media_stream_audio_track.h" | |
14 #include "third_party/webrtc/api/mediastreaminterface.h" | |
15 | |
16 namespace media { | |
17 class AudioBus; | |
18 } | |
19 | |
20 namespace content { | |
21 | |
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; | |
30 | |
31 // If |track| is an instance of PeerConnectionRemoteAudioTrack, return a | |
32 // type-casted pointer to it. Otherwise, return null. | |
33 static PeerConnectionRemoteAudioTrack* From(MediaStreamAudioTrack* track); | |
34 | |
35 webrtc::AudioTrackInterface* track_interface() const { | |
36 return track_interface_.get(); | |
37 } | |
38 | |
39 // MediaStreamAudioTrack override. | |
40 void SetEnabled(bool enabled) override; | |
41 | |
42 private: | |
43 // MediaStreamAudioTrack overrides. | |
44 void* GetClassIdentifier() const final; | |
45 | |
46 const scoped_refptr<webrtc::AudioTrackInterface> track_interface_; | |
47 | |
48 // In debug builds, check that all methods that could cause object graph | |
49 // or data flow changes are being called on the main thread. | |
50 base::ThreadChecker thread_checker_; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(PeerConnectionRemoteAudioTrack); | |
53 }; | |
54 | |
55 // Represents the audio provided by the receiving end of a PeerConnection. | |
56 class PeerConnectionRemoteAudioSource final | |
57 : NON_EXPORTED_BASE(public MediaStreamAudioSource), | |
58 NON_EXPORTED_BASE(protected webrtc::AudioTrackSinkInterface) { | |
59 public: | |
60 explicit PeerConnectionRemoteAudioSource( | |
61 scoped_refptr<webrtc::AudioTrackInterface> track_interface); | |
62 ~PeerConnectionRemoteAudioSource() final; | |
63 | |
64 protected: | |
65 // MediaStreamAudioSource implementation. | |
66 std::unique_ptr<MediaStreamAudioTrack> CreateMediaStreamAudioTrack( | |
67 const std::string& id) final; | |
68 bool EnsureSourceIsStarted() final; | |
69 void EnsureSourceIsStopped() final; | |
70 | |
71 // webrtc::AudioTrackSinkInterface implementation. | |
72 void OnData(const void* audio_data, int bits_per_sample, int sample_rate, | |
73 size_t number_of_channels, size_t number_of_frames) final; | |
74 | |
75 private: | |
76 // Interface to the implementation that calls OnData(). | |
77 const scoped_refptr<webrtc::AudioTrackInterface> track_interface_; | |
78 | |
79 // In debug builds, check that all methods that could cause object graph | |
80 // or data flow changes are being called on the main thread. | |
81 base::ThreadChecker thread_checker_; | |
82 | |
83 // True if |this| is receiving an audio flow as a sink of the remote | |
84 // PeerConnection via |track_interface_|. | |
85 bool is_sink_of_peer_connection_; | |
86 | |
87 // Buffer for converting from interleaved signed-integer PCM samples to the | |
88 // planar float format. Only used on the thread that calls OnData(). | |
89 std::unique_ptr<media::AudioBus> audio_bus_; | |
90 | |
91 // In debug builds, use a "try lock" to sanity-check that there are no | |
92 // concurrent calls to OnData(). See notes in OnData() implementation. | |
93 #ifndef NDEBUG | |
94 base::Lock single_audio_thread_guard_; | |
95 #endif | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(PeerConnectionRemoteAudioSource); | |
98 }; | |
99 | |
100 } // namespace content | |
101 | |
102 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_REMOTE_AUDIO_SOURCE_H_ | |
OLD | NEW |