Advertisement
Guest User

Untitled

a guest
Jul 6th, 2014
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #ifndef RTMPSTREAMER_H
  2. #define RTMPSTREAMER_H
  3. #include "curl\curl.h"
  4. #include "curl\multi.h"
  5. #include <iostream>
  6. #include <WinSock2.h>
  7. #pragma comment(lib, "Ws2_32.lib")
  8. extern "C"
  9. {
  10. #include "librtmp/rtmp.h"
  11. #include "librtmp/log.h"
  12. }
  13. #include <string>
  14. #include <vector>
  15. #include <stdint.h>
  16. #include <inttypes.h>
  17. #define STR2AVAL(av,str) av.av_val = str; av.av_len = strlen(av.av_val)
  18.  
  19. static void rtmp_log(int level, const char *fmt, va_list args)
  20. {
  21. std::cout<< fmt<< std::endl;
  22. }
  23. class RTMPStreamer
  24. {
  25. public:
  26. RTMPStreamer()
  27. {
  28. }
  29. WSADATA wsaData;
  30. RTMPStreamer(std::string url, bool writer)
  31. {
  32. RTMP_LogSetLevel(RTMP_LOGDEBUG);
  33. RTMP_LogSetCallback(rtmp_log);
  34. int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  35. if (iResult != 0) {
  36. printf("WSAStartup failed: %d\n", iResult);
  37. }
  38. Connect(writer);
  39. //Example rtmp link
  40. //"rtmp://localhost:1935/live/desktop";
  41. }
  42.  
  43. ~RTMPStreamer()
  44. {
  45. }
  46.  
  47. bool RTMPStreamer::Connect(bool writer)
  48. {
  49. try
  50. {
  51. r = RTMP_Alloc();
  52. RTMP_Init(r);
  53.  
  54. //This link works as rtmpdump as param -r
  55. char *uri = "rtmp://localhost:1935/flvplayback/bunny.flv";
  56.  
  57. int retSetup = RTMP_SetupURL(r, uri);
  58.  
  59. if(writer)
  60. {
  61. RTMP_EnableWrite(r);
  62. }
  63. int ver = RTMP_LibVersion();
  64. int pro = r->Link.protocol;
  65.  
  66. int retVal = RTMP_Connect(r,NULL);
  67. retVal = RTMP_ConnectStream(r, 0);
  68. retVal = RTMP_IsConnected(r);
  69. //Return vals are all good here.
  70. return true;
  71. }
  72. catch(int e)
  73. {
  74. return false;
  75. }
  76. }
  77. void RTMPStreamer::Disconnect()
  78. {
  79. RTMP_Close(r);
  80. RTMP_Free(r);
  81. }
  82. int RTMPStreamer::Write(std::vector<unsigned char> data)
  83. {
  84. char *charArray = new char[data.size()+1];
  85. charArray = reinterpret_cast<char*> (&data[0]);
  86. return RTMP_Write(r,charArray,0);
  87. }
  88.  
  89. std::vector<unsigned char> RTMPStreamer::Read()
  90. {
  91. bool packetComplete = false;
  92. char* buf = new char[4096];
  93. std::vector<unsigned char> bufVec;
  94. //Returns false
  95. int timedOut = RTMP_IsTimedout(r);
  96.  
  97. //RETURNS 0.0000
  98. double duration = RTMP_GetDuration(r);
  99.  
  100. // Returns 4, the bytes can be printed via cout and "FLV:)" where the :) is a single character. Seriously!?!
  101. int retVal = RTMP_Read(r,buf,sizeof(buf));
  102. return bufVec;
  103. }
  104.  
  105. void RTMPStreamer::Pause()
  106. {
  107. RTMP_Pause(r,true);
  108. }
  109. void RTMPStreamer::Play()
  110. {
  111. RTMP_Pause(r,false);
  112. }
  113. void RTMPStreamer::Seek(int dTime)
  114. {
  115. RTMP_SendSeek(r,dTime);
  116. }
  117.  
  118. private:
  119. RTMP *r;
  120. };
  121. #endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement