Skip to content

Commit 3eed2b3

Browse files
authored
support restart camera. (#79)
1 parent ec7892e commit 3eed2b3

10 files changed

+98
-53
lines changed

include/rtc_audio_track.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ namespace libwebrtc {
1212
* This class is a subclass of the RTCMediaTrack class, which provides a base
1313
* interface for all media tracks in WebRTC.
1414
*/
15-
class RTCAudioTrack : public RTCMediaTrack {
15+
class RTCAudioTrack : public RTCMediaTrack {
1616
public:
1717
// volume in [0-10]
1818
virtual void SetVolume(double volume) = 0;
19-
19+
2020
protected:
2121
/**
2222
* The destructor for the RTCAudioTrack class.

include/rtc_frame_cryptor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class KeyProvider : public RefCountInterface {
4242
int key_index) = 0;
4343

4444
virtual vector<uint8_t> ExportKey(const string participant_id,
45-
int key_index) = 0;
45+
int key_index) = 0;
46+
4647
protected:
4748
virtual ~KeyProvider() {}
4849
};

include/rtc_video_device.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ namespace libwebrtc {
88
class RTCVideoCapturer : public RefCountInterface {
99
public:
1010
virtual ~RTCVideoCapturer() {}
11+
12+
virtual bool StartCapture() = 0;
13+
14+
virtual bool CaptureStarted() = 0;
15+
16+
virtual void StopCapture() = 0;
1117
};
1218

1319
class RTCVideoDevice : public RefCountInterface {

src/internal/vcm_capturer.cc

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ bool VcmCapturer::Init(size_t width,
5656
capability_.maxFPS = static_cast<int32_t>(target_fps);
5757
capability_.videoType = VideoType::kI420;
5858

59+
return true;
60+
}
61+
62+
std::shared_ptr<VcmCapturer> VcmCapturer::Create(rtc::Thread* worker_thread,
63+
size_t width,
64+
size_t height,
65+
size_t target_fps,
66+
size_t capture_device_index) {
67+
std::shared_ptr<VcmCapturer> vcm_capturer(
68+
std::make_shared<VcmCapturer>(worker_thread));
69+
if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
70+
RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
71+
<< ", h = " << height << ", fps = " << target_fps
72+
<< ")";
73+
return nullptr;
74+
}
75+
return vcm_capturer;
76+
}
77+
78+
bool VcmCapturer::StartCapture() {
5979
int32_t result = worker_thread_->Invoke<bool>(
6080
RTC_FROM_HERE, [&] { return vcm_->StartCapture(capability_); });
6181

@@ -64,25 +84,21 @@ bool VcmCapturer::Init(size_t width,
6484
return false;
6585
}
6686

67-
RTC_CHECK(worker_thread_->Invoke<bool>(
68-
RTC_FROM_HERE, [&] { return vcm_->CaptureStarted(); }));
69-
7087
return true;
7188
}
7289

73-
VcmCapturer* VcmCapturer::Create(rtc::Thread* worker_thread,
74-
size_t width,
75-
size_t height,
76-
size_t target_fps,
77-
size_t capture_device_index) {
78-
std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer(worker_thread));
79-
if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
80-
RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
81-
<< ", h = " << height << ", fps = " << target_fps
82-
<< ")";
83-
return nullptr;
84-
}
85-
return vcm_capturer.release();
90+
bool VcmCapturer::CaptureStarted() {
91+
return vcm_ != nullptr && worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
92+
return vcm_->CaptureStarted();
93+
});
94+
}
95+
96+
void VcmCapturer::StopCapture() {
97+
worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
98+
vcm_->StopCapture();
99+
// Release reference to VCM.
100+
vcm_ = nullptr;
101+
});
86102
}
87103

88104
void VcmCapturer::Destroy() {
@@ -91,11 +107,7 @@ void VcmCapturer::Destroy() {
91107

92108
vcm_->DeRegisterCaptureDataCallback();
93109

94-
worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
95-
vcm_->StopCapture();
96-
// Release reference to VCM.
97-
vcm_ = nullptr;
98-
});
110+
StopCapture();
99111
}
100112

101113
VcmCapturer::~VcmCapturer() {
@@ -111,19 +123,18 @@ rtc::scoped_refptr<CapturerTrackSource> CapturerTrackSource::Create(
111123
const size_t kWidth = 640;
112124
const size_t kHeight = 480;
113125
const size_t kFps = 30;
114-
std::unique_ptr<VcmCapturer> capturer;
126+
std::shared_ptr<VcmCapturer> capturer;
115127
std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
116128
webrtc::VideoCaptureFactory::CreateDeviceInfo());
117129
if (!info) {
118130
return nullptr;
119131
}
120132
int num_devices = info->NumberOfDevices();
121133
for (int i = 0; i < num_devices; ++i) {
122-
capturer = absl::WrapUnique(
123-
VcmCapturer::Create(worker_thread, kWidth, kHeight, kFps, i));
134+
capturer = VcmCapturer::Create(worker_thread, kWidth, kHeight, kFps, i);
124135
if (capturer) {
125136
return rtc::scoped_refptr<CapturerTrackSource>(
126-
new rtc::RefCountedObject<CapturerTrackSource>(std::move(capturer)));
137+
new rtc::RefCountedObject<CapturerTrackSource>(capturer));
127138
}
128139
}
129140

src/internal/vcm_capturer.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,24 @@ namespace internal {
2424
class VcmCapturer : public VideoCapturer,
2525
public rtc::VideoSinkInterface<VideoFrame> {
2626
public:
27-
static VcmCapturer* Create(rtc::Thread* worker_thread,
28-
size_t width,
29-
size_t height,
30-
size_t target_fps,
31-
size_t capture_device_index);
27+
static std::shared_ptr<VcmCapturer> Create(rtc::Thread* worker_thread,
28+
size_t width,
29+
size_t height,
30+
size_t target_fps,
31+
size_t capture_device_index);
32+
VcmCapturer(rtc::Thread* worker_thread);
33+
3234
virtual ~VcmCapturer();
3335

36+
bool StartCapture() override;
37+
38+
bool CaptureStarted() override;
39+
40+
void StopCapture() override;
41+
3442
void OnFrame(const VideoFrame& frame) override;
3543

3644
private:
37-
VcmCapturer(rtc::Thread* worker_thread);
3845
bool Init(size_t width,
3946
size_t height,
4047
size_t target_fps,
@@ -52,14 +59,14 @@ class CapturerTrackSource : public webrtc::VideoTrackSource {
5259
rtc::Thread* worker_thread);
5360

5461
public:
55-
explicit CapturerTrackSource(std::unique_ptr<VideoCapturer> capturer)
56-
: VideoTrackSource(/*remote=*/false), capturer_(std::move(capturer)) {}
62+
explicit CapturerTrackSource(std::shared_ptr<VideoCapturer> capturer)
63+
: VideoTrackSource(/*remote=*/false), capturer_(capturer) {}
5764

5865
private:
5966
rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override {
6067
return capturer_.get();
6168
}
62-
std::unique_ptr<VideoCapturer> capturer_;
69+
std::shared_ptr<VideoCapturer> capturer_;
6370
};
6471
} // namespace internal
6572
} // namespace webrtc

