forked from rsfreitas/go-rtsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teardown.go
70 lines (56 loc) · 1.48 KB
/
teardown.go
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
//
// Description:
// Author: Rodrigo Freitas
// Created at: Sun Apr 28 10:05:05 -03 2019
//
package rtsp
import (
"net/http"
"github.com/rsfreitas/go-rtsp/internal/adt"
"github.com/rsfreitas/go-rtsp/internal/packet"
"github.com/rsfreitas/go-rtsp/internal/rtp"
)
type teardownMethod struct {
clientHandler ClientTeardown
ActiveSessions map[string]*rtp.Session
AvailablePorts *adt.RangeBox
}
func (t *teardownMethod) Verify(p *packet.Packet, handler interface{}) error {
if m, ok := handler.(interface{ Teardown() }); ok {
t.clientHandler = m
// } else {
// return &methodError{"client method not implemented", true}
}
return nil
}
func (t *teardownMethod) Handle(p *packet.Packet) {
var (
clientSession string
session *rtp.Session
)
if t.clientHandler != nil {
t.clientHandler.Teardown()
}
if field, ok := p.Request.Headers["Session"]; ok {
if t.ActiveSessions == nil {
p.Response.StatusCode = StatusAggregateOperationNotAllowed
p.Response.StatusText = StatusText(p.Response.StatusCode)
return
}
clientSession = field[0]
session, ok = t.ActiveSessions[clientSession]
if !ok {
p.Response.StatusCode = StatusSessionNotFound
p.Response.StatusText = StatusText(p.Response.StatusCode)
return
}
}
port := session.Port()
t.AvailablePorts.Release(uint32(port))
session.Close()
p.Response.StatusCode = http.StatusOK
p.Response.StatusText = http.StatusText(http.StatusOK)
}
func (t *teardownMethod) Type() methodType {
return methodTeardown
}