blob: 1531fccc0c796c72029e69a2c3cb021109eab3a7 [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
14#include <string>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/androidvideocapturer.h"
17#include "webrtc/api/java/jni/jni_helpers.h"
perkj@webrtc.org112f1272015-02-25 09:20:07 +000018#include "webrtc/base/asyncinvoker.h"
Magnus Jedvertc464f502015-08-25 23:22:08 +020019#include "webrtc/base/criticalsection.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000020#include "webrtc/base/thread_checker.h"
perkj88518a22015-12-18 00:37:06 -080021#include "webrtc/common_video/include/i420_buffer_pool.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000022
23namespace webrtc_jni {
24
kjellander60ca31b2016-01-04 10:15:53 -080025struct NativeHandleImpl;
perkj88518a22015-12-18 00:37:06 -080026class SurfaceTextureHelper;
perkjac306422015-10-08 15:32:38 +020027
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000028// AndroidVideoCapturerJni implements AndroidVideoCapturerDelegate.
29// The purpose of the delegate is to hide the JNI specifics from the C++ only
30// AndroidVideoCapturer.
31class AndroidVideoCapturerJni : public webrtc::AndroidVideoCapturerDelegate {
32 public:
33 static int SetAndroidObjects(JNIEnv* jni, jobject appliction_context);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000034
nissec490e012015-12-10 06:23:33 -080035 AndroidVideoCapturerJni(JNIEnv* jni,
36 jobject j_video_capturer,
magjed0dc23162016-03-14 03:59:38 -070037 jobject j_egl_context);
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000038
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000039 void Start(int width, int height, int framerate,
40 webrtc::AndroidVideoCapturer* capturer) override;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000041 void Stop() override;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000042
Magnus Jedvert5199c742016-02-18 13:09:54 +010043 std::vector<cricket::VideoFormat> GetSupportedFormats() override;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000044
Magnus Jedvert5e7834e2016-02-12 17:05:29 +010045 // Called from VideoCapturer::NativeObserver on a Java thread.
perkj@webrtc.org112f1272015-02-25 09:20:07 +000046 void OnCapturerStarted(bool success);
perkjac306422015-10-08 15:32:38 +020047 void OnMemoryBufferFrame(void* video_frame, int length, int width,
48 int height, int rotation, int64_t timestamp_ns);
Per71f5a9a2015-12-11 09:32:37 +010049 void OnTextureFrame(int width, int height, int rotation, int64_t timestamp_ns,
Per488e75f2015-11-19 10:43:36 +010050 const NativeHandleImpl& handle);
Åsa Persson2b679252015-06-15 09:53:05 +020051 void OnOutputFormatRequest(int width, int height, int fps);
perkje0bce242015-10-05 16:21:54 +020052
53 protected:
Per33544192015-04-02 12:30:51 +020054 ~AndroidVideoCapturerJni();
perkj@webrtc.org112f1272015-02-25 09:20:07 +000055
perkje0bce242015-10-05 16:21:54 +020056 private:
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000057 JNIEnv* jni();
58
olka30a5b5e2015-10-20 11:04:56 -070059 // To avoid deducing Args from the 3rd parameter of AsyncCapturerInvoke.
60 template <typename T>
61 struct Identity {
62 typedef T type;
63 };
64
Magnus Jedvertc464f502015-08-25 23:22:08 +020065 // Helper function to make safe asynchronous calls to |capturer_|. The calls
66 // are not guaranteed to be delivered.
67 template <typename... Args>
68 void AsyncCapturerInvoke(
69 const char* method_name,
70 void (webrtc::AndroidVideoCapturer::*method)(Args...),
olka30a5b5e2015-10-20 11:04:56 -070071 typename Identity<Args>::type... args);
Magnus Jedvertc464f502015-08-25 23:22:08 +020072
nissec490e012015-12-10 06:23:33 -080073 const ScopedGlobalRef<jobject> j_video_capturer_;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000074 const ScopedGlobalRef<jclass> j_video_capturer_class_;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000075 const ScopedGlobalRef<jclass> j_observer_class_;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000076
perkj88518a22015-12-18 00:37:06 -080077 // Used on the Java thread running the camera.
78 webrtc::I420BufferPool buffer_pool_;
79 rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000080 rtc::ThreadChecker thread_checker_;
81
perkj@webrtc.org112f1272015-02-25 09:20:07 +000082 // |capturer| is a guaranteed to be a valid pointer between a call to
83 // AndroidVideoCapturerDelegate::Start
84 // until AndroidVideoCapturerDelegate::Stop.
Magnus Jedvertc464f502015-08-25 23:22:08 +020085 rtc::CriticalSection capturer_lock_;
86 webrtc::AndroidVideoCapturer* capturer_ GUARDED_BY(capturer_lock_);
87 // |invoker_| is used to communicate with |capturer_| on the thread Start() is
88 // called on.
89 rtc::scoped_ptr<rtc::GuardedAsyncInvoker> invoker_ GUARDED_BY(capturer_lock_);
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000090
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000091 static jobject application_context_;
92
henrikg3c089d72015-09-16 05:37:44 -070093 RTC_DISALLOW_COPY_AND_ASSIGN(AndroidVideoCapturerJni);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000094};
95
96} // namespace webrtc_jni
97
Henrik Kjellander15583c12016-02-10 10:53:12 +010098#endif // WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_