Guest User

Untitled

a guest
Dec 27th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include "LiveSourceWithx264.h"
  2.  
  3. LiveSourceWithx264* LiveSourceWithx264::createNew(UsageEnvironment& env)
  4. {
  5. return new LiveSourceWithx264(env);
  6. }
  7.  
  8. EventTriggerId LiveSourceWithx264::eventTriggerId = 0;
  9.  
  10. unsigned LiveSourceWithx264::referenceCount = 0;
  11.  
  12. LiveSourceWithx264::LiveSourceWithx264(UsageEnvironment& env):FramedSource(env)
  13. {
  14. if(referenceCount == 0)
  15. {
  16.  
  17. }
  18. ++referenceCount;
  19. //videoCaptureDevice.open(0);
  20. encoder = new x264Encoder();
  21. encoder->initilize();
  22. if(eventTriggerId == 0)
  23. {
  24. eventTriggerId = envir().taskScheduler().createEventTrigger(deliverFrame0);
  25. }
  26. rawImage = new char[(1920 * 1080) * 4];
  27. state_copy = false;
  28. }
  29.  
  30.  
  31. LiveSourceWithx264::~LiveSourceWithx264(void)
  32. {
  33. --referenceCount;
  34. //videoCaptureDevice.release();
  35. encoder->unInitilize();
  36. envir().taskScheduler().deleteEventTrigger(eventTriggerId);
  37. eventTriggerId = 0;
  38. }
  39.  
  40. void LiveSourceWithx264::encodeNewFrame()
  41. {
  42. /*
  43. rawImage.data = NULL;
  44. while(rawImage.data == NULL)
  45. {
  46. videoCaptureDevice >> rawImage;
  47. cv::waitKey(100);
  48. }
  49. */
  50. // Got new image to stream
  51. //assert(rawImage.data != NULL);
  52.  
  53. while (iris_ext->state_copy == COPY_STATE_X264_WAIT) { } //Note to people looking here. This delay is never more than 16ms... this allows for a framerate of 60fps
  54.  
  55. //iris_ext->state_copy = COPY_STATE_X264_STARTED;
  56.  
  57. if (iris_ext->login->state != LOGIN_SUCCESS) {
  58. encoder->encodeFrame(iris_ext->output_buffer_back);
  59. } else {
  60. encoder->encodeFrame_no_convert(iris_ext->swapper);
  61. }
  62.  
  63. //fill((int*)rawImage, (int*)rawImage + (1920 * 1080), 0xff0000ff);
  64.  
  65. iris_ext->state_copy = COPY_STATE_X264_WAIT;
  66.  
  67. // Take all nals from encoder output queue to our input queue
  68. while(encoder->isNalsAvailableInOutputQueue() == true)
  69. {
  70. x264_nal_t nal = encoder->getNalUnit();
  71. nalQueue.push(nal);
  72. }
  73. }
  74.  
  75. void LiveSourceWithx264::deliverFrame0(void* clientData)
  76. {
  77. ((LiveSourceWithx264*)clientData)->deliverFrame();
  78. }
  79.  
  80. void LiveSourceWithx264::doGetNextFrame()
  81. {
  82. if(nalQueue.empty() == true)
  83. {
  84. encodeNewFrame();
  85. gettimeofday(&currentTime,NULL);
  86. deliverFrame();
  87. }
  88. else
  89. {
  90. deliverFrame();
  91. }
  92. }
  93.  
  94. void LiveSourceWithx264::deliverFrame()
  95. {
  96. if(!isCurrentlyAwaitingData()) return;
  97. x264_nal_t nal = nalQueue.front();
  98. nalQueue.pop();
  99. assert(nal.p_payload != NULL);
  100. // You need to remove the start code which is there in front of every nal unit.
  101. // the start code might be 0x00000001 or 0x000001. so detect it and remove it. pass remaining data to live555
  102. int trancate = 0;
  103. if (nal.i_payload >= 4 && nal.p_payload[0] == 0 && nal.p_payload[1] == 0 && nal.p_payload[2] == 0 && nal.p_payload[3] == 1 )
  104. {
  105. trancate = 4;
  106. }
  107. else
  108. {
  109. if(nal.i_payload >= 3 && nal.p_payload[0] == 0 && nal.p_payload[1] == 0 && nal.p_payload[2] == 1 )
  110. {
  111. trancate = 3;
  112. }
  113. }
  114.  
  115. if(nal.i_payload-trancate > fMaxSize)
  116. {
  117. fFrameSize = fMaxSize;
  118. fNumTruncatedBytes = nal.i_payload-trancate - fMaxSize;
  119. }
  120. else
  121. {
  122. fFrameSize = nal.i_payload-trancate;
  123. }
  124. fPresentationTime = currentTime;
  125. memmove(fTo,nal.p_payload+trancate,fFrameSize);
  126. FramedSource::afterGetting(this);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment