blob: eea56adb3e8a97325a69b5d992a94689327696f3 [file] [log] [blame]
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
12#define WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000013
kwibergd1fe2812016-04-27 06:47:29 -070014#include <memory>
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000015#include <string>
16
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/androidvideocapturer.h"
18#include "webrtc/api/java/jni/jni_helpers.h"
perkj@webrtc.org112f1272015-02-25 09:20:07 +000019#include "webrtc/base/asyncinvoker.h"
kwiberg4485ffb2016-04-26 08:14:39 -070020#include "webrtc/base/constructormagic.h"
Magnus Jedvertc464f502015-08-25 23:22:08 +020021#include "webrtc/base/criticalsection.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000022#include "webrtc/base/thread_checker.h"
perkj88518a22015-12-18 00:37:06 -080023#include "webrtc/common_video/include/i420_buffer_pool.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000024
25namespace webrtc_jni {
26
kjellander60ca31b2016-01-04 10:15:53 -080027struct NativeHandleImpl;
perkj88518a22015-12-18 00:37:06 -080028class SurfaceTextureHelper;
perkjac306422015-10-08 15:32:38 +020029
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000030// AndroidVideoCapturerJni implements AndroidVideoCapturerDelegate.
31// The purpose of the delegate is to hide the JNI specifics from the C++ only
32// AndroidVideoCapturer.
33class AndroidVideoCapturerJni : public webrtc::AndroidVideoCapturerDelegate {
34 public:
35 static int SetAndroidObjects(JNIEnv* jni, jobject appliction_context);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000036
nissec490e012015-12-10 06:23:33 -080037 AndroidVideoCapturerJni(JNIEnv* jni,
38 jobject j_video_capturer,
magjed0dc23162016-03-14 03:59:38 -070039 jobject j_egl_context);
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000040
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000041 void Start(int width, int height, int framerate,
42 webrtc::AndroidVideoCapturer* capturer) override;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000043 void Stop() override;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000044
Magnus Jedvert5199c742016-02-18 13:09:54 +010045 std::vector<cricket::VideoFormat> GetSupportedFormats() override;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000046
Magnus Jedvert5e7834e2016-02-12 17:05:29 +010047 // Called from VideoCapturer::NativeObserver on a Java thread.
perkj@webrtc.org112f1272015-02-25 09:20:07 +000048 void OnCapturerStarted(bool success);
perkjac306422015-10-08 15:32:38 +020049 void OnMemoryBufferFrame(void* video_frame, int length, int width,
50 int height, int rotation, int64_t timestamp_ns);
Per71f5a9a2015-12-11 09:32:37 +010051 void OnTextureFrame(int width, int height, int rotation, int64_t timestamp_ns,
Per488e75f2015-11-19 10:43:36 +010052 const NativeHandleImpl& handle);
Åsa Persson2b679252015-06-15 09:53:05 +020053 void OnOutputFormatRequest(int width, int height, int fps);
perkje0bce242015-10-05 16:21:54 +020054
55 protected:
Per33544192015-04-02 12:30:51 +020056 ~AndroidVideoCapturerJni();
perkj@webrtc.org112f1272015-02-25 09:20:07 +000057
perkje0bce242015-10-05 16:21:54 +020058 private:
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000059 JNIEnv* jni();
60
olka30a5b5e2015-10-20 11:04:56 -070061 // To avoid deducing Args from the 3rd parameter of AsyncCapturerInvoke.
62 template <typename T>
63 struct Identity {
64 typedef T type;
65 };
66
Magnus Jedvertc464f502015-08-25 23:22:08 +020067 // Helper function to make safe asynchronous calls to |capturer_|. The calls
68 // are not guaranteed to be delivered.
69 template <typename... Args>
70 void AsyncCapturerInvoke(
71 const char* method_name,
72 void (webrtc::AndroidVideoCapturer::*method)(Args...),
olka30a5b5e2015-10-20 11:04:56 -070073 typename Identity<Args>::type... args);
Magnus Jedvertc464f502015-08-25 23:22:08 +020074
nissec490e012015-12-10 06:23:33 -080075 const ScopedGlobalRef<jobject> j_video_capturer_;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000076 const ScopedGlobalRef<jclass> j_video_capturer_class_;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000077 const ScopedGlobalRef<jclass> j_observer_class_;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000078
perkj88518a22015-12-18 00:37:06 -080079 // Used on the Java thread running the camera.
80 webrtc::I420BufferPool buffer_pool_;
81 rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000082 rtc::ThreadChecker thread_checker_;
83
perkj@webrtc.org112f1272015-02-25 09:20:07 +000084 // |capturer| is a guaranteed to be a valid pointer between a call to
85 // AndroidVideoCapturerDelegate::Start
86 // until AndroidVideoCapturerDelegate::Stop.
Magnus Jedvertc464f502015-08-25 23:22:08 +020087 rtc::CriticalSection capturer_lock_;
88 webrtc::AndroidVideoCapturer* capturer_ GUARDED_BY(capturer_lock_);
89 // |invoker_| is used to communicate with |capturer_| on the thread Start() is
90 // called on.
kwibergd1fe2812016-04-27 06:47:29 -070091 std::unique_ptr<rtc::GuardedAsyncInvoker> invoker_ GUARDED_BY(capturer_lock_);
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000092
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000093 static jobject application_context_;
94
henrikg3c089d72015-09-16 05:37:44 -070095 RTC_DISALLOW_COPY_AND_ASSIGN(AndroidVideoCapturerJni);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000096};
97
98} // namespace webrtc_jni
99
Henrik Kjellander15583c12016-02-10 10:53:12 +0100100#endif // WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_