-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,571 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package accessory | ||
|
||
import ( | ||
"github.com/brutella/hc/service" | ||
) | ||
|
||
// Camera provides RTP video streaming. | ||
type Camera struct { | ||
*Accessory | ||
Control *service.CameraControl | ||
StreamManagement1 *service.CameraRTPStreamManagement | ||
StreamManagement2 *service.CameraRTPStreamManagement | ||
} | ||
|
||
// NewCamera returns an IP camera accessory. | ||
func NewCamera(info Info) *Camera { | ||
acc := Camera{} | ||
acc.Accessory = New(info, TypeIPCamera) | ||
acc.Control = service.NewCameraControl() | ||
acc.AddService(acc.Control.Service) | ||
|
||
// TODO (mah) a camera must support at least 2 rtp streams | ||
acc.StreamManagement1 = service.NewCameraRTPStreamManagement() | ||
acc.StreamManagement2 = service.NewCameraRTPStreamManagement() | ||
acc.AddService(acc.StreamManagement1.Service) | ||
// acc.AddService(acc.StreamManagement2.Service) | ||
|
||
return &acc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package endpoint | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/brutella/hc/hap" | ||
"github.com/brutella/hc/log" | ||
"image" | ||
"image/jpeg" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
type GetImageFunc func(width, height uint) (*image.Image, error) | ||
|
||
// Resource handles the /resource endpoint | ||
type Resource struct { | ||
http.Handler | ||
imgFn GetImageFunc | ||
context hap.Context | ||
} | ||
|
||
// NewResource returns a new handler for resource requests | ||
func NewResource(context hap.Context, imgFn GetImageFunc) *Resource { | ||
r := Resource{ | ||
context: context, | ||
imgFn: imgFn, | ||
} | ||
|
||
return &r | ||
} | ||
|
||
type ImageRequest struct { | ||
Type string `json:"resource-type"` | ||
Width uint `json:"image-width"` | ||
Height uint `json:"image-height"` | ||
} | ||
|
||
func (handler *Resource) ServeHTTP(response http.ResponseWriter, request *http.Request) { | ||
switch request.Method { | ||
case hap.MethodPOST: | ||
log.Debug.Printf("%v POST /resource\n", request.RemoteAddr) | ||
if err := handler.postResource(response, request); err != nil { | ||
log.Info.Println(err) | ||
response.WriteHeader(http.StatusInternalServerError) | ||
} | ||
default: | ||
log.Debug.Println("Cannot handle HTTP method", request.Method) | ||
response.WriteHeader(http.StatusNoContent) | ||
} | ||
} | ||
|
||
func (r *Resource) postResource(resp http.ResponseWriter, req *http.Request) error { | ||
body, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var imgRq ImageRequest | ||
err = json.Unmarshal(body, &imgRq) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if imgRq.Type == "image" { | ||
b, err := r.getJPEGImage(imgRq) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp.Header().Set("Content-Type", "image/jpeg") | ||
wr := hap.NewChunkedWriter(resp, 2048) | ||
wr.Write(b) | ||
return nil | ||
} | ||
|
||
resp.WriteHeader(http.StatusNoContent) | ||
|
||
return nil | ||
} | ||
|
||
func (r *Resource) getJPEGImage(req ImageRequest) ([]byte, error) { | ||
img, err := r.imgFn(req.Width, req.Height) | ||
if err != nil { | ||
return nil, fmt.Errorf("r.imgFn() %v", err) | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
if err := jpeg.Encode(buf, *img, nil); err != nil { | ||
return nil, fmt.Errorf("jpeg.Encode() %v", err) | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.