File: webrtc_audio_sink_adapter.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (80 lines) | stat: -rw-r--r-- 2,517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/webrtc_audio_sink_adapter.h"

#include "base/callback.h"
#include "remoting/proto/audio.pb.h"
#include "remoting/protocol/audio_stub.h"

namespace remoting {
namespace protocol {

WebrtcAudioSinkAdapter::WebrtcAudioSinkAdapter(
    scoped_refptr<webrtc::MediaStreamInterface> stream,
    base::WeakPtr<AudioStub> audio_stub) {
  audio_stub_ = audio_stub;

  media_stream_ = std::move(stream);

  webrtc::AudioTrackVector audio_tracks = media_stream_->GetAudioTracks();

  // Caller must verify that the media stream contains audio tracks.
  DCHECK(!audio_tracks.empty());

  if (audio_tracks.size() > 1U) {
    LOG(WARNING) << "Received media stream with multiple audio tracks.";
  }

  audio_track_ = audio_tracks[0];
  audio_track_->GetSource()->AddSink(this);
}

WebrtcAudioSinkAdapter::~WebrtcAudioSinkAdapter() {
  audio_track_->GetSource()->RemoveSink(this);
}

void WebrtcAudioSinkAdapter::OnData(const void* audio_data,
                                    int bits_per_sample,
                                    int sample_rate,
                                    size_t number_of_channels,
                                    size_t number_of_frames) {
  if (!audio_stub_)
    return;

  std::unique_ptr<AudioPacket> audio_packet(new AudioPacket());
  audio_packet->set_encoding(AudioPacket::ENCODING_RAW);

  switch (sample_rate) {
    case 44100:
      audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_44100);
      break;
    case 48000:
      audio_packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_48000);
      break;
    default:
      LOG(WARNING) << "Unsupported sampling rate: " << sample_rate;
      return;
  }

  if (bits_per_sample != 16) {
    LOG(WARNING) << "Unsupported bits/sample: " << bits_per_sample;
    return;
  }
  audio_packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);

  if (number_of_channels != 2) {
    LOG(WARNING) << "Unsupported number of channels: " << number_of_channels;
    return;
  }
  audio_packet->set_channels(AudioPacket::CHANNELS_STEREO);

  size_t data_size =
      number_of_frames * number_of_channels * (bits_per_sample / 8);
  audio_packet->add_data(reinterpret_cast<const char*>(audio_data), data_size);
  audio_stub_->ProcessAudioPacket(std::move(audio_packet), base::Closure());
}

}  // namespace protocol
}  // namespace remoting