Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(562)

Side by Side Diff: webrtc/examples/unityplugin/simple_peer_connection.cc

Issue 2993273002: Add Android Camera To Unity Plugin (Closed)
Patch Set: Presubmit Fix Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/examples/unityplugin/simple_peer_connection.h" 11 #include "webrtc/examples/unityplugin/simple_peer_connection.h"
12 12
13 #include <utility> 13 #include <utility>
14 14
15 #include "webrtc/api/test/fakeconstraints.h" 15 #include "webrtc/api/test/fakeconstraints.h"
16 #include "webrtc/api/videosourceproxy.h"
16 #include "webrtc/media/engine/webrtcvideocapturerfactory.h" 17 #include "webrtc/media/engine/webrtcvideocapturerfactory.h"
17 #include "webrtc/modules/video_capture/video_capture_factory.h" 18 #include "webrtc/modules/video_capture/video_capture_factory.h"
18 19
20 #if defined(WEBRTC_ANDROID)
21 #include "webrtc/examples/unityplugin/classreferenceholder.h"
22 #include "webrtc/sdk/android/src/jni/androidvideotracksource.h"
23 #include "webrtc/sdk/android/src/jni/jni_helpers.h"
24 #endif
25
19 // Names used for media stream labels. 26 // Names used for media stream labels.
20 const char kAudioLabel[] = "audio_label"; 27 const char kAudioLabel[] = "audio_label";
21 const char kVideoLabel[] = "video_label"; 28 const char kVideoLabel[] = "video_label";
22 const char kStreamLabel[] = "stream_label"; 29 const char kStreamLabel[] = "stream_label";
23 30
24 namespace { 31 namespace {
25 static int g_peer_count = 0; 32 static int g_peer_count = 0;
26 static std::unique_ptr<rtc::Thread> g_worker_thread; 33 static std::unique_ptr<rtc::Thread> g_worker_thread;
27 static std::unique_ptr<rtc::Thread> g_signaling_thread; 34 static std::unique_ptr<rtc::Thread> g_signaling_thread;
28 static rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> 35 static rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
29 g_peer_connection_factory; 36 g_peer_connection_factory;
37 #if defined(WEBRTC_ANDROID)
38 // Android case: the video track does not own the capturer, and it
39 // relies on the app to dispose the capturer when the peerconnection
40 // shuts down.
41 static jobject g_camera = nullptr;
42 #endif
30 43
31 std::string GetEnvVarOrDefault(const char* env_var_name, 44 std::string GetEnvVarOrDefault(const char* env_var_name,
32 const char* default_value) { 45 const char* default_value) {
33 std::string value; 46 std::string value;
34 const char* env_var = getenv(env_var_name); 47 const char* env_var = getenv(env_var_name);
35 if (env_var) 48 if (env_var)
36 value = env_var; 49 value = env_var;
37 50
38 if (value.empty()) 51 if (value.empty())
39 value = default_value; 52 value = default_value;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 155
143 peer_connection_ = g_peer_connection_factory->CreatePeerConnection( 156 peer_connection_ = g_peer_connection_factory->CreatePeerConnection(
144 config_, &constraints, nullptr, nullptr, this); 157 config_, &constraints, nullptr, nullptr, this);
145 158
146 return peer_connection_.get() != nullptr; 159 return peer_connection_.get() != nullptr;
147 } 160 }
148 161
149 void SimplePeerConnection::DeletePeerConnection() { 162 void SimplePeerConnection::DeletePeerConnection() {
150 g_peer_count--; 163 g_peer_count--;
151 164
165 #if defined(WEBRTC_ANDROID)
166 if (g_camera) {
167 JNIEnv* env = webrtc_jni::GetEnv();
168 jclass pc_factory_class =
169 unity_plugin::FindClass(env, "org/webrtc/UnityUtility");
170 jmethodID stop_camera_method = webrtc_jni::GetStaticMethodID(
171 env, pc_factory_class, "StopCamera", "(Lorg/webrtc/VideoCapturer;)V");
172
173 env->CallStaticVoidMethod(pc_factory_class, stop_camera_method, g_camera);
174 CHECK_EXCEPTION(env);
175
176 g_camera = nullptr;
177 }
178 #endif
179
152 CloseDataChannel(); 180 CloseDataChannel();
153 peer_connection_ = nullptr; 181 peer_connection_ = nullptr;
154 active_streams_.clear(); 182 active_streams_.clear();
155 183
156 if (g_peer_count == 0) { 184 if (g_peer_count == 0) {
157 g_peer_connection_factory = nullptr; 185 g_peer_connection_factory = nullptr;
158 g_signaling_thread.reset(); 186 g_signaling_thread.reset();
159 g_worker_thread.reset(); 187 g_worker_thread.reset();
160 } 188 }
161 } 189 }
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = 401 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
374 g_peer_connection_factory->CreateLocalMediaStream(kStreamLabel); 402 g_peer_connection_factory->CreateLocalMediaStream(kStreamLabel);
375 403
376 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track( 404 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
377 g_peer_connection_factory->CreateAudioTrack( 405 g_peer_connection_factory->CreateAudioTrack(
378 kAudioLabel, g_peer_connection_factory->CreateAudioSource(nullptr))); 406 kAudioLabel, g_peer_connection_factory->CreateAudioSource(nullptr)));
379 std::string id = audio_track->id(); 407 std::string id = audio_track->id();
380 stream->AddTrack(audio_track); 408 stream->AddTrack(audio_track);
381 409
382 if (!audio_only) { 410 if (!audio_only) {
411 #if defined(WEBRTC_ANDROID)
412 JNIEnv* env = webrtc_jni::GetEnv();
413 jclass pc_factory_class =
414 unity_plugin::FindClass(env, "org/webrtc/UnityUtility");
415 jmethodID load_texture_helper_method = webrtc_jni::GetStaticMethodID(
416 env, pc_factory_class, "LoadSurfaceTextureHelper",
417 "()Lorg/webrtc/SurfaceTextureHelper;");
418 jobject texture_helper = env->CallStaticObjectMethod(
419 pc_factory_class, load_texture_helper_method);
420 CHECK_EXCEPTION(env);
421 RTC_DCHECK(texture_helper != nullptr)
422 << "Cannot get the Surface Texture Helper.";
423
424 rtc::scoped_refptr<webrtc::AndroidVideoTrackSource> source(
425 new rtc::RefCountedObject<webrtc::AndroidVideoTrackSource>(
426 g_signaling_thread.get(), env, texture_helper, false));
427 rtc::scoped_refptr<webrtc::VideoTrackSourceProxy> proxy_source =
428 webrtc::VideoTrackSourceProxy::Create(g_signaling_thread.get(),
429 g_worker_thread.get(), source);
430
431 // link with VideoCapturer (Camera);
432 jmethodID link_camera_method = webrtc_jni::GetStaticMethodID(
433 env, pc_factory_class, "LinkCamera",
434 "(JLorg/webrtc/SurfaceTextureHelper;)Lorg/webrtc/VideoCapturer;");
435 jobject camera_tmp =
436 env->CallStaticObjectMethod(pc_factory_class, link_camera_method,
437 (jlong)proxy_source.get(), texture_helper);
438 CHECK_EXCEPTION(env);
439 g_camera = (jobject)env->NewGlobalRef(camera_tmp);
440
441 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
442 g_peer_connection_factory->CreateVideoTrack(kVideoLabel,
443 proxy_source.release()));
444 stream->AddTrack(video_track);
445 #else
383 std::unique_ptr<cricket::VideoCapturer> capture = OpenVideoCaptureDevice(); 446 std::unique_ptr<cricket::VideoCapturer> capture = OpenVideoCaptureDevice();
384 if (capture) { 447 if (capture) {
385 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track( 448 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
386 g_peer_connection_factory->CreateVideoTrack( 449 g_peer_connection_factory->CreateVideoTrack(
387 kVideoLabel, g_peer_connection_factory->CreateVideoSource( 450 kVideoLabel, g_peer_connection_factory->CreateVideoSource(
388 std::move(capture), nullptr))); 451 std::move(capture), nullptr)));
389 452
390 stream->AddTrack(video_track); 453 stream->AddTrack(video_track);
391 if (local_video_observer_ && !stream->GetVideoTracks().empty()) { 454 }
392 stream->GetVideoTracks()[0]->AddOrUpdateSink( 455 #endif
393 local_video_observer_.get(), rtc::VideoSinkWants()); 456 if (local_video_observer_ && !stream->GetVideoTracks().empty()) {
394 } 457 stream->GetVideoTracks()[0]->AddOrUpdateSink(local_video_observer_.get(),
458 rtc::VideoSinkWants());
395 } 459 }
396 } 460 }
397 461
398 if (!peer_connection_->AddStream(stream)) { 462 if (!peer_connection_->AddStream(stream)) {
399 LOG(LS_ERROR) << "Adding stream to PeerConnection failed"; 463 LOG(LS_ERROR) << "Adding stream to PeerConnection failed";
400 } 464 }
401 465
402 typedef std::pair<std::string, 466 typedef std::pair<std::string,
403 rtc::scoped_refptr<webrtc::MediaStreamInterface>> 467 rtc::scoped_refptr<webrtc::MediaStreamInterface>>
404 MediaStreamPair; 468 MediaStreamPair;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 556
493 for (const auto& param : params) { 557 for (const auto& param : params) {
494 uint32_t ssrc = param.ssrc.value_or(0); 558 uint32_t ssrc = param.ssrc.value_or(0);
495 if (ssrc > 0) 559 if (ssrc > 0)
496 ssrcs.push_back(ssrc); 560 ssrcs.push_back(ssrc);
497 } 561 }
498 } 562 }
499 563
500 return ssrcs; 564 return ssrcs;
501 } 565 }
OLDNEW
« no previous file with comments | « webrtc/examples/unityplugin/jni_onload.cc ('k') | webrtc/examples/unityplugin/unity_plugin_apis.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698