summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/peerconnection/rtc_encoded_audio_frame_delegate.cc
blob: 2c71c02c522381ed45d371e20c8d0cc54892f5bb (plain)
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
// Copyright 2020 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 "third_party/blink/renderer/modules/peerconnection/rtc_encoded_audio_frame_delegate.h"

#include <utility>

#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"

namespace blink {

const void* RTCEncodedAudioFramesAttachment::kAttachmentKey;

RTCEncodedAudioFrameDelegate::RTCEncodedAudioFrameDelegate(
    std::unique_ptr<webrtc::TransformableFrameInterface> webrtc_frame,
    Vector<uint32_t> contributing_sources)
    : webrtc_frame_(std::move(webrtc_frame)),
      contributing_sources_(std::move(contributing_sources)) {}

uint64_t RTCEncodedAudioFrameDelegate::Timestamp() const {
  MutexLocker lock(mutex_);
  return webrtc_frame_ ? webrtc_frame_->GetTimestamp() : 0;
}

DOMArrayBuffer* RTCEncodedAudioFrameDelegate::CreateDataBuffer() const {
  ArrayBufferContents contents;
  {
    MutexLocker lock(mutex_);
    if (!webrtc_frame_)
      return nullptr;

    auto data = webrtc_frame_->GetData();
    contents =
        ArrayBufferContents(data.size(), 1, ArrayBufferContents::kNotShared,
                            ArrayBufferContents::kDontInitialize);
    if (UNLIKELY(!contents.Data()))
      OOM_CRASH(data.size());
    memcpy(contents.Data(), data.data(), data.size());
  }
  return DOMArrayBuffer::Create(std::move(contents));
}

void RTCEncodedAudioFrameDelegate::SetData(const DOMArrayBuffer* data) {
  MutexLocker lock(mutex_);
  if (webrtc_frame_ && data) {
    webrtc_frame_->SetData(rtc::ArrayView<const uint8_t>(
        static_cast<const uint8_t*>(data->Data()), data->ByteLengthAsSizeT()));
  }
}

uint32_t RTCEncodedAudioFrameDelegate::Ssrc() const {
  MutexLocker lock(mutex_);
  return webrtc_frame_ ? webrtc_frame_->GetSsrc() : 0;
}

Vector<uint32_t> RTCEncodedAudioFrameDelegate::ContributingSources() const {
  MutexLocker lock(mutex_);
  return contributing_sources_;
}

std::unique_ptr<webrtc::TransformableFrameInterface>
RTCEncodedAudioFrameDelegate::PassWebRtcFrame() {
  MutexLocker lock(mutex_);
  return std::move(webrtc_frame_);
}

}  // namespace blink