VideoSourceAndroid.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Created by Grishka on 12.08.2018.
  3. //
  4. #include "VideoSourceAndroid.h"
  5. #include "JNIUtilities.h"
  6. #include "../../logging.h"
  7. #include "../../PrivateDefines.h"
  8. using namespace tgvoip;
  9. using namespace tgvoip::video;
  10. extern JavaVM* sharedJVM;
  11. std::vector<uint32_t> VideoSourceAndroid::availableEncoders;
  12. VideoSourceAndroid::VideoSourceAndroid(jobject jobj) : javaObject(jobj){
  13. jni::DoWithJNI([&](JNIEnv* env){
  14. jclass cls=env->GetObjectClass(javaObject);
  15. startMethod=env->GetMethodID(cls, "start", "()V");
  16. stopMethod=env->GetMethodID(cls, "stop", "()V");
  17. prepareEncoderMethod=env->GetMethodID(cls, "prepareEncoder", "(Ljava/lang/String;I)V");
  18. requestKeyFrameMethod=env->GetMethodID(cls, "requestKeyFrame", "()V");
  19. setBitrateMethod=env->GetMethodID(cls, "setBitrate", "(I)V");
  20. });
  21. }
  22. VideoSourceAndroid::~VideoSourceAndroid(){
  23. jni::DoWithJNI([this](JNIEnv* env){
  24. env->DeleteGlobalRef(javaObject);
  25. });
  26. }
  27. void VideoSourceAndroid::Start(){
  28. jni::DoWithJNI([this](JNIEnv* env){
  29. env->CallVoidMethod(javaObject, startMethod);
  30. });
  31. }
  32. void VideoSourceAndroid::Stop(){
  33. jni::DoWithJNI([this](JNIEnv* env){
  34. env->CallVoidMethod(javaObject, stopMethod);
  35. });
  36. }
  37. void VideoSourceAndroid::SendFrame(Buffer frame, uint32_t flags){
  38. callback(frame, flags);
  39. }
  40. void VideoSourceAndroid::SetStreamParameters(std::vector<Buffer> csd, unsigned int width, unsigned int height){
  41. LOGD("Video stream parameters: %d x %d", width, height);
  42. this->width=width;
  43. this->height=height;
  44. this->csd=std::move(csd);
  45. }
  46. void VideoSourceAndroid::Reset(uint32_t codec, int maxResolution){
  47. jni::DoWithJNI([&](JNIEnv* env){
  48. std::string codecStr="";
  49. switch(codec){
  50. case CODEC_AVC:
  51. codecStr="video/avc";
  52. break;
  53. case CODEC_HEVC:
  54. codecStr="video/hevc";
  55. break;
  56. case CODEC_VP8:
  57. codecStr="video/x-vnd.on2.vp8";
  58. break;
  59. case CODEC_VP9:
  60. codecStr="video/x-vnd.on2.vp9";
  61. break;
  62. }
  63. env->CallVoidMethod(javaObject, prepareEncoderMethod, env->NewStringUTF(codecStr.c_str()), maxResolution);
  64. });
  65. }
  66. void VideoSourceAndroid::RequestKeyFrame(){
  67. jni::DoWithJNI([this](JNIEnv* env){
  68. env->CallVoidMethod(javaObject, requestKeyFrameMethod);
  69. });
  70. }
  71. void VideoSourceAndroid::SetBitrate(uint32_t bitrate){
  72. jni::DoWithJNI([&](JNIEnv* env){
  73. env->CallVoidMethod(javaObject, setBitrateMethod, (jint)bitrate);
  74. });
  75. }