src/internal/video_capturer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class VideoCapturer : public rtc::VideoSourceInterface<VideoFrame> {
3030
VideoCapturer();
3131
virtual ~VideoCapturer();
3232

33+
virtual bool StartCapture() { return false; }
34+
35+
virtual bool CaptureStarted() { return false; }
36+
37+
virtual void StopCapture() {}
38+
3339
void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
3440
const rtc::VideoSinkWants& wants) override;
3541
void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;

src/rtc_frame_cryptor_impl.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ webrtc::FrameCryptorTransformer::Algorithm AlgorithmToFrameCryptorAlgorithm(
3333
}
3434
}
3535

36-
RTCFrameCryptorImpl::RTCFrameCryptorImpl(const string participant_id,
37-
Algorithm algorithm,
38-
scoped_refptr<KeyProvider> key_provider,
39-
scoped_refptr<RTCRtpSender> sender)
36+
RTCFrameCryptorImpl::RTCFrameCryptorImpl(
37+
const string participant_id,
38+
Algorithm algorithm,
39+
scoped_refptr<KeyProvider> key_provider,
40+
scoped_refptr<RTCRtpSender> sender)
4041
: participant_id_(participant_id),
4142
enabled_(false),
4243
key_index_(0),
@@ -59,10 +60,11 @@ RTCFrameCryptorImpl::RTCFrameCryptorImpl(const string participant_id,
5960
e2ee_transformer_->SetEnabled(false);
6061
}
6162

