|
| 1 | +/* |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2013-2014 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_app_recv_thread.hpp> |
| 25 | + |
| 26 | +#include <srs_protocol_rtmp.hpp> |
| 27 | +#include <srs_protocol_stack.hpp> |
| 28 | + |
| 29 | +SrsRecvThread::SrsRecvThread(SrsRtmpServer* rtmp_sdk) |
| 30 | +{ |
| 31 | + rtmp = rtmp_sdk; |
| 32 | + trd = new SrsThread(this, 0, true); |
| 33 | +} |
| 34 | + |
| 35 | +SrsRecvThread::~SrsRecvThread() |
| 36 | +{ |
| 37 | + // stop recv thread. |
| 38 | + stop(); |
| 39 | + |
| 40 | + // destroy the thread. |
| 41 | + srs_freep(trd); |
| 42 | + |
| 43 | + // clear all messages. |
| 44 | + std::vector<SrsMessage*>::iterator it; |
| 45 | + for (it = queue.begin(); it != queue.end(); ++it) { |
| 46 | + SrsMessage* msg = *it; |
| 47 | + srs_freep(msg); |
| 48 | + } |
| 49 | + queue.clear(); |
| 50 | +} |
| 51 | + |
| 52 | +bool SrsRecvThread::empty() |
| 53 | +{ |
| 54 | + return queue.empty(); |
| 55 | +} |
| 56 | + |
| 57 | +SrsMessage* SrsRecvThread::pump() |
| 58 | +{ |
| 59 | + srs_assert(!queue.empty()); |
| 60 | + |
| 61 | + SrsMessage* msg = *queue.begin(); |
| 62 | + |
| 63 | + queue.erase(queue.begin()); |
| 64 | + |
| 65 | + return msg; |
| 66 | +} |
| 67 | + |
| 68 | +int SrsRecvThread::start() |
| 69 | +{ |
| 70 | + return trd->start(); |
| 71 | +} |
| 72 | + |
| 73 | +void SrsRecvThread::stop() |
| 74 | +{ |
| 75 | + trd->stop(); |
| 76 | +} |
| 77 | + |
| 78 | +int SrsRecvThread::cycle() |
| 79 | +{ |
| 80 | + int ret = ERROR_SUCCESS; |
| 81 | + |
| 82 | + SrsMessage* msg = NULL; |
| 83 | + |
| 84 | + if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) { |
| 85 | + if (!srs_is_client_gracefully_close(ret)) { |
| 86 | + srs_error("recv client control message failed. ret=%d", ret); |
| 87 | + } |
| 88 | + |
| 89 | + // we use no timeout to recv, should never got any error. |
| 90 | + trd->stop_loop(); |
| 91 | + |
| 92 | + return ret; |
| 93 | + } |
| 94 | + srs_verbose("play loop recv message. ret=%d", ret); |
| 95 | + |
| 96 | + return ret; |
| 97 | +} |
| 98 | + |
0 commit comments