Skip to content

Commit

Permalink
support restart camera. (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc authored May 7, 2023
1 parent ec7892e commit 3eed2b3
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 53 deletions.
4 changes: 2 additions & 2 deletions include/rtc_audio_track.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace libwebrtc {
* This class is a subclass of the RTCMediaTrack class, which provides a base
* interface for all media tracks in WebRTC.
*/
class RTCAudioTrack : public RTCMediaTrack {
class RTCAudioTrack : public RTCMediaTrack {
public:
// volume in [0-10]
virtual void SetVolume(double volume) = 0;

protected:
/**
* The destructor for the RTCAudioTrack class.
Expand Down
3 changes: 2 additions & 1 deletion include/rtc_frame_cryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class KeyProvider : public RefCountInterface {
int key_index) = 0;

virtual vector<uint8_t> ExportKey(const string participant_id,
int key_index) = 0;
int key_index) = 0;

protected:
virtual ~KeyProvider() {}
};
Expand Down
6 changes: 6 additions & 0 deletions include/rtc_video_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace libwebrtc {
class RTCVideoCapturer : public RefCountInterface {
public:
virtual ~RTCVideoCapturer() {}

virtual bool StartCapture() = 0;

virtual bool CaptureStarted() = 0;

virtual void StopCapture() = 0;
};

class RTCVideoDevice : public RefCountInterface {
Expand Down
61 changes: 36 additions & 25 deletions src/internal/vcm_capturer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ bool VcmCapturer::Init(size_t width,
capability_.maxFPS = static_cast<int32_t>(target_fps);
capability_.videoType = VideoType::kI420;

return true;
}

std::shared_ptr<VcmCapturer> VcmCapturer::Create(rtc::Thread* worker_thread,
size_t width,
size_t height,
size_t target_fps,
size_t capture_device_index) {
std::shared_ptr<VcmCapturer> vcm_capturer(
std::make_shared<VcmCapturer>(worker_thread));
if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
<< ", h = " << height << ", fps = " << target_fps
<< ")";
return nullptr;
}
return vcm_capturer;
}

bool VcmCapturer::StartCapture() {
int32_t result = worker_thread_->Invoke<bool>(
RTC_FROM_HERE, [&] { return vcm_->StartCapture(capability_); });

Expand All @@ -64,25 +84,21 @@ bool VcmCapturer::Init(size_t width,
return false;
}

RTC_CHECK(worker_thread_->Invoke<bool>(
RTC_FROM_HERE, [&] { return vcm_->CaptureStarted(); }));

return true;
}

VcmCapturer* VcmCapturer::Create(rtc::Thread* worker_thread,
size_t width,
size_t height,
size_t target_fps,
size_t capture_device_index) {
std::unique_ptr<VcmCapturer> vcm_capturer(new VcmCapturer(worker_thread));
if (!vcm_capturer->Init(width, height, target_fps, capture_device_index)) {
RTC_LOG(LS_WARNING) << "Failed to create VcmCapturer(w = " << width
<< ", h = " << height << ", fps = " << target_fps
<< ")";
return nullptr;
}
return vcm_capturer.release();
bool VcmCapturer::CaptureStarted() {
return vcm_ != nullptr && worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
return vcm_->CaptureStarted();
});
}

void VcmCapturer::StopCapture() {
worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
vcm_->StopCapture();
// Release reference to VCM.
vcm_ = nullptr;
});
}

void VcmCapturer::Destroy() {
Expand All @@ -91,11 +107,7 @@ void VcmCapturer::Destroy() {

vcm_->DeRegisterCaptureDataCallback();

worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
vcm_->StopCapture();
// Release reference to VCM.
vcm_ = nullptr;
});
StopCapture();
}

VcmCapturer::~VcmCapturer() {
Expand All @@ -111,19 +123,18 @@ rtc::scoped_refptr<CapturerTrackSource> CapturerTrackSource::Create(
const size_t kWidth = 640;
const size_t kHeight = 480;
const size_t kFps = 30;
std::unique_ptr<VcmCapturer> capturer;
std::shared_ptr<VcmCapturer> capturer;
std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
webrtc::VideoCaptureFactory::CreateDeviceInfo());
if (!info) {
return nullptr;
}
int num_devices = info->NumberOfDevices();
for (int i = 0; i < num_devices; ++i) {
capturer = absl::WrapUnique(
VcmCapturer::Create(worker_thread, kWidth, kHeight, kFps, i));
capturer = VcmCapturer::Create(worker_thread, kWidth, kHeight, kFps, i);
if (capturer) {
return rtc::scoped_refptr<CapturerTrackSource>(
new rtc::RefCountedObject<CapturerTrackSource>(std::move(capturer)));
new rtc::RefCountedObject<CapturerTrackSource>(capturer));
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/internal/vcm_capturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ namespace internal {
class VcmCapturer : public VideoCapturer,
public rtc::VideoSinkInterface<VideoFrame> {
public:
static VcmCapturer* Create(rtc::Thread* worker_thread,
size_t width,
size_t height,
size_t target_fps,
size_t capture_device_index);
static std::shared_ptr<VcmCapturer> Create(rtc::Thread* worker_thread,
size_t width,
size_t height,
size_t target_fps,
size_t capture_device_index);
VcmCapturer(rtc::Thread* worker_thread);

virtual ~VcmCapturer();

bool StartCapture() override;

bool CaptureStarted() override;

void StopCapture() override;

void OnFrame(const VideoFrame& frame) override;

private:
VcmCapturer(rtc::Thread* worker_thread);
bool Init(size_t width,
size_t height,
size_t target_fps,
Expand All @@ -52,14 +59,14 @@ class CapturerTrackSource : public webrtc::VideoTrackSource {
rtc::Thread* worker_thread);

public:
explicit CapturerTrackSource(std::unique_ptr<VideoCapturer> capturer)
: VideoTrackSource(/*remote=*/false), capturer_(std::move(capturer)) {}
explicit CapturerTrackSource(std::shared_ptr<VideoCapturer> capturer)
: VideoTrackSource(/*remote=*/false), capturer_(capturer) {}

private:
rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override {
return capturer_.get();
}
std::unique_ptr<VideoCapturer> capturer_;
std::shared_ptr<VideoCapturer> capturer_;
};
} // namespace internal
} // namespace webrtc
Expand Down
6 changes: 6 additions & 0 deletions src/internal/video_capturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class VideoCapturer : public rtc::VideoSourceInterface<VideoFrame> {
VideoCapturer();
virtual ~VideoCapturer();

virtual bool StartCapture() { return false; }

virtual bool CaptureStarted() { return false; }

virtual void StopCapture() {}

void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
const rtc::VideoSinkWants& wants) override;
void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
Expand Down
18 changes: 10 additions & 8 deletions src/rtc_frame_cryptor_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ webrtc::FrameCryptorTransformer::Algorithm AlgorithmToFrameCryptorAlgorithm(
}
}

