正在显示
11 个修改的文件
包含
348 行增加
和
26 行删除
| @@ -157,8 +157,8 @@ stream_caster { | @@ -157,8 +157,8 @@ stream_caster { | ||
| 157 | # rtmp://127.0.0.1/live/livestream | 157 | # rtmp://127.0.0.1/live/livestream |
| 158 | output rtmp://127.0.0.1/live/livestream; | 158 | output rtmp://127.0.0.1/live/livestream; |
| 159 | # the listen port for stream caster. | 159 | # the listen port for stream caster. |
| 160 | - # for mpegts_over_udp caster, listen at udp port. | ||
| 161 | - # for rtsp caster, listen at tcp port. | 160 | + # for mpegts_over_udp caster, listen at udp port. for example, 8935. |
| 161 | + # for rtsp caster, listen at tcp port. for example, 554. | ||
| 162 | listen 8935; | 162 | listen 8935; |
| 163 | } | 163 | } |
| 164 | stream_caster { | 164 | stream_caster { |
| @@ -168,7 +168,7 @@ stream_caster { | @@ -168,7 +168,7 @@ stream_caster { | ||
| 168 | listen 8935; | 168 | listen 8935; |
| 169 | } | 169 | } |
| 170 | stream_caster { | 170 | stream_caster { |
| 171 | - enabled on; | 171 | + enabled off; |
| 172 | caster rtsp; | 172 | caster rtsp; |
| 173 | output rtmp://127.0.0.1/[app]/[stream]; | 173 | output rtmp://127.0.0.1/[app]/[stream]; |
| 174 | listen 554; | 174 | listen 554; |
| @@ -376,7 +376,7 @@ MODULE_DEPENDS=("CORE" "KERNEL") | @@ -376,7 +376,7 @@ MODULE_DEPENDS=("CORE" "KERNEL") | ||
| 376 | ModuleLibIncs=(${SRS_OBJS_DIR} ${LibSSLRoot}) | 376 | ModuleLibIncs=(${SRS_OBJS_DIR} ${LibSSLRoot}) |
| 377 | MODULE_FILES=("srs_rtmp_amf0" "srs_rtmp_io" "srs_rtmp_stack" "srs_rtmp_sdk" | 377 | MODULE_FILES=("srs_rtmp_amf0" "srs_rtmp_io" "srs_rtmp_stack" "srs_rtmp_sdk" |
| 378 | "srs_rtmp_handshake" "srs_rtmp_utility" "srs_rtmp_msg_array" "srs_rtmp_buffer" | 378 | "srs_rtmp_handshake" "srs_rtmp_utility" "srs_rtmp_msg_array" "srs_rtmp_buffer" |
| 379 | - "srs_raw_avc") | 379 | + "srs_raw_avc" "srs_rtsp_stack") |
| 380 | RTMP_INCS="src/protocol"; MODULE_DIR=${RTMP_INCS} . auto/modules.sh | 380 | RTMP_INCS="src/protocol"; MODULE_DIR=${RTMP_INCS} . auto/modules.sh |
| 381 | RTMP_OBJS="${MODULE_OBJS[@]}" | 381 | RTMP_OBJS="${MODULE_OBJS[@]}" |
| 382 | # | 382 | # |
| @@ -36,6 +36,8 @@ file | @@ -36,6 +36,8 @@ file | ||
| 36 | ../../src/kernel/srs_kernel_log.cpp, | 36 | ../../src/kernel/srs_kernel_log.cpp, |
| 37 | ../../src/kernel/srs_kernel_mp3.hpp, | 37 | ../../src/kernel/srs_kernel_mp3.hpp, |
| 38 | ../../src/kernel/srs_kernel_mp3.cpp, | 38 | ../../src/kernel/srs_kernel_mp3.cpp, |
| 39 | + ../../src/kernel/srs_rtsp_stack.hpp, | ||
| 40 | + ../../src/kernel/srs_rtsp_stack.cpp, | ||
| 39 | ../../src/kernel/srs_kernel_stream.hpp, | 41 | ../../src/kernel/srs_kernel_stream.hpp, |
| 40 | ../../src/kernel/srs_kernel_stream.cpp, | 42 | ../../src/kernel/srs_kernel_stream.cpp, |
| 41 | ../../src/kernel/srs_kernel_ts.cpp, | 43 | ../../src/kernel/srs_kernel_ts.cpp, |
| @@ -89,6 +89,7 @@ | @@ -89,6 +89,7 @@ | ||
| 89 | <ClInclude Include="..\..\src\app\srs_app_refer.hpp" /> | 89 | <ClInclude Include="..\..\src\app\srs_app_refer.hpp" /> |
| 90 | <ClInclude Include="..\..\src\app\srs_app_reload.hpp" /> | 90 | <ClInclude Include="..\..\src\app\srs_app_reload.hpp" /> |
| 91 | <ClInclude Include="..\..\src\app\srs_app_rtmp_conn.hpp" /> | 91 | <ClInclude Include="..\..\src\app\srs_app_rtmp_conn.hpp" /> |
| 92 | + <ClInclude Include="..\..\src\app\srs_app_rtsp.hpp" /> | ||
| 92 | <ClInclude Include="..\..\src\app\srs_app_security.hpp" /> | 93 | <ClInclude Include="..\..\src\app\srs_app_security.hpp" /> |
| 93 | <ClInclude Include="..\..\src\app\srs_app_server.hpp" /> | 94 | <ClInclude Include="..\..\src\app\srs_app_server.hpp" /> |
| 94 | <ClInclude Include="..\..\src\app\srs_app_source.hpp" /> | 95 | <ClInclude Include="..\..\src\app\srs_app_source.hpp" /> |
| @@ -125,6 +126,7 @@ | @@ -125,6 +126,7 @@ | ||
| 125 | <ClInclude Include="..\..\src\protocol\srs_rtmp_sdk.hpp" /> | 126 | <ClInclude Include="..\..\src\protocol\srs_rtmp_sdk.hpp" /> |
| 126 | <ClInclude Include="..\..\src\protocol\srs_rtmp_stack.hpp" /> | 127 | <ClInclude Include="..\..\src\protocol\srs_rtmp_stack.hpp" /> |
| 127 | <ClInclude Include="..\..\src\protocol\srs_rtmp_utility.hpp" /> | 128 | <ClInclude Include="..\..\src\protocol\srs_rtmp_utility.hpp" /> |
| 129 | + <ClInclude Include="..\..\src\protocol\srs_rtsp_stack.hpp" /> | ||
| 128 | </ItemGroup> | 130 | </ItemGroup> |
| 129 | <ItemGroup> | 131 | <ItemGroup> |
| 130 | <ClCompile Include="..\..\research\hls\ts_info.cc" /> | 132 | <ClCompile Include="..\..\research\hls\ts_info.cc" /> |
| @@ -166,6 +168,7 @@ | @@ -166,6 +168,7 @@ | ||
| 166 | <ClCompile Include="..\..\src\app\srs_app_refer.cpp" /> | 168 | <ClCompile Include="..\..\src\app\srs_app_refer.cpp" /> |
| 167 | <ClCompile Include="..\..\src\app\srs_app_reload.cpp" /> | 169 | <ClCompile Include="..\..\src\app\srs_app_reload.cpp" /> |
| 168 | <ClCompile Include="..\..\src\app\srs_app_rtmp_conn.cpp" /> | 170 | <ClCompile Include="..\..\src\app\srs_app_rtmp_conn.cpp" /> |
| 171 | + <ClCompile Include="..\..\src\app\srs_app_rtsp.cpp" /> | ||
| 169 | <ClCompile Include="..\..\src\app\srs_app_security.cpp" /> | 172 | <ClCompile Include="..\..\src\app\srs_app_security.cpp" /> |
| 170 | <ClCompile Include="..\..\src\app\srs_app_server.cpp" /> | 173 | <ClCompile Include="..\..\src\app\srs_app_server.cpp" /> |
| 171 | <ClCompile Include="..\..\src\app\srs_app_source.cpp" /> | 174 | <ClCompile Include="..\..\src\app\srs_app_source.cpp" /> |
| @@ -203,6 +206,7 @@ | @@ -203,6 +206,7 @@ | ||
| 203 | <ClCompile Include="..\..\src\protocol\srs_rtmp_sdk.cpp" /> | 206 | <ClCompile Include="..\..\src\protocol\srs_rtmp_sdk.cpp" /> |
| 204 | <ClCompile Include="..\..\src\protocol\srs_rtmp_stack.cpp" /> | 207 | <ClCompile Include="..\..\src\protocol\srs_rtmp_stack.cpp" /> |
| 205 | <ClCompile Include="..\..\src\protocol\srs_rtmp_utility.cpp" /> | 208 | <ClCompile Include="..\..\src\protocol\srs_rtmp_utility.cpp" /> |
| 209 | + <ClCompile Include="..\..\src\protocol\srs_rtsp_stack.cpp" /> | ||
| 206 | </ItemGroup> | 210 | </ItemGroup> |
| 207 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | 211 | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> |
| 208 | <ImportGroup Label="ExtensionTargets"> | 212 | <ImportGroup Label="ExtensionTargets"> |
| @@ -229,6 +229,12 @@ | @@ -229,6 +229,12 @@ | ||
| 229 | <ClCompile Include="..\..\src\protocol\srs_raw_avc.cpp"> | 229 | <ClCompile Include="..\..\src\protocol\srs_raw_avc.cpp"> |
| 230 | <Filter>srs</Filter> | 230 | <Filter>srs</Filter> |
| 231 | </ClCompile> | 231 | </ClCompile> |
| 232 | + <ClCompile Include="..\..\src\app\srs_app_rtsp.cpp"> | ||
| 233 | + <Filter>srs</Filter> | ||
| 234 | + </ClCompile> | ||
| 235 | + <ClCompile Include="..\..\src\protocol\srs_rtsp_stack.cpp"> | ||
| 236 | + <Filter>srs</Filter> | ||
| 237 | + </ClCompile> | ||
| 232 | </ItemGroup> | 238 | </ItemGroup> |
| 233 | <ItemGroup> | 239 | <ItemGroup> |
| 234 | <ClInclude Include="..\..\src\app\srs_app_bandwidth.hpp"> | 240 | <ClInclude Include="..\..\src\app\srs_app_bandwidth.hpp"> |
| @@ -420,6 +426,12 @@ | @@ -420,6 +426,12 @@ | ||
| 420 | <ClInclude Include="..\..\src\protocol\srs_raw_avc.hpp"> | 426 | <ClInclude Include="..\..\src\protocol\srs_raw_avc.hpp"> |
| 421 | <Filter>srs</Filter> | 427 | <Filter>srs</Filter> |
| 422 | </ClInclude> | 428 | </ClInclude> |
| 429 | + <ClInclude Include="..\..\src\app\srs_app_rtsp.hpp"> | ||
| 430 | + <Filter>srs</Filter> | ||
| 431 | + </ClInclude> | ||
| 432 | + <ClInclude Include="..\..\src\protocol\srs_rtsp_stack.hpp"> | ||
| 433 | + <Filter>srs</Filter> | ||
| 434 | + </ClInclude> | ||
| 423 | </ItemGroup> | 435 | </ItemGroup> |
| 424 | <ItemGroup> | 436 | <ItemGroup> |
| 425 | <Filter Include="research"> | 437 | <Filter Include="research"> |
| @@ -23,7 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -23,7 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | 23 | ||
| 24 | #include <srs_app_rtsp.hpp> | 24 | #include <srs_app_rtsp.hpp> |
| 25 | 25 | ||
| 26 | +#include <algorithm> | ||
| 27 | +using namespace std; | ||
| 28 | + | ||
| 26 | #include <srs_app_config.hpp> | 29 | #include <srs_app_config.hpp> |
| 30 | +#include <srs_kernel_error.hpp> | ||
| 31 | +#include <srs_rtsp_stack.hpp> | ||
| 32 | +#include <srs_app_st_socket.hpp> | ||
| 33 | +#include <srs_kernel_log.hpp> | ||
| 34 | +#include <srs_app_utility.hpp> | ||
| 27 | 35 | ||
| 28 | #ifdef SRS_AUTO_STREAM_CASTER | 36 | #ifdef SRS_AUTO_STREAM_CASTER |
| 29 | 37 | ||
| @@ -35,13 +43,115 @@ ISrsRtspHandler::~ISrsRtspHandler() | @@ -35,13 +43,115 @@ ISrsRtspHandler::~ISrsRtspHandler() | ||
| 35 | { | 43 | { |
| 36 | } | 44 | } |
| 37 | 45 | ||
| 38 | -SrsRtspConn::SrsRtspConn(SrsConfDirective* c) | 46 | +SrsRtspConn::SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o) |
| 39 | { | 47 | { |
| 40 | - output = _srs_config->get_stream_caster_output(c); | 48 | + output = o; |
| 49 | + caster = c; | ||
| 50 | + stfd = fd; | ||
| 51 | + skt = new SrsStSocket(fd); | ||
| 52 | + rtsp = new SrsRtspStack(skt); | ||
| 53 | + trd = new SrsThread("rtsp", this, 0, false); | ||
| 41 | } | 54 | } |
| 42 | 55 | ||
| 43 | SrsRtspConn::~SrsRtspConn() | 56 | SrsRtspConn::~SrsRtspConn() |
| 44 | { | 57 | { |
| 58 | + srs_close_stfd(stfd); | ||
| 59 | + trd->stop(); | ||
| 60 | + | ||
| 61 | + srs_freep(trd); | ||
| 62 | + srs_freep(skt); | ||
| 63 | + srs_freep(rtsp); | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +int SrsRtspConn::serve() | ||
| 67 | +{ | ||
| 68 | + return trd->start(); | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +int SrsRtspConn::do_cycle() | ||
| 72 | +{ | ||
| 73 | + int ret = ERROR_SUCCESS; | ||
| 74 | + | ||
| 75 | + // retrieve ip of client. | ||
| 76 | + std::string ip = srs_get_peer_ip(st_netfd_fileno(stfd)); | ||
| 77 | + srs_trace("rtsp: serve %s", ip.c_str()); | ||
| 78 | + | ||
| 79 | + return ret; | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +int SrsRtspConn::cycle() | ||
| 83 | +{ | ||
| 84 | + // serve the rtsp client. | ||
| 85 | + int ret = do_cycle(); | ||
| 86 | + | ||
| 87 | + // if socket io error, set to closed. | ||
| 88 | + if (srs_is_client_gracefully_close(ret)) { | ||
| 89 | + ret = ERROR_SOCKET_CLOSED; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + // success. | ||
| 93 | + if (ret == ERROR_SUCCESS) { | ||
| 94 | + srs_trace("client finished."); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + // client close peer. | ||
| 98 | + if (ret == ERROR_SOCKET_CLOSED) { | ||
| 99 | + srs_warn("client disconnect peer. ret=%d", ret); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + // terminate thread in the thread cycle itself. | ||
| 103 | + trd->stop_loop(); | ||
| 104 | + | ||
| 105 | + return ERROR_SUCCESS; | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +void SrsRtspConn::on_thread_stop() | ||
| 109 | +{ | ||
| 110 | + caster->remove(this); | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +SrsRtspCaster::SrsRtspCaster(SrsConfDirective* c) | ||
| 114 | +{ | ||
| 115 | + // TODO: FIXME: support reload. | ||
| 116 | + output = _srs_config->get_stream_caster_output(c); | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +SrsRtspCaster::~SrsRtspCaster() | ||
| 120 | +{ | ||
| 121 | + std::vector<SrsRtspConn*>::iterator it; | ||
| 122 | + for (it = clients.begin(); it != clients.end(); ++it) { | ||
| 123 | + SrsRtspConn* conn = *it; | ||
| 124 | + srs_freep(conn); | ||
| 125 | + } | ||
| 126 | + clients.clear(); | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +int SrsRtspCaster::serve_client(st_netfd_t stfd) | ||
| 130 | +{ | ||
| 131 | + int ret = ERROR_SUCCESS; | ||
| 132 | + | ||
| 133 | + SrsRtspConn* conn = new SrsRtspConn(this, stfd, output); | ||
| 134 | + if ((ret = conn->serve()) != ERROR_SUCCESS) { | ||
| 135 | + srs_error("rtsp: serve client failed. ret=%d", ret); | ||
| 136 | + srs_freep(conn); | ||
| 137 | + return ret; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + clients.push_back(conn); | ||
| 141 | + srs_info("rtsp: start thread to serve client."); | ||
| 142 | + | ||
| 143 | + return ret; | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +void SrsRtspCaster::remove(SrsRtspConn* conn) | ||
| 147 | +{ | ||
| 148 | + std::vector<SrsRtspConn*>::iterator it = find(clients.begin(), clients.end(), conn); | ||
| 149 | + if (it != clients.end()) { | ||
| 150 | + clients.erase(it); | ||
| 151 | + } | ||
| 152 | + srs_info("rtsp: remove connection from caster."); | ||
| 153 | + | ||
| 154 | + srs_freep(conn); | ||
| 45 | } | 155 | } |
| 46 | 156 | ||
| 47 | #endif | 157 | #endif |
| @@ -31,10 +31,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -31,10 +31,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 31 | #include <srs_core.hpp> | 31 | #include <srs_core.hpp> |
| 32 | 32 | ||
| 33 | #include <string> | 33 | #include <string> |
| 34 | +#include <vector> | ||
| 35 | + | ||
| 36 | +#include <srs_app_st.hpp> | ||
| 37 | +#include <srs_app_thread.hpp> | ||
| 34 | 38 | ||
| 35 | #ifdef SRS_AUTO_STREAM_CASTER | 39 | #ifdef SRS_AUTO_STREAM_CASTER |
| 36 | 40 | ||
| 37 | class SrsConfDirective; | 41 | class SrsConfDirective; |
| 42 | +class SrsStSocket; | ||
| 43 | +class SrsRtspStack; | ||
| 44 | +class SrsRtspCaster; | ||
| 38 | 45 | ||
| 39 | /** | 46 | /** |
| 40 | * the handler for rtsp handler. | 47 | * the handler for rtsp handler. |
| @@ -44,18 +51,54 @@ class ISrsRtspHandler | @@ -44,18 +51,54 @@ class ISrsRtspHandler | ||
| 44 | public: | 51 | public: |
| 45 | ISrsRtspHandler(); | 52 | ISrsRtspHandler(); |
| 46 | virtual ~ISrsRtspHandler(); | 53 | virtual ~ISrsRtspHandler(); |
| 54 | +public: | ||
| 55 | + /** | ||
| 56 | + * serve the rtsp connection. | ||
| 57 | + */ | ||
| 58 | + virtual int serve_client(st_netfd_t stfd) = 0; | ||
| 47 | }; | 59 | }; |
| 48 | 60 | ||
| 49 | /** | 61 | /** |
| 50 | -* the connection for rtsp. | 62 | +* the rtsp connection serve the fd. |
| 51 | */ | 63 | */ |
| 52 | -class SrsRtspConn : public ISrsRtspHandler | 64 | +class SrsRtspConn : public ISrsThreadHandler |
| 53 | { | 65 | { |
| 54 | private: | 66 | private: |
| 55 | std::string output; | 67 | std::string output; |
| 68 | + st_netfd_t stfd; | ||
| 69 | + SrsStSocket* skt; | ||
| 70 | + SrsRtspStack* rtsp; | ||
| 71 | + SrsRtspCaster* caster; | ||
| 72 | + SrsThread* trd; | ||
| 56 | public: | 73 | public: |
| 57 | - SrsRtspConn(SrsConfDirective* c); | 74 | + SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o); |
| 58 | virtual ~SrsRtspConn(); | 75 | virtual ~SrsRtspConn(); |
| 76 | +public: | ||
| 77 | + virtual int serve(); | ||
| 78 | +private: | ||
| 79 | + virtual int do_cycle(); | ||
| 80 | +// interface ISrsThreadHandler | ||
| 81 | +public: | ||
| 82 | + virtual int cycle(); | ||
| 83 | + virtual void on_thread_stop(); | ||
| 84 | +}; | ||
| 85 | + | ||
| 86 | +/** | ||
| 87 | +* the caster for rtsp. | ||
| 88 | +*/ | ||
| 89 | +class SrsRtspCaster : public ISrsRtspHandler | ||
| 90 | +{ | ||
| 91 | +private: | ||
| 92 | + std::string output; | ||
| 93 | + std::vector<SrsRtspConn*> clients; | ||
| 94 | +public: | ||
| 95 | + SrsRtspCaster(SrsConfDirective* c); | ||
| 96 | + virtual ~SrsRtspCaster(); | ||
| 97 | +public: | ||
| 98 | + virtual int serve_client(st_netfd_t stfd); | ||
| 99 | +// internal methods. | ||
| 100 | +public: | ||
| 101 | + virtual void remove(SrsRtspConn* conn); | ||
| 59 | }; | 102 | }; |
| 60 | 103 | ||
| 61 | #endif | 104 | #endif |
| @@ -240,7 +240,7 @@ SrsRtspListener::SrsRtspListener(SrsServer* server, SrsListenerType type, SrsCon | @@ -240,7 +240,7 @@ SrsRtspListener::SrsRtspListener(SrsServer* server, SrsListenerType type, SrsCon | ||
| 240 | // we just assert here for unknown stream caster. | 240 | // we just assert here for unknown stream caster. |
| 241 | srs_assert(_type == SrsListenerRtsp); | 241 | srs_assert(_type == SrsListenerRtsp); |
| 242 | if (_type == SrsListenerRtsp) { | 242 | if (_type == SrsListenerRtsp) { |
| 243 | - caster = new SrsRtspConn(c); | 243 | + caster = new SrsRtspCaster(c); |
| 244 | } | 244 | } |
| 245 | } | 245 | } |
| 246 | 246 | ||
| @@ -262,7 +262,7 @@ int SrsRtspListener::cycle() | @@ -262,7 +262,7 @@ int SrsRtspListener::cycle() | ||
| 262 | } | 262 | } |
| 263 | srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd)); | 263 | srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd)); |
| 264 | 264 | ||
| 265 | - if ((ret = _server->accept_client(_type, client_stfd)) != ERROR_SUCCESS) { | 265 | + if ((ret = caster->serve_client(client_stfd)) != ERROR_SUCCESS) { |
| 266 | srs_warn("accept client error. ret=%d", ret); | 266 | srs_warn("accept client error. ret=%d", ret); |
| 267 | return ret; | 267 | return ret; |
| 268 | } | 268 | } |
| @@ -46,22 +46,78 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -46,22 +46,78 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 46 | * which will cause the socket to return error and | 46 | * which will cause the socket to return error and |
| 47 | * terminate the cycle thread. | 47 | * terminate the cycle thread. |
| 48 | * | 48 | * |
| 49 | -* when thread interrupt, the socket maybe not got EINT, | ||
| 50 | -* espectially on st_usleep(), so the cycle must check the loop, | ||
| 51 | -* when handler->cycle() has loop itself, for example: | ||
| 52 | -* while (true): | ||
| 53 | -* if (read_from_socket(skt) < 0) break; | ||
| 54 | -* if thread stop when read_from_socket, it's ok, the loop will break, | ||
| 55 | -* but when thread stop interrupt the s_usleep(0), then the loop is | ||
| 56 | -* death loop. | ||
| 57 | -* in a word, the handler->cycle() must: | ||
| 58 | -* while (pthread->can_loop()): | ||
| 59 | -* if (read_from_socket(skt) < 0) break; | ||
| 60 | -* check the loop, then it works. | 49 | +* Usage 1: stop by other thread. |
| 50 | +* user can create thread and stop then start again and again, | ||
| 51 | +* generally must provides a start and stop method, @see SrsIngester. | ||
| 52 | +* the step to create a thread stop by other thread: | ||
| 53 | +* 1. create SrsThread field, with joinable true. | ||
| 54 | +* 2. must use stop to stop and join the thread. | ||
| 55 | +* for example: | ||
| 56 | +* class SrsIngester : public ISrsThreadHandler { | ||
| 57 | +* public: SrsIngester() { pthread = new SrsThread("ingest", this, SRS_AUTO_INGESTER_SLEEP_US, true); } | ||
| 58 | +* public: virtual int start() { return pthread->start(); } | ||
| 59 | +* public: virtual void stop() { pthread->stop(); } | ||
| 60 | +* public: virtual int cycle() { | ||
| 61 | +* // check status, start ffmpeg when stopped. | ||
| 62 | +* } | ||
| 63 | +* }; | ||
| 61 | * | 64 | * |
| 62 | -* in the thread itself, that is the cycle method, | ||
| 63 | -* if itself want to terminate the thread, should never use stop(), | ||
| 64 | -* but use stop_loop() to set the loop to false and terminate normally. | 65 | +* Usage 2: stop by thread itself. |
| 66 | +* user can create thread which stop itself, | ||
| 67 | +* generally only need to provides a start method, | ||
| 68 | +* the object will destroy itself then terminate the thread, @see SrsConnection | ||
| 69 | +* 1. create SrsThread field, with joinable false. | ||
| 70 | +* 2. owner stop thread loop, destroy itself when thread stop. | ||
| 71 | +* for example: | ||
| 72 | +* class SrsConnection : public ISrsThreadHandler { | ||
| 73 | +* public: SrsConnection() { pthread = new SrsThread("conn", this, 0, false); } | ||
| 74 | +* public: virtual int start() { return pthread->start(); } | ||
| 75 | +* public: virtual int cycle() { | ||
| 76 | +* // serve client. | ||
| 77 | +* // set loop to stop to quit, stop thread itself. | ||
| 78 | +* pthread->stop_loop(); | ||
| 79 | +* } | ||
| 80 | +* public: virtual int on_thread_stop() { | ||
| 81 | +* // remove the connection in thread itself. | ||
| 82 | +* server->remove(this); | ||
| 83 | +* } | ||
| 84 | +* }; | ||
| 85 | +* | ||
| 86 | +* Usage 3: loop in the cycle method. | ||
| 87 | +* user can use loop code in the cycle method, @see SrsForwarder | ||
| 88 | +* 1. create SrsThread field, with or without joinable is ok. | ||
| 89 | +* 2. loop code in cycle method, check the can_loop() for thread to quit. | ||
| 90 | +* for example: | ||
| 91 | +* class SrsForwarder : public ISrsThreadHandler { | ||
| 92 | +* public: virtual int cycle() { | ||
| 93 | +* while (pthread->can_loop()) { | ||
| 94 | +* // read msgs from queue and forward to server. | ||
| 95 | +* } | ||
| 96 | +* } | ||
| 97 | +* }; | ||
| 98 | +* | ||
| 99 | +* @remark why should check can_loop() in cycle method? | ||
| 100 | +* when thread interrupt, the socket maybe not got EINT, | ||
| 101 | +* espectially on st_usleep(), so the cycle must check the loop, | ||
| 102 | +* when handler->cycle() has loop itself, for example: | ||
| 103 | +* while (true): | ||
| 104 | +* if (read_from_socket(skt) < 0) break; | ||
| 105 | +* if thread stop when read_from_socket, it's ok, the loop will break, | ||
| 106 | +* but when thread stop interrupt the s_usleep(0), then the loop is | ||
| 107 | +* death loop. | ||
| 108 | +* in a word, the handler->cycle() must: | ||
| 109 | +* while (pthread->can_loop()): | ||
| 110 | +* if (read_from_socket(skt) < 0) break; | ||
| 111 | +* check the loop, then it works. | ||
| 112 | +* | ||
| 113 | +* @remark why should use stop_loop() to terminate thread in itself? | ||
| 114 | +* in the thread itself, that is the cycle method, | ||
| 115 | +* if itself want to terminate the thread, should never use stop(), | ||
| 116 | +* but use stop_loop() to set the loop to false and terminate normally. | ||
| 117 | +* | ||
| 118 | +* @remark when should set the interval_us, and when not? | ||
| 119 | +* the cycle will invoke util cannot loop, eventhough the return code of cycle is error, | ||
| 120 | +* so the interval_us used to sleep for each cycle. | ||
| 65 | */ | 121 | */ |
| 66 | class ISrsThreadHandler | 122 | class ISrsThreadHandler |
| 67 | { | 123 | { |
trunk/src/protocol/srs_rtsp_stack.cpp
0 → 100644
| 1 | +/* | ||
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013-2015 winlin | ||
| 5 | + | ||
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | +this software and associated documentation files (the "Software"), to deal in | ||
| 8 | +the Software without restriction, including without limitation the rights to | ||
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 11 | +subject to the following conditions: | ||
| 12 | + | ||
| 13 | +The above copyright notice and this permission notice shall be included in all | ||
| 14 | +copies or substantial portions of the Software. | ||
| 15 | + | ||
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 22 | +*/ | ||
| 23 | + | ||
| 24 | +#include <srs_rtsp_stack.hpp> | ||
| 25 | + | ||
| 26 | +#include <srs_rtmp_io.hpp> | ||
| 27 | + | ||
| 28 | +#ifdef SRS_AUTO_STREAM_CASTER | ||
| 29 | + | ||
| 30 | +SrsRtspStack::SrsRtspStack(ISrsProtocolReaderWriter* s) | ||
| 31 | +{ | ||
| 32 | + skt = s; | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +SrsRtspStack::~SrsRtspStack() | ||
| 36 | +{ | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +#endif | ||
| 40 | + |
trunk/src/protocol/srs_rtsp_stack.hpp
0 → 100644
| 1 | +/* | ||
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013-2015 winlin | ||
| 5 | + | ||
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | +this software and associated documentation files (the "Software"), to deal in | ||
| 8 | +the Software without restriction, including without limitation the rights to | ||
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 11 | +subject to the following conditions: | ||
| 12 | + | ||
| 13 | +The above copyright notice and this permission notice shall be included in all | ||
| 14 | +copies or substantial portions of the Software. | ||
| 15 | + | ||
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 22 | +*/ | ||
| 23 | + | ||
| 24 | +#ifndef SRS_PROTOCOL_RTSP_STACK_HPP | ||
| 25 | +#define SRS_PROTOCOL_RTSP_STACK_HPP | ||
| 26 | + | ||
| 27 | +/* | ||
| 28 | +#include <srs_rtsp_stack.hpp> | ||
| 29 | +*/ | ||
| 30 | + | ||
| 31 | +#include <srs_core.hpp> | ||
| 32 | + | ||
| 33 | +#ifdef SRS_AUTO_STREAM_CASTER | ||
| 34 | + | ||
| 35 | +class ISrsProtocolReaderWriter; | ||
| 36 | + | ||
| 37 | +/** | ||
| 38 | +* the rtsp protocol stack to parse the rtsp packets. | ||
| 39 | +*/ | ||
| 40 | +class SrsRtspStack | ||
| 41 | +{ | ||
| 42 | +private: | ||
| 43 | + /** | ||
| 44 | + * underlayer socket object, send/recv bytes. | ||
| 45 | + */ | ||
| 46 | + ISrsProtocolReaderWriter* skt; | ||
| 47 | +public: | ||
| 48 | + SrsRtspStack(ISrsProtocolReaderWriter* s); | ||
| 49 | + virtual ~SrsRtspStack(); | ||
| 50 | +}; | ||
| 51 | + | ||
| 52 | +#endif | ||
| 53 | + | ||
| 54 | +#endif | ||
| 55 | + |
-
请 注册 或 登录 后发表评论