Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1100)

Side by Side Diff: remoting/protocol/webrtc_audio_sink_adapter.cc

Issue 2650633003: Fix threading issues in the audio pipeline for WebRTC remoting protocol. (Closed)
Patch Set: . Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/protocol/webrtc_audio_sink_adapter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "remoting/protocol/webrtc_audio_sink_adapter.h" 5 #include "remoting/protocol/webrtc_audio_sink_adapter.h"
6 6
7 #include "base/bind.h"
7 #include "base/callback.h" 8 #include "base/callback.h"
8 #include "remoting/proto/audio.pb.h" 9 #include "remoting/proto/audio.pb.h"
9 #include "remoting/protocol/audio_stub.h" 10 #include "remoting/protocol/audio_stub.h"
10 11
11 namespace remoting { 12 namespace remoting {
12 namespace protocol { 13 namespace protocol {
13 14
14 WebrtcAudioSinkAdapter::WebrtcAudioSinkAdapter( 15 WebrtcAudioSinkAdapter::WebrtcAudioSinkAdapter(
15 scoped_refptr<webrtc::MediaStreamInterface> stream, 16 scoped_refptr<webrtc::MediaStreamInterface> stream,
16 base::WeakPtr<AudioStub> audio_stub) { 17 base::WeakPtr<AudioStub> audio_stub)
17 audio_stub_ = audio_stub; 18 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
18 19 audio_stub_(audio_stub),
19 media_stream_ = std::move(stream); 20 media_stream_(std::move(stream)) {
20
21 webrtc::AudioTrackVector audio_tracks = media_stream_->GetAudioTracks(); 21 webrtc::AudioTrackVector audio_tracks = media_stream_->GetAudioTracks();
22 22
23 // Caller must verify that the media stream contains audio tracks. 23 // Caller must verify that the media stream contains audio tracks.
24 DCHECK(!audio_tracks.empty()); 24 DCHECK(!audio_tracks.empty());
25
26 if (audio_tracks.size() > 1U) { 25 if (audio_tracks.size() > 1U) {
27 LOG(WARNING) << "Received media stream with multiple audio tracks."; 26 LOG(WARNING) << "Received media stream with multiple audio tracks.";
28 } 27 }
29
30 audio_track_ = audio_tracks[0]; 28 audio_track_ = audio_tracks[0];
31 audio_track_->GetSource()->AddSink(this); 29 audio_track_->GetSource()->AddSink(this);
32 } 30 }
33 31
34 WebrtcAudioSinkAdapter::~WebrtcAudioSinkAdapter() { 32 WebrtcAudioSinkAdapter::~WebrtcAudioSinkAdapter() {
35 audio_track_->GetSource()->RemoveSink(this); 33 audio_track_->GetSource()->RemoveSink(this);
36 } 34 }
37 35
38 void WebrtcAudioSinkAdapter::OnData(const void* audio_data, 36 void WebrtcAudioSinkAdapter::OnData(const void* audio_data,
39 int bits_per_sample, 37 int bits_per_sample,
40 int sample_rate, 38 int sample_rate,
41 size_t number_of_channels, 39 size_t number_of_channels,
42 size_t number_of_frames) { 40 size_t number_of_frames) {
43 if (!audio_stub_)
44 return;
45
46 std::unique_ptr<AudioPacket> audio_packet(new AudioPacket()); 41 std::unique_ptr<AudioPacket> audio_packet(new AudioPacket());
47 audio_packet->set_encoding(AudioPacket::ENCODING_RAW); 42 audio_packet->set_encoding(AudioPacket::ENCODING_RAW);
48 43
49 switch (sample_rate) { 44 switch (sample_rate) {
50 case 44100: 45 case 44100:
51 audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_44100); 46 audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_44100);
52 break; 47 break;
53 case 48000: 48 case 48000:
54 audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_48000); 49 audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_48000);
55 break; 50 break;
(...skipping 10 matching lines...) Expand all
66 61
67 if (number_of_channels != 2) { 62 if (number_of_channels != 2) {
68 LOG(WARNING) << "Unsupported number of channels: " << number_of_channels; 63 LOG(WARNING) << "Unsupported number of channels: " << number_of_channels;
69 return; 64 return;
70 } 65 }
71 audio_packet->set_channels(AudioPacket::CHANNELS_STEREO); 66 audio_packet->set_channels(AudioPacket::CHANNELS_STEREO);
72 67
73 size_t data_size = 68 size_t data_size =
74 number_of_frames * number_of_channels * (bits_per_sample / 8); 69 number_of_frames * number_of_channels * (bits_per_sample / 8);
75 audio_packet->add_data(reinterpret_cast<const char*>(audio_data), data_size); 70 audio_packet->add_data(reinterpret_cast<const char*>(audio_data), data_size);
76 audio_stub_->ProcessAudioPacket(std::move(audio_packet), base::Closure()); 71
72 task_runner_->PostTask(
73 FROM_HERE, base::Bind(&AudioStub::ProcessAudioPacket, audio_stub_,
74 base::Passed(&audio_packet), base::Closure()));
77 } 75 }
78 76
79 } // namespace protocol 77 } // namespace protocol
80 } // namespace remoting 78 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/webrtc_audio_sink_adapter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698