Skip to content

Commit b306cf0

Browse files
taste1981jianjunz
authored andcommitted
Fix issue open-webrtc-toolkit#177: windows client crash of destruction of video stream (open-webrtc-toolkit#184)
1 parent d9165bc commit b306cf0

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

talk/owt/sdk/base/vcmcapturer.cc

+38-9
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
#include <memory>
1515

1616
#include "modules/video_capture/video_capture_factory.h"
17-
#include "rtc_base/checks.h"
18-
#include "rtc_base/logging.h"
17+
#include "webrtc/rtc_base/bind.h"
18+
#include "webrtc/rtc_base/checks.h"
19+
#include "webrtc/rtc_base/logging.h"
1920

2021
// This file is borrowed from webrtc project.
2122
namespace owt {
2223
namespace base {
2324

24-
VcmCapturer::VcmCapturer() : vcm_(nullptr) {}
25+
VcmCapturer::VcmCapturer()
26+
: vcm_(nullptr),
27+
vcm_thread_(rtc::Thread::CreateWithSocketServer()) {
28+
vcm_thread_->Start();
29+
}
2530

2631
bool VcmCapturer::Init(size_t width,
2732
size_t height,
@@ -39,7 +44,10 @@ bool VcmCapturer::Init(size_t width,
3944
return false;
4045
}
4146

42-
vcm_ = webrtc::VideoCaptureFactory::Create(unique_name);
47+
vcm_ = vcm_thread_->Invoke<rtc::scoped_refptr<webrtc::VideoCaptureModule>>(
48+
RTC_FROM_HERE,
49+
Bind(&VcmCapturer::CreateDeviceOnVCMThread, this,
50+
unique_name));
4351
if (!vcm_) {
4452
return false;
4553
}
@@ -52,15 +60,33 @@ bool VcmCapturer::Init(size_t width,
5260
capability_.maxFPS = static_cast<int32_t>(target_fps);
5361
capability_.videoType = webrtc::VideoType::kI420;
5462

55-
if (vcm_->StartCapture(capability_) != 0) {
63+
if (vcm_thread_->Invoke<int32_t>(
64+
RTC_FROM_HERE,
65+
Bind(&VcmCapturer::StartCaptureOnVCMThread, this, capability_)) != 0) {
5666
Destroy();
5767
return false;
5868
}
5969

60-
RTC_CHECK(vcm_->CaptureStarted());
61-
6270
return true;
6371
}
72+
rtc::scoped_refptr<webrtc::VideoCaptureModule>
73+
VcmCapturer::CreateDeviceOnVCMThread(
74+
const char* unique_device_utf8) {
75+
return webrtc::VideoCaptureFactory::Create(unique_device_utf8);
76+
}
77+
78+
int32_t VcmCapturer::StartCaptureOnVCMThread(
79+
webrtc::VideoCaptureCapability capability) {
80+
return vcm_->StartCapture(capability);
81+
}
82+
83+
int32_t VcmCapturer::StopCaptureOnVCMThread() {
84+
return vcm_->StopCapture();
85+
}
86+
87+
void VcmCapturer::ReleaseOnVCMThread() {
88+
vcm_ = nullptr;
89+
}
6490

6591
VcmCapturer* VcmCapturer::Create(size_t width,
6692
size_t height,
@@ -80,14 +106,17 @@ void VcmCapturer::Destroy() {
80106
if (!vcm_)
81107
return;
82108

83-
vcm_->StopCapture();
109+
vcm_thread_->Invoke<int32_t>(RTC_FROM_HERE,
110+
Bind(&VcmCapturer::StopCaptureOnVCMThread, this));
84111
vcm_->DeRegisterCaptureDataCallback();
85112
// Release reference to VCM.
86-
vcm_ = nullptr;
113+
vcm_thread_->Invoke<void>(RTC_FROM_HERE,
114+
Bind(&VcmCapturer::ReleaseOnVCMThread, this));
87115
}
88116

89117
VcmCapturer::~VcmCapturer() {
90118
Destroy();
119+
vcm_thread_->Stop();
91120
}
92121

93122
void VcmCapturer::OnFrame(const webrtc::VideoFrame& frame) {

talk/owt/sdk/base/vcmcapturer.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515

1616
#include "api/scoped_refptr.h"
1717
#include "modules/video_capture/video_capture.h"
18+
#include "webrtc/rtc_base/thread.h"
1819
#include "talk/owt/sdk/base/cameravideocapturer.h"
1920

2021
// This file is borrowed from webrtc project.
21-
2222
namespace owt {
2323
namespace base {
24-
2524
class VcmCapturer : public CameraVideoCapturer,
2625
public rtc::VideoSinkInterface<webrtc::VideoFrame> {
2726
public:
@@ -40,9 +39,14 @@ class VcmCapturer : public CameraVideoCapturer,
4039
size_t target_fps,
4140
size_t capture_device_index);
4241
void Destroy();
43-
42+
rtc::scoped_refptr<webrtc::VideoCaptureModule> CreateDeviceOnVCMThread(
43+
const char* unique_device_utf8);
44+
int32_t StartCaptureOnVCMThread(webrtc::VideoCaptureCapability);
45+
int32_t StopCaptureOnVCMThread();
46+
void ReleaseOnVCMThread();
4447
rtc::scoped_refptr<webrtc::VideoCaptureModule> vcm_;
4548
webrtc::VideoCaptureCapability capability_;
49+
std::unique_ptr<rtc::Thread> vcm_thread_;
4650
};
4751

4852
} // namespace base

0 commit comments

Comments
 (0)