RTCFrameCryptorImpl::RTCFrameCryptorImpl(const string participant_id,
Algorithm algorithm,
scoped_refptr<KeyProvider> key_provider,
scoped_refptr<RTCRtpSender> sender)
RTCFrameCryptorImpl::RTCFrameCryptorImpl(
const string participant_id,
Algorithm algorithm,
scoped_refptr<KeyProvider> key_provider,
scoped_refptr<RTCRtpSender> sender)
: participant_id_(participant_id),
enabled_(false),
key_index_(0),
Expand All @@ -59,10 +60,11 @@ RTCFrameCryptorImpl::RTCFrameCryptorImpl(const string participant_id,
e2ee_transformer_->SetEnabled(false);
}

RTCFrameCryptorImpl::RTCFrameCryptorImpl(const string participant_id,
Algorithm algorithm,
scoped_refptr<KeyProvider> key_provider,
scoped_refptr<RTCRtpReceiver> receiver)
RTCFrameCryptorImpl::RTCFrameCryptorImpl(
const string participant_id,
Algorithm algorithm,
scoped_refptr<KeyProvider> key_provider,
scoped_refptr<RTCRtpReceiver> receiver)
: participant_id_(participant_id),
enabled_(false),
key_index_(0),
Expand Down
3 changes: 1 addition & 2 deletions src/rtc_frame_cryptor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ class DefaultKeyProviderImpl : public KeyProvider {
return impl_->RatchetKey(participant_id.std_string(), key_index);
}


vector<uint8_t> ExportKey(const string participant_id,
int key_index)override {
int key_index) override {
return impl_->ExportKey(participant_id.std_string(), key_index);
}

Expand Down
2 changes: 1 addition & 1 deletion src/rtc_video_device_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ scoped_refptr<RTCVideoCapturer> RTCVideoDeviceImpl::Create(const char* name,
return signaling_thread_->Invoke<scoped_refptr<RTCVideoCapturerImpl>>(
RTC_FROM_HERE, [vcm] {
return scoped_refptr<RTCVideoCapturerImpl>(
new RefCountedObject<RTCVideoCapturerImpl>(absl::WrapUnique(vcm)));
new RefCountedObject<RTCVideoCapturerImpl>(vcm));
});
}

Expand Down
23 changes: 18 additions & 5 deletions src/rtc_video_device_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ namespace libwebrtc {
class RTCVideoCapturerImpl : public RTCVideoCapturer {
public:
RTCVideoCapturerImpl(
std::unique_ptr<webrtc::internal::VideoCapturer> video_capturer)
: video_capturer_(std::move(video_capturer)) {}
std::unique_ptr<webrtc::internal::VideoCapturer> video_capturer() {
return std::move(video_capturer_);
std::shared_ptr<webrtc::internal::VideoCapturer> video_capturer)
: video_capturer_(video_capturer) {}
std::shared_ptr<webrtc::internal::VideoCapturer> video_capturer() {
return video_capturer_;
}

bool StartCapture() override {
return video_capturer_ != nullptr && video_capturer_->StartCapture();
}

bool CaptureStarted() override {
return video_capturer_ != nullptr && video_capturer_->CaptureStarted();
}

void StopCapture() override {
if (video_capturer_ != nullptr)
video_capturer_->StopCapture();
}

private:
std::unique_ptr<webrtc::internal::VideoCapturer> video_capturer_;
std::shared_ptr<webrtc::internal::VideoCapturer> video_capturer_;
};

class RTCVideoDeviceImpl : public RTCVideoDevice {
Expand Down

0 comments on commit 3eed2b3

Please sign in to comment.