File: rtc_encoded_audio_frame_delegate.h

package info (click to toggle)
chromium 141.0.7390.107-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,254,812 kB
  • sloc: cpp: 35,264,957; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,636; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,319; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 4,596 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_RTC_ENCODED_AUDIO_FRAME_DELEGATE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_RTC_ENCODED_AUDIO_FRAME_DELEGATE_H_

#include <stdint.h>

#include <memory>

#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_encoded_audio_frame_metadata.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/peerconnection/peer_connection_util.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
#include "third_party/webrtc/api/frame_transformer_interface.h"
#include "third_party/webrtc/api/units/timestamp.h"

namespace blink {

class DOMArrayBuffer;

// This class wraps a WebRTC audio frame and allows making shallow
// copies. Its purpose is to support making RTCEncodedVideoFrames
// serializable in the same process.
class RTCEncodedAudioFrameDelegate
    : public ThreadSafeRefCounted<RTCEncodedAudioFrameDelegate> {
 public:
  explicit RTCEncodedAudioFrameDelegate(
      std::unique_ptr<webrtc::TransformableAudioFrameInterface> webrtc_frame,
      webrtc::ArrayView<const unsigned int> contributing_sources,
      std::optional<uint16_t> sequence_number);

  uint32_t RtpTimestamp() const;
  DOMArrayBuffer* CreateDataBuffer(v8::Isolate* isolate) const;
  void SetData(const DOMArrayBuffer* data);

  // This method can only be called from the main thread.
  base::expected<void, String> SetWebRtcFrameMetadata(
      ExecutionContext*,
      const RTCEncodedAudioFrameMetadata*);

  std::optional<uint32_t> Ssrc() const;
  std::optional<uint8_t> PayloadType() const;
  std::optional<std::string> MimeType() const;
  std::optional<uint16_t> SequenceNumber() const;
  Vector<uint32_t> ContributingSources() const;
  std::optional<base::TimeTicks> ReceiveTime() const;
  std::optional<CaptureTimeInfo> CaptureTime() const;
  std::optional<base::TimeDelta> SenderCaptureTimeOffset() const;
  std::optional<double> AudioLevel() const;
  std::unique_ptr<webrtc::TransformableAudioFrameInterface> PassWebRtcFrame();
  std::unique_ptr<webrtc::TransformableAudioFrameInterface> CloneWebRtcFrame();

 private:
  std::optional<base::TimeTicks> ComputeReceiveTime() const
      EXCLUSIVE_LOCKS_REQUIRED(&lock_);
  std::optional<CaptureTimeInfo> ComputeCaptureTime() const
      EXCLUSIVE_LOCKS_REQUIRED(&lock_);
  std::optional<base::TimeDelta> ComputeSenderCaptureTimeOffset() const
      EXCLUSIVE_LOCKS_REQUIRED(&lock_);
  std::optional<double> ComputeAudioLevel() const
      EXCLUSIVE_LOCKS_REQUIRED(&lock_);

  mutable base::Lock lock_;
  std::unique_ptr<webrtc::TransformableAudioFrameInterface> webrtc_frame_
      GUARDED_BY(lock_);
  const Vector<uint32_t> contributing_sources_;
  const std::optional<uint16_t> sequence_number_;

  struct Metadata {
    std::optional<uint32_t> ssrc;
    std::optional<uint8_t> payload_type;
    std::optional<std::string> mime_type;
    std::optional<base::TimeTicks> receive_time;
    std::optional<CaptureTimeInfo> capture_time_info;
    std::optional<base::TimeDelta> sender_capture_time_offset;
    std::optional<double> audio_level;
    uint32_t rtp_timestamp = 0;
  };
  // This field is set after the frame is neutered (e.g., written to a stream or
  // transferred).
  Metadata post_neuter_metadata_ GUARDED_BY(lock_);
};

class MODULES_EXPORT RTCEncodedAudioFramesAttachment
    : public SerializedScriptValue::Attachment {
 public:
  static const void* kAttachmentKey;
  RTCEncodedAudioFramesAttachment() = default;
  ~RTCEncodedAudioFramesAttachment() override = default;

  bool IsLockedToAgentCluster() const override {
    return !encoded_audio_frames_.empty();
  }

  Vector<scoped_refptr<RTCEncodedAudioFrameDelegate>>& EncodedAudioFrames() {
    return encoded_audio_frames_;
  }

  const Vector<scoped_refptr<RTCEncodedAudioFrameDelegate>>&
  EncodedAudioFrames() const {
    return encoded_audio_frames_;
  }

 private:
  Vector<scoped_refptr<RTCEncodedAudioFrameDelegate>> encoded_audio_frames_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_RTC_ENCODED_AUDIO_FRAME_DELEGATE_H_