-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (96 loc) · 3.16 KB
/
main.go
File metadata and controls
111 lines (96 loc) · 3.16 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Query media transcoding service status and media metrics session IDs.
//
// Uses the MediaTranscodingService proxy to query active client count,
// and the MediaMetricsManager proxy to obtain session IDs for
// transcoding, playback, recording, editing, and bundle operations.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/media_transcoding ./examples/media_transcoding/
// adb push media_transcoding /data/local/tmp/ && adb shell /data/local/tmp/media_transcoding
package main
import (
"context"
"fmt"
"os"
"github.com/AndroidGoLab/binder/android/media"
"github.com/AndroidGoLab/binder/android/media/metrics"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
func main() {
ctx := context.Background()
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer driver.Close(ctx)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
fmt.Println("=== Media Transcoding & Metrics ===")
fmt.Println()
// 1. MediaTranscodingService — active client count
fmt.Println("-- Transcoding Service --")
svc, err := sm.GetService(ctx, servicemanager.MediaTranscodingService)
if err != nil {
fmt.Fprintf(os.Stderr, "get media_transcoding service: %v\n", err)
fmt.Fprintf(os.Stderr, "Media transcoding service is not present on all devices (requires hardware transcoding support).\n")
fmt.Println("Skipping transcoding queries.")
svc = nil
}
if svc != nil {
tc := media.NewMediaTranscodingServiceProxy(svc)
numClients, err := tc.GetNumOfClients(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetNumOfClients: %v\n", err)
} else {
fmt.Printf("Active transcoding clients: %d\n", numClients)
}
}
// 2. MediaMetricsManager — obtain session IDs for various media operations
fmt.Println()
fmt.Println("-- Media Metrics Session IDs --")
mm, err := metrics.GetMediaMetricsManager(ctx, sm)
if err != nil {
fmt.Fprintf(os.Stderr, "get media_metrics service: %v\n", err)
return
}
type sessionQuery struct {
name string
fn func(context.Context) (string, error)
}
queries := []sessionQuery{
{"Transcoding", mm.GetTranscodingSessionId},
{"Playback", mm.GetPlaybackSessionId},
{"Recording", mm.GetRecordingSessionId},
{"Editing", mm.GetEditingSessionId},
{"Bundle", mm.GetBundleSessionId},
}
for _, q := range queries {
sid, err := q.fn(ctx)
if err != nil {
fmt.Printf(" %-14s error: %v\n", q.name+":", err)
} else {
if sid == "" {
sid = "(empty)"
}
fmt.Printf(" %-14s %s\n", q.name+":", sid)
}
}
// Release one of the sessions to demonstrate cleanup
if transID, err := mm.GetTranscodingSessionId(ctx); err == nil && transID != "" {
err = mm.ReleaseSessionId(ctx, transID)
if err != nil {
fmt.Printf("\nReleaseSessionId(%s): %v\n", transID, err)
} else {
fmt.Printf("\nReleased transcoding session: %s\n", transID)
}
}
}