File: webrtc_audio_sink_adapter.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (79 lines) | stat: -rw-r--r-- 2,789 bytes parent folder | download | duplicates (5)
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
// Copyright 2016 The Chromium Authors
// 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/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "remoting/proto/audio.pb.h"
#include "remoting/protocol/audio_stub.h"

namespace remoting::protocol {

WebrtcAudioSinkAdapter::WebrtcAudioSinkAdapter(
    webrtc::scoped_refptr<webrtc::MediaStreamInterface> stream,
    base::WeakPtr<AudioStub> audio_stub)
    : task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
      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) {
  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);

  task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&AudioStub::ProcessAudioPacket, audio_stub_,
                                std::move(audio_packet), base::OnceClosure()));
}

}  // namespace remoting::protocol