-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRtspPlayer.hpp
More file actions
123 lines (105 loc) · 3.11 KB
/
RtspPlayer.hpp
File metadata and controls
123 lines (105 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// RtspPlayer.hpp
// toolForTest
//
// Created by cx on 2018/9/6.
// Copyright © 2018年 cx. All rights reserved.
//
#ifndef RtspPlayer_hpp
#define RtspPlayer_hpp
#include <iostream>
#include <thread>
#include <vector>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/select.h>
extern "C" {
#include "sdp.h"
}
namespace RK {
enum RtspPlayerState {
RtspSendOptions = 0,
RtspHandleOptions,
RtspSendDescribe,
RtspHandleDescribe,
RtspSendVideoSetup,
RtspHandleVideoSetup,
RtspSendAudioSetup,
RtspHandleAudioSetup,
RtspSendPlay,
RtspHandlePlay,
RtspSendPause,
RtspHandlePause,
RtspSendTerminate,
RtspHandleTerminate,
RtspIdle,
RtspTurnOff,
};
enum RtspPlayerCSeq {
RTSPOPTIONS = 1,
RTSPDESCRIBE,
RTSPVIDEO_SETUP,
RTSPAUDIO_SETUP,
RTSPPLAY,
RTSPPAUSE,
RTSPTEARDOWN,
};
// h264 nalu
struct Nalu {
unsigned type :5;
unsigned nal_ref_idc :2;
unsigned forbidden_zero_bit :1;
};
// h264 rtp fu
struct FU {
unsigned type :5;
unsigned R :1;
unsigned E :1;
unsigned S :1;
};
class RtspPlayer {
public:
typedef std::shared_ptr<RtspPlayer> Ptr;
RtspPlayer();
~RtspPlayer();
bool Play(std::string url);
void Stop();
protected:
bool NetworkInit(const char *ip, const short port);
bool RTPSocketInit(int videoPort, int audioPort);
bool getIPFromUrl(std::string url, char *ip, unsigned short *port);
void EventInit();
bool HandleRtspMsg(const char *buf, ssize_t bufsize);
void HandleRtspState();
void HandleRtpMsg(const char *buf, ssize_t bufsize);
// rtsp message send/handle function
void SendDescribe(std::string url);
void HandleDescribe(const char *buf, ssize_t bufsize);
void RtspSetup(const std::string url, int track, int CSeq, char *proto, short rtp_port, short rtcp_port);
void SendVideoSetup();
bool HandleVideoSetup(const char *buf, ssize_t bufsize);
void SendPlay(const std::string url);
std::vector<std::string> GetSDPFromMessage(const char *buffer, size_t length, const char *pattern);
private:
std::atomic<bool> _Terminated;
std::atomic<bool> _NetWorked;
std::shared_ptr<std::thread> _PlayThreadPtr;
std::atomic<RtspPlayerState> _PlayState;
fd_set _readfd;
fd_set _writefd;
fd_set _errorfd;
std::string _rtspurl;
char _rtspip[256];
int _Eventfd = 0;
int _RtspSocket = 0;
int _RtpVideoSocket = 0;
int _RtpAudioSocket = 0;
struct sockaddr_in _RtpVideoAddr;
struct sdp_payload *_SdpParser;
long _RtspSessionID = 0;
std::function<void(unsigned char *nalu, ssize_t size)> onVideoFrameGet;
FILE *_fp;
};
} //namespace RK
#endif /* RtspPlayer_hpp */