fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

Théo Monnomfeat: add ffi datachannel & mute events (#88)25f4c9a

main
4.1 KiB140 linesraw
1/*
2 * Copyright 2023 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the “License”);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <memory>
20
21#include "api/media_stream_interface.h"
22#include "api/video/video_frame.h"
23#include "livekit/helper.h"
24#include "livekit/media_stream_track.h"
25#include "livekit/video_frame.h"
26#include "livekit/webrtc.h"
27#include "media/base/adapted_video_track_source.h"
28#include "rtc_base/synchronization/mutex.h"
29#include "rtc_base/timestamp_aligner.h"
30#include "rust/cxx.h"
31
32namespace livekit {
33class VideoTrack;
34class NativeVideoSink;
35class VideoTrackSource;
36}  // namespace livekit
37#include "webrtc-sys/src/video_track.rs.h"
38
39namespace livekit {
40
41class VideoTrack : public MediaStreamTrack {
42 private:
43  friend RtcRuntime;
44  VideoTrack(std::shared_ptr<RtcRuntime> rtc_runtime,
45             rtc::scoped_refptr<webrtc::VideoTrackInterface> track);
46
47 public:
48  ~VideoTrack();
49
50  void add_sink(const std::shared_ptr<NativeVideoSink>& sink) const;
51  void remove_sink(const std::shared_ptr<NativeVideoSink>& sink) const;
52
53  void set_should_receive(bool should_receive) const;
54  bool should_receive() const;
55  ContentHint content_hint() const;
56  void set_content_hint(ContentHint hint) const;
57
58 private:
59  webrtc::VideoTrackInterface* track() const {
60    return static_cast<webrtc::VideoTrackInterface*>(track_.get());
61  }
62
63  mutable webrtc::Mutex mutex_;
64
65  // Same for AudioTrack:
66  // Keep a strong reference to the added sinks, so we don't need to
67  // manage the lifetime safety on the Rust side
68  mutable std::vector<std::shared_ptr<NativeVideoSink>> sinks_;
69};
70
71class NativeVideoSink : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
72 public:
73  explicit NativeVideoSink(rust::Box<VideoSinkWrapper> observer);
74
75  void OnFrame(const webrtc::VideoFrame& frame) override;
76  void OnDiscardedFrame() override;
77  void OnConstraintsChanged(
78      const webrtc::VideoTrackSourceConstraints& constraints) override;
79
80 private:
81  rust::Box<VideoSinkWrapper> observer_;
82};
83
84std::shared_ptr<NativeVideoSink> new_native_video_sink(
85    rust::Box<VideoSinkWrapper> observer);
86
87class VideoTrackSource {
88  class InternalSource : public rtc::AdaptedVideoTrackSource {
89   public:
90    InternalSource(const VideoResolution&
91                       resolution);  // (0, 0) means no resolution/optional, the
92                                     // source will guess the resolution at the
93                                     // first captured frame
94    ~InternalSource() override;
95
96    bool is_screencast() const override;
97    absl::optional<bool> needs_denoising() const override;
98    SourceState state() const override;
99    bool remote() const override;
100    VideoResolution video_resolution() const;
101    bool on_captured_frame(const webrtc::VideoFrame& frame);
102
103   private:
104    mutable webrtc::Mutex mutex_;
105    rtc::TimestampAligner timestamp_aligner_;
106    VideoResolution resolution_;
107  };
108
109 public:
110  VideoTrackSource(const VideoResolution& resolution);
111
112  VideoResolution video_resolution() const;
113
114  bool on_captured_frame(const std::unique_ptr<VideoFrame>& frame)
115      const;  // frames pushed from Rust (+interior mutability)
116
117  rtc::scoped_refptr<InternalSource> get() const;
118
119 private:
120  rtc::scoped_refptr<InternalSource> source_;
121};
122
123std::shared_ptr<VideoTrackSource> new_video_track_source(
124    const VideoResolution& resolution);
125
126static std::shared_ptr<MediaStreamTrack> video_to_media(
127    std::shared_ptr<VideoTrack> track) {
128  return track;
129}
130
131static std::shared_ptr<VideoTrack> media_to_video(
132    std::shared_ptr<MediaStreamTrack> track) {
133  return std::static_pointer_cast<VideoTrack>(track);
134}
135
136static std::shared_ptr<VideoTrack> _shared_video_track() {
137  return nullptr;  // Ignore
138}
139
140}  // namespace livekit