main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include "../common/comm.h"
  4. #include "../common/iobuffer.h"
  5. #include "api.h"
  6. #include "protocol.pb.h"
  7. #include "message_queue.h"
  8. #include <signal.h>
  9. #include <cstring>
  10. #include <stdlib.h>
  11. void daemonize(void) {
  12. signal(SIGTTOU, SIG_IGN);
  13. signal(SIGTTIN, SIG_IGN);
  14. signal(SIGTSTP, SIG_IGN);
  15. if (0 != fork()) exit(0);
  16. if (-1 == setsid()) exit(0);
  17. signal(SIGHUP, SIG_IGN);
  18. if (0 != fork()) exit(0);
  19. //if (0 != chdir("/")) exit(0);
  20. }
  21. bool checkOnly()
  22. {
  23. const char filename[] = "./lockfile";
  24. int fd = open (filename, O_WRONLY | O_CREAT , 0644);
  25. int flock = lockf(fd, F_TLOCK, 0 );
  26. if (fd == -1) {
  27. return false;
  28. }
  29. //给文件加锁
  30. if (flock == -1) {
  31. return false;
  32. }
  33. //程序退出后,文件自动解锁
  34. return true;
  35. }
  36. std::string getpath()
  37. {
  38. char exec_name [BUFSIZ];
  39. readlink ("/proc/self/exe", exec_name, BUFSIZ);
  40. int32_t len=strlen(exec_name);
  41. for(int i=len;i>=0;i--)
  42. {
  43. if(exec_name[i]=='/')
  44. {
  45. exec_name[i]=0;
  46. break;
  47. }
  48. }
  49. return std::string(exec_name);
  50. }
  51. int main(int argc,char *argv[])
  52. {
  53. //system("pulseaudio --start");
  54. // daemonize();
  55. auto path=getpath();
  56. std::cout<<path<<std::endl;
  57. if(-1==chdir(path.c_str())) return 0;
  58. if(checkOnly()==false)
  59. {
  60. std::cout<<"进程已经在运行"<<std::endl;
  61. return 0;
  62. }
  63. //20231106-YUV转h.264
  64. /*
  65. AVCodec* codec = NULL;
  66. //AVCodecContext* codecContent = NULL;
  67. //AVPacket* packet = NULL;
  68. //AVFrame* frame = NULL;
  69. FILE* inFile = nullptr;
  70. FILE* outFile = nullptr;
  71. //查找指定编码器
  72. codec = avcodec_find_encoder(AV_CODEC_ID_H264);
  73. if (codec == NULL)
  74. printf("could not find h264 encoder!\r\n");
  75. else
  76. printf("could find h264 encoder!\r\n");
  77. */
  78. /*
  79. //申请编码器上下文
  80. codecContent = avcodec_alloc_context3(codec);
  81. if (codecContent == nullptr)
  82. printf("could not alloc h264 content!\r\n");
  83. //必设参数
  84. codecContent->width = 1280;
  85. codecContent->height = 720;
  86. codecContent->time_base = AVRational{ 1, 25 };
  87. codecContent->max_b_frames = 1;
  88. codecContent->pix_fmt = AV_PIX_FMT_YUV420P;
  89. codecContent->gop_size = 10; //关键帧间隔,默认250
  90. codecContent->framerate = AVRational{ 25, 1 };
  91. //初始化编码器上下文
  92. ret = avcodec_open2(codecContent, codec, NULL);
  93. if (ret < 0)
  94. printf("Could not open codec: \r\n");
  95. packet = av_packet_alloc();
  96. if (packet == nullptr)
  97. printf("alloc packet error\r\n");
  98. frame = av_frame_alloc();
  99. if (packet == nullptr)
  100. printf("alloc frame error\r\n");
  101. //必设参数
  102. frame->width = codecContent->width;
  103. frame->height = codecContent->height;
  104. frame->format = codecContent->pix_fmt;
  105. //申请视频数据存储空间
  106. ret = av_frame_get_buffer(frame, 0);
  107. if (ret)
  108. printf("alloc frame buffer error!\r\n");
  109. inFile = fopen(inFileName, "rb");
  110. if (inFile == nullptr)
  111. printf("error to open inFile: %s\r\n", inFileName);
  112. outFile = fopen(outFileName, "wb+");
  113. if (inFile == nullptr)
  114. printf("error to open outFile: %s\n", outFileName);
  115. int framecount = 0;
  116. frame->pts = 0;
  117. int start_time = av_gettime() / 1000; //毫秒级
  118. while (!feof(inFile))
  119. {
  120. ret = av_frame_is_writable(frame);
  121. if (ret < 0)
  122. ret = av_frame_make_writable(frame);
  123. fread(frame->data[0], 1, frame->width * frame->height, inFile); //y
  124. fread(frame->data[1], 1, frame->width * frame->height / 4, inFile); //u
  125. fread(frame->data[2], 1, frame->width * frame->height / 4, inFile); //v
  126. printf("encode frame num: %d\n", ++framecount);
  127. frame->pts += 1000 / (codecContent->time_base.den / codecContent->time_base.num);
  128. encode(codecContent, packet, frame, outFile);
  129. }
  130. encode(codecContent, packet, nullptr, outFile);
  131. printf("encode time cost: %d ms\n ", av_gettime() / 1000 - start_time);
  132. av_packet_free(&packet);
  133. av_frame_free(&frame);
  134. avcodec_free_context(&codecContent);
  135. fclose(inFile);
  136. fclose(outFile);
  137. */
  138. /*
  139. int16_t hi=(int16_t)(0x8c & 0x00FF);
  140. int16_t low= (int16_t)(0x9E & 0x00FF);
  141. unsigned int mmmm = (hi<<8)|low;
  142. */
  143. CMessageQueue Q;
  144. Q.Create();
  145. while(true)
  146. {
  147. Q.Process();
  148. }
  149. //gtk_main();
  150. return 0;
  151. }