OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "content/renderer/media/media_stream_audio_source.h" | 5 #include "content/renderer/media/media_stream_audio_source.h" |
6 | 6 |
7 #include "content/renderer/media/webrtc_local_audio_track.h" | 7 #include "base/bind.h" |
8 #include "content/renderer/render_frame_impl.h" | 8 #include "content/renderer/media/media_stream_audio_track.h" |
9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 10 #include "third_party/WebKit/public/platform/WebString.h" |
10 | 11 |
11 namespace content { | 12 namespace content { |
12 | 13 |
13 MediaStreamAudioSource::MediaStreamAudioSource( | 14 MediaStreamAudioSource::MediaStreamAudioSource(bool is_local_source) |
14 int render_frame_id, | 15 : is_local_source_(is_local_source), |
15 const StreamDeviceInfo& device_info, | 16 is_stopped_(false), |
16 const SourceStoppedCallback& stop_callback, | |
17 PeerConnectionDependencyFactory* factory) | |
18 : render_frame_id_(render_frame_id), factory_(factory), | |
19 weak_factory_(this) { | 17 weak_factory_(this) { |
20 SetDeviceInfo(device_info); | 18 DVLOG(1) << "MediaStreamAudioSource@" << this << "::MediaStreamAudioSource(" |
21 SetStopCallback(stop_callback); | 19 << (is_local_source_ ? "local" : "remote") << " source)"; |
22 } | 20 } |
23 | 21 |
24 MediaStreamAudioSource::MediaStreamAudioSource() | 22 MediaStreamAudioSource::~MediaStreamAudioSource() { |
25 : render_frame_id_(-1), factory_(NULL), weak_factory_(this) { | 23 DCHECK(thread_checker_.CalledOnValidThread()); |
| 24 DVLOG(1) << "MediaStreamAudioSource@" << this << " is being destroyed."; |
26 } | 25 } |
27 | 26 |
28 MediaStreamAudioSource::~MediaStreamAudioSource() {} | |
29 | |
30 // static | 27 // static |
31 MediaStreamAudioSource* MediaStreamAudioSource::From( | 28 MediaStreamAudioSource* MediaStreamAudioSource::From( |
32 const blink::WebMediaStreamSource& source) { | 29 const blink::WebMediaStreamSource& source) { |
33 if (source.isNull() || | 30 if (source.isNull() || |
34 source.getType() != blink::WebMediaStreamSource::TypeAudio) { | 31 source.getType() != blink::WebMediaStreamSource::TypeAudio) { |
35 return nullptr; | 32 return nullptr; |
36 } | 33 } |
37 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); | 34 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); |
38 } | 35 } |
39 | 36 |
40 void MediaStreamAudioSource::DoStopSource() { | 37 bool MediaStreamAudioSource::ConnectToTrack( |
41 if (audio_capturer_) | 38 const blink::WebMediaStreamTrack& blink_track) { |
42 audio_capturer_->Stop(); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
43 if (webaudio_capturer_) | 40 DCHECK(!blink_track.isNull()); |
44 webaudio_capturer_->Stop(); | 41 |
| 42 // Sanity-check that there is not already a MediaStreamAudioTrack instance |
| 43 // associated with |blink_track|. |
| 44 if (MediaStreamAudioTrack::From(blink_track)) { |
| 45 LOG(DFATAL) |
| 46 << "Attempting to connect another source to a WebMediaStreamTrack."; |
| 47 return false; |
| 48 } |
| 49 |
| 50 // Unless the source has already been permanently stopped, ensure it is |
| 51 // started. If the source is not started, the new MediaStreamAudioTrack will |
| 52 // be initialized to the stopped/ended state. |
| 53 const bool initializing_ended_track = is_stopped_ || !EnsureSourceIsStarted(); |
| 54 |
| 55 // Create and initialize a new MediaStreamAudioTrack and pass ownership of it |
| 56 // to the WebMediaStreamTrack. |
| 57 blink::WebMediaStreamTrack mutable_blink_track = blink_track; |
| 58 mutable_blink_track.setExtraData( |
| 59 CreateMediaStreamAudioTrack(blink_track.id().utf8()).release()); |
| 60 |
| 61 // Propagate initial "enabled" state. |
| 62 MediaStreamAudioTrack* const track = MediaStreamAudioTrack::From(blink_track); |
| 63 DCHECK(track); |
| 64 track->SetEnabled(blink_track.isEnabled()); |
| 65 |
| 66 // If the source is stopped, do not start the track. |
| 67 if (initializing_ended_track) |
| 68 return false; |
| 69 |
| 70 track->Start(base::Bind(&MediaStreamAudioSource::StopAudioDeliveryTo, |
| 71 weak_factory_.GetWeakPtr(), track)); |
| 72 DVLOG(1) << "Adding MediaStreamAudioTrack@" << track |
| 73 << " as a consumer of MediaStreamAudioSource@" << this << '.'; |
| 74 deliverer_.AddConsumer(track); |
| 75 return true; |
45 } | 76 } |
46 | 77 |
47 void MediaStreamAudioSource::AddTrack( | 78 media::AudioParameters MediaStreamAudioSource::GetAudioParameters() const { |
48 const blink::WebMediaStreamTrack& track, | 79 return deliverer_.GetAudioParameters(); |
49 const blink::WebMediaConstraints& constraints, | 80 } |
50 const ConstraintsCallback& callback) { | |
51 // TODO(xians): Properly implement for audio sources. | |
52 if (!local_audio_source_.get()) { | |
53 if (!factory_->InitializeMediaStreamAudioSource(render_frame_id_, | |
54 constraints, this)) { | |
55 // The source failed to start. | |
56 // UserMediaClientImpl rely on the |stop_callback| to be triggered when | |
57 // the last track is removed from the source. But in this case, the | |
58 // source is is not even started. So we need to fail both adding the | |
59 // track and trigger |stop_callback|. | |
60 callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, ""); | |
61 StopSource(); | |
62 return; | |
63 } | |
64 } | |
65 | 81 |
66 factory_->CreateLocalAudioTrack(track); | 82 void* MediaStreamAudioSource::GetClassIdentifier() const { |
67 callback.Run(this, MEDIA_DEVICE_OK, ""); | 83 return nullptr; |
| 84 } |
| 85 |
| 86 std::unique_ptr<MediaStreamAudioTrack> |
| 87 MediaStreamAudioSource::CreateMediaStreamAudioTrack(const std::string& id) { |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 89 return std::unique_ptr<MediaStreamAudioTrack>( |
| 90 new MediaStreamAudioTrack(is_local_source())); |
| 91 } |
| 92 |
| 93 bool MediaStreamAudioSource::EnsureSourceIsStarted() { |
| 94 DCHECK(thread_checker_.CalledOnValidThread()); |
| 95 DVLOG(1) << "MediaStreamAudioSource@" << this << "::EnsureSourceIsStarted()"; |
| 96 return true; |
| 97 } |
| 98 |
| 99 void MediaStreamAudioSource::EnsureSourceIsStopped() { |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 DVLOG(1) << "MediaStreamAudioSource@" << this << "::EnsureSourceIsStopped()"; |
| 102 } |
| 103 |
| 104 void MediaStreamAudioSource::SetFormat(const media::AudioParameters& params) { |
| 105 DVLOG(1) << "MediaStreamAudioSource@" << this << "::SetFormat(" |
| 106 << params.AsHumanReadableString() << "), was previously set to {" |
| 107 << deliverer_.GetAudioParameters().AsHumanReadableString() << "}."; |
| 108 deliverer_.OnSetFormat(params); |
| 109 } |
| 110 |
| 111 void MediaStreamAudioSource::DeliverDataToTracks( |
| 112 const media::AudioBus& audio_bus, |
| 113 base::TimeTicks reference_time) { |
| 114 deliverer_.OnData(audio_bus, reference_time); |
| 115 } |
| 116 |
| 117 void MediaStreamAudioSource::DoStopSource() { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 EnsureSourceIsStopped(); |
| 120 is_stopped_ = true; |
68 } | 121 } |
69 | 122 |
70 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { | 123 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { |
71 DCHECK(track); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
72 if (audio_capturer_) { | 125 |
73 // The cast here is safe because only WebRtcLocalAudioTracks are ever used | 126 const bool did_remove_last_track = deliverer_.RemoveConsumer(track); |
74 // with WebRtcAudioCapturer sources. | 127 DVLOG(1) << "Removed MediaStreamAudioTrack@" << track |
75 // | 128 << " as a consumer of MediaStreamAudioSource@" << this << '.'; |
76 // TODO(miu): That said, this ugly cast won't be necessary after my | 129 |
77 // soon-upcoming refactoring change. | 130 // The W3C spec requires a source automatically stop when the last track is |
78 audio_capturer_->RemoveTrack(static_cast<WebRtcLocalAudioTrack*>(track)); | 131 // stopped. |
79 } | 132 if (!is_stopped_ && did_remove_last_track) |
80 if (webaudio_capturer_) { | 133 MediaStreamSource::StopSource(); |
81 // A separate source is created for each track, so just stop the source. | |
82 webaudio_capturer_->Stop(); | |
83 } | |
84 } | 134 } |
85 | 135 |
86 } // namespace content | 136 } // namespace content |
OLD | NEW |