fork/rust-sdks

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

Théo Monnomcleanup: webrtc-sys & fix RtcRuntime disposing crashes (#81)55bda13

main
3.3 KiB101 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#include "livekit/media_stream.h"
18
19#include <algorithm>
20#include <iostream>
21#include <memory>
22
23#include "api/media_stream_interface.h"
24#include "api/video/video_frame.h"
25#include "api/video/video_rotation.h"
26#include "audio/remix_resample.h"
27#include "common_audio/include/audio_util.h"
28#include "rtc_base/logging.h"
29#include "rtc_base/ref_counted_object.h"
30#include "rtc_base/time_utils.h"
31
32namespace livekit {
33
34MediaStream::MediaStream(
35    std::shared_ptr<RtcRuntime> rtc_runtime,
36    rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
37    : rtc_runtime_(rtc_runtime), media_stream_(std::move(stream)) {}
38
39rust::String MediaStream::id() const {
40  return media_stream_->id();
41}
42
43rust::Vec<VideoTrackPtr> MediaStream::get_video_tracks() const {
44  rust::Vec<VideoTrackPtr> rust;
45  for (auto video : media_stream_->GetVideoTracks())
46    rust.push_back(
47        VideoTrackPtr{rtc_runtime_->get_or_create_video_track(video)});
48
49  return rust;
50}
51
52rust::Vec<AudioTrackPtr> MediaStream::get_audio_tracks() const {
53  rust::Vec<AudioTrackPtr> rust;
54  for (auto audio : media_stream_->GetAudioTracks())
55    rust.push_back(
56        AudioTrackPtr{rtc_runtime_->get_or_create_audio_track(audio)});
57
58  return rust;
59}
60
61std::shared_ptr<AudioTrack> MediaStream::find_audio_track(
62    rust::String track_id) const {
63  return rtc_runtime_->get_or_create_audio_track(
64      media_stream_->FindAudioTrack(track_id.c_str()));
65}
66
67std::shared_ptr<VideoTrack> MediaStream::find_video_track(
68    rust::String track_id) const {
69  return rtc_runtime_->get_or_create_video_track(
70      media_stream_->FindVideoTrack(track_id.c_str()));
71}
72
73bool MediaStream::add_track(std::shared_ptr<MediaStreamTrack> track) const {
74  if (track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
75    return media_stream_->AddTrack(
76        rtc::scoped_refptr<webrtc::VideoTrackInterface>(
77            static_cast<webrtc::VideoTrackInterface*>(
78                track->rtc_track().get())));
79  } else {
80    return media_stream_->AddTrack(
81        rtc::scoped_refptr<webrtc::AudioTrackInterface>(
82            static_cast<webrtc::AudioTrackInterface*>(
83                track->rtc_track().get())));
84  }
85}
86
87bool MediaStream::remove_track(std::shared_ptr<MediaStreamTrack> track) const {
88  if (track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
89    return media_stream_->RemoveTrack(
90        rtc::scoped_refptr<webrtc::VideoTrackInterface>(
91            static_cast<webrtc::VideoTrackInterface*>(
92                track->rtc_track().get())));
93  } else {
94    return media_stream_->RemoveTrack(
95        rtc::scoped_refptr<webrtc::AudioTrackInterface>(
96            static_cast<webrtc::AudioTrackInterface*>(
97                track->rtc_track().get())));
98  }
99}
100
101}  // namespace livekit