winlin

fix #209, cleanup hls when stop and timeout. 2.0.173.

@@ -343,6 +343,8 @@ Remark: @@ -343,6 +343,8 @@ Remark:
343 ## History 343 ## History
344 344
345 ### SRS 2.0 history 345 ### SRS 2.0 history
  346 +
  347 +* v2.0, 2015-05-30, fix [#209](https://github.com/simple-rtmp-server/srs/issues/209) cleanup hls when stop and timeout. 2.0.173.
346 * v2.0, 2015-05-29, fix [#409](https://github.com/simple-rtmp-server/srs/issues/409) support pure video hls. 2.0.172. 348 * v2.0, 2015-05-29, fix [#409](https://github.com/simple-rtmp-server/srs/issues/409) support pure video hls. 2.0.172.
347 * v2.0, 2015-05-28, support [srs-dolphin][srs-dolphin], the multiple-process SRS. 349 * v2.0, 2015-05-28, support [srs-dolphin][srs-dolphin], the multiple-process SRS.
348 * v2.0, 2015-05-24, fix [#404](https://github.com/simple-rtmp-server/srs/issues/404) register handler then start http thread. 2.0.167. 350 * v2.0, 2015-05-24, fix [#404](https://github.com/simple-rtmp-server/srs/issues/404) register handler then start http thread. 2.0.167.
@@ -292,7 +292,6 @@ SrsHlsMuxer::SrsHlsMuxer() @@ -292,7 +292,6 @@ SrsHlsMuxer::SrsHlsMuxer()
292 292
293 SrsHlsMuxer::~SrsHlsMuxer() 293 SrsHlsMuxer::~SrsHlsMuxer()
294 { 294 {
295 -  
296 std::vector<SrsHlsSegment*>::iterator it; 295 std::vector<SrsHlsSegment*>::iterator it;
297 for (it = segments.begin(); it != segments.end(); ++it) { 296 for (it = segments.begin(); it != segments.end(); ++it) {
298 SrsHlsSegment* segment = *it; 297 SrsHlsSegment* segment = *it;
@@ -308,38 +307,35 @@ SrsHlsMuxer::~SrsHlsMuxer() @@ -308,38 +307,35 @@ SrsHlsMuxer::~SrsHlsMuxer()
308 307
309 void SrsHlsMuxer::dispose() 308 void SrsHlsMuxer::dispose()
310 { 309 {
311 - if (!should_write_file) {  
312 - return;  
313 - }  
314 -  
315 - std::vector<SrsHlsSegment*>::iterator it;  
316 - for (it = segments.begin(); it != segments.end(); ++it) {  
317 - SrsHlsSegment* segment = *it;  
318 - if (unlink(segment->full_path.c_str()) < 0) {  
319 - srs_warn("dispose unlink path failed, file=%s.", segment->full_path.c_str()); 310 + if (should_write_file) {
  311 + std::vector<SrsHlsSegment*>::iterator it;
  312 + for (it = segments.begin(); it != segments.end(); ++it) {
  313 + SrsHlsSegment* segment = *it;
  314 + if (unlink(segment->full_path.c_str()) < 0) {
  315 + srs_warn("dispose unlink path failed, file=%s.", segment->full_path.c_str());
  316 + }
  317 + srs_freep(segment);
320 } 318 }
321 - }  
322 -  
323 - if (current) {  
324 - std::string path = current->full_path + ".tmp";  
325 - if (unlink(path.c_str()) < 0) {  
326 - srs_warn("dispose unlink path failed, file=%s", path.c_str()); 319 + segments.clear();
  320 +
  321 + if (current) {
  322 + std::string path = current->full_path + ".tmp";
  323 + if (unlink(path.c_str()) < 0) {
  324 + srs_warn("dispose unlink path failed, file=%s", path.c_str());
  325 + }
  326 + srs_freep(current);
  327 + }
  328 +
  329 + if (unlink(m3u8.c_str()) < 0) {
  330 + srs_warn("dispose unlink path failed. file=%s", m3u8.c_str());
327 } 331 }
328 } 332 }
329 333
330 - if (unlink(m3u8.c_str()) < 0) {  
331 - srs_warn("dispose unlink path failed. file=%s", m3u8.c_str());  
332 - } 334 + // TODO: FIXME: support hls dispose in HTTP cache.
  335 +
333 srs_trace("gracefully dispose hls %s", req? req->get_stream_url().c_str() : ""); 336 srs_trace("gracefully dispose hls %s", req? req->get_stream_url().c_str() : "");
334 } 337 }
335 338
336 -int SrsHlsMuxer::cycle()  
337 -{  
338 - int ret = ERROR_SUCCESS;  
339 - // TODO: FIXME: implements it.  
340 - return ret;  
341 -}  
342 -  
343 int SrsHlsMuxer::sequence_no() 339 int SrsHlsMuxer::sequence_no()
344 { 340 {
345 return _sequence_no; 341 return _sequence_no;
@@ -1125,6 +1121,8 @@ SrsHls::SrsHls() @@ -1125,6 +1121,8 @@ SrsHls::SrsHls()
1125 handler = NULL; 1121 handler = NULL;
1126 1122
1127 hls_enabled = false; 1123 hls_enabled = false;
  1124 + hls_can_dispose = false;
  1125 + last_update_time = 0;
1128 1126
1129 codec = new SrsAvcAacCodec(); 1127 codec = new SrsAvcAacCodec();
1130 sample = new SrsCodecSample(); 1128 sample = new SrsCodecSample();
@@ -1160,8 +1158,33 @@ void SrsHls::dispose() @@ -1160,8 +1158,33 @@ void SrsHls::dispose()
1160 1158
1161 int SrsHls::cycle() 1159 int SrsHls::cycle()
1162 { 1160 {
  1161 + int ret = ERROR_SUCCESS;
  1162 +
1163 srs_info("hls cycle for source %d", source->source_id()); 1163 srs_info("hls cycle for source %d", source->source_id());
1164 - return muxer->cycle(); 1164 +
  1165 + if (last_update_time <= 0) {
  1166 + last_update_time = srs_get_system_time_ms();
  1167 + }
  1168 +
  1169 + if (!_req) {
  1170 + return ret;
  1171 + }
  1172 +
  1173 + int hls_dispose = _srs_config->get_hls_dispose(_req->vhost) * 1000;
  1174 + if (srs_get_system_time_ms() - last_update_time <= hls_dispose) {
  1175 + return ret;
  1176 + }
  1177 + last_update_time = srs_get_system_time_ms();
  1178 +
  1179 + if (!hls_can_dispose) {
  1180 + return ret;
  1181 + }
  1182 + hls_can_dispose = false;
  1183 +
  1184 + srs_trace("hls cycle to dispose hls %s, timeout=%dms", _req->get_stream_url().c_str(), hls_dispose);
  1185 + dispose();
  1186 +
  1187 + return ret;
1165 } 1188 }
1166 1189
1167 int SrsHls::initialize(SrsSource* s, ISrsHlsHandler* h) 1190 int SrsHls::initialize(SrsSource* s, ISrsHlsHandler* h)
@@ -1182,6 +1205,11 @@ int SrsHls::on_publish(SrsRequest* req) @@ -1182,6 +1205,11 @@ int SrsHls::on_publish(SrsRequest* req)
1182 { 1205 {
1183 int ret = ERROR_SUCCESS; 1206 int ret = ERROR_SUCCESS;
1184 1207
  1208 + _req = req;
  1209 +
  1210 + // update the hls time, for hls_dispose.
  1211 + last_update_time = srs_get_system_time_ms();
  1212 +
1185 // support multiple publish. 1213 // support multiple publish.
1186 if (hls_enabled) { 1214 if (hls_enabled) {
1187 return ret; 1215 return ret;
@@ -1199,6 +1227,9 @@ int SrsHls::on_publish(SrsRequest* req) @@ -1199,6 +1227,9 @@ int SrsHls::on_publish(SrsRequest* req)
1199 // if enabled, open the muxer. 1227 // if enabled, open the muxer.
1200 hls_enabled = true; 1228 hls_enabled = true;
1201 1229
  1230 + // ok, the hls can be dispose, or need to be dispose.
  1231 + hls_can_dispose = true;
  1232 +
1202 // notice the source to get the cached sequence header. 1233 // notice the source to get the cached sequence header.
1203 // when reload to start hls, hls will never get the sequence header in stream, 1234 // when reload to start hls, hls will never get the sequence header in stream,
1204 // use the SrsSource.on_hls_start to push the sequence header to HLS. 1235 // use the SrsSource.on_hls_start to push the sequence header to HLS.
@@ -1250,6 +1281,9 @@ int SrsHls::on_audio(SrsSharedPtrMessage* shared_audio) @@ -1250,6 +1281,9 @@ int SrsHls::on_audio(SrsSharedPtrMessage* shared_audio)
1250 if (!hls_enabled) { 1281 if (!hls_enabled) {
1251 return ret; 1282 return ret;
1252 } 1283 }
  1284 +
  1285 + // update the hls time, for hls_dispose.
  1286 + last_update_time = srs_get_system_time_ms();
1253 1287
1254 SrsSharedPtrMessage* audio = shared_audio->copy(); 1288 SrsSharedPtrMessage* audio = shared_audio->copy();
1255 SrsAutoFree(SrsSharedPtrMessage, audio); 1289 SrsAutoFree(SrsSharedPtrMessage, audio);
@@ -1311,6 +1345,9 @@ int SrsHls::on_video(SrsSharedPtrMessage* shared_video) @@ -1311,6 +1345,9 @@ int SrsHls::on_video(SrsSharedPtrMessage* shared_video)
1311 if (!hls_enabled) { 1345 if (!hls_enabled) {
1312 return ret; 1346 return ret;
1313 } 1347 }
  1348 +
  1349 + // update the hls time, for hls_dispose.
  1350 + last_update_time = srs_get_system_time_ms();
1314 1351
1315 SrsSharedPtrMessage* video = shared_video->copy(); 1352 SrsSharedPtrMessage* video = shared_video->copy();
1316 SrsAutoFree(SrsSharedPtrMessage, video); 1353 SrsAutoFree(SrsSharedPtrMessage, video);
@@ -261,7 +261,6 @@ public: @@ -261,7 +261,6 @@ public:
261 virtual ~SrsHlsMuxer(); 261 virtual ~SrsHlsMuxer();
262 public: 262 public:
263 virtual void dispose(); 263 virtual void dispose();
264 - virtual int cycle();  
265 public: 264 public:
266 virtual int sequence_no(); 265 virtual int sequence_no();
267 virtual std::string ts_url(); 266 virtual std::string ts_url();
@@ -382,7 +381,11 @@ private: @@ -382,7 +381,11 @@ private:
382 SrsHlsCache* hls_cache; 381 SrsHlsCache* hls_cache;
383 ISrsHlsHandler* handler; 382 ISrsHlsHandler* handler;
384 private: 383 private:
  384 + SrsRequest* _req;
385 bool hls_enabled; 385 bool hls_enabled;
  386 + bool hls_can_dispose;
  387 + int64_t last_update_time;
  388 +private:
386 SrsSource* source; 389 SrsSource* source;
387 SrsAvcAacCodec* codec; 390 SrsAvcAacCodec* codec;
388 SrsCodecSample* sample; 391 SrsCodecSample* sample;
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR 2 32 #define VERSION_MAJOR 2
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 172 34 +#define VERSION_REVISION 173
35 35
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"