OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/webrtc_audio_sink_adapter.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "remoting/proto/audio.pb.h" |
| 9 #include "remoting/protocol/audio_stub.h" |
| 10 |
| 11 namespace remoting { |
| 12 namespace protocol { |
| 13 |
| 14 WebrtcAudioSinkAdapter::WebrtcAudioSinkAdapter( |
| 15 scoped_refptr<webrtc::MediaStreamInterface> stream, |
| 16 base::WeakPtr<AudioStub> audio_stub) { |
| 17 audio_stub_ = audio_stub; |
| 18 |
| 19 media_stream_ = std::move(stream); |
| 20 |
| 21 webrtc::AudioTrackVector audio_tracks = media_stream_->GetAudioTracks(); |
| 22 |
| 23 // Caller must verify that the media stream contains audio tracks. |
| 24 DCHECK(!audio_tracks.empty()); |
| 25 |
| 26 if (audio_tracks.size() > 1U) { |
| 27 LOG(WARNING) << "Received media stream with multiple audio tracks."; |
| 28 } |
| 29 |
| 30 audio_track_ = audio_tracks[0]; |
| 31 audio_track_->GetSource()->AddSink(this); |
| 32 } |
| 33 |
| 34 WebrtcAudioSinkAdapter::~WebrtcAudioSinkAdapter() { |
| 35 audio_track_->GetSource()->RemoveSink(this); |
| 36 } |
| 37 |
| 38 void WebrtcAudioSinkAdapter::OnData(const void* audio_data, |
| 39 int bits_per_sample, |
| 40 int sample_rate, |
| 41 size_t number_of_channels, |
| 42 size_t number_of_frames) { |
| 43 if (!audio_stub_) |
| 44 return; |
| 45 |
| 46 std::unique_ptr<AudioPacket> audio_packet(new AudioPacket()); |
| 47 audio_packet->set_encoding(AudioPacket::ENCODING_RAW); |
| 48 |
| 49 switch (sample_rate) { |
| 50 case 44100: |
| 51 audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_44100); |
| 52 break; |
| 53 case 48000: |
| 54 audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_48000); |
| 55 break; |
| 56 default: |
| 57 LOG(WARNING) << "Unsupported sampling rate: " << sample_rate; |
| 58 return; |
| 59 } |
| 60 |
| 61 if (bits_per_sample != 16) { |
| 62 LOG(WARNING) << "Unsupported bits/sample: " << bits_per_sample; |
| 63 return; |
| 64 } |
| 65 audio_packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
| 66 |
| 67 if (number_of_channels != 2) { |
| 68 LOG(WARNING) << "Unsupported number of channels: " << number_of_channels; |
| 69 return; |
| 70 } |
| 71 audio_packet->set_channels(AudioPacket::CHANNELS_STEREO); |
| 72 |
| 73 size_t data_size = |
| 74 number_of_frames * number_of_channels * (bits_per_sample / 8); |
| 75 audio_packet->add_data(reinterpret_cast<const char*>(audio_data), data_size); |
| 76 audio_stub_->ProcessAudioPacket(std::move(audio_packet), base::Closure()); |
| 77 } |
| 78 |
| 79 } // namespace protocol |
| 80 } // namespace remoting |
OLD | NEW |