62-
RTCFrameCryptorImpl::RTCFrameCryptorImpl(const string participant_id,
63-
Algorithm algorithm,
64-
scoped_refptr<KeyProvider> key_provider,
65-
scoped_refptr<RTCRtpReceiver> receiver)
63+
RTCFrameCryptorImpl::RTCFrameCryptorImpl(
64+
const string participant_id,
65+
Algorithm algorithm,
66+
scoped_refptr<KeyProvider> key_provider,
67+
scoped_refptr<RTCRtpReceiver> receiver)
6668
: participant_id_(participant_id),
6769
enabled_(false),
6870
key_index_(0),

src/rtc_frame_cryptor_impl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ class DefaultKeyProviderImpl : public KeyProvider {
3535
return impl_->RatchetKey(participant_id.std_string(), key_index);
3636
}
3737

38-
3938
vector<uint8_t> ExportKey(const string participant_id,
40-
int key_index)override {
39+
int key_index) override {
4140
return impl_->ExportKey(participant_id.std_string(), key_index);
4241
}
4342

src/rtc_video_device_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ scoped_refptr<RTCVideoCapturer> RTCVideoDeviceImpl::Create(const char* name,
5252
return signaling_thread_->Invoke<scoped_refptr<RTCVideoCapturerImpl>>(
5353
RTC_FROM_HERE, [vcm] {
5454
return scoped_refptr<RTCVideoCapturerImpl>(
55-
new RefCountedObject<RTCVideoCapturerImpl>(absl::WrapUnique(vcm)));
55+
new RefCountedObject<RTCVideoCapturerImpl>(vcm));
5656
});
5757
}
5858

src/rtc_video_device_impl.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,27 @@ namespace libwebrtc {
1515
class RTCVideoCapturerImpl : public RTCVideoCapturer {
1616
public:
1717
RTCVideoCapturerImpl(
18-
std::unique_ptr<webrtc::internal::VideoCapturer> video_capturer)
19-
: video_capturer_(std::move(video_capturer)) {}
20-
std::unique_ptr<webrtc::internal::VideoCapturer> video_capturer() {
21-
return std::move(video_capturer_);
18+
std::shared_ptr<webrtc::internal::VideoCapturer> video_capturer)
19+
: video_capturer_(video_capturer) {}
20+
std::shared_ptr<webrtc::internal::VideoCapturer> video_capturer() {
21+
return video_capturer_;
22+
}
23+
24+
bool StartCapture() override {
25+
return video_capturer_ != nullptr && video_capturer_->StartCapture();
26+
}
27+
28+
bool CaptureStarted() override {
29+
return video_capturer_ != nullptr && video_capturer_->CaptureStarted();
30+
}
31+
32+
void StopCapture() override {
33+
if (video_capturer_ != nullptr)
34+
video_capturer_->StopCapture();
2235
}
2336

2437
private:
25-
std::unique_ptr<webrtc::internal::VideoCapturer> video_capturer_;
38+
std::shared_ptr<webrtc::internal::VideoCapturer> video_capturer_;
2639
};
2740

2841
class RTCVideoDeviceImpl : public RTCVideoDevice {

0 commit comments

Comments
 